Spaces:
Sleeping
Sleeping
stream word
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.responses import StreamingResponse
|
| 4 |
from pydantic import BaseModel
|
|
@@ -44,9 +45,20 @@ async def ask_question_stream(question: Question):
|
|
| 44 |
|
| 45 |
async def generate():
|
| 46 |
full_response = ""
|
|
|
|
| 47 |
async for chunk in llm.astream(question.text):
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# Log the full response after streaming is complete
|
| 52 |
logger.info(f"Full streamed response: {full_response}")
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
from fastapi.responses import StreamingResponse
|
| 5 |
from pydantic import BaseModel
|
|
|
|
| 45 |
|
| 46 |
async def generate():
|
| 47 |
full_response = ""
|
| 48 |
+
buffer = ""
|
| 49 |
async for chunk in llm.astream(question.text):
|
| 50 |
+
buffer += chunk
|
| 51 |
+
words = re.findall(r'\S+|\s+', buffer)
|
| 52 |
+
|
| 53 |
+
for word in words[:-1]:
|
| 54 |
+
full_response += word
|
| 55 |
+
yield word
|
| 56 |
+
|
| 57 |
+
buffer = words[-1] if words else ""
|
| 58 |
+
|
| 59 |
+
if buffer:
|
| 60 |
+
full_response += buffer
|
| 61 |
+
yield buffer
|
| 62 |
|
| 63 |
# Log the full response after streaming is complete
|
| 64 |
logger.info(f"Full streamed response: {full_response}")
|