wlchee commited on
Commit
4558342
·
verified ·
1 Parent(s): 046eef9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import sseclient
4
+
5
+ def stream_response(prompt):
6
+ url = "https://wlchee-mcp-sentiment.hf.space/gradio_api/mcp/sse"
7
+ headers = {
8
+ "Accept": "text/event-stream",
9
+ "Content-Type": "application/json"
10
+ }
11
+ payload = {"input": prompt}
12
+
13
+ try:
14
+ # Make a streaming request
15
+ response = requests.post(url, json=payload, stream=True, headers=headers)
16
+ client = sseclient.SSEClient(response)
17
+
18
+ full_output = ""
19
+ for event in client.events():
20
+ if event.data == "[DONE]":
21
+ break
22
+ full_output += event.data
23
+ yield full_output # Gradio can stream this in real-time
24
+ except Exception as e:
25
+ yield f"Error: {e}"
26
+
27
+ demo = gr.Interface(fn=stream_response, inputs="text", outputs="text", live=True)
28
+ demo.launch()