Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py (on Hugging Face Spaces)
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import httpx
|
| 4 |
+
import asyncio
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
# Replace with your Modal API endpoint URL
|
| 8 |
+
MODAL_API_ENDPOINT = "https://blastingneurons--collective-hive-backend-orchestrate-hive-api.modal.run"
|
| 9 |
+
|
| 10 |
+
# Helper function to format chat history for Gradio's 'messages' type
|
| 11 |
+
def format_chat_history_for_gradio(log_entries: list[dict]) -> list[dict]:
|
| 12 |
+
formatted_messages = []
|
| 13 |
+
for entry in log_entries:
|
| 14 |
+
# Default to 'System' if agent name is not found
|
| 15 |
+
role = entry.get("agent", "System")
|
| 16 |
+
content = entry.get("text", "")
|
| 17 |
+
formatted_messages.append({"role": role, "content": content})
|
| 18 |
+
return formatted_messages
|
| 19 |
+
|
| 20 |
+
async def call_modal_backend(problem_input: str, complexity: int):
|
| 21 |
+
full_chat_history = []
|
| 22 |
+
# Initial yield to clear previous state and show connecting message
|
| 23 |
+
yield {
|
| 24 |
+
"status": "Connecting to Hive...",
|
| 25 |
+
"chat_history": [],
|
| 26 |
+
"solution": "", "confidence": "", "minority_opinions": ""
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
async with httpx.AsyncClient(timeout=600.0) as client: # Longer timeout for the full process
|
| 31 |
+
async with client.stream("POST", MODAL_API_ENDPOINT, json={"problem": problem_input, "complexity": complexity}) as response:
|
| 32 |
+
response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)
|
| 33 |
+
# We need to buffer chunks to ensure we parse complete JSON lines
|
| 34 |
+
buffer = ""
|
| 35 |
+
async for chunk in response.aiter_bytes():
|
| 36 |
+
buffer += chunk.decode('utf-8')
|
| 37 |
+
while "\n" in buffer:
|
| 38 |
+
line, buffer = buffer.split("\n", 1)
|
| 39 |
+
if not line.strip(): continue # Skip empty lines
|
| 40 |
+
try:
|
| 41 |
+
data = json.loads(line)
|
| 42 |
+
event_type = data.get("event")
|
| 43 |
+
|
| 44 |
+
if event_type == "status_update":
|
| 45 |
+
yield {
|
| 46 |
+
"status": data["data"],
|
| 47 |
+
"chat_history": format_chat_history_for_gradio(full_chat_history)
|
| 48 |
+
}
|
| 49 |
+
elif event_type == "chat_update":
|
| 50 |
+
full_chat_history.append(data["data"])
|
| 51 |
+
yield {
|
| 52 |
+
"status": "In Progress...",
|
| 53 |
+
"chat_history": format_chat_history_for_gradio(full_chat_history)
|
| 54 |
+
}
|
| 55 |
+
elif event_type == "final_solution":
|
| 56 |
+
yield {
|
| 57 |
+
"status": "Solution Complete!",
|
| 58 |
+
"chat_history": format_chat_history_for_gradio(full_chat_history + [{"agent": "System", "text": "Final solution synthesized."}]),
|
| 59 |
+
"solution": data["solution"],
|
| 60 |
+
"confidence": data["confidence"],
|
| 61 |
+
"minority_opinions": data["minority_opinions"]
|
| 62 |
+
}
|
| 63 |
+
return # Done processing
|
| 64 |
+
|
| 65 |
+
except json.JSONDecodeError as e:
|
| 66 |
+
print(f"JSON Decode Error: {e} in line: {line}")
|
| 67 |
+
# This could happen if a partial JSON is received.
|
| 68 |
+
# The buffering logic should help, but if it's consistently failing, check Modal's streaming output.
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"Error processing event: {e}, Data: {data}")
|
| 71 |
+
yield {"status": f"Error: {e}", "chat_history": format_chat_history_for_gradio(full_chat_history)}
|
| 72 |
+
return
|
| 73 |
+
|
| 74 |
+
except httpx.HTTPStatusError as e:
|
| 75 |
+
error_message = f"HTTP Error: {e.response.status_code} - {e.response.text}"
|
| 76 |
+
print(error_message)
|
| 77 |
+
yield {"status": error_message, "chat_history": format_chat_history_for_gradio(full_chat_history)}
|
| 78 |
+
except httpx.RequestError as e:
|
| 79 |
+
error_message = f"Request Error: Could not connect to Modal backend: {e}"
|
| 80 |
+
print(error_message)
|
| 81 |
+
yield {"status": error_message, "chat_history": format_chat_history_for_gradio(full_chat_history)}
|
| 82 |
+
except Exception as e:
|
| 83 |
+
error_message = f"An unexpected error occurred: {e}"
|
| 84 |
+
print(error_message)
|
| 85 |
+
yield {"status": error_message, "chat_history": format_chat_history_for_gradio(full_chat_history)}
|
| 86 |
+
|
| 87 |
+
yield {"status": "Process finished unexpectedly or ended.", "chat_history": format_chat_history_for_gradio(full_chat_history)}
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
with gr.Blocks() as demo:
|
| 91 |
+
gr.Markdown("# Collective Intelligence Hive")
|
| 92 |
+
gr.Markdown("Enter a problem and watch a hive of AI agents collaborate to solve it! Powered by Modal and Nebius.")
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
problem_input = gr.Textbox(label="Problem to Solve", lines=3, placeholder="e.g., 'Develop a marketing strategy for a new eco-friendly smart home device targeting millennials.'", scale=3)
|
| 96 |
+
complexity_slider = gr.Slider(minimum=1, maximum=5, value=3, step=1, label="Problem Complexity", scale=1)
|
| 97 |
+
|
| 98 |
+
initiate_btn = gr.Button("Initiate Hive", variant="primary")
|
| 99 |
+
|
| 100 |
+
status_output = gr.Textbox(label="Hive Status", interactive=False)
|
| 101 |
+
|
| 102 |
+
with gr.Row():
|
| 103 |
+
with gr.Column(scale=2):
|
| 104 |
+
chat_display = gr.Chatbot(
|
| 105 |
+
label="Agent Discussion Log",
|
| 106 |
+
height=500,
|
| 107 |
+
type='messages',
|
| 108 |
+
autoscroll=True
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
with gr.Column(scale=1):
|
| 112 |
+
solution_output = gr.Textbox(label="Synthesized Solution", lines=10, interactive=False)
|
| 113 |
+
confidence_output = gr.Textbox(label="Solution Confidence", interactive=False)
|
| 114 |
+
minority_output = gr.Textbox(label="Minority Opinions", lines=3, interactive=False)
|
| 115 |
+
|
| 116 |
+
initiate_btn.click(
|
| 117 |
+
call_modal_backend,
|
| 118 |
+
inputs=[problem_input, complexity_slider],
|
| 119 |
+
outputs=[
|
| 120 |
+
status_output,
|
| 121 |
+
chat_display,
|
| 122 |
+
solution_output,
|
| 123 |
+
confidence_output,
|
| 124 |
+
minority_output
|
| 125 |
+
],
|
| 126 |
+
queue=True
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
demo.launch()
|