| import gradio as gr | |
| import os, threading | |
| from multi_agent import run_multi_agent | |
| lock = threading.Lock() | |
| LLM = "gpt-4o" | |
| def invoke(openai_api_key, task): | |
| if not openai_api_key: | |
| raise gr.Error("OpenAI API Key is required.") | |
| if not task: | |
| raise gr.Error("Task is required.") | |
| raise gr.Error("Please clone space due to local code execution.") | |
| with lock: | |
| os.environ["OPENAI_API_KEY"] = openai_api_key | |
| result = run_multi_agent(LLM, task) | |
| del os.environ["OPENAI_API_KEY"] | |
| return result | |
| def clear(): | |
| return "" | |
| gr.close_all() | |
| with gr.Blocks() as assistant: | |
| gr.Markdown("## Multi-Agent AI: Coding") | |
| gr.Markdown(os.environ.get("DESCRIPTION")) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| with gr.Row(): | |
| openai_api_key = gr.Textbox(label = "OpenAI API Key", type = "password", lines = 1) | |
| task = gr.Textbox(label = "Task", value=os.environ["INPUT"], lines = 1) | |
| with gr.Row(): | |
| clear_btn = gr.ClearButton( | |
| components=[openai_api_key, task] | |
| ) | |
| submit_btn = gr.Button("Submit", variant="primary") | |
| with gr.Column(scale=3): | |
| output = gr.Markdown(label = "Output", value=os.environ["OUTPUT"], line_breaks = True, sanitize_html = False) | |
| clear_btn.click( | |
| fn=clear, | |
| outputs=output | |
| ) | |
| submit_btn.click( | |
| fn=invoke, | |
| inputs=[openai_api_key, task], | |
| outputs=output | |
| ) | |
| assistant.launch(mcp_server=True) |