File size: 3,709 Bytes
1729503 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | import gradio as gr
import google.generativeai as genai
# β
Configure Gemini 2.0 Flash with your public API key
genai.configure(api_key="AIzaSyCWxM6Ic9zs-hZiovnMU8a4UJko7ObqPqU")
model = genai.GenerativeModel("gemini-2.0-flash")
# === π Auto Stack Detection ===
def detect_stack(idea):
idea = idea.lower()
if "streamlit" in idea:
return "Streamlit"
elif "gradio" in idea:
return "Gradio"
elif "react" in idea:
return "React + Tailwind"
elif "flask" in idea:
return "Flask"
elif "fastapi" in idea:
return "FastAPI"
elif "django" in idea:
return "Django"
elif "html" in idea or "vanilla" in idea:
return "Vanilla JS/HTML/CSS"
else:
return "Python"
# === π§ Task Breakdown Agent ===
def manager_agent(idea):
prompt = f"""
You are the team manager for a 36-hour hackathon project.
Based on the following idea:
"{idea}"
Break the project into:
1. Frontend tasks
2. Backend tasks
3. AI/ML tasks (if any)
Format strictly as:
### Frontend Tasks
- ...
### Backend Tasks
- ...
### AI/ML Tasks
- ...
"""
return model.generate_content(prompt).text
# === π§° Code-Only Developer Agent ===
def agent_prompt(role, task, stack):
return f"""
You are a professional {role} developer.
Your only task is to generate PURE, WORKING code in {stack} for:
\"{task}\"
β οΈ NO explanations.
β οΈ NO comments.
β οΈ NO greetings.
β οΈ Output ONLY the CODE.
"""
def frontend_agent(task, stack):
return model.generate_content(agent_prompt("frontend", task, stack)).text
def backend_agent(task, stack):
return model.generate_content(agent_prompt("backend", task, stack)).text
def ai_agent(task, stack):
return model.generate_content(agent_prompt("AI/ML", task, stack)).text
# === π Main Multi-Agent Pipeline ===
def run_agents(idea, tech_stack, f_task, b_task, a_task):
stack = tech_stack if tech_stack.strip() else detect_stack(idea)
breakdown = manager_agent(idea)
frontend_code = frontend_agent(f_task, stack)
backend_code = backend_agent(b_task, stack)
ai_code = ai_agent(a_task, stack)
return stack, breakdown, frontend_code, backend_code, ai_code
# === π¨ Gradio UI ===
with gr.Blocks(title="Hackathon AI Agent Team") as demo:
gr.Markdown("# π€ Hackathon AI Agent Team (Gemini 2.0 Flash)")
gr.Markdown("Enter your idea, optionally suggest a tech stack, and let your agents do the coding.")
with gr.Row():
idea = gr.Textbox(label="π‘ Hackathon Idea", placeholder="e.g., AI agent that builds projects", lines=3)
tech_stack = gr.Textbox(label="π¦ Suggested Tech Stack (optional)", placeholder="e.g., React + FastAPI")
submit = gr.Button("π§ Generate Task Breakdown")
breakdown = gr.Textbox(label="π Task Breakdown", lines=12)
with gr.Row():
f_task = gr.Textbox(label="π¨ Frontend Task", placeholder="Copy from breakdown or write manually")
b_task = gr.Textbox(label="βοΈ Backend Task")
a_task = gr.Textbox(label="π€ AI/ML Task")
run_all = gr.Button("π Run All Agents")
detected_stack = gr.Textbox(label="π§ Detected/Used Stack", interactive=False)
frontend_output = gr.Code(label="π¨ Frontend Code", language="javascript")
backend_output = gr.Code(label="βοΈ Backend Code", language="python")
ai_output = gr.Code(label="π€ AI/ML Code", language="python")
submit.click(fn=manager_agent, inputs=idea, outputs=breakdown)
run_all.click(fn=run_agents,
inputs=[idea, tech_stack, f_task, b_task, a_task],
outputs=[detected_stack, breakdown, frontend_output, backend_output, ai_output])
demo.launch()
|