-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv2_CoT_Llama.py
147 lines (125 loc) · 5.01 KB
/
v2_CoT_Llama.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
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import dspy
import gradio as gr
from typing import Dict
# ======================
# DSPy Modules
# ======================
class CodeAnalyzer(dspy.Module):
def __init__(self):
super().__init__()
self.analyze_code = dspy.ChainOfThought("code -> issues, suggestions")
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__()
self.optimize_code = dspy.ChainOfThought("code, suggestions -> optimized_code")
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__()
self.generate_tests = dspy.ChainOfThought("code -> test_cases, test_code")
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
# ======================
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")