|
|
import gradio as gr |
|
|
|
|
|
|
|
|
def generate_code(task, language="Python"): |
|
|
boilerplate = { |
|
|
"Python": "# Python example\n\ndef main():\n print('Hello World')\n\nmain()", |
|
|
"JavaScript": "// JavaScript example\n\nconsole.log('Hello World');", |
|
|
"Java": "public class Main {\n public static void main(String[] args) {\n System.out.println('Hello World');\n }\n}", |
|
|
"Windows Batch": "@echo off\necho Hello World\npause" |
|
|
} |
|
|
return f"// Generated Task: {task}\n\n{boilerplate.get(language, '// Language not supported yet')}" |
|
|
|
|
|
def explain_code(code): |
|
|
return "π§ This code prints a greeting message.\nI will provide deeper analysis in the upgraded AI version." |
|
|
|
|
|
def fix_code(code): |
|
|
return "β
Fixed Code:\n" + code.replace(";;", ";").replace("pass", "# TODO: add logic") |
|
|
|
|
|
def chat(message, history): |
|
|
history = history or [] |
|
|
reply = f"π€ AI Programmer: I understand your message:\n{message}\n\nTell me what you want to build!" |
|
|
history.append((message, reply)) |
|
|
return history |
|
|
|
|
|
def chat_handler(message, history): |
|
|
""" |
|
|
message: str from textbox |
|
|
history: existing chat history list (list of tuples/lists) |
|
|
Must return: updated history (list of tuples/lists) |
|
|
""" |
|
|
history = history or [] |
|
|
|
|
|
|
|
|
msg = (message or "").strip() |
|
|
if not msg: |
|
|
history.append(("", "Please type a question or command.")) |
|
|
return history |
|
|
|
|
|
|
|
|
low = msg.lower() |
|
|
if "hello" in low or "hi" in low: |
|
|
reply = "Hello! π What would you like to build or debug today?" |
|
|
elif "generate" in low or "build" in low: |
|
|
reply = "Tell me the language and a short description (e.g., 'python: web scraper that saves to csv')." |
|
|
elif "fix" in low or "bug" in low: |
|
|
reply = "Paste the code (or error message) and I'll suggest fixes and diagnostics." |
|
|
elif "explain" in low or "what does" in low: |
|
|
reply = "Paste the code you want explained and I'll walk through it step-by-step." |
|
|
else: |
|
|
|
|
|
reply = "I can generate, explain, and fix code. Try commands like: `generate`, `fix`, or `explain`." |
|
|
|
|
|
|
|
|
history.append((message, reply)) |
|
|
return history |
|
|
|
|
|
|
|
|
with gr.Blocks() as app: |
|
|
|
|
|
with gr.Tab("Chat"): |
|
|
|
|
|
chatbot = gr.Chatbot(label="Programming Assistant", type="messages") |
|
|
chat_input = gr.Textbox(label="Ask anything...", placeholder="Type a question here...") |
|
|
send_btn = gr.Button("Send") |
|
|
clear_btn = gr.Button("Clear") |
|
|
|
|
|
def chat_handler(message, history): |
|
|
history = history or [] |
|
|
msg = message.strip() |
|
|
if not msg: |
|
|
history.append({"role": "assistant", "content": "Please type a message."}) |
|
|
return history |
|
|
history.append({"role": "user", "content": msg}) |
|
|
reply = f"I heard you: {msg}" |
|
|
history.append({"role": "assistant", "content": reply}) |
|
|
return history |
|
|
|
|
|
send_btn.click(chat_handler, [chat_input, chatbot], chatbot) |
|
|
chat_input.submit(chat_handler, [chat_input, chatbot], chatbot) |
|
|
clear_btn.click(lambda: [], None, chatbot) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def chat_handler(message, history): |
|
|
history = history or [] |
|
|
msg = (message or "").strip() |
|
|
if not msg: |
|
|
history.append({"role": "assistant", "content": "Please type a question!"}) |
|
|
return history |
|
|
|
|
|
|
|
|
low = msg.lower() |
|
|
if "generate" in low: |
|
|
reply = "Tell me the language and a short description, e.g. 'python: web scraper'" |
|
|
elif "fix" in low: |
|
|
reply = "Paste your code and error message; I can suggest fixes." |
|
|
else: |
|
|
reply = "I can generate, explain, and fix code. Try: generate, fix, explain." |
|
|
|
|
|
|
|
|
history.append({"role": "user", "content": message}) |
|
|
history.append({"role": "assistant", "content": reply}) |
|
|
return history |
|
|
|
|
|
|
|
|
send_btn.click(chat_handler, inputs=[chat_input, chatbot], outputs=chatbot) |
|
|
chat_input.submit(chat_handler, inputs=[chat_input, chatbot], outputs=chatbot) |
|
|
clear_btn.click(lambda: [], None, chatbot, queue=False) |
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks(title="Synthetic AI Coder") as app: |
|
|
|
|
|
gr.Markdown( |
|
|
""" |
|
|
# π€ Synthetic AI Coder |
|
|
Build real software β Windows, Python, JavaScript, Java. |
|
|
""" |
|
|
) |
|
|
|
|
|
with gr.Tab("β¨ Generate Code"): |
|
|
task = gr.Textbox(label="Describe the program you want") |
|
|
lang = gr.Dropdown( |
|
|
["Python", "JavaScript", "Java", "Windows Batch"], |
|
|
label="Language", |
|
|
value="Python" |
|
|
) |
|
|
output = gr.Code(label="Generated Code", language="python") |
|
|
generate_btn = gr.Button("Generate Software Code") |
|
|
generate_btn.click(generate_code, [task, lang], output) |
|
|
|
|
|
with gr.Tab("π Explain Code"): |
|
|
code_input = gr.Textbox(label="Paste code here", lines=10) |
|
|
explanation = gr.Textbox(label="Explanation", lines=10) |
|
|
explain_btn = gr.Button("Explain Code") |
|
|
explain_btn.click(explain_code, code_input, explanation) |
|
|
|
|
|
with gr.Tab("π Fix/Improve Code"): |
|
|
broken = gr.Textbox(label="Paste broken code", lines=10) |
|
|
fixed = gr.Code(label="Fixed Code") |
|
|
fix_btn = gr.Button("Fix Code") |
|
|
fix_btn.click(fix_code, broken, fixed) |
|
|
|
|
|
with gr.Tab("π¬ AI Programming Chat"): |
|
|
chatbot = gr.Chatbot() |
|
|
chat_input = gr.Textbox(label="Ask about software development") |
|
|
chat_input.submit(chat, [chat_input, chatbot], chatbot) |
|
|
gr.Button("Clear").click(lambda: [], None, chatbot) |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
app.launch() |
|
|
|