Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,16 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import aiohttp
|
3 |
import asyncio
|
4 |
-
import json
|
|
|
5 |
|
6 |
LLM_API = os.environ.get("LLM_API")
|
7 |
LLM_URL = os.environ.get("LLM_URL")
|
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": {},
|
@@ -19,48 +22,53 @@ async def send_chat_message(LLM_URL, LLM_API, user_input):
|
|
19 |
print("Sending chat message payload:", payload) # Debug information
|
20 |
|
21 |
async with aiohttp.ClientSession() as session:
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
continue
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
53 |
|
54 |
async def handle_input(user_input):
|
|
|
55 |
chat_response = await send_chat_message(LLM_URL, LLM_API, user_input)
|
56 |
print("Chat response:", chat_response) # Debug information
|
57 |
return chat_response
|
58 |
|
59 |
def run_sync(user_input):
|
|
|
60 |
return asyncio.run(handle_input(user_input))
|
61 |
|
62 |
-
#
|
63 |
-
user_input = gr.Textbox(label='歡迎問我加密貨幣交易所的各種疑難雜症')
|
64 |
examples = [
|
65 |
["MAX 帳號刪除關戶後,又重新註冊 MAX 後要怎辦?"],
|
66 |
["手機APP怎麼操作掛單交易?"],
|
@@ -77,12 +85,20 @@ with gr.Blocks() as iface:
|
|
77 |
gr.HTML(TITLE)
|
78 |
gr.HTML(SUBTITLE)
|
79 |
gr.HTML(LINKS)
|
80 |
-
gr.
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
iface.launch()
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
import aiohttp
|
4 |
import asyncio
|
5 |
+
import json
|
6 |
+
from functools import lru_cache
|
7 |
|
8 |
LLM_API = os.environ.get("LLM_API")
|
9 |
LLM_URL = os.environ.get("LLM_URL")
|
10 |
|
11 |
USER_ID = "HuggingFace Space" # Placeholder user ID
|
12 |
|
13 |
+
@lru_cache(maxsize=32)
|
14 |
async def send_chat_message(LLM_URL, LLM_API, user_input):
|
15 |
payload = {
|
16 |
"inputs": {},
|
|
|
22 |
print("Sending chat message payload:", payload) # Debug information
|
23 |
|
24 |
async with aiohttp.ClientSession() as session:
|
25 |
+
try:
|
26 |
+
async with session.post(
|
27 |
+
url=f"{LLM_URL}/chat-messages",
|
28 |
+
headers={"Authorization": f"Bearer {LLM_API}"},
|
29 |
+
json=payload,
|
30 |
+
timeout=aiohttp.ClientTimeout(total=60)
|
31 |
+
) as response:
|
32 |
+
if response.status != 200:
|
33 |
+
print(f"Error: {response.status}")
|
34 |
+
return f"Error: {response.status}"
|
35 |
|
36 |
+
full_response = []
|
37 |
+
async for line in response.content:
|
38 |
+
line = line.decode('utf-8').strip()
|
39 |
+
if not line:
|
40 |
+
continue
|
41 |
+
if "data: " not in line:
|
42 |
+
continue
|
43 |
+
try:
|
44 |
+
print("Received line:", line) # Debug information
|
45 |
+
data = json.loads(line.split("data: ")[1])
|
46 |
+
if "answer" in data:
|
47 |
+
full_response.append(data["answer"])
|
48 |
+
except (IndexError, json.JSONDecodeError) as e:
|
49 |
+
print(f"Error parsing line: {line}, error: {e}") # Debug information
|
50 |
+
continue
|
|
|
51 |
|
52 |
+
if full_response:
|
53 |
+
return ''.join(full_response).strip()
|
54 |
+
else:
|
55 |
+
return "Error: No response found in the response"
|
56 |
+
except Exception as e:
|
57 |
+
print(f"Exception: {e}")
|
58 |
+
return f"Exception: {e}"
|
59 |
|
60 |
async def handle_input(user_input):
|
61 |
+
print(f"Handling input: {user_input}")
|
62 |
chat_response = await send_chat_message(LLM_URL, LLM_API, user_input)
|
63 |
print("Chat response:", chat_response) # Debug information
|
64 |
return chat_response
|
65 |
|
66 |
def run_sync(user_input):
|
67 |
+
print(f"Running sync with input: {user_input}")
|
68 |
return asyncio.run(handle_input(user_input))
|
69 |
|
70 |
+
# 定義 Gradio 界面
|
71 |
+
user_input = gr.Textbox(label='歡迎問我加密貨幣交易所的各種疑難雜症', placeholder='在此輸入問題...')
|
72 |
examples = [
|
73 |
["MAX 帳號刪除關戶後,又重新註冊 MAX 後要怎辦?"],
|
74 |
["手機APP怎麼操作掛單交易?"],
|
|
|
85 |
gr.HTML(TITLE)
|
86 |
gr.HTML(SUBTITLE)
|
87 |
gr.HTML(LINKS)
|
88 |
+
with gr.Row():
|
89 |
+
chatbot = gr.Chatbot()
|
90 |
+
with gr.Row():
|
91 |
+
user_input = gr.Textbox(label='輸入您的問題', placeholder="在此輸入問題...")
|
92 |
+
submit_button = gr.Button("送出")
|
93 |
+
gr.Examples(examples=examples, inputs=user_input)
|
94 |
+
with gr.Row():
|
95 |
+
feedback_output = gr.Textbox(label='反饋結果', interactive=False)
|
96 |
+
|
97 |
+
def chat(user_input, history):
|
98 |
+
response = run_sync(user_input)
|
99 |
+
history.append((user_input, response))
|
100 |
+
return history, history
|
101 |
+
|
102 |
+
submit_button.click(fn=chat, inputs=[user_input, chatbot], outputs=[chatbot, chatbot])
|
103 |
|
104 |
iface.launch()
|