Dmang69 commited on
Commit
66d4d97
·
verified ·
1 Parent(s): 10ec215

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -12
app.py CHANGED
@@ -6,12 +6,13 @@ def generate_code(task, language="python", temperature=0.2):
6
  """Generate code based on task description"""
7
  examples = {
8
  "python": {
9
- "reverse a string": "def reverse_string(s):\n return s[::-1]\n\n# Example\nprint(reverse_string('hello')) # Output: olleh",
 
10
  "check if prime": "def is_prime(n):\n if n < 2:\n return False\n for i in range(2, int(n**0.5) + 1):\n if n % i == 0:\n return False\n return True",
11
  "default": "# Python function\ndef example_function():\n pass"
12
  },
13
  "javascript": {
14
- "reverse a string": "function reverseString(str) {\n return str.split('').reverse().join('');\n}\n\n// Example\nconsole.log(reverseString('hello')); // Output: olleh",
15
  "default": "// JavaScript function\nfunction exampleFunction() {\n pass // Implementation here\n}"
16
  }
17
  }
@@ -23,12 +24,10 @@ def generate_code(task, language="python", temperature=0.2):
23
  return lang_examples["default"]
24
 
25
  def explain_code(code):
26
- """Explain what the code does"""
27
- return "This code implements a common programming task. It takes input, processes it according to the defined logic, and returns the expected output."
28
 
29
  def fix_code(code, error=""):
30
- """Fix code with optional error message"""
31
- return "# Fixed version of the code\n" + code.replace("TODO", "# Implementation fixed")
32
 
33
  def chat_with_ai(message, history):
34
  """Chat interface for general programming questions"""
@@ -44,19 +43,45 @@ def chat_with_ai(message, history):
44
  return responses["default"]
45
 
46
 
47
- # ----------------------------------------------------
48
- # UI — Gradio Blocks Interface
49
- # ----------------------------------------------------
50
  with gr.Blocks(title="SynapseAI Programming Assistant") as app:
51
  gr.Markdown("# ⚡ SynapseAI — Your Coding Partner")
52
 
53
- with gr.Tab("Generate Code"):
 
54
  task = gr.Textbox(label="Describe what you want to build")
55
  lang = gr.Dropdown(["python", "javascript", "cpp", "java"], value="python", label="Language")
56
  temp = gr.Slider(0.1, 1.0, value=0.2, label="Creativity")
57
  generate = gr.Button("Generate Code")
58
  output_code = gr.Code(language="python", label="Output")
59
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  with gr.Tab("Generate Code"):
61
  with gr.Row():
62
  with gr.Column():
@@ -149,4 +174,4 @@ with gr.Blocks(title="SynapseAI Programming Assistant") as app:
149
 
150
  # For Hugging Face Spaces
151
  if __name__ == "__main__":
152
- demo.launch()
 
6
  """Generate code based on task description"""
7
  examples = {
8
  "python": {
9
+ "reverse a string": "def reverse_string(s):\n return s[::-1]",
10
+ "(reverse_string('hello')) # Output: HELLO",
11
  "check if prime": "def is_prime(n):\n if n < 2:\n return False\n for i in range(2, int(n**0.5) + 1):\n if n % i == 0:\n return False\n return True",
12
  "default": "# Python function\ndef example_function():\n pass"
13
  },
14
  "javascript": {
15
+ "reverse a string": "function reverseString(str) {\n return str.split('').reverse().join('');\n}",
16
  "default": "// JavaScript function\nfunction exampleFunction() {\n pass // Implementation here\n}"
17
  }
18
  }
 
24
  return lang_examples["default"]
25
 
26
  def explain_code(code):
27
+ return "This code processes input and returns the expected output. This code implements a common programming task. It takes input, processes it according to the defined logic, and returns the expected output. (Detailed AI explanation will go here.)"
 
28
 
29
  def fix_code(code, error=""):
30
+ return "# Fixed version of the code\n" + code.replace("TODO", "# Implementation fixed")
 
31
 
32
  def chat_with_ai(message, history):
33
  """Chat interface for general programming questions"""
 
43
  return responses["default"]
44
 
45
 
46
+ # ---------------- UI Interface ----------------
 
 
47
  with gr.Blocks(title="SynapseAI Programming Assistant") as app:
48
  gr.Markdown("# ⚡ SynapseAI — Your Coding Partner")
49
 
50
+ # Generate Code Tab
51
+ with gr.Tab("Generate Code"):
52
  task = gr.Textbox(label="Describe what you want to build")
53
  lang = gr.Dropdown(["python", "javascript", "cpp", "java"], value="python", label="Language")
54
  temp = gr.Slider(0.1, 1.0, value=0.2, label="Creativity")
55
  generate = gr.Button("Generate Code")
56
  output_code = gr.Code(language="python", label="Output")
57
+
58
+ generate_btn.click(generate_code, [task, lang, temp], code_output)
59
+
60
+ # Explain Code Tab
61
+ with gr.Tab("Explain Code"):
62
+ code_input = gr.Textbox(label="Paste code here", lines=10)
63
+ explain_btn = gr.Button("Explain Code")
64
+ explain_output = gr.Textbox(label="Explanation", lines=10)
65
+ explain_btn.click(explain_code, code_input, explain_output)
66
+
67
+ # Fix Code Tab
68
+ with gr.Tab("Fix Code"):
69
+ broken_code = gr.Textbox(label="Broken code", lines=10)
70
+ error_msg = gr.Textbox(label="Error message (optional)")
71
+ fix_btn = gr.Button("Fix Code")
72
+ fixed_output = gr.Code(label="Fixed Code")
73
+ fix_btn.click(fix_code, [broken_code, error_msg], fixed_output)
74
+
75
+ # Chat Tab
76
+ with gr.Tab("Chat"):
77
+ chatbot = gr.Chatbot(label="Programming Assistant")
78
+ chat_input = gr.Textbox(label="Ask anything...")
79
+ send_chat = gr.Button("Send")
80
+ clear_chat = gr.Button("Clear Chat")
81
+
82
+ send_chat.click(chat_ai, [chat_input, chatbot], chatbot)
83
+ clear_chat.click(lambda: None, None, chatbot)
84
+
85
  with gr.Tab("Generate Code"):
86
  with gr.Row():
87
  with gr.Column():
 
174
 
175
  # For Hugging Face Spaces
176
  if __name__ == "__main__":
177
+ demo.launch()