Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -65,20 +65,14 @@ def generate_search(query: str, stream: bool = True) -> str:
|
|
65 |
json_modified_value = json.loads(value[6:])
|
66 |
content = json_modified_value.get("choices", [{}])[0].get("delta", {}).get("content", "")
|
67 |
if stream:
|
68 |
-
|
69 |
-
yield f"data: {json_modified_value}\n\n"
|
70 |
streaming_text += content
|
71 |
except json.JSONDecodeError:
|
72 |
continue
|
73 |
|
74 |
if not stream:
|
75 |
-
#
|
76 |
-
yield
|
77 |
-
"choices": [{
|
78 |
-
"message": {"content": streaming_text},
|
79 |
-
"finish_reason": "stop"
|
80 |
-
}]
|
81 |
-
}
|
82 |
|
83 |
@app.get("/searchgpt")
|
84 |
async def search_gpt(q: str, stream: Optional[bool] = False):
|
@@ -91,10 +85,10 @@ async def search_gpt(q: str, stream: Optional[bool] = False):
|
|
91 |
media_type="text/event-stream"
|
92 |
)
|
93 |
else:
|
94 |
-
# For non-streaming
|
95 |
response_text = "".join([chunk for chunk in generate_search(q, stream=False)])
|
96 |
return JSONResponse(content={"response": response_text})
|
97 |
-
|
98 |
@app.get("/", response_class=HTMLResponse)
|
99 |
async def root():
|
100 |
# Open and read the content of index.html (in the same folder as the app)
|
|
|
65 |
json_modified_value = json.loads(value[6:])
|
66 |
content = json_modified_value.get("choices", [{}])[0].get("delta", {}).get("content", "")
|
67 |
if stream:
|
68 |
+
yield f"data: {json.dumps(json_modified_value)}\n\n"
|
|
|
69 |
streaming_text += content
|
70 |
except json.JSONDecodeError:
|
71 |
continue
|
72 |
|
73 |
if not stream:
|
74 |
+
# For non-streaming, just yield the text content
|
75 |
+
yield streaming_text
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
@app.get("/searchgpt")
|
78 |
async def search_gpt(q: str, stream: Optional[bool] = False):
|
|
|
85 |
media_type="text/event-stream"
|
86 |
)
|
87 |
else:
|
88 |
+
# For non-streaming, collect the text and return as JSON response
|
89 |
response_text = "".join([chunk for chunk in generate_search(q, stream=False)])
|
90 |
return JSONResponse(content={"response": response_text})
|
91 |
+
|
92 |
@app.get("/", response_class=HTMLResponse)
|
93 |
async def root():
|
94 |
# Open and read the content of index.html (in the same folder as the app)
|