Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,21 +11,24 @@ def chat(message, history):
|
|
| 11 |
}
|
| 12 |
payload = json.dumps({"input": message})
|
| 13 |
|
|
|
|
| 14 |
try:
|
| 15 |
response = requests.post(url, data=payload, headers=headers, stream=True)
|
| 16 |
client = sseclient.SSEClient(response)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
-
yield [{"role": "assistant", "content": f"Error: {e}"}]
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
| 11 |
}
|
| 12 |
payload = json.dumps({"input": message})
|
| 13 |
|
| 14 |
+
output = ""
|
| 15 |
try:
|
| 16 |
response = requests.post(url, data=payload, headers=headers, stream=True)
|
| 17 |
client = sseclient.SSEClient(response)
|
| 18 |
|
| 19 |
+
for event in client.events():
|
| 20 |
+
if event.data == "[DONE]":
|
| 21 |
+
break
|
| 22 |
+
output += event.data
|
| 23 |
+
# Always yield messages in OpenAI format
|
| 24 |
+
yield [{"role": "assistant", "content": output}]
|
| 25 |
+
|
| 26 |
+
# If no output was produced at all
|
| 27 |
+
if output.strip() == "":
|
| 28 |
+
yield [{"role": "assistant", "content": "⚠️ No response received from the MCP server."}]
|
| 29 |
+
|
| 30 |
except Exception as e:
|
| 31 |
+
yield [{"role": "assistant", "content": f"❌ Error: {str(e)}"}]
|
| 32 |
|
| 33 |
+
# Launch without share=True in a Space
|
| 34 |
+
gr.ChatInterface(fn=chat, title="Sentiment Tiny Agent", type="messages").launch()
|