Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,103 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="Have A Good Day")
|
| 7 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from zhipuai import ZhipuAI
|
| 4 |
|
| 5 |
+
# === GLM setup ===
|
| 6 |
+
api_key = os.getenv("ZHIPUAI_API_KEY")
|
| 7 |
+
if not api_key:
|
| 8 |
+
print("β [Startup Error] Missing ZHIPUAI_API_KEY in environment variables.")
|
| 9 |
+
else:
|
| 10 |
+
print("β
[Startup] ZHIPUAI_API_KEY loaded successfully.")
|
| 11 |
+
|
| 12 |
+
zhipu_client = ZhipuAI(api_key=api_key) if api_key else None
|
| 13 |
+
|
| 14 |
+
# === Helper: call GLM ===
|
| 15 |
+
def glm_call(prompt: str) -> str:
|
| 16 |
+
if not zhipu_client:
|
| 17 |
+
return "[GLM Error] API key not found. Please set ZHIPUAI_API_KEY."
|
| 18 |
+
try:
|
| 19 |
+
print(f"π‘ Sending prompt to GLM: {prompt}")
|
| 20 |
+
resp = zhipu_client.chat.completions.create(
|
| 21 |
+
model="glm-4-0520",
|
| 22 |
+
messages=[{"role": "user", "content": prompt}]
|
| 23 |
+
)
|
| 24 |
+
reply = resp.choices[0].message["content"]
|
| 25 |
+
print(f"π€ GLM replied: {reply}")
|
| 26 |
+
return reply
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"[GLM Error] {e}"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# === Agent Implementations ===
|
| 32 |
+
|
| 33 |
+
# Autogen Agent (wired to GLM)
|
| 34 |
+
def autogen_agent(prompt: str) -> str:
|
| 35 |
+
return f"[Autogen Agent] {glm_call(prompt)}"
|
| 36 |
+
|
| 37 |
+
# LangGraph Agent (wired to GLM)
|
| 38 |
+
def langgraph_agent(prompt: str) -> str:
|
| 39 |
+
return f"[LangGraph Agent] {glm_call(prompt)}"
|
| 40 |
+
|
| 41 |
+
# CrewAI Agent (stub for now)
|
| 42 |
+
def crewai_agent(prompt: str) -> str:
|
| 43 |
+
return "[CrewAI Agent] β not connected to GLM (stubbed for now)"
|
| 44 |
+
|
| 45 |
+
# OpenDevin Agent (stub for now)
|
| 46 |
+
def opendevin_agent(prompt: str) -> str:
|
| 47 |
+
return "[OpenDevin Agent] β not connected to GLM (stubbed for now)"
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# === Gradio Handlers ===
|
| 51 |
+
|
| 52 |
+
def single_agent(agent: str, prompt: str) -> str:
|
| 53 |
+
if agent == "Autogen":
|
| 54 |
+
return autogen_agent(prompt)
|
| 55 |
+
elif agent == "LangGraph":
|
| 56 |
+
return langgraph_agent(prompt)
|
| 57 |
+
elif agent == "CrewAI":
|
| 58 |
+
return crewai_agent(prompt)
|
| 59 |
+
elif agent == "OpenDevin":
|
| 60 |
+
return opendevin_agent(prompt)
|
| 61 |
+
else:
|
| 62 |
+
return "[Error] Unknown agent selected."
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def all_agents(prompt: str) -> str:
|
| 66 |
+
outputs = []
|
| 67 |
+
outputs.append(autogen_agent(prompt))
|
| 68 |
+
outputs.append(langgraph_agent(prompt))
|
| 69 |
+
outputs.append(crewai_agent(prompt))
|
| 70 |
+
outputs.append(opendevin_agent(prompt))
|
| 71 |
+
return "\n\n".join(outputs)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# === Gradio UI ===
|
| 75 |
+
def build_ui():
|
| 76 |
+
with gr.Blocks() as demo:
|
| 77 |
+
gr.Markdown("## π€ Multi-Agent GLM Playground")
|
| 78 |
+
|
| 79 |
+
with gr.Tab("Single Agent"):
|
| 80 |
+
agent = gr.Dropdown(
|
| 81 |
+
["Autogen", "LangGraph", "CrewAI", "OpenDevin"],
|
| 82 |
+
label="Choose an agent"
|
| 83 |
+
)
|
| 84 |
+
prompt = gr.Textbox(label="Your prompt")
|
| 85 |
+
output = gr.Textbox(label="Agent response")
|
| 86 |
+
run_btn = gr.Button("Run")
|
| 87 |
+
|
| 88 |
+
run_btn.click(fn=single_agent, inputs=[agent, prompt], outputs=output)
|
| 89 |
+
|
| 90 |
+
with gr.Tab("All Agents"):
|
| 91 |
+
prompt_all = gr.Textbox(label="Your prompt")
|
| 92 |
+
output_all = gr.Textbox(label="Responses")
|
| 93 |
+
run_all_btn = gr.Button("Run All")
|
| 94 |
+
|
| 95 |
+
run_all_btn.click(fn=all_agents, inputs=prompt_all, outputs=output_all)
|
| 96 |
+
|
| 97 |
+
return demo
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
if __name__ == "__main__":
|
| 101 |
+
demo = build_ui()
|
| 102 |
+
demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))
|
| 103 |
|
|
|
|
|
|