DeepLearning101 commited on
Commit
9cf6750
·
verified ·
1 Parent(s): bd20a22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -130,19 +130,26 @@ async def send_chat_message(user_input):
130
  return f"Error: {response.status}"
131
 
132
  full_response = []
133
- async for line in response.content:
134
- line = line.decode('utf-8').strip()
135
- if not line:
136
- continue
137
- if "data: " not in line:
138
- continue
139
- try:
140
- data = json.loads(line.split("data: ")[1])
141
- if "answer" in data:
142
- full_response.append(data["answer"])
143
- except (IndexError, json.JSONDecodeError) as e:
144
- print(f"Error parsing line: {line}, error: {e}")
145
  continue
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
 
147
  if full_response:
148
  return ''.join(full_response).strip()
 
130
  return f"Error: {response.status}"
131
 
132
  full_response = []
133
+ async for chunk in response.content.iter_any():
134
+ if len(chunk) > 10 * 1024:
135
+ print("Chunk too big, skipping")
 
 
 
 
 
 
 
 
 
136
  continue
137
+
138
+ decoded = chunk.decode('utf-8', errors='ignore')
139
+ lines = decoded.splitlines()
140
+
141
+ for line in lines:
142
+ line = line.strip()
143
+ if not line.startswith("data: "):
144
+ continue
145
+ try:
146
+ data_str = line.split("data: ")[1]
147
+ data = json.loads(data_str)
148
+ if "answer" in data:
149
+ full_response.append(data["answer"])
150
+ except (IndexError, json.JSONDecodeError) as e:
151
+ print(f"Error parsing line: {line}, error: {e}")
152
+ continue
153
 
154
  if full_response:
155
  return ''.join(full_response).strip()