Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -57,18 +57,20 @@ def generate_search(query: str, stream: bool = True) -> str:
|
|
57 |
|
58 |
# Collect streamed text content
|
59 |
streaming_text = ""
|
60 |
-
for value in response.iter_lines(decode_unicode=True
|
61 |
-
|
62 |
-
if
|
63 |
try:
|
64 |
-
json_modified_value = json.loads(
|
65 |
-
content = json_modified_value
|
66 |
-
if
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
71 |
|
|
|
72 |
if not stream:
|
73 |
yield streaming_text
|
74 |
|
|
|
57 |
|
58 |
# Collect streamed text content
|
59 |
streaming_text = ""
|
60 |
+
for value in response.iter_lines(decode_unicode=True):
|
61 |
+
# Ensure the value is clean and processable
|
62 |
+
if value.startswith("data: "): # Ensure the prefix matches expected format
|
63 |
try:
|
64 |
+
json_modified_value = json.loads(value[6:]) # Remove 'data: ' prefix
|
65 |
+
content = json_modified_value.get("choices", [{}])[0].get("delta", {}).get("content", "")
|
66 |
+
if content.strip(): # Avoid empty content from delta
|
67 |
+
if stream:
|
68 |
+
yield f"data: {json.dumps({'response': content})}\n\n"
|
69 |
+
streaming_text += content
|
70 |
+
except json.JSONDecodeError:
|
71 |
+
continue # Skip lines that are not valid JSON
|
72 |
|
73 |
+
# If not streaming, yield the full text
|
74 |
if not stream:
|
75 |
yield streaming_text
|
76 |
|