File size: 2,835 Bytes
fae3e6b 79218dd fae3e6b 7aa6874 fae3e6b 79218dd fae3e6b 7aa6874 fae3e6b fbafcc6 943752f fae3e6b dd9a7ec 7aa6874 0133f13 cf287d2 fae3e6b 7aa6874 fae3e6b b927da5 7aa6874 fae3e6b b927da5 fae3e6b b927da5 fae3e6b b927da5 fae3e6b b927da5 79218dd b927da5 fae3e6b 7aa6874 fae3e6b | 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 | import logging
import os
import sys
from pathlib import Path
import gradio as gr
from multi_agent import run_multi_agent
LLM = "gpt-6.2-sol"
CONCURRENCY_LIMIT = 8
# Example output shown before the first run; too large for a Space variable because of the inline image
EXAMPLE_OUTPUT = Path(__file__).with_name("example_output.md")
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logger = logging.getLogger(__name__)
async def invoke(openai_api_key: str, task: str) -> str:
"""Write and run Python code for a task with two GPT-6.2 Sol agents built with Microsoft Agent Framework: a
Code Writer that writes and runs the code in OpenAI's sandboxed code interpreter and a Code Reviewer that checks
the result, code quality, and plots, requesting changes until it approves. Returns the final images with the
conversation.
Args:
openai_api_key: OpenAI API key.
task: Coding task, e.g. creating a plot of stock data.
"""
if not openai_api_key:
raise gr.Error("OpenAI API Key is required.")
if not task:
raise gr.Error("Task is required.")
try:
result = await run_multi_agent(openai_api_key, LLM, task)
except Exception as e:
logger.exception("Task failed")
# Agent Framework wraps the OpenAI error in a ChatClientException; show the original message
raise gr.Error(str(e.__cause__ or e)) from e
if not result:
raise gr.Error("The model returned no output.")
return result
with gr.Blocks() as demo:
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.get("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=EXAMPLE_OUTPUT.read_text(encoding="utf-8") if EXAMPLE_OUTPUT.exists() else os.environ.get("OUTPUT", ""), line_breaks=True, sanitize_html=False)
clear_btn.add(output)
submit_btn.click(fn=invoke, inputs=[openai_api_key, task], outputs=output, concurrency_limit=CONCURRENCY_LIMIT)
if __name__ == "__main__":
# Gradio 6's bundled SourceSansPro-SemiBold.woff2 is a 55-glyph subset, so
# semibold text mixes fonts; load the full family from Google Fonts instead.
theme = gr.themes.Default(
font=[gr.themes.GoogleFont("Source Sans 3", weights=(400, 600, 700)), "ui-sans-serif", "system-ui", "sans-serif"]
)
demo.launch(mcp_server=True, theme=theme)
|