ai / app.py
Dmang69's picture
Update app.py
3a0cbad verified
import gradio as gr
# βœ… Placeholder AI logic (will upgrade to real LLM next)
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 []
# sanitize input
msg = (message or "").strip()
if not msg:
history.append(("", "Please type a question or command."))
return history
# simple keyword-driven assistant (expand to real LLM later)
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:
# fallback behavior β€” useful when switching to a real model later
reply = "I can generate, explain, and fix code. Try commands like: `generate`, `fix`, or `explain`."
# Append user message and bot reply as a tuple (user, bot)
history.append((message, reply))
return history
# UI wiring for the Chat tab
with gr.Blocks() as app:
with gr.Tab("Chat"): # line 60
# everything inside the tab must be indented
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)
# Callback for chat
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
# Simple AI placeholder logic
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."
# Use 'messages' format
history.append({"role": "user", "content": message})
history.append({"role": "assistant", "content": reply})
return history
# Wire buttons and submit inside Blocks context
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)
# βœ… UI Layout
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)
# βœ… Required for Hugging Face Spaces
if __name__ == "__main__":
app.launch()