Jeremy commited on
Commit ·
eca28dc
1
Parent(s): 211caf7
Simplify app for testing
Browse files- app.py +12 -132
- requirements.txt +1 -2
app.py
CHANGED
|
@@ -1,139 +1,19 @@
|
|
| 1 |
"""
|
| 2 |
Live OFP Backend - HuggingFace Space
|
| 3 |
-
Gradio interface
|
| 4 |
"""
|
| 5 |
-
import os
|
| 6 |
-
import json
|
| 7 |
import gradio as gr
|
| 8 |
-
from gradio import components as grc
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY", "")
|
| 14 |
-
HF_API_KEY = os.getenv("HF_API_KEY", "")
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"""Create a new OFP session"""
|
| 22 |
-
import uuid
|
| 23 |
-
session_id = str(uuid.uuid4())
|
| 24 |
-
|
| 25 |
-
agents = []
|
| 26 |
-
if agents_config:
|
| 27 |
-
for i, agent_spec in enumerate(agents_config.split("\n")):
|
| 28 |
-
if agent_spec.strip():
|
| 29 |
-
parts = agent_spec.split(":")
|
| 30 |
-
if len(parts) >= 2:
|
| 31 |
-
agents.append({
|
| 32 |
-
"provider": parts[0].strip(),
|
| 33 |
-
"name": parts[1].strip(),
|
| 34 |
-
"type": parts[2].strip() if len(parts) > 2 else "text",
|
| 35 |
-
"system": parts[3].strip() if len(parts) > 3 else ""
|
| 36 |
-
})
|
| 37 |
-
|
| 38 |
-
sessions[session_id] = {
|
| 39 |
-
"policy": policy,
|
| 40 |
-
"agents": agents,
|
| 41 |
-
"history": []
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
-
return session_id, f"Session created! Policy: {policy}, Agents: {len(agents)}"
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def process_message(session_id, message, history):
|
| 48 |
-
"""Process a message in the session"""
|
| 49 |
-
if session_id not in sessions:
|
| 50 |
-
return history + [[message, "No active session. Please create one first."]]
|
| 51 |
-
|
| 52 |
-
session = sessions[session_id]
|
| 53 |
-
|
| 54 |
-
# Add user message
|
| 55 |
-
session["history"].append({
|
| 56 |
-
"sender": "Human",
|
| 57 |
-
"content": message
|
| 58 |
-
})
|
| 59 |
-
|
| 60 |
-
# Simulate response (placeholder - real implementation would call OFP Playground)
|
| 61 |
-
responses = []
|
| 62 |
-
for agent in session["agents"]:
|
| 63 |
-
response = f"[{agent['name']}] Received: \"{message}\"\n\n(This is a demo - connect to real OFP backend for full functionality)"
|
| 64 |
-
responses.append(response)
|
| 65 |
-
session["history"].append({
|
| 66 |
-
"sender": agent["name"],
|
| 67 |
-
"content": response
|
| 68 |
-
})
|
| 69 |
-
|
| 70 |
-
if not responses:
|
| 71 |
-
response = "No agents configured. Add agents and create a session."
|
| 72 |
-
responses.append(response)
|
| 73 |
-
|
| 74 |
-
# Update history
|
| 75 |
-
new_history = history + [[message, "\n\n".join(responses)]]
|
| 76 |
-
return new_history
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
# Demo mode - simple chat interface
|
| 80 |
-
with gr.Blocks(title="Live OFP Backend") as demo:
|
| 81 |
-
gr.Markdown("# 🎭 Live OFP Playground Backend")
|
| 82 |
-
gr.Markdown("Configure your session below, then start chatting!")
|
| 83 |
-
|
| 84 |
-
with gr.Row():
|
| 85 |
-
with gr.Column(scale=1):
|
| 86 |
-
gr.Markdown("### Session Config")
|
| 87 |
-
policy = gr.Dropdown(
|
| 88 |
-
choices=["sequential", "round_robin", "moderated", "free_for_all", "showrunner_driven"],
|
| 89 |
-
value="sequential",
|
| 90 |
-
label="Floor Policy"
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
agents_config = gr.Textbox(
|
| 94 |
-
label="Agents (format: provider:name:type:system)",
|
| 95 |
-
placeholder="anthropic:Claude:text:You are helpful\nopenai:GPT:text:You are creative",
|
| 96 |
-
lines=4
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
create_btn = gr.Button("Create Session", variant="primary")
|
| 100 |
-
session_id = gr.Textbox(label="Session ID", interactive=False)
|
| 101 |
-
|
| 102 |
-
with gr.Column(scale=2):
|
| 103 |
-
gr.Markdown("### Chat")
|
| 104 |
-
chatbot = gr.Chatbot(label="Conversation", height=400)
|
| 105 |
-
with gr.Row():
|
| 106 |
-
msg = gr.Textbox(label="Message", placeholder="Type here...", scale=4)
|
| 107 |
-
send_btn = gr.Button("Send", variant="primary", scale=1)
|
| 108 |
-
|
| 109 |
-
# Event handlers
|
| 110 |
-
def on_create(policy_val, agents_val):
|
| 111 |
-
sid, info = create_session(policy_val, agents_val)
|
| 112 |
-
return sid, f"✅ Session: {sid[:8]}"
|
| 113 |
-
|
| 114 |
-
create_btn.click(
|
| 115 |
-
on_create,
|
| 116 |
-
inputs=[policy, agents_config],
|
| 117 |
-
outputs=[session_id, chatbot]
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
-
def on_send(session_id_val, message_val, history_val):
|
| 121 |
-
if not session_id_val:
|
| 122 |
-
return history_val, "", history_val
|
| 123 |
-
return process_message(session_id_val, message_val, history_val), "", process_message(session_id_val, message_val, history_val)
|
| 124 |
-
|
| 125 |
-
send_btn.click(
|
| 126 |
-
on_send,
|
| 127 |
-
inputs=[session_id, msg, chatbot],
|
| 128 |
-
outputs=[chatbot, msg, chatbot]
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
msg.submit(
|
| 132 |
-
on_send,
|
| 133 |
-
inputs=[session_id, msg, chatbot],
|
| 134 |
-
outputs=[chatbot, msg, chatbot]
|
| 135 |
-
)
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
# Required for HuggingFace Spaces
|
| 139 |
-
demo.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
Live OFP Backend - HuggingFace Space
|
| 3 |
+
Simple Gradio interface
|
| 4 |
"""
|
|
|
|
|
|
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
|
| 7 |
+
# Simple demo app
|
| 8 |
+
def greet(name):
|
| 9 |
+
return f"Hello {name}! This is the Live OFP Backend."
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
demo = gr.Interface(
|
| 12 |
+
fn=greet,
|
| 13 |
+
inputs=gr.Textbox(label="Your name"),
|
| 14 |
+
outputs=gr.Textbox(label="Greeting"),
|
| 15 |
+
title="🎭 Live OFP Playground Backend"
|
| 16 |
+
)
|
| 17 |
|
| 18 |
+
if __name__ == "__main__":
|
| 19 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,2 +1 @@
|
|
| 1 |
-
gradio
|
| 2 |
-
huggingface_hub>=0.19.0
|
|
|
|
| 1 |
+
gradio==4.44.0
|
|
|