developerjeremylive commited on
Commit
17b7415
·
1 Parent(s): 30d4849

Add Gradio app

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