DeepLearning101 commited on
Commit
a97fead
·
verified ·
1 Parent(s): 8303b37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -15
app.py CHANGED
@@ -4,19 +4,17 @@ import aiohttp
4
  import asyncio
5
  import json
6
  import urllib.parse
7
- import traceback # 新增此行以便捕捉完整的異常堆疊
8
-
9
 
10
  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
 
15
  # 設置重試次數和延遲
16
- MAX_RETRIES = 2
17
- RETRY_DELAY = 1 # 每次重試之間的延遲秒數
18
- TIMEOUT_DURATION = 120 # 超時時間設定為 120
19
- MAX_RESPONSES = 10 # 設置最大接收數量,避免無限等待
20
 
21
  async def send_chat_message(LLM_URL, LLM_API, user_input):
22
  payload = {
@@ -42,12 +40,9 @@ async def send_chat_message(LLM_URL, LLM_API, user_input):
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)
@@ -55,7 +50,7 @@ async def send_chat_message(LLM_URL, LLM_API, user_input):
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}")
@@ -67,6 +62,7 @@ async def send_chat_message(LLM_URL, LLM_API, user_input):
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:")
@@ -77,7 +73,7 @@ async def send_chat_message(LLM_URL, LLM_API, user_input):
77
  async def handle_input(user_input):
78
  print(f"Handling input: {user_input}")
79
  chat_response = await send_chat_message(LLM_URL, LLM_API, user_input)
80
- print("Chat response:", chat_response) # Debug information
81
  return chat_response
82
 
83
  def run_sync(func, *args):
 
4
  import asyncio
5
  import json
6
  import urllib.parse
7
+ import traceback
 
8
 
9
  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 = {
 
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)
 
50
  if "answer" in data:
51
  decoded_answer = urllib.parse.unquote(data["answer"])
52
  full_response.append(decoded_answer)
53
+ if len(full_response) >= MAX_RESPONSES:
54
  break
55
  except (IndexError, json.JSONDecodeError) as e:
56
  print(f"Error parsing line: {line}, error: {e}")
 
62
  return "Error: No response found in the response"
63
  except asyncio.TimeoutError:
64
  print(f"Attempt {attempt + 1} timed out. Retrying...")
65
+ await asyncio.sleep(RETRY_DELAY)
66
  continue
67
  except Exception as e:
68
  print("Exception occurred in send_chat_message:")
 
73
  async def handle_input(user_input):
74
  print(f"Handling input: {user_input}")
75
  chat_response = await send_chat_message(LLM_URL, LLM_API, user_input)
76
+ print("Chat response:", chat_response)
77
  return chat_response
78
 
79
  def run_sync(func, *args):