-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv3_ReAct.py
186 lines (158 loc) · 6.62 KB
/
v3_ReAct.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import dspy
import gradio as gr
from typing import Dict
# ======================
# DSPy Modules (Fixed ReAct Implementation)
# ======================
class CodeAnalyzer(dspy.Module):
def __init__(self):
super().__init__()
# Define tools for ReAct
self.tools = [
dspy.Tool(name="code_analysis", func=self._analyze_code),
dspy.Tool(name="suggestion_generator", func=self._generate_suggestions)
]
self.analyze_code = dspy.ReAct("code -> issues, suggestions", tools=self.tools)
def _analyze_code(self, code: str) -> str:
"""Tool for analyzing code issues"""
return f"Analyzing code for potential issues..."
def _generate_suggestions(self, issues: str) -> str:
"""Tool for generating suggestions"""
return f"Generating optimization suggestions based on issues..."
def analyze(self, code: str) -> Dict[str, str]:
try:
prediction = self.analyze_code(code=code)
return {
"issues": prediction.issues,
"suggestions": prediction.suggestions
}
except Exception as e:
return {
"issues": f"Analysis error: {str(e)}",
"suggestions": "No suggestions available"
}
class CodeOptimizer(dspy.Module):
def __init__(self):
super().__init__()
# Define tools for ReAct
self.tools = [
dspy.Tool(name="code_optimizer", func=self._optimize_code),
dspy.Tool(name="code_refactor", func=self._refactor_code)
]
self.optimize_code = dspy.ReAct("code, suggestions -> optimized_code", tools=self.tools)
def _optimize_code(self, code: str) -> str:
"""Tool for code optimization"""
return f"Optimizing code structure..."
def _refactor_code(self, code: str) -> str:
"""Tool for code refactoring"""
return f"Refactoring code for better readability..."
def optimize(self, code: str, suggestions: str) -> str:
try:
prediction = self.optimize_code(code=code, suggestions=suggestions)
return prediction.optimized_code
except Exception as e:
return f"Optimization error: {str(e)}"
class TestGenerator(dspy.Module):
def __init__(self):
super().__init__()
# Define tools for ReAct
self.tools = [
dspy.Tool(name="test_case_generator", func=self._generate_test_cases),
dspy.Tool(name="test_code_writer", func=self._write_test_code)
]
self.generate_tests = dspy.ReAct("code -> test_cases, test_code", tools=self.tools)
def _generate_test_cases(self, code: str) -> str:
"""Tool for generating test cases"""
return f"Generating test cases..."
def _write_test_code(self, test_cases: str) -> str:
"""Tool for writing test code"""
return f"Writing test code implementation..."
def create_tests(self, code: str) -> Dict[str, str]:
try:
prediction = self.generate_tests(code=code)
return {
"test_cases": prediction.test_cases,
"test_code": prediction.test_code
}
except Exception as e:
return {
"test_cases": f"Test generation failed: {str(e)}",
"test_code": "Unable to generate test code"
}
# ======================
# Core System (Unchanged)
# ======================
class CodeForge:
def __init__(self):
dspy.configure(lm=dspy.OllamaLocal(model='llama3.2:3b', device='cuda', temperature=0.2))
self.analyzer = CodeAnalyzer()
self.optimizer = CodeOptimizer()
self.tester = TestGenerator()
def process(self, code: str) -> Dict[str, str]:
analysis = self.analyzer.analyze(code)
optimized = self.optimizer.optimize(code, analysis["suggestions"])
tests = self.tester.create_tests(code)
return {
"issues": analysis["issues"],
"suggestions": analysis["suggestions"],
"optimized_code": optimized,
"test_cases": tests["test_cases"],
"test_code": tests["test_code"]
}
# ======================
# Gradio Interface
# ======================
def create_interface():
forge = CodeForge()
with gr.Blocks(theme=gr.themes.Soft(), title="CodeForge AI") as app:
gr.Markdown("# CoDSPy - AI Code Optimizer & Test Generator")
with gr.Row():
with gr.Column():
input_code = gr.Code(
label="Input Python Code",
language="python",
lines=15,
elem_id="input-code"
)
process_btn = gr.Button("Analyze & Improve", variant="primary")
with gr.Column():
with gr.Tabs():
with gr.Tab("Analysis"):
issues_out = gr.Textbox(label="Detected Issues", interactive=False, lines=3)
suggestions_out = gr.Textbox(label="Optimization Suggestions", lines=5, interactive=False)
with gr.Tab("Optimized Code"):
optimized_code_out = gr.Code(
label="Improved Code",
language="python",
lines=15,
interactive=False
)
with gr.Tab("Tests"):
test_cases_out = gr.Textbox(label="Suggested Test Cases", lines=4, interactive=False)
test_code_out = gr.Code(
label="Generated Test Code",
language="python",
lines=15,
interactive=False
)
@process_btn.click(
inputs=[input_code],
outputs=[issues_out, suggestions_out, optimized_code_out, test_cases_out, test_code_out]
)
def process_code(code):
if not code.strip():
raise gr.Error("Please input some code to analyze!")
results = forge.process(code)
return (
results["issues"],
results["suggestions"],
results["optimized_code"],
results["test_cases"],
results["test_code"]
)
return app
if __name__ == "__main__":
web_app = create_interface()
web_app.launch(server_port=7860, server_name="0.0.0.0")