Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -54,7 +54,7 @@ async def stream_response(response_text: str):
|
|
54 |
words = response_text.split()
|
55 |
for i in range(0, len(words), 2):
|
56 |
chunk = " ".join(words[i:i+2]) + " "
|
57 |
-
|
58 |
'id': f'chatcmpl-{os.urandom(12).hex()}',
|
59 |
'object': 'chat.completion.chunk',
|
60 |
'created': int(time.time()),
|
@@ -64,11 +64,12 @@ async def stream_response(response_text: str):
|
|
64 |
'delta': {'content': chunk},
|
65 |
'finish_reason': None
|
66 |
}]
|
67 |
-
}
|
|
|
68 |
await asyncio.sleep(0.1) # Небольшая задержка между чанками
|
69 |
|
70 |
# Отправляем финальное сообщение
|
71 |
-
|
72 |
'id': f'chatcmpl-{os.urandom(12).hex()}',
|
73 |
'object': 'chat.completion.chunk',
|
74 |
'created': int(time.time()),
|
@@ -78,7 +79,8 @@ async def stream_response(response_text: str):
|
|
78 |
'delta': {},
|
79 |
'finish_reason': 'stop'
|
80 |
}]
|
81 |
-
}
|
|
|
82 |
|
83 |
@app.get("/")
|
84 |
async def root():
|
|
|
54 |
words = response_text.split()
|
55 |
for i in range(0, len(words), 2):
|
56 |
chunk = " ".join(words[i:i+2]) + " "
|
57 |
+
chunk_data = {
|
58 |
'id': f'chatcmpl-{os.urandom(12).hex()}',
|
59 |
'object': 'chat.completion.chunk',
|
60 |
'created': int(time.time()),
|
|
|
64 |
'delta': {'content': chunk},
|
65 |
'finish_reason': None
|
66 |
}]
|
67 |
+
}
|
68 |
+
yield f"data: {json.dumps(chunk_data, ensure_ascii=False)}\n\n"
|
69 |
await asyncio.sleep(0.1) # Небольшая задержка между чанками
|
70 |
|
71 |
# Отправляем финальное сообщение
|
72 |
+
final_data = {
|
73 |
'id': f'chatcmpl-{os.urandom(12).hex()}',
|
74 |
'object': 'chat.completion.chunk',
|
75 |
'created': int(time.time()),
|
|
|
79 |
'delta': {},
|
80 |
'finish_reason': 'stop'
|
81 |
}]
|
82 |
+
}
|
83 |
+
yield f"data: {json.dumps(final_data, ensure_ascii=False)}\n\n"
|
84 |
|
85 |
@app.get("/")
|
86 |
async def root():
|