Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -25,71 +25,44 @@ all_agents = [
|
|
25 |
agent_map = {agent.name: agent for agent in all_agents}
|
26 |
|
27 |
|
28 |
-
# β
Core
|
29 |
def chat(prompt, selected_agents):
|
30 |
responses = {}
|
31 |
for name in selected_agents:
|
32 |
agent = agent_map.get(name)
|
33 |
try:
|
34 |
output = agent.generate([ACPMessage(role="user", content=prompt)])
|
35 |
-
if isinstance(output, list): # Handle output like [msg]
|
36 |
-
output = output[0]
|
37 |
except Exception as e:
|
38 |
output = f"[ERROR: {e}]"
|
39 |
responses[name] = output
|
|
|
40 |
|
41 |
-
return [responses] # β
Must return list for Gradio API format
|
42 |
-
# That results in: { "data": [ { "Philosopher": "...", ... } ] }
|
43 |
|
44 |
-
|
45 |
-
# β
Interface for direct testing/debugging (optional if using just API)
|
46 |
iface = gr.Interface(
|
47 |
fn=chat,
|
48 |
inputs=[
|
49 |
gr.Textbox(
|
50 |
label="Ask Anything",
|
51 |
lines=4,
|
52 |
-
placeholder="Ask your question here..."
|
53 |
-
elem_id="chat-input"
|
54 |
),
|
55 |
gr.CheckboxGroup(
|
56 |
choices=list(agent_map.keys()),
|
57 |
label="Choose Which Personalities to Ask",
|
58 |
-
value=list(agent_map.keys()) # β
|
59 |
)
|
60 |
],
|
61 |
outputs=gr.JSON(label="Responses"),
|
62 |
title="PerspectiveAI",
|
63 |
-
description=
|
64 |
-
|
65 |
-
"Created by Aymn Β· [GitHub](https://github.com/aymnsk) Β· "
|
66 |
-
"[Instagram](https://instagram.com/damnn_aymn)"
|
67 |
-
),
|
68 |
-
theme=gr.themes.Soft(
|
69 |
-
primary_hue="blue",
|
70 |
-
secondary_hue="gray",
|
71 |
-
font=[gr.themes.GoogleFont("Inter")],
|
72 |
-
radius_size=gr.themes.sizes.radius_md,
|
73 |
-
spacing_size=gr.themes.sizes.spacing_md
|
74 |
-
),
|
75 |
-
css="""
|
76 |
-
#chat-input textarea {
|
77 |
-
font-size: 1.1rem;
|
78 |
-
padding: 12px;
|
79 |
-
background: #1e1e2f;
|
80 |
-
color: white;
|
81 |
-
border-radius: 12px;
|
82 |
-
}
|
83 |
-
body {
|
84 |
-
background-color: #111827;
|
85 |
-
color: white;
|
86 |
-
font-family: 'Inter', sans-serif;
|
87 |
-
}
|
88 |
-
.gr-box {
|
89 |
-
border: none;
|
90 |
-
}
|
91 |
-
"""
|
92 |
)
|
93 |
|
94 |
-
# β
Launch
|
95 |
-
iface.launch(
|
|
|
|
|
|
|
|
|
|
|
|
25 |
agent_map = {agent.name: agent for agent in all_agents}
|
26 |
|
27 |
|
28 |
+
# β
Core chat function for API call
|
29 |
def chat(prompt, selected_agents):
|
30 |
responses = {}
|
31 |
for name in selected_agents:
|
32 |
agent = agent_map.get(name)
|
33 |
try:
|
34 |
output = agent.generate([ACPMessage(role="user", content=prompt)])
|
|
|
|
|
35 |
except Exception as e:
|
36 |
output = f"[ERROR: {e}]"
|
37 |
responses[name] = output
|
38 |
+
return [responses] # β
Must be inside a list for Gradio JSON API
|
39 |
|
|
|
|
|
40 |
|
41 |
+
# β
Gradio Interface
|
|
|
42 |
iface = gr.Interface(
|
43 |
fn=chat,
|
44 |
inputs=[
|
45 |
gr.Textbox(
|
46 |
label="Ask Anything",
|
47 |
lines=4,
|
48 |
+
placeholder="Ask your question here..."
|
|
|
49 |
),
|
50 |
gr.CheckboxGroup(
|
51 |
choices=list(agent_map.keys()),
|
52 |
label="Choose Which Personalities to Ask",
|
53 |
+
value=list(agent_map.keys()) # β
All selected by default
|
54 |
)
|
55 |
],
|
56 |
outputs=gr.JSON(label="Responses"),
|
57 |
title="PerspectiveAI",
|
58 |
+
description="Ask a question, get responses from 8 unique AI personas.",
|
59 |
+
theme=gr.themes.Soft()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
)
|
61 |
|
62 |
+
# β
Launch with API mode enabled (important!)
|
63 |
+
iface.launch(
|
64 |
+
server_name="0.0.0.0",
|
65 |
+
server_port=7860,
|
66 |
+
show_api=True,
|
67 |
+
ssr=False # π¨ This disables SvelteKit SSR and enables API mode
|
68 |
+
)
|