Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,2 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
import time
|
| 4 |
import os
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
llm = Llama(model_path="llama-2-7b-chat.Q3_K_S.gguf", n_ctx=2048, n_batch=512, use_mlock=True, n_threads=8)
|
| 9 |
+
|
| 10 |
+
def typewrite(text, delay=0.01):
|
| 11 |
+
for char in text:
|
| 12 |
+
print(char, end='', flush=True)
|
| 13 |
+
time.sleep(delay)
|
| 14 |
+
print(end='', flush=True) # Print newline to move to the next line
|
| 15 |
+
|
| 16 |
+
@app.post("/chat")
|
| 17 |
+
async def chat(user_input: str):
|
| 18 |
+
try:
|
| 19 |
+
os.system("cls")
|
| 20 |
+
print("Chatbot by Aritra Roy & DVLH")
|
| 21 |
+
|
| 22 |
+
ask = user_input
|
| 23 |
+
|
| 24 |
+
prompt = f"Llama-2-Chat [INST] <<SYS>>You're an assistant named Tusti. You are Developed by Aritra Roy. Don't share any false information.<</SYS>> {ask} [/INST]"
|
| 25 |
+
|
| 26 |
+
# Send the request with stream=True
|
| 27 |
+
output_stream = llm(prompt, max_tokens=1024, echo=False, temperature=0.2, top_p=0.1, stream=True)
|
| 28 |
+
|
| 29 |
+
response_text_chunk = ''
|
| 30 |
+
|
| 31 |
+
while True:
|
| 32 |
+
try:
|
| 33 |
+
chunk = next(output_stream)
|
| 34 |
+
if chunk.get('choices') and chunk['choices'][0].get('text'):
|
| 35 |
+
response_text_chunk += chunk['choices'][0]['text']
|
| 36 |
+
typewrite(response_text_chunk, delay=0.00) # live response
|
| 37 |
+
except StopIteration:
|
| 38 |
+
break
|
| 39 |
+
|
| 40 |
+
if ask == 'clear':
|
| 41 |
+
os.system("cls")
|
| 42 |
+
|
| 43 |
+
return {"response": response_text_chunk}
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
import uvicorn
|
| 50 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|