Dmang69 commited on
Commit
239f89f
Β·
verified Β·
1 Parent(s): c09c072

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -88
app.py CHANGED
@@ -1,102 +1,68 @@
1
  import gradio as gr
2
 
3
- # ---------------------- Code Functions ----------------------
4
-
5
- def generate_code(task, language="python", temperature=0.2):
6
- examples = {
7
- "python": {
8
- "reverse a string": "def reverse_string(s):\n return s[::-1]",
9
- "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",
10
- "default": "# Python function\ndef example_function():\n pass"
11
- },
12
- "javascript": {
13
- "reverse a string": "function reverseString(str) {\n return str.split('').reverse().join('');\n}",
14
- "default": "// JS function\nfunction exampleFunction() {\n // TODO\n}"
15
- }
16
  }
17
-
18
- lang_examples = examples.get(language, examples["python"])
19
- lower_task = task.lower()
20
-
21
- for key, code in lang_examples.items():
22
- if key in lower_task:
23
- return code
24
-
25
- return lang_examples["default"]
26
-
27
 
28
  def explain_code(code):
29
- if not code.strip():
30
- return "Please paste some code to explain."
31
- return "πŸ” **Explanation**:\nThis code performs logic based on its syntax β€” replace this with real model output later."
32
-
33
-
34
- def fix_code(code, error_msg=""):
35
- if not code.strip():
36
- return "No code provided."
37
-
38
- return "# βœ… Fixed version (placeholder)\n" + code
39
-
40
-
41
- def chat_with_ai(message, history):
42
- if not message.strip():
43
- return history + [["", "Please say something!"]]
44
-
45
- reply = "You said: " + message # placeholder
46
- return history + [[message, reply]]
47
-
48
-
49
- # ---------------------- UI Layout ----------------------
50
-
51
- with gr.Blocks(title="SynapseAI Programming Assistant") as app:
52
- gr.Markdown("# ⚑ SynapseAI β€” Your Coding Partner")
53
-
54
- # Generate Code
55
- with gr.Tab("Generate Code"):
56
- task = gr.Textbox(label="Describe what you want to build")
57
- lang = gr.Dropdown(["python", "javascript"], label="Language", value="python")
58
- temp = gr.Slider(0.1, 1.0, 0.2, label="Creativity")
59
- generate_btn = gr.Button("Generate Code")
60
- generated_output = gr.Code(label="Generated Code")
61
- generate_btn.click(generate_code, [task, lang, temp], generated_output)
62
-
63
- # Explain Code
64
- with gr.Tab("Explain Code"):
65
- code_input = gr.Textbox(label="Paste code", lines=10)
66
  explain_btn = gr.Button("Explain Code")
67
- explanation_output = gr.Textbox(label="Explanation", lines=10)
68
-
69
 
70
-
71
-
72
- explain_btn.click(explain_code, code_input, explanation_output)
73
-
74
- # Fix Code
75
- with gr.Tab("Fix Code"):
76
- broken_input = gr.Textbox(label="Broken Code", lines=10)
77
- err = gr.Textbox(label="Error (optional)")
78
  fix_btn = gr.Button("Fix Code")
79
- fixed_output = gr.Code(label="Fixed Code")
80
- fix_btn.click (fix_code, [broken_input, err], fixed_output)
81
 
82
- # Chatbot
83
- with gr.Tab("Chat"):
84
  chatbot = gr.Chatbot()
85
- chat_input = gr.Textbox(label="Ask something...")
86
-
87
- def chat_with_ai(message, history):
88
- history = history or []
89
- if not message.strip():
90
- return history + [["", "Please say something!"]]
91
- reply = "πŸ’¬ You said: " + message
92
- history.append([message, reply])
93
- return history
94
-
95
- chat_input.submit(chat_with_ai, [chat_input, chatbot], chatbot)
96
-
97
- clear_btn = gr.Button("Clear Chat")
98
- clear_btn.click(lambda: [], None, chatbot)
99
 
100
 
 
101
  if __name__ == "__main__":
102
  app.launch()
 
1
  import gradio as gr
2
 
3
+ # βœ… Placeholder AI logic (will upgrade to real LLM next)
4
+ def generate_code(task, language="Python"):
5
+ boilerplate = {
6
+ "Python": "# Python example\n\ndef main():\n print('Hello World')\n\nmain()",
7
+ "JavaScript": "// JavaScript example\n\nconsole.log('Hello World');",
8
+ "Java": "public class Main {\n public static void main(String[] args) {\n System.out.println('Hello World');\n }\n}",
9
+ "Windows Batch": "@echo off\necho Hello World\npause"
 
 
 
 
 
 
10
  }
11
+ return f"// Generated Task: {task}\n\n{boilerplate.get(language, '// Language not supported yet')}"
 
 
 
 
 
 
 
 
 
12
 
13
  def explain_code(code):
14
+ return "🧠 This code prints a greeting message.\nI will provide deeper analysis in the upgraded AI version."
15
+
16
+ def fix_code(code):
17
+ return "βœ… Fixed Code:\n" + code.replace(";;", ";").replace("pass", "# TODO: add logic")
18
+
19
+ def chat(message, history):
20
+ history = history or []
21
+ reply = f"πŸ€– AI Programmer: I understand your message:\n{message}\n\nTell me what you want to build!"
22
+ history.append((message, reply))
23
+ return history
24
+
25
+
26
+ # βœ… UI Layout
27
+ with gr.Blocks(title="Synthetic AI Coder") as app:
28
+
29
+ gr.Markdown(
30
+ """
31
+ # πŸ€– Synthetic AI Coder
32
+ Build real software β€” Windows, Python, JavaScript, Java.
33
+ """
34
+ )
35
+
36
+ with gr.Tab("✨ Generate Code"):
37
+ task = gr.Textbox(label="Describe the program you want")
38
+ lang = gr.Dropdown(
39
+ ["Python", "JavaScript", "Java", "Windows Batch"],
40
+ label="Language",
41
+ value="Python"
42
+ )
43
+ output = gr.Code(label="Generated Code", language="python")
44
+ generate_btn = gr.Button("Generate Software Code")
45
+ generate_btn.click(generate_code, [task, lang], output)
46
+
47
+ with gr.Tab("πŸ“˜ Explain Code"):
48
+ code_input = gr.Textbox(label="Paste code here", lines=10)
49
+ explanation = gr.Textbox(label="Explanation", lines=10)
 
50
  explain_btn = gr.Button("Explain Code")
51
+ explain_btn.click(explain_code, code_input, explanation)
 
52
 
53
+ with gr.Tab("πŸ›  Fix/Improve Code"):
54
+ broken = gr.Textbox(label="Paste broken code", lines=10)
55
+ fixed = gr.Code(label="Fixed Code")
 
 
 
 
 
56
  fix_btn = gr.Button("Fix Code")
57
+ fix_btn.click(fix_code, broken, fixed)
 
58
 
59
+ with gr.Tab("πŸ’¬ AI Programming Chat"):
 
60
  chatbot = gr.Chatbot()
61
+ chat_input = gr.Textbox(label="Ask about software development")
62
+ chat_input.submit(chat, [chat_input, chatbot], chatbot)
63
+ gr.Button("Clear").click(lambda: [], None, chatbot)
 
 
 
 
 
 
 
 
 
 
 
64
 
65
 
66
+ # βœ… Required for Hugging Face Spaces
67
  if __name__ == "__main__":
68
  app.launch()