Spaces:
Runtime error
Runtime error
Create app_1.py
Browse files
app_1.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Form
|
| 2 |
+
|
| 3 |
+
app = FastAPI()
|
| 4 |
+
|
| 5 |
+
from llama_cpp import Llama
|
| 6 |
+
import time
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
llm = Llama(model_path="llama-2-7b-chat.Q3_K_S.gguf", n_ctx=2048, n_batch=512, use_mlock=True, n_threads=8)
|
| 10 |
+
time.sleep(8)
|
| 11 |
+
os.system("cls")
|
| 12 |
+
print("Chatbot by Aritra Roy & DVLH")
|
| 13 |
+
import warnings
|
| 14 |
+
warnings.filterwarnings("ignore")
|
| 15 |
+
|
| 16 |
+
@app.post("/chat")
|
| 17 |
+
async def chat_endpoint(ask: str = Form(...)):
|
| 18 |
+
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]"
|
| 19 |
+
|
| 20 |
+
output_stream = llm(prompt, max_tokens=1024, echo=False, temperature=0.2, top_p=0.1, stream=True)
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
while True:
|
| 24 |
+
try:
|
| 25 |
+
chunk = next(output_stream)
|
| 26 |
+
if chunk.get('choices') and chunk['choices'][0].get('text'):
|
| 27 |
+
response_text_chunk = chunk['choices'][0]['text']
|
| 28 |
+
print(response_text_chunk, delay=0.00) # live response
|
| 29 |
+
return {"response": response_text_chunk}
|
| 30 |
+
except StopIteration:
|
| 31 |
+
break
|
| 32 |
+
except StopIteration:
|
| 33 |
+
pass
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
import uvicorn
|
| 37 |
+
|
| 38 |
+
uvicorn.run(app, host="127.0.0.1", port=8000)
|