Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -51,28 +51,34 @@ C Code to Analyze:
|
|
51 |
response = CLIENT.chat(model="codestral-latest", messages=messages)
|
52 |
return response.choices[0].message.content
|
53 |
|
54 |
-
# --- 3. ๋ฉ์ธ
|
55 |
-
def
|
56 |
-
if not instruction:
|
57 |
-
|
|
|
58 |
lower_instruction = instruction.lower()
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
elif "generate" in lower_instruction or "create" in lower_instruction or "๋ง๋ค์ด์ค" in lower_instruction:
|
65 |
-
# ์๋ก์ด ์ฝ๋๋ฅผ ์์ฑํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ฝ๋ ์๋ํฐ์ ์ง์ ๋ฐํ
|
66 |
new_code = generate_c_code(instruction)
|
67 |
-
|
68 |
-
return
|
|
|
|
|
|
|
|
|
|
|
69 |
|
|
|
|
|
|
|
|
|
|
|
70 |
else: # Refactor, analyze, add comments, etc.
|
71 |
-
|
72 |
-
# ๋ถ์/๋ฆฌํฉํ ๋ง ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐ๊ณผ์ฐฝ์ ๋ฐํ
|
73 |
analysis_result = analyze_and_refactor_code(code, instruction)
|
74 |
-
|
75 |
-
return code, analysis_result
|
76 |
|
77 |
# --- 4. Gradio UI ๊ตฌ์ฑ (IDE ํํ) ---
|
78 |
with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="indigo", secondary_hue="blue"), css="footer {visibility: hidden}") as demo:
|
@@ -84,22 +90,19 @@ with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="indigo", secondary_hue="b
|
|
84 |
with gr.Column(scale=2):
|
85 |
code_editor = gr.Code(label="C Code Editor", language="c", lines=25, interactive=True)
|
86 |
with gr.Column(scale=1):
|
87 |
-
instruction_box = gr.Textbox(
|
|
|
|
|
|
|
|
|
88 |
execute_btn = gr.Button("Execute Instruction", variant="primary")
|
89 |
-
output_box = gr.
|
90 |
-
|
91 |
-
def handle_ide_submit(code, instruction):
|
92 |
-
# ์์ฑ ์์ฒญ ์ ๋์์ ๋ถ๊ธฐ
|
93 |
-
if "generate" in instruction.lower() or "create" in instruction.lower():
|
94 |
-
new_code, message = agent_logic(code, instruction)
|
95 |
-
return new_code, message
|
96 |
-
else:
|
97 |
-
# ์ปดํ์ผ ๋๋ ๋ถ์
|
98 |
-
result = agent_logic(code, instruction)
|
99 |
-
# ์ด ๊ฒฝ์ฐ, agent_logic์ ํ๋์ ๊ฐ๋ง ๋ฐํํ๋ฏ๋ก, ์ฝ๋ ์๋ํฐ๋ ๊ทธ๋๋ก ์ ์ง
|
100 |
-
return code, result
|
101 |
|
102 |
-
execute_btn.click(
|
|
|
|
|
|
|
|
|
103 |
|
104 |
with gr.TabItem("๐ ๏ธ MCP Tools API"):
|
105 |
gr.Markdown("## Available MCP Tools\nThese APIs can be used by any MCP-compliant client.")
|
@@ -111,4 +114,4 @@ with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="indigo", secondary_hue="b
|
|
111 |
gr.Interface(fn=analyze_and_refactor_code, inputs=[gr.Code(language="c"), "text"], outputs=gr.Markdown())
|
112 |
|
113 |
if __name__ == "__main__":
|
114 |
-
demo.launch()
|
|
|
51 |
response = CLIENT.chat(model="codestral-latest", messages=messages)
|
52 |
return response.choices[0].message.content
|
53 |
|
54 |
+
# --- 3. ๋ฉ์ธ ์ด๋ฒคํธ ํธ๋ค๋ฌ ---
|
55 |
+
def handle_instruction(code: str, instruction: str):
|
56 |
+
if not instruction:
|
57 |
+
return code, "Error: Instruction cannot be empty."
|
58 |
+
|
59 |
lower_instruction = instruction.lower()
|
60 |
|
61 |
+
# 'generate' ๋๋ 'create'๊ฐ ํฌํจ๋ ๊ฒฝ์ฐ, ์ฝ๋ ์๋ํฐ๊ฐ ๋น์ด์์ด๋ ๋ฌด์กฐ๊ฑด ์์ฑ
|
62 |
+
if "generate" in lower_instruction or "create" in lower_instruction or "๋ง๋ค์ด์ค" in lower_instruction:
|
63 |
+
yield code, "Generating new code..."
|
|
|
|
|
|
|
64 |
new_code = generate_c_code(instruction)
|
65 |
+
yield new_code, "Code generation successful. New code is in the editor."
|
66 |
+
return
|
67 |
+
|
68 |
+
# ๊ทธ ์ธ์ ๊ฒฝ์ฐ, ์ฝ๋ ์๋ํฐ๊ฐ ๋น์ด์์ผ๋ฉด ์๋ฌ
|
69 |
+
if not code:
|
70 |
+
yield code, "Error: Code editor is empty. Please provide code to work with, or ask to 'generate' new code."
|
71 |
+
return
|
72 |
|
73 |
+
if "compile" in lower_instruction or "run" in lower_instruction:
|
74 |
+
yield code, "Compiling and running..."
|
75 |
+
result = compile_and_run_c_code(code)
|
76 |
+
yield code, result
|
77 |
+
|
78 |
else: # Refactor, analyze, add comments, etc.
|
79 |
+
yield code, f"Analyzing code with instruction: '{instruction}'..."
|
|
|
80 |
analysis_result = analyze_and_refactor_code(code, instruction)
|
81 |
+
yield code, analysis_result
|
|
|
82 |
|
83 |
# --- 4. Gradio UI ๊ตฌ์ฑ (IDE ํํ) ---
|
84 |
with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="indigo", secondary_hue="blue"), css="footer {visibility: hidden}") as demo:
|
|
|
90 |
with gr.Column(scale=2):
|
91 |
code_editor = gr.Code(label="C Code Editor", language="c", lines=25, interactive=True)
|
92 |
with gr.Column(scale=1):
|
93 |
+
instruction_box = gr.Textbox(
|
94 |
+
label="Instruction",
|
95 |
+
placeholder="e.g., 'Compile and run', 'Find bugs', 'Generate a factorial function'...",
|
96 |
+
lines=3
|
97 |
+
)
|
98 |
execute_btn = gr.Button("Execute Instruction", variant="primary")
|
99 |
+
output_box = gr.Textbox(label="Output / Console", lines=15, interactive=False, show_copy_button=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
+
execute_btn.click(
|
102 |
+
fn=handle_instruction,
|
103 |
+
inputs=[code_editor, instruction_box],
|
104 |
+
outputs=[code_editor, output_box]
|
105 |
+
)
|
106 |
|
107 |
with gr.TabItem("๐ ๏ธ MCP Tools API"):
|
108 |
gr.Markdown("## Available MCP Tools\nThese APIs can be used by any MCP-compliant client.")
|
|
|
114 |
gr.Interface(fn=analyze_and_refactor_code, inputs=[gr.Code(language="c"), "text"], outputs=gr.Markdown())
|
115 |
|
116 |
if __name__ == "__main__":
|
117 |
+
demo.queue().launch()
|