Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -23,7 +23,7 @@ def call_mistral_api(messages):
|
|
23 |
except requests.exceptions.RequestException as e:
|
24 |
return f"API Call Error: {e}"
|
25 |
|
26 |
-
# --- 2. λ°±μλ ν΅μ¬ κΈ°λ₯
|
27 |
def parse_code_from_response(response_text: str) -> str | None:
|
28 |
"""LLM μλ΅μμ C μ½λ λΈλ‘μ μΆμΆν©λλ€."""
|
29 |
match = re.search(r'```c\n(.*?)\n```', response_text, re.DOTALL)
|
@@ -31,6 +31,10 @@ def parse_code_from_response(response_text: str) -> str | None:
|
|
31 |
return match.group(1).strip()
|
32 |
return None
|
33 |
|
|
|
|
|
|
|
|
|
34 |
def compile_and_run_c_code(code: str) -> str:
|
35 |
try:
|
36 |
with open("main.c", "w", encoding='utf-8') as f: f.write(code)
|
@@ -42,73 +46,55 @@ def compile_and_run_c_code(code: str) -> str:
|
|
42 |
return f"--- EXECUTION SUCCEEDED ---\n{output}" if output.strip() else "--- EXECUTION SUCCEEDED ---\n(No output was produced)"
|
43 |
except Exception as e: return f"--- SYSTEM ERROR ---\nAn unexpected error occurred: {str(e)}"
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
yield code, "Error: Instruction cannot be empty."
|
49 |
-
return
|
50 |
-
|
51 |
-
lower_instruction = instruction.lower()
|
52 |
|
53 |
-
#
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
yield code, "Error: Code editor is empty. Please provide code or ask to 'generate' it."
|
63 |
-
return
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
prompt = f"You are a senior C code reviewer. Fulfill this instruction: '{instruction}'. If you refactor the code, YOU MUST provide the complete, improved code in a ```c code block. \n\nC Code to Analyze:\n```c\n{code}\n```"
|
69 |
-
analysis_result = call_mistral_api([{"role": "user", "content": prompt}])
|
70 |
-
|
71 |
-
# μ§λ₯ν νμ±: 리ν©ν λ§λ μ½λκ° μλμ§ νμΈ
|
72 |
-
refactored_code = parse_code_from_response(analysis_result)
|
73 |
-
if refactored_code:
|
74 |
-
yield refactored_code, f"2. Refactoring successful. Code editor updated.\n\n{analysis_result}"
|
75 |
-
# 'and compile' κ°μ νμ λͺ
λ Ήμ΄ μλμ§ νμΈ
|
76 |
-
if "compile" in lower_instruction or "run" in lower_instruction:
|
77 |
-
yield refactored_code, "3. Compiling the new code..."
|
78 |
-
compile_result = compile_and_run_c_code(refactored_code)
|
79 |
-
yield refactored_code, f"4. Compilation finished.\n\n{compile_result}"
|
80 |
-
else:
|
81 |
-
# 리ν©ν λ§λ μ½λκ° μμΌλ©΄, λΆμ κ²°κ³Όλ§ λ³΄μ¬μ€
|
82 |
-
yield code, f"2. Analysis complete.\n\n{analysis_result}"
|
83 |
-
return
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
-
|
96 |
-
with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="indigo", secondary_hue="blue"), css="footer {visibility: hidden}") as demo:
|
97 |
-
gr.Markdown("# π» The True C-Codestral IDE Agent")
|
98 |
-
|
99 |
-
with gr.Row():
|
100 |
-
with gr.Column(scale=2):
|
101 |
-
code_editor = gr.Code(label="C Code Editor", language="c", lines=25, interactive=True, value='#include <stdio.h>\n\nint main() {\n printf("Hello, World!\\n");\n return 0;\n}')
|
102 |
-
with gr.Column(scale=1):
|
103 |
-
instruction_box = gr.Textbox(label="Instruction", placeholder="e.g., 'Refactor this code and run it', 'Generate a factorial function', 'Find bugs'...", lines=4)
|
104 |
-
execute_btn = gr.Button("Execute", variant="primary", size="lg")
|
105 |
-
output_box = gr.Markdown(label="Console / Output", value="Welcome! Enter an instruction and press 'Execute'.")
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
if __name__ == "__main__":
|
114 |
demo.queue().launch()
|
|
|
23 |
except requests.exceptions.RequestException as e:
|
24 |
return f"API Call Error: {e}"
|
25 |
|
26 |
+
# --- 2. λ°±μλ ν΅μ¬ κΈ°λ₯ ν¨μ ---
|
27 |
def parse_code_from_response(response_text: str) -> str | None:
|
28 |
"""LLM μλ΅μμ C μ½λ λΈλ‘μ μΆμΆν©λλ€."""
|
29 |
match = re.search(r'```c\n(.*?)\n```', response_text, re.DOTALL)
|
|
|
31 |
return match.group(1).strip()
|
32 |
return None
|
33 |
|
34 |
+
def generate_c_code(description: str) -> str:
|
35 |
+
prompt = f"You are a C programming expert. Generate a complete, compilable C code for this request: '{description}'. ONLY output the raw C code, without any markdown formatting or explanations."
|
36 |
+
return call_mistral_api([{"role": "user", "content": prompt}])
|
37 |
+
|
38 |
def compile_and_run_c_code(code: str) -> str:
|
39 |
try:
|
40 |
with open("main.c", "w", encoding='utf-8') as f: f.write(code)
|
|
|
46 |
return f"--- EXECUTION SUCCEEDED ---\n{output}" if output.strip() else "--- EXECUTION SUCCEEDED ---\n(No output was produced)"
|
47 |
except Exception as e: return f"--- SYSTEM ERROR ---\nAn unexpected error occurred: {str(e)}"
|
48 |
|
49 |
+
def analyze_and_refactor_code(code: str, instruction: str):
|
50 |
+
prompt = f""You are a senior C code reviewer. Fulfill this instruction: '{instruction}'. If you refactor the code, YOU MUST provide the complete, improved code in a ```c code block. \n\nC Code to Analyze:\n```c\n{code}\n```""
|
51 |
+
analysis_result = call_mistral_api([{"role": "user", "content": prompt}])
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
# μ§λ₯ν νμ±: 리ν©ν λ§λ μ½λκ° μλμ§ νμΈ
|
54 |
+
refactored_code = parse_code_from_response(analysis_result)
|
55 |
+
|
56 |
+
if refactored_code:
|
57 |
+
# 리ν©ν λ§λ μ½λκ° μμΌλ©΄, κ·Έ μ½λλ₯Ό μλν°μ, μ 체 μλ΅μ κ²°κ³Όμ°½μ λ°ν
|
58 |
+
return refactored_code, analysis_result
|
59 |
+
else:
|
60 |
+
# 리ν©ν λ§λ μ½λκ° μμΌλ©΄, μλ μ½λλ κ·Έλλ‘ λκ³ , λΆμ κ²°κ³Όλ§ λ°ν
|
61 |
+
return code, analysis_result
|
|
|
|
|
62 |
|
63 |
+
# --- 3. Gradio UI κ΅¬μ± (μμ μ μΈ ν ꡬ쑰 + μ§λ₯ν νμ±) ---
|
64 |
+
with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="indigo", secondary_hue="blue"), css="footer {visibility: hidden}") as demo:
|
65 |
+
gr.Markdown("# π» The C-Codestral IDE Agent (Stable Tabbed Version)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
with gr.Tabs():
|
68 |
+
with gr.TabItem("π¨βπ» IDE"):
|
69 |
+
with gr.Row():
|
70 |
+
with gr.Column(scale=2):
|
71 |
+
code_editor = gr.Code(label="C Code Editor", language="c", lines=20, interactive=True, value='#include <stdio.h>\n\nint main() {\n printf("Hello, World!\\n");\n return 0;\n}')
|
72 |
+
|
73 |
+
with gr.Column(scale=1):
|
74 |
+
# λͺ
λ Ήμ΄ ν ꡬμ±
|
75 |
+
with gr.Tabs():
|
76 |
+
with gr.TabItem("Generate"):
|
77 |
+
gen_instr = gr.Textbox(label="Generation Instruction", placeholder="e.g., 'a program that calculates factorial'")
|
78 |
+
gen_btn = gr.Button("Generate Code", variant="primary")
|
79 |
+
with gr.TabItem("Analyze / Refactor"):
|
80 |
+
ana_instr = gr.Textbox(label="Analysis Instruction", placeholder="e.g., 'add comments', 'refactor for efficiency'")
|
81 |
+
ana_btn = gr.Button("Analyze / Refactor", variant="primary")
|
82 |
+
with gr.TabItem("Compile / Run"):
|
83 |
+
run_btn = gr.Button("Compile and Run", variant="stop")
|
84 |
|
85 |
+
output_box = gr.Textbox(label="Output / Console", lines=8, interactive=False, show_copy_button=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
+
# μ΄λ²€νΈ νΈλ€λ¬ μ°κ²°
|
88 |
+
gen_btn.click(fn=generate_c_code, inputs=[gen_instr], outputs=[code_editor])
|
89 |
+
ana_btn.click(fn=analyze_and_refactor_code, inputs=[code_editor, ana_instr], outputs=[code_editor, output_box])
|
90 |
+
run_btn.click(fn=compile_and_run_c_code, inputs=[code_editor], outputs=[output_box])
|
91 |
+
|
92 |
+
with gr.TabItem("π οΈ MCP Tools"):
|
93 |
+
gr.Markdown("## Available MCP Tools for other Agents")
|
94 |
+
with gr.Accordion("Tool: Generate C Code", open=False):
|
95 |
+
gr.Interface(fn=generate_c_code, inputs="text", outputs=gr.Code(language="c", label="Generated C Code"))
|
96 |
+
with gr.Accordion("Tool: Compile & Run C Code", open=False):
|
97 |
+
gr.Interface(fn=compile_and_run_c_code, inputs=gr.Code(language="c"), outputs=gr.Textbox(label="Output"))
|
98 |
|
99 |
if __name__ == "__main__":
|
100 |
demo.queue().launch()
|