-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
135 lines (106 loc) · 4.54 KB
/
app.py
File metadata and controls
135 lines (106 loc) · 4.54 KB
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
import gradio as gr
from pathlib import Path
from dotenv import load_dotenv
from agents import Translator, TestGenerator, Evaluator
load_dotenv()
PI_EXAMPLE = open("examples/pi_calculation.py").read()
SUBARRAY_EXAMPLE = open("examples/max_subarray.py").read()
MAX_RETRIES = 3
def run_pipeline(python_code: str):
if not python_code.strip():
return "No input provided.", "", "", ""
try:
translator = Translator()
test_generator = TestGenerator()
evaluator = Evaluator()
yield "🔄 Translating Python → C++...", "", "", ""
cpp_code = translator.run(python_code)
yield "🧪 Generating test cases...", cpp_code, "", ""
test_cases = test_generator.run(python_code)
for attempt in range(1, MAX_RETRIES + 1):
yield (
f"🔍 Evaluating translation (attempt {attempt}/{MAX_RETRIES})...",
cpp_code,
format_test_cases(test_cases),
"",
)
evaluation = evaluator.run(python_code, cpp_code, test_cases)
verdict = evaluation.get("verdict", "UNKNOWN")
if verdict == "PASS":
break
if attempt < MAX_RETRIES:
issues = evaluation.get("issues", [])
summary = evaluation.get("summary", "Translation has issues.")
all_feedback = issues if issues else [summary]
yield (
f"🔧 Attempt {attempt} got {verdict} — fixing issues...",
cpp_code,
format_test_cases(test_cases),
format_evaluation(evaluation),
)
cpp_code = translator.fix(python_code, cpp_code, all_feedback)
save_cpp(cpp_code)
status = f"✅ Passed on attempt {attempt}!" if verdict == "PASS" else f"⚠️ Best effort after {MAX_RETRIES} attempts ({verdict})"
yield status, cpp_code, format_test_cases(test_cases), format_evaluation(evaluation)
except Exception as e:
yield f"❌ Error: {str(e)}", "", "", ""
def format_test_cases(test_cases: dict) -> str:
lines = []
for i, tc in enumerate(test_cases.get("test_cases", []), 1):
lines.append(f"Test {i}: {tc.get('description', '')}")
lines.append(f" Expected output : {tc.get('expected_output', '')}")
lines.append(f" Logic tested : {tc.get('logic_tested', '')}")
lines.append("")
return "\n".join(lines)
def format_evaluation(evaluation: dict) -> str:
verdict = evaluation.get("verdict", "UNKNOWN")
confidence = evaluation.get("confidence", 0)
summary = evaluation.get("summary", "")
issues = evaluation.get("issues", [])
lines = [
f"Verdict : {verdict}",
f"Confidence : {confidence}%",
f"Summary : {summary}",
]
if issues:
lines.append("\nIssues found:")
for issue in issues:
lines.append(f" ⚠️ {issue}")
return "\n".join(lines)
def save_cpp(cpp_code: str):
Path("output").mkdir(exist_ok=True)
with open("output/optimized.cpp", "w") as f:
f.write(cpp_code)
with gr.Blocks(title="Python → C++ Agent", theme=gr.themes.Monochrome()) as demo:
gr.Markdown("# Python → C++ Translation Agent")
gr.Markdown(
"Paste Python code below. The pipeline will translate it to C++, "
"generate test cases, and evaluate the translation — no compiler needed."
)
with gr.Row():
with gr.Column():
python_input = gr.Code(
label="Python Code",
language="python",
value=PI_EXAMPLE,
lines=20,
)
with gr.Row():
example_pi = gr.Button("Load Pi Example")
example_sub = gr.Button("Load Subarray Example")
run_btn = gr.Button("Run Pipeline", variant="primary")
with gr.Column():
status = gr.Textbox(label="Status", interactive=False)
cpp_output = gr.Code(label="Generated C++", language="cpp", lines=20)
with gr.Row():
test_output = gr.Textbox(label="Test Cases", lines=10, interactive=False)
eval_output = gr.Textbox(label="Evaluation Report", lines=10, interactive=False)
run_btn.click(
fn=run_pipeline,
inputs=[python_input],
outputs=[status, cpp_output, test_output, eval_output],
)
example_pi.click(fn=lambda: PI_EXAMPLE, outputs=[python_input])
example_sub.click(fn=lambda: SUBARRAY_EXAMPLE, outputs=[python_input])
if __name__ == "__main__":
demo.launch()