Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -213,7 +213,7 @@ async def generate_text(request: Request):
|
|
| 213 |
|
| 214 |
body["model"] = chosen_model
|
| 215 |
|
| 216 |
-
stream = body.get("stream",
|
| 217 |
|
| 218 |
if provider == "groq":
|
| 219 |
API_KEY = os.getenv("GROQ_KEY", "")
|
|
@@ -233,21 +233,21 @@ async def generate_text(request: Request):
|
|
| 233 |
|
| 234 |
headers = {"Authorization": f"Bearer {API_KEY}"}
|
| 235 |
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
async def event_generator():
|
| 240 |
async with client.stream("POST", url, json=body, headers=headers) as r:
|
| 241 |
async for chunk in r.aiter_raw():
|
| 242 |
yield chunk
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
|
|
|
| 251 |
return JSONResponse(
|
| 252 |
status_code=r.status_code,
|
| 253 |
content=r.json()
|
|
|
|
| 213 |
|
| 214 |
body["model"] = chosen_model
|
| 215 |
|
| 216 |
+
stream = body.get("stream", False)
|
| 217 |
|
| 218 |
if provider == "groq":
|
| 219 |
API_KEY = os.getenv("GROQ_KEY", "")
|
|
|
|
| 233 |
|
| 234 |
headers = {"Authorization": f"Bearer {API_KEY}"}
|
| 235 |
|
| 236 |
+
if stream:
|
| 237 |
+
async def event_generator():
|
| 238 |
+
async with httpx.AsyncClient(timeout=None) as client:
|
|
|
|
| 239 |
async with client.stream("POST", url, json=body, headers=headers) as r:
|
| 240 |
async for chunk in r.aiter_raw():
|
| 241 |
yield chunk
|
| 242 |
+
|
| 243 |
+
return StreamingResponse(
|
| 244 |
+
event_generator(),
|
| 245 |
+
media_type="text/event-stream",
|
| 246 |
+
)
|
| 247 |
+
else:
|
| 248 |
+
async with httpx.AsyncClient(timeout=None) as client:
|
| 249 |
+
r = await client.post(url, json=body, headers=headers)
|
| 250 |
+
|
| 251 |
return JSONResponse(
|
| 252 |
status_code=r.status_code,
|
| 253 |
content=r.json()
|