Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,10 +11,6 @@ LLM_API = os.environ.get("LLM_API")
|
|
11 |
LLM_URL = os.environ.get("LLM_URL")
|
12 |
USER_ID = "HuggingFace Space" # Placeholder user ID
|
13 |
|
14 |
-
import traceback
|
15 |
-
import aiohttp
|
16 |
-
import asyncio
|
17 |
-
import urllib.parse
|
18 |
|
19 |
# 設置重試次數和延遲
|
20 |
MAX_RETRIES = 2
|
@@ -32,46 +28,51 @@ async def send_chat_message(LLM_URL, LLM_API, user_input):
|
|
32 |
}
|
33 |
print("Sending chat message payload:", payload)
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
full_response = []
|
48 |
-
|
49 |
-
async for line in response.content.iter_chunked(2048): # 增大分段大小
|
50 |
-
line = line.decode('utf-8').strip()
|
51 |
-
if not line:
|
52 |
-
continue
|
53 |
-
if "data: " not in line:
|
54 |
-
continue
|
55 |
-
try:
|
56 |
-
print("Received line:", line)
|
57 |
-
data = json.loads(line.split("data: ")[1])
|
58 |
-
if "answer" in data:
|
59 |
-
decoded_answer = urllib.parse.unquote(data["answer"])
|
60 |
-
full_response.append(decoded_answer)
|
61 |
-
if len(full_response) >= MAX_RESPONSES: # 接收上限,避免無限等待
|
62 |
-
break
|
63 |
-
except (IndexError, json.JSONDecodeError) as e:
|
64 |
-
print(f"Error parsing line: {line}, error: {e}")
|
65 |
continue
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
async def handle_input(user_input):
|
77 |
print(f"Handling input: {user_input}")
|
@@ -85,7 +86,7 @@ def run_sync(func, *args):
|
|
85 |
result = loop.run_until_complete(func(*args))
|
86 |
loop.close()
|
87 |
return result
|
88 |
-
|
89 |
# 定義 Gradio 介面
|
90 |
user_input = gr.Textbox(label='請輸入您想查詢的關鍵公司名稱')
|
91 |
examples = [
|
|
|
11 |
LLM_URL = os.environ.get("LLM_URL")
|
12 |
USER_ID = "HuggingFace Space" # Placeholder user ID
|
13 |
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# 設置重試次數和延遲
|
16 |
MAX_RETRIES = 2
|
|
|
28 |
}
|
29 |
print("Sending chat message payload:", payload)
|
30 |
|
31 |
+
for attempt in range(MAX_RETRIES):
|
32 |
+
async with aiohttp.ClientSession() as session:
|
33 |
+
try:
|
34 |
+
async with session.post(
|
35 |
+
url=f"{LLM_URL}/chat-messages",
|
36 |
+
headers={"Authorization": f"Bearer {LLM_API}"},
|
37 |
+
json=payload,
|
38 |
+
timeout=aiohttp.ClientTimeout(total=TIMEOUT_DURATION)
|
39 |
+
) as response:
|
40 |
+
if response.status != 200:
|
41 |
+
print(f"Error: {response.status}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
continue
|
43 |
|
44 |
+
full_response = []
|
45 |
+
|
46 |
+
async for line in response.content.iter_chunked(4096): # 再次增大分段大小
|
47 |
+
line = line.decode('utf-8').strip()
|
48 |
+
if not line:
|
49 |
+
continue
|
50 |
+
if "data: " not in line:
|
51 |
+
continue
|
52 |
+
try:
|
53 |
+
print("Received line:", line)
|
54 |
+
data = json.loads(line.split("data: ")[1])
|
55 |
+
if "answer" in data:
|
56 |
+
decoded_answer = urllib.parse.unquote(data["answer"])
|
57 |
+
full_response.append(decoded_answer)
|
58 |
+
if len(full_response) >= MAX_RESPONSES: # 接收上限,避免無限等待
|
59 |
+
break
|
60 |
+
except (IndexError, json.JSONDecodeError) as e:
|
61 |
+
print(f"Error parsing line: {line}, error: {e}")
|
62 |
+
continue
|
63 |
+
|
64 |
+
if full_response:
|
65 |
+
return ''.join(full_response).strip()
|
66 |
+
else:
|
67 |
+
return "Error: No response found in the response"
|
68 |
+
except asyncio.TimeoutError:
|
69 |
+
print(f"Attempt {attempt + 1} timed out. Retrying...")
|
70 |
+
continue
|
71 |
+
except Exception as e:
|
72 |
+
print("Exception occurred in send_chat_message:")
|
73 |
+
print(traceback.format_exc())
|
74 |
+
return f"Exception: {e}"
|
75 |
+
return "Error: Reached maximum retry attempts."
|
76 |
|
77 |
async def handle_input(user_input):
|
78 |
print(f"Handling input: {user_input}")
|
|
|
86 |
result = loop.run_until_complete(func(*args))
|
87 |
loop.close()
|
88 |
return result
|
89 |
+
|
90 |
# 定義 Gradio 介面
|
91 |
user_input = gr.Textbox(label='請輸入您想查詢的關鍵公司名稱')
|
92 |
examples = [
|