Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
|
|
3 |
import json, os
|
4 |
|
5 |
LLM_API = os.environ.get("LLM_API")
|
@@ -7,7 +8,7 @@ LLM_URL = os.environ.get("LLM_URL")
|
|
7 |
|
8 |
USER_ID = "HuggingFace Space" # Placeholder user ID
|
9 |
|
10 |
-
def send_chat_message(LLM_URL, LLM_API, user_input):
|
11 |
payload = {
|
12 |
"inputs": {},
|
13 |
"query": user_input,
|
@@ -16,42 +17,45 @@ def send_chat_message(LLM_URL, LLM_API, user_input):
|
|
16 |
"user": USER_ID,
|
17 |
}
|
18 |
print("Sending chat message payload:", payload) # Debug information
|
19 |
-
response = requests.post(
|
20 |
-
url=f"{LLM_URL}/chat-messages",
|
21 |
-
headers={"Authorization": f"Bearer {LLM_API}"},
|
22 |
-
json=payload,
|
23 |
-
stream=True # Enable streaming
|
24 |
-
)
|
25 |
-
if response.status_code != 200:
|
26 |
-
print(f"Error: {response.status_code}")
|
27 |
-
return f"Error: {response.status_code}"
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
52 |
print("Chat response:", chat_response) # Debug information
|
53 |
return chat_response
|
54 |
|
|
|
|
|
|
|
55 |
# Define Gradio interface
|
56 |
user_input = gr.Textbox(label='歡迎問我加密貨幣交易所的各種疑難雜症')
|
57 |
examples = [
|
@@ -71,7 +75,7 @@ with gr.Blocks() as iface:
|
|
71 |
gr.HTML(SUBTITLE)
|
72 |
gr.HTML(LINKS)
|
73 |
gr.Interface(
|
74 |
-
fn=
|
75 |
inputs=user_input,
|
76 |
outputs="text",
|
77 |
examples=examples,
|
|
|
1 |
import gradio as gr
|
2 |
+
import aiohttp
|
3 |
+
import asyncio
|
4 |
import json, os
|
5 |
|
6 |
LLM_API = os.environ.get("LLM_API")
|
|
|
8 |
|
9 |
USER_ID = "HuggingFace Space" # Placeholder user ID
|
10 |
|
11 |
+
async def send_chat_message(LLM_URL, LLM_API, user_input):
|
12 |
payload = {
|
13 |
"inputs": {},
|
14 |
"query": user_input,
|
|
|
17 |
"user": USER_ID,
|
18 |
}
|
19 |
print("Sending chat message payload:", payload) # Debug information
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
async with aiohttp.ClientSession() as session:
|
22 |
+
async with session.post(
|
23 |
+
url=f"{LLM_URL}/chat-messages",
|
24 |
+
headers={"Authorization": f"Bearer {LLM_API}"},
|
25 |
+
json=payload,
|
26 |
+
timeout=aiohttp.ClientTimeout(total=60)
|
27 |
+
) as response:
|
28 |
+
if response.status != 200:
|
29 |
+
print(f"Error: {response.status}")
|
30 |
+
return f"Error: {response.status}"
|
31 |
+
|
32 |
+
# Handle the stream of events
|
33 |
+
full_response = []
|
34 |
+
async for line in response.content:
|
35 |
+
line = line.decode('utf-8')
|
36 |
+
if line:
|
37 |
+
try:
|
38 |
+
print("Received line:", line) # Debug information
|
39 |
+
data = json.loads(line.split("data: ")[1])
|
40 |
+
if "answer" in data:
|
41 |
+
full_response.append(data["answer"])
|
42 |
+
except (IndexError, json.JSONDecodeError) as e:
|
43 |
+
print(f"Error parsing line: {line}, error: {e}") # Debug information
|
44 |
+
continue
|
45 |
|
46 |
+
if full_response:
|
47 |
+
return ''.join(full_response).strip()
|
48 |
+
else:
|
49 |
+
return "Error: No thought found in the response"
|
50 |
+
|
51 |
+
async def handle_input(user_input):
|
52 |
+
chat_response = await send_chat_message(LLM_URL, LLM_API, user_input)
|
53 |
print("Chat response:", chat_response) # Debug information
|
54 |
return chat_response
|
55 |
|
56 |
+
def run_sync(user_input):
|
57 |
+
return asyncio.run(handle_input(user_input))
|
58 |
+
|
59 |
# Define Gradio interface
|
60 |
user_input = gr.Textbox(label='歡迎問我加密貨幣交易所的各種疑難雜症')
|
61 |
examples = [
|
|
|
75 |
gr.HTML(SUBTITLE)
|
76 |
gr.HTML(LINKS)
|
77 |
gr.Interface(
|
78 |
+
fn=run_sync,
|
79 |
inputs=user_input,
|
80 |
outputs="text",
|
81 |
examples=examples,
|