Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -33,39 +33,41 @@ def respond(
|
|
33 |
temperature=temperature,
|
34 |
top_p=top_p,
|
35 |
):
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
print(f"
|
|
|
|
|
60 |
|
61 |
# Final yield to ensure all content is returned
|
62 |
if response:
|
63 |
yield response
|
64 |
|
65 |
except Exception as e:
|
66 |
-
print(f"An error occurred: {e}")
|
67 |
if response:
|
68 |
-
yield response
|
69 |
else:
|
70 |
yield f"An error occurred: {e}"
|
71 |
|
|
|
33 |
temperature=temperature,
|
34 |
top_p=top_p,
|
35 |
):
|
36 |
+
try:
|
37 |
+
if isinstance(message, ChatCompletionStreamOutput):
|
38 |
+
content = message.choices[0].delta.content
|
39 |
+
if content is not None:
|
40 |
+
response += content
|
41 |
+
yield response
|
42 |
+
if message.choices[0].finish_reason == 'stop':
|
43 |
+
break
|
44 |
+
elif isinstance(message, dict):
|
45 |
+
content = message.get('choices', [{}])[0].get('delta', {}).get('content')
|
46 |
+
if content:
|
47 |
+
response += content
|
48 |
+
yield response
|
49 |
+
if message.get('choices', [{}])[0].get('finish_reason') == 'stop':
|
50 |
+
break
|
51 |
+
elif isinstance(message, str):
|
52 |
+
if message.strip(): # Only process non-empty strings
|
53 |
+
response += message
|
54 |
+
yield response
|
55 |
+
else:
|
56 |
+
print(f"Unexpected message type: {type(message)}")
|
57 |
+
print(f"Message content: {message}")
|
58 |
+
except Exception as e:
|
59 |
+
print(f"Error processing message: {e}")
|
60 |
+
print(f"Problematic message: {message}")
|
61 |
+
continue # Continue to the next message even if there's an error
|
62 |
|
63 |
# Final yield to ensure all content is returned
|
64 |
if response:
|
65 |
yield response
|
66 |
|
67 |
except Exception as e:
|
68 |
+
print(f"An error occurred in the main loop: {e}")
|
69 |
if response:
|
70 |
+
yield response
|
71 |
else:
|
72 |
yield f"An error occurred: {e}"
|
73 |
|