Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -33,6 +33,9 @@ class Payload(BaseModel):
|
|
33 |
messages: list
|
34 |
stream: bool
|
35 |
|
|
|
|
|
|
|
36 |
def generate_search(query: str, stream: bool = True) -> str:
|
37 |
headers = {"User-Agent": ""}
|
38 |
prompt = [
|
@@ -56,20 +59,22 @@ def generate_search(query: str, stream: bool = True) -> str:
|
|
56 |
# Collect streamed text content
|
57 |
streaming_text = ""
|
58 |
for value in response.iter_lines(decode_unicode=True, chunk_size=12):
|
59 |
-
|
60 |
-
if modified_value:
|
61 |
try:
|
62 |
-
|
63 |
-
content =
|
|
|
|
|
64 |
if stream:
|
65 |
-
yield
|
|
|
66 |
streaming_text += content
|
67 |
-
except:
|
68 |
continue
|
69 |
|
70 |
if not stream:
|
71 |
yield streaming_text
|
72 |
-
|
73 |
@app.get("/searchgpt")
|
74 |
async def search_gpt(q: str, stream: Optional[bool] = False):
|
75 |
if not q:
|
|
|
33 |
messages: list
|
34 |
stream: bool
|
35 |
|
36 |
+
import requests
|
37 |
+
import json
|
38 |
+
|
39 |
def generate_search(query: str, stream: bool = True) -> str:
|
40 |
headers = {"User-Agent": ""}
|
41 |
prompt = [
|
|
|
59 |
# Collect streamed text content
|
60 |
streaming_text = ""
|
61 |
for value in response.iter_lines(decode_unicode=True, chunk_size=12):
|
62 |
+
if value:
|
|
|
63 |
try:
|
64 |
+
json_value = json.loads(value) # Process the JSON content directly
|
65 |
+
content = json_value["choices"][0]["delta"]["content"]
|
66 |
+
|
67 |
+
# Stream the raw content directly (no 'data:' prefix)
|
68 |
if stream:
|
69 |
+
yield content
|
70 |
+
|
71 |
streaming_text += content
|
72 |
+
except json.JSONDecodeError:
|
73 |
continue
|
74 |
|
75 |
if not stream:
|
76 |
yield streaming_text
|
77 |
+
|
78 |
@app.get("/searchgpt")
|
79 |
async def search_gpt(q: str, stream: Optional[bool] = False):
|
80 |
if not q:
|