DeepLearning101 commited on
Commit
7942eff
·
verified ·
1 Parent(s): 4a8a1fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -49
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
- for attempt in range(MAX_RETRIES):
30
- async with aiohttp.ClientSession() as session:
31
- try:
32
- async with session.post(
33
- url=f"{LLM_URL}/chat-messages",
34
- headers={"Authorization": f"Bearer {LLM_API}"},
35
- json=payload,
36
- timeout=aiohttp.ClientTimeout(total=TIMEOUT_DURATION)
37
- ) as response:
38
- if response.status != 200:
39
- print(f"Error: {response.status}")
40
- continue
41
 
42
- full_response = []
43
- async for line in response.content.iter_chunked(2048):
44
- line = line.decode('utf-8').strip()
45
- if not line or "data: " not in line:
46
- continue
47
- try:
48
- print("Received line:", line)
49
- json_str = line.split("data: ", 1)[-1]
50
- data = json.loads(json_str)
51
- if "answer" in data:
52
- decoded_answer = urllib.parse.unquote(data["answer"])
53
- full_response.append(decoded_answer)
54
- if len(full_response) >= MAX_RESPONSES:
55
- break
56
- except (IndexError, json.JSONDecodeError) as e:
57
- print(f"Error parsing line: {line}, error: {e}")
58
- continue
59
 
60
- if full_response:
61
- return ''.join(full_response).strip()
62
- else:
63
- return "Error: No response found in the response"
64
- except asyncio.TimeoutError:
65
- print(f"Attempt {attempt + 1} timed out. Retrying...")
66
- await asyncio.sleep(RETRY_DELAY)
67
- continue
68
- except Exception as e:
69
- print("Exception occurred in send_chat_message:")
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>