wlchee commited on
Commit
691ca73
·
verified ·
1 Parent(s): f930515

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
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
- output = ""
19
- try:
20
- for event in client.events():
21
- if event.data == "[DONE]":
22
- break
23
- output += event.data
24
- yield [{"role": "assistant", "content": output}]
25
- except (StopIteration, StopAsyncIteration):
26
- # Graceful end of generator
27
- return
 
28
  except Exception as e:
29
- yield [{"role": "assistant", "content": f"Error: {e}"}]
30
 
31
- gr.ChatInterface(fn=chat, title="Sentiment Tiny Agent", type="messages").launch(share=True)
 
 
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()