Update app.py
Browse files
app.py
CHANGED
|
@@ -1,102 +1,68 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
"
|
| 8 |
-
|
| 9 |
-
|
| 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 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
def
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
return
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
with gr.
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
generate_btn = gr.Button("Generate Code")
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
code_input = gr.Textbox(label="Paste code", lines=10)
|
| 66 |
explain_btn = gr.Button("Explain Code")
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 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 |
-
|
| 80 |
-
fix_btn.click (fix_code, [broken_input, err], fixed_output)
|
| 81 |
|
| 82 |
-
|
| 83 |
-
with gr.Tab("Chat"):
|
| 84 |
chatbot = gr.Chatbot()
|
| 85 |
-
chat_input = gr.Textbox(label="Ask
|
| 86 |
-
|
| 87 |
-
|
| 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()
|