Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -45,14 +45,20 @@ def process_response(response, history):
|
|
45 |
partial_words = ""
|
46 |
for chunk in response.iter_lines():
|
47 |
if chunk:
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)]
|
57 |
return chat, history, token_counter
|
58 |
|
|
|
45 |
partial_words = ""
|
46 |
for chunk in response.iter_lines():
|
47 |
if chunk:
|
48 |
+
try:
|
49 |
+
chunk_json = json.loads(chunk.decode('utf-8'))
|
50 |
+
if 'choices' in chunk_json and len(chunk_json['choices']) > 0:
|
51 |
+
chunk_data = chunk_json['choices'][0].get('delta', {})
|
52 |
+
if 'content' in chunk_data:
|
53 |
+
partial_words += chunk_data['content']
|
54 |
+
if token_counter == 0:
|
55 |
+
history.append(" " + partial_words)
|
56 |
+
else:
|
57 |
+
history[-1] = partial_words
|
58 |
+
token_counter += 1
|
59 |
+
except json.JSONDecodeError:
|
60 |
+
print("Error decoding JSON response")
|
61 |
+
break # or handle the error as you see fit
|
62 |
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)]
|
63 |
return chat, history, token_counter
|
64 |
|