Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -31,6 +31,11 @@ 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)
|
@@ -41,6 +46,10 @@ def compile_and_run_c_code(code: str) -> str:
|
|
41 |
output = run_proc.stdout
|
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 |
# --- 3. ์ง์ง '์ง๋ฅํ' ์์ด์ ํธ ๋ก์ง ---
|
46 |
def intelligent_agent_ide(initial_code: str, full_instruction: str):
|
@@ -48,7 +57,6 @@ def intelligent_agent_ide(initial_code: str, full_instruction: str):
|
|
48 |
yield initial_code, "Error: Instruction cannot be empty."
|
49 |
return
|
50 |
|
51 |
-
# 'and', 'then', ',' ๋ฑ์ ๊ธฐ์ค์ผ๋ก ๋ช
๋ น์ด๋ฅผ ์ฌ๋ฌ ๋จ๊ณ๋ก ๋ถ๋ฆฌ
|
52 |
tasks = [task.strip() for task in re.split(r'\s+and\s+|\s*,\s*then\s*|\s*๊ทธ๋ฆฌ๊ณ \s*', full_instruction, flags=re.IGNORECASE)]
|
53 |
|
54 |
current_code = initial_code
|
@@ -61,15 +69,12 @@ def intelligent_agent_ide(initial_code: str, full_instruction: str):
|
|
61 |
yield current_code, "\n".join(output_log)
|
62 |
time.sleep(0.5)
|
63 |
|
64 |
-
# 1. ์์ฑ (Generation)
|
65 |
if "generate" in lower_task or "create" in lower_task or "๋ง๋ค์ด์ค" in lower_task:
|
66 |
-
|
67 |
-
new_code = call_mistral_api([{"role": "user", "content": prompt}])
|
68 |
current_code = new_code if new_code and "Error" not in new_code else current_code
|
69 |
output_log.append(f"โ
Code generated and updated in the editor.")
|
70 |
yield current_code, "\n".join(output_log)
|
71 |
|
72 |
-
# 2. ์ปดํ์ผ ๋ฐ ์คํ (Compile & Run)
|
73 |
elif "compile" in lower_task or "run" in lower_task or "์คํ" in lower_task:
|
74 |
if not current_code:
|
75 |
output_log.append("โ Error: Code is empty. Cannot compile.")
|
@@ -79,14 +84,12 @@ def intelligent_agent_ide(initial_code: str, full_instruction: str):
|
|
79 |
output_log.append(result)
|
80 |
yield current_code, "\n".join(output_log)
|
81 |
|
82 |
-
# 3. ๋ถ์/๋ฆฌํฉํ ๋ง (Analyze/Refactor)
|
83 |
else:
|
84 |
if not current_code:
|
85 |
output_log.append("โ Error: Code is empty. Cannot analyze.")
|
86 |
yield current_code, "\n".join(output_log)
|
87 |
break
|
88 |
-
|
89 |
-
analysis_result = call_mistral_api([{"role": "user", "content": prompt}])
|
90 |
|
91 |
refactored_code = parse_code_from_response(analysis_result)
|
92 |
if refactored_code:
|
@@ -100,7 +103,6 @@ def intelligent_agent_ide(initial_code: str, full_instruction: str):
|
|
100 |
output_log.append("\n--- All tasks complete. ---")
|
101 |
yield current_code, "\n".join(output_log)
|
102 |
|
103 |
-
|
104 |
# --- 4. ํตํฉ๋ Gradio UI ---
|
105 |
with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="indigo", secondary_hue="blue"), css="footer {visibility: hidden}") as demo:
|
106 |
gr.Markdown("# ๐ป The True C-Codestral IDE Agent")
|
@@ -125,14 +127,11 @@ with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="indigo", secondary_hue="b
|
|
125 |
with gr.TabItem("๐ ๏ธ MCP Tools API"):
|
126 |
gr.Markdown("## Available MCP Tools for other Agents\nThese APIs are the building blocks of our IDE agent.")
|
127 |
with gr.Accordion("Tool: Generate C Code", open=False):
|
128 |
-
|
129 |
-
prompt_input = gr.Textbox(label="Description")
|
130 |
-
code_output = gr.Code(language="c", label="Generated C Code")
|
131 |
-
gr.Interface(fn=generate_c_code, inputs=prompt_input, outputs=code_output)
|
132 |
with gr.Accordion("Tool: Compile & Run C Code", open=False):
|
133 |
-
|
134 |
-
|
135 |
-
gr.Interface(fn=
|
136 |
|
137 |
if __name__ == "__main__":
|
138 |
demo.queue().launch()
|
|
|
31 |
return match.group(1).strip()
|
32 |
return None
|
33 |
|
34 |
+
# --- MCP ํญ๊ณผ ์์ด์ ํธ๊ฐ ๋ชจ๋ ์ฌ์ฉํ ์ ์ญ ํจ์๋ก ์ ์ ---
|
35 |
+
def generate_c_code(description: str) -> str:
|
36 |
+
prompt = f"Generate a complete, compilable C code for this request: '{description}'. ONLY output the raw C code, without markdown or explanations."
|
37 |
+
return call_mistral_api([{"role": "user", "content": prompt}])
|
38 |
+
|
39 |
def compile_and_run_c_code(code: str) -> str:
|
40 |
try:
|
41 |
with open("main.c", "w", encoding='utf-8') as f: f.write(code)
|
|
|
46 |
output = run_proc.stdout
|
47 |
return f"--- EXECUTION SUCCEEDED ---\n{output}" if output.strip() else "--- EXECUTION SUCCEEDED ---\n(No output was produced)"
|
48 |
except Exception as e: return f"--- SYSTEM ERROR ---\nAn unexpected error occurred: {str(e)}"
|
49 |
+
|
50 |
+
def analyze_and_refactor_code(code: str, instruction: str) -> str:
|
51 |
+
prompt = f"You are a senior C code reviewer. Fulfill this instruction: '{instruction}'. If you refactor or change the code, YOU MUST provide the complete, new code in a ```c code block. \n\nC Code to Analyze:\n```c\n{code}\n```"
|
52 |
+
return call_mistral_api([{"role": "user", "content": prompt}])
|
53 |
|
54 |
# --- 3. ์ง์ง '์ง๋ฅํ' ์์ด์ ํธ ๋ก์ง ---
|
55 |
def intelligent_agent_ide(initial_code: str, full_instruction: str):
|
|
|
57 |
yield initial_code, "Error: Instruction cannot be empty."
|
58 |
return
|
59 |
|
|
|
60 |
tasks = [task.strip() for task in re.split(r'\s+and\s+|\s*,\s*then\s*|\s*๊ทธ๋ฆฌ๊ณ \s*', full_instruction, flags=re.IGNORECASE)]
|
61 |
|
62 |
current_code = initial_code
|
|
|
69 |
yield current_code, "\n".join(output_log)
|
70 |
time.sleep(0.5)
|
71 |
|
|
|
72 |
if "generate" in lower_task or "create" in lower_task or "๋ง๋ค์ด์ค" in lower_task:
|
73 |
+
new_code = generate_c_code(task)
|
|
|
74 |
current_code = new_code if new_code and "Error" not in new_code else current_code
|
75 |
output_log.append(f"โ
Code generated and updated in the editor.")
|
76 |
yield current_code, "\n".join(output_log)
|
77 |
|
|
|
78 |
elif "compile" in lower_task or "run" in lower_task or "์คํ" in lower_task:
|
79 |
if not current_code:
|
80 |
output_log.append("โ Error: Code is empty. Cannot compile.")
|
|
|
84 |
output_log.append(result)
|
85 |
yield current_code, "\n".join(output_log)
|
86 |
|
|
|
87 |
else:
|
88 |
if not current_code:
|
89 |
output_log.append("โ Error: Code is empty. Cannot analyze.")
|
90 |
yield current_code, "\n".join(output_log)
|
91 |
break
|
92 |
+
analysis_result = analyze_and_refactor_code(current_code, task)
|
|
|
93 |
|
94 |
refactored_code = parse_code_from_response(analysis_result)
|
95 |
if refactored_code:
|
|
|
103 |
output_log.append("\n--- All tasks complete. ---")
|
104 |
yield current_code, "\n".join(output_log)
|
105 |
|
|
|
106 |
# --- 4. ํตํฉ๋ Gradio UI ---
|
107 |
with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="indigo", secondary_hue="blue"), css="footer {visibility: hidden}") as demo:
|
108 |
gr.Markdown("# ๐ป The True C-Codestral IDE Agent")
|
|
|
127 |
with gr.TabItem("๐ ๏ธ MCP Tools API"):
|
128 |
gr.Markdown("## Available MCP Tools for other Agents\nThese APIs are the building blocks of our IDE agent.")
|
129 |
with gr.Accordion("Tool: Generate C Code", open=False):
|
130 |
+
gr.Interface(fn=generate_c_code, inputs="text", outputs=gr.Code(language="c", label="Generated C Code"))
|
|
|
|
|
|
|
131 |
with gr.Accordion("Tool: Compile & Run C Code", open=False):
|
132 |
+
gr.Interface(fn=compile_and_run_c_code, inputs=gr.Code(language="c"), outputs=gr.Textbox(label="Output"))
|
133 |
+
with gr.Accordion("Tool: Analyze & Refactor C Code", open=False):
|
134 |
+
gr.Interface(fn=analyze_and_refactor_code, inputs=[gr.Code(language="c"), "text"], outputs=gr.Markdown())
|
135 |
|
136 |
if __name__ == "__main__":
|
137 |
demo.queue().launch()
|