|
import gradio as gr
|
|
import asyncio
|
|
import os
|
|
import threading
|
|
import uvicorn
|
|
import time
|
|
from src.forge_agent import ForgeApp
|
|
|
|
def run_server(app_path: str, port: int):
|
|
"""Helper function to run a uvicorn server."""
|
|
uvicorn.run(app_path, host="127.0.0.1", port=port, log_level="info")
|
|
|
|
|
|
|
|
servers_to_run = {
|
|
"mock_mcp_servers.github_server:app": 8001,
|
|
"mock_mcp_servers.sandbox_server:app": 8002,
|
|
}
|
|
for app_path, port in servers_to_run.items():
|
|
thread = threading.Thread(target=run_server, args=(app_path, port), daemon=True)
|
|
thread.start()
|
|
time.sleep(2)
|
|
|
|
async def run_forge_agent(goal: str, hf_token: str, progress=gr.Progress(track_tqdm=True)):
|
|
"""
|
|
The main function to be called by the Gradio interface.
|
|
It instantiates and runs the ForgeApp, yielding updates to the UI.
|
|
"""
|
|
if not goal:
|
|
yield "Please enter a goal.", ""
|
|
return
|
|
|
|
if not hf_token:
|
|
yield [("Please provide a Hugging Face API Token to use the planner agent.", None)]
|
|
return
|
|
|
|
|
|
|
|
mcp_server_urls = [
|
|
"http://127.0.0.1:8001",
|
|
"http://127.0.0.1:8002",
|
|
]
|
|
|
|
app = ForgeApp(goal=goal, mcp_server_urls=mcp_server_urls, hf_token=hf_token)
|
|
|
|
|
|
chatbot_history = []
|
|
full_log = ""
|
|
|
|
|
|
async for update in app.run():
|
|
|
|
full_log += update + "\n"
|
|
chatbot_history.append((None, update.strip()))
|
|
yield chatbot_history
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
gr.Markdown("# π Forge - The Autonomous AI Agent")
|
|
gr.Markdown("Enter a high-level goal, and watch the AI agent create and execute a plan to achieve it.")
|
|
|
|
chatbot = gr.Chatbot(label="Agent Log", height=500, show_copy_button=True)
|
|
|
|
with gr.Row():
|
|
goal_input = gr.Textbox(
|
|
label="Agent's Goal",
|
|
placeholder="e.g., Scaffold a new Next.js blog and create a GitHub repo for it.",
|
|
scale=4,
|
|
)
|
|
run_button = gr.Button("Start", variant="primary", scale=1)
|
|
|
|
with gr.Accordion("Advanced Settings", open=False):
|
|
hf_token_input = gr.Textbox(
|
|
label="Hugging Face API Token",
|
|
placeholder="hf_...",
|
|
type="password",
|
|
value=os.environ.get("HF_TOKEN", ""),
|
|
)
|
|
|
|
run_button.click(fn=run_forge_agent, inputs=[goal_input, hf_token_input], outputs=[chatbot])
|
|
|
|
if __name__ == "__main__":
|
|
demo.launch() |