Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,77 @@
|
|
1 |
-
from fastapi import FastAPI,
|
|
|
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 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
async def chat(user_input: str):
|
18 |
-
try:
|
19 |
-
os.system("cls")
|
20 |
-
print("Chatbot by Aritra Roy & DVLH")
|
21 |
|
22 |
-
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
|
|
28 |
|
29 |
-
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
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 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
|
48 |
if __name__ == "__main__":
|
49 |
import uvicorn
|
|
|
1 |
+
from fastapi import FastAPI, WebSocket
|
2 |
+
from fastapi.responses import HTMLResponse
|
3 |
from llama_cpp import Llama
|
|
|
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 |
+
html = """
|
11 |
+
<!DOCTYPE html>
|
12 |
+
<html>
|
13 |
+
<head>
|
14 |
+
<title>Chatbot by Aritra Roy & DVLH</title>
|
15 |
+
</head>
|
16 |
+
<body>
|
17 |
+
<h1>Chatbot by Aritra Roy & DVLH</h1>
|
18 |
+
<div>
|
19 |
+
<ul id="chat"></ul>
|
20 |
+
<input type="text" id="user_input" onkeydown="sendMessage(event)">
|
21 |
+
</div>
|
22 |
+
<script>
|
23 |
+
var chat = document.getElementById('chat');
|
24 |
|
25 |
+
var socket = new WebSocket('ws://localhost:7860/ws/chat');
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
socket.onmessage = function(event) {
|
28 |
+
var li = document.createElement('li');
|
29 |
+
li.appendChild(document.createTextNode(event.data));
|
30 |
+
chat.appendChild(li);
|
31 |
+
};
|
32 |
|
33 |
+
function sendMessage(event) {
|
34 |
+
if (event.key === 'Enter') {
|
35 |
+
var user_input = document.getElementById('user_input').value;
|
36 |
+
var li = document.createElement('li');
|
37 |
+
li.appendChild(document.createTextNode('> ' + user_input));
|
38 |
+
chat.appendChild(li);
|
39 |
+
socket.send(user_input);
|
40 |
+
document.getElementById('user_input').value = '';
|
41 |
+
}
|
42 |
+
}
|
43 |
+
</script>
|
44 |
+
</body>
|
45 |
+
</html>
|
46 |
+
"""
|
47 |
|
48 |
+
@app.websocket("/ws/chat")
|
49 |
+
async def chat(websocket: WebSocket):
|
50 |
+
await websocket.accept()
|
51 |
|
52 |
+
while True:
|
53 |
+
try:
|
54 |
+
user_input = await websocket.receive_text()
|
55 |
+
os.system("cls")
|
56 |
|
57 |
+
ask = user_input
|
58 |
+
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]"
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
output_stream = llm(prompt, max_tokens=1024, echo=False, temperature=0.2, top_p=0.1, stream=True)
|
61 |
+
|
62 |
+
while True:
|
63 |
+
try:
|
64 |
+
chunk = next(output_stream)
|
65 |
+
if chunk.get('choices') and chunk['choices'][0].get('text'):
|
66 |
+
await websocket.send_text(chunk['choices'][0]['text'])
|
67 |
+
except StopIteration:
|
68 |
+
break
|
69 |
|
70 |
+
if ask == 'clear':
|
71 |
+
os.system("cls")
|
72 |
|
73 |
+
except Exception as e:
|
74 |
+
await websocket.send_text(str(e))
|
75 |
|
76 |
if __name__ == "__main__":
|
77 |
import uvicorn
|