Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -39,6 +39,9 @@ async def favicon():
|
|
39 |
favicon_path = Path(__file__).parent / "favicon.ico"
|
40 |
return FileResponse(favicon_path, media_type="image/x-icon")
|
41 |
|
|
|
|
|
|
|
42 |
def generate_search(query: str, stream: bool = True) -> str:
|
43 |
headers = {"User-Agent": ""}
|
44 |
prompt = [
|
@@ -65,19 +68,20 @@ def generate_search(query: str, stream: bool = True) -> str:
|
|
65 |
# Ensure the value starts with 'data: ' and process it
|
66 |
if value.startswith("data: "):
|
67 |
try:
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
74 |
except json.JSONDecodeError:
|
75 |
continue # Skip lines that are not valid JSON
|
76 |
|
77 |
# If not streaming, yield the full collected content
|
78 |
if not stream:
|
79 |
yield streaming_text
|
80 |
-
|
81 |
@app.get("/searchgpt")
|
82 |
async def search_gpt(q: str, stream: Optional[bool] = False):
|
83 |
if not q:
|
|
|
39 |
favicon_path = Path(__file__).parent / "favicon.ico"
|
40 |
return FileResponse(favicon_path, media_type="image/x-icon")
|
41 |
|
42 |
+
import requests
|
43 |
+
import json
|
44 |
+
|
45 |
def generate_search(query: str, stream: bool = True) -> str:
|
46 |
headers = {"User-Agent": ""}
|
47 |
prompt = [
|
|
|
68 |
# Ensure the value starts with 'data: ' and process it
|
69 |
if value.startswith("data: "):
|
70 |
try:
|
71 |
+
# Parse JSON and remove 'data: ' prefix
|
72 |
+
json_modified_value = json.loads(value[6:])
|
73 |
+
content = json_modified_value.get("content", "") # Get the actual content
|
74 |
+
|
75 |
+
if content.strip(): # Ensure non-empty content
|
76 |
+
if stream:
|
77 |
+
yield content # Pass content directly, removing 'data: ' and JSON overhead
|
78 |
+
streaming_text += content # Accumulate content for non-streaming case
|
79 |
except json.JSONDecodeError:
|
80 |
continue # Skip lines that are not valid JSON
|
81 |
|
82 |
# If not streaming, yield the full collected content
|
83 |
if not stream:
|
84 |
yield streaming_text
|
|
|
85 |
@app.get("/searchgpt")
|
86 |
async def search_gpt(q: str, stream: Optional[bool] = False):
|
87 |
if not q:
|