kimhyunwoo commited on
Commit
9c4c102
ยท
verified ยท
1 Parent(s): bb4dd6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -31
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 agent_logic(code: str, instruction: str):
56
- if not instruction: return "Error: Instruction cannot be empty."
57
-
 
58
  lower_instruction = instruction.lower()
59
 
60
- if "compile" in lower_instruction or "run" in lower_instruction:
61
- if not code: return "Error: Code editor is empty. Nothing to compile."
62
- return compile_and_run_c_code(code)
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 new_code, f"Code generation successful. The new code is now in the editor."
 
 
 
 
 
69
 
 
 
 
 
 
70
  else: # Refactor, analyze, add comments, etc.
71
- if not code: return "Error: Code editor is empty. Nothing to analyze."
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(label="Instruction", placeholder="e.g., 'Compile and run', 'Find bugs', 'Add comments', 'Generate a factorial function'...")
 
 
 
 
88
  execute_btn = gr.Button("Execute Instruction", variant="primary")
89
- output_box = gr.Markdown(label="Output / Console")
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(handle_ide_submit, inputs=[code_editor, instruction_box], outputs=[code_editor, output_box])
 
 
 
 
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()