Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,12 +10,6 @@ LLM_API = os.environ.get("LLM_API")
|
|
10 |
LLM_URL = os.environ.get("LLM_URL")
|
11 |
USER_ID = "HuggingFace Space"
|
12 |
|
13 |
-
# 設置重試次數和延遲
|
14 |
-
MAX_RETRIES = 3
|
15 |
-
RETRY_DELAY = 2
|
16 |
-
TIMEOUT_DURATION = 180 # 超時時間設定為 180 秒
|
17 |
-
MAX_RESPONSES = 15
|
18 |
-
|
19 |
async def send_chat_message(LLM_URL, LLM_API, user_input):
|
20 |
payload = {
|
21 |
"inputs": {},
|
@@ -26,50 +20,42 @@ async def send_chat_message(LLM_URL, LLM_API, user_input):
|
|
26 |
}
|
27 |
print("Sending chat message payload:", payload)
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
continue
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
break
|
56 |
-
except (IndexError, json.JSONDecodeError) as e:
|
57 |
-
print(f"Error parsing line: {line}, error: {e}")
|
58 |
-
continue
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
print(traceback.format_exc())
|
71 |
-
return f"Exception: {e}"
|
72 |
-
return "Error: Reached maximum retry attempts."
|
73 |
|
74 |
async def handle_input(user_input):
|
75 |
print(f"Handling input: {user_input}")
|
@@ -98,7 +84,7 @@ examples = [
|
|
98 |
|
99 |
|
100 |
|
101 |
-
TITLE = """<h1>Social Media Trends 💬 分析社群相關資訊,並判斷其正、負、中立等評價及趨勢 </h1>"""
|
102 |
SUBTITLE = """<h2><a href='https://www.twman.org' target='_blank'>TonTon Huang Ph.D. @ 2024/11 </a><br></h2>"""
|
103 |
LINKS = """
|
104 |
<a href='https://github.com/Deep-Learning-101' target='_blank'>Deep Learning 101 Github</a> | <a href='http://deeplearning101.twman.org' target='_blank'>Deep Learning 101</a> | <a href='https://www.facebook.com/groups/525579498272187/' target='_blank'>台灣人工智慧社團 FB</a> | <a href='https://www.youtube.com/c/DeepLearning101' target='_blank'>YouTube</a><br>
|
|
|
10 |
LLM_URL = os.environ.get("LLM_URL")
|
11 |
USER_ID = "HuggingFace Space"
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
async def send_chat_message(LLM_URL, LLM_API, user_input):
|
14 |
payload = {
|
15 |
"inputs": {},
|
|
|
20 |
}
|
21 |
print("Sending chat message payload:", payload)
|
22 |
|
23 |
+
async with aiohttp.ClientSession() as session:
|
24 |
+
try:
|
25 |
+
async with session.post(
|
26 |
+
url=f"{LLM_URL}/chat-messages",
|
27 |
+
headers={"Authorization": f"Bearer {LLM_API}"},
|
28 |
+
json=payload,
|
29 |
+
timeout=aiohttp.ClientTimeout(total=180)
|
30 |
+
) as response:
|
31 |
+
if response.status != 200:
|
32 |
+
print(f"Error: {response.status}")
|
33 |
+
return f"Error: Status code {response.status}"
|
|
|
34 |
|
35 |
+
full_response = []
|
36 |
+
async for line in response.content.iter_chunked(2048):
|
37 |
+
line = line.decode('utf-8').strip()
|
38 |
+
if not line or "data: " not in line:
|
39 |
+
continue
|
40 |
+
try:
|
41 |
+
data = json.loads(line.split("data: ")[1])
|
42 |
+
if "answer" in data:
|
43 |
+
decoded_answer = urllib.parse.unquote(data["answer"])
|
44 |
+
full_response.append(decoded_answer)
|
45 |
+
except (IndexError, json.JSONDecodeError) as e:
|
46 |
+
print(f"Error parsing line: {line}, error: {e}")
|
47 |
+
continue
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
if full_response:
|
50 |
+
return ''.join(full_response).strip()
|
51 |
+
else:
|
52 |
+
return "Error: No response found in the response"
|
53 |
+
except aiohttp.ClientConnectorError:
|
54 |
+
return "Error: Cannot connect to the API server. Please check the URL and server status."
|
55 |
+
except Exception as e:
|
56 |
+
print("Exception occurred in send_chat_message:")
|
57 |
+
print(traceback.format_exc())
|
58 |
+
return f"Exception: {e}"
|
|
|
|
|
|
|
59 |
|
60 |
async def handle_input(user_input):
|
61 |
print(f"Handling input: {user_input}")
|
|
|
84 |
|
85 |
|
86 |
|
87 |
+
TITLE = """<h1>Social Media Trends 💬 分析社群相關資訊,並判斷其正、負、中立等評價及趨勢 (數據大會跑很久)) </h1>"""
|
88 |
SUBTITLE = """<h2><a href='https://www.twman.org' target='_blank'>TonTon Huang Ph.D. @ 2024/11 </a><br></h2>"""
|
89 |
LINKS = """
|
90 |
<a href='https://github.com/Deep-Learning-101' target='_blank'>Deep Learning 101 Github</a> | <a href='http://deeplearning101.twman.org' target='_blank'>Deep Learning 101</a> | <a href='https://www.facebook.com/groups/525579498272187/' target='_blank'>台灣人工智慧社團 FB</a> | <a href='https://www.youtube.com/c/DeepLearning101' target='_blank'>YouTube</a><br>
|