Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -7,6 +7,7 @@ import os
|
|
| 7 |
import time
|
| 8 |
import httpx
|
| 9 |
import json
|
|
|
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
|
| 12 |
load_dotenv()
|
|
@@ -129,6 +130,7 @@ async def create_completion(model: str, content: str, return_stream: bool, retry
|
|
| 129 |
token = await request_token()
|
| 130 |
|
| 131 |
try:
|
|
|
|
| 132 |
async with httpx.AsyncClient() as client:
|
| 133 |
response = await client.post(
|
| 134 |
"https://duckduckgo.com/duckchat/v1/chat",
|
|
@@ -141,8 +143,7 @@ async def create_completion(model: str, content: str, return_stream: bool, retry
|
|
| 141 |
json={
|
| 142 |
"model": model,
|
| 143 |
"messages": [{"role": "user", "content": content}]
|
| 144 |
-
}
|
| 145 |
-
stream=True
|
| 146 |
)
|
| 147 |
|
| 148 |
if response.status_code != 200:
|
|
@@ -165,8 +166,9 @@ async def process_stream(model: str, response, return_stream: bool):
|
|
| 165 |
nonlocal buffer, full_text
|
| 166 |
|
| 167 |
# Process chunks as they arrive
|
| 168 |
-
|
| 169 |
-
|
|
|
|
| 170 |
|
| 171 |
# Handle buffer from previous chunk if needed
|
| 172 |
if buffer:
|
|
@@ -208,8 +210,8 @@ async def process_stream(model: str, response, return_stream: bool):
|
|
| 208 |
if return_stream:
|
| 209 |
return StreamingResponse(generate_stream(), media_type="text/event-stream")
|
| 210 |
else:
|
| 211 |
-
# For non-streaming,
|
| 212 |
-
async for
|
| 213 |
pass # Just collecting text in full_text
|
| 214 |
|
| 215 |
return JSONResponse(content=create_complete_response(full_text, model))
|
|
@@ -263,9 +265,6 @@ def create_complete_response(text: str, model: str) -> dict:
|
|
| 263 |
],
|
| 264 |
}
|
| 265 |
|
| 266 |
-
# Only needed for retry delays
|
| 267 |
-
import asyncio
|
| 268 |
-
|
| 269 |
if __name__ == "__main__":
|
| 270 |
import uvicorn
|
| 271 |
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)
|
|
|
|
| 7 |
import time
|
| 8 |
import httpx
|
| 9 |
import json
|
| 10 |
+
import asyncio
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
|
| 13 |
load_dotenv()
|
|
|
|
| 130 |
token = await request_token()
|
| 131 |
|
| 132 |
try:
|
| 133 |
+
# Fixed: Don't pass stream=True to AsyncClient.post() - it's not supported
|
| 134 |
async with httpx.AsyncClient() as client:
|
| 135 |
response = await client.post(
|
| 136 |
"https://duckduckgo.com/duckchat/v1/chat",
|
|
|
|
| 143 |
json={
|
| 144 |
"model": model,
|
| 145 |
"messages": [{"role": "user", "content": content}]
|
| 146 |
+
}
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
if response.status_code != 200:
|
|
|
|
| 166 |
nonlocal buffer, full_text
|
| 167 |
|
| 168 |
# Process chunks as they arrive
|
| 169 |
+
# Use response.aiter_text() instead of aiter_bytes() for easier text handling
|
| 170 |
+
async for chunk_str in response.aiter_text():
|
| 171 |
+
chunk_str = chunk_str.strip()
|
| 172 |
|
| 173 |
# Handle buffer from previous chunk if needed
|
| 174 |
if buffer:
|
|
|
|
| 210 |
if return_stream:
|
| 211 |
return StreamingResponse(generate_stream(), media_type="text/event-stream")
|
| 212 |
else:
|
| 213 |
+
# For non-streaming, collect all the text
|
| 214 |
+
async for chunk in generate_stream():
|
| 215 |
pass # Just collecting text in full_text
|
| 216 |
|
| 217 |
return JSONResponse(content=create_complete_response(full_text, model))
|
|
|
|
| 265 |
],
|
| 266 |
}
|
| 267 |
|
|
|
|
|
|
|
|
|
|
| 268 |
if __name__ == "__main__":
|
| 269 |
import uvicorn
|
| 270 |
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)
|