Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
from fastapi import FastAPI, HTTPException, Request
|
|
|
2 |
from llama_cpp import Llama
|
3 |
import time
|
4 |
import os
|
@@ -31,24 +32,23 @@ async def chat(request: Request):
|
|
31 |
|
32 |
output_stream = llm(prompt, max_tokens=1024, echo=False, temperature=0.2, top_p=0.1, stream=True)
|
33 |
|
34 |
-
response_text_chunk = ''
|
35 |
-
|
36 |
async def generate_chunks():
|
37 |
-
nonlocal
|
|
|
38 |
try:
|
39 |
while True:
|
40 |
chunk = next(output_stream)
|
41 |
if chunk.get('choices') and chunk['choices'][0].get('text'):
|
42 |
response_text_chunk += chunk['choices'][0]['text']
|
43 |
typewrite(response_text_chunk, delay=0.00)
|
44 |
-
yield
|
45 |
except StopIteration:
|
46 |
pass
|
47 |
|
48 |
if ask == 'clear':
|
49 |
os.system("cls")
|
50 |
|
51 |
-
return
|
52 |
|
53 |
except Exception as e:
|
54 |
print(f"Exception: {e}")
|
|
|
1 |
from fastapi import FastAPI, HTTPException, Request
|
2 |
+
from fastapi.responses import EventSourceResponse
|
3 |
from llama_cpp import Llama
|
4 |
import time
|
5 |
import os
|
|
|
32 |
|
33 |
output_stream = llm(prompt, max_tokens=1024, echo=False, temperature=0.2, top_p=0.1, stream=True)
|
34 |
|
|
|
|
|
35 |
async def generate_chunks():
|
36 |
+
nonlocal output_stream
|
37 |
+
response_text_chunk = ''
|
38 |
try:
|
39 |
while True:
|
40 |
chunk = next(output_stream)
|
41 |
if chunk.get('choices') and chunk['choices'][0].get('text'):
|
42 |
response_text_chunk += chunk['choices'][0]['text']
|
43 |
typewrite(response_text_chunk, delay=0.00)
|
44 |
+
yield f"data: {response_text_chunk}\n\n"
|
45 |
except StopIteration:
|
46 |
pass
|
47 |
|
48 |
if ask == 'clear':
|
49 |
os.system("cls")
|
50 |
|
51 |
+
return EventSourceResponse(generate_chunks())
|
52 |
|
53 |
except Exception as e:
|
54 |
print(f"Exception: {e}")
|