Manofem commited on
Commit
ea3442d
·
1 Parent(s): 8c3e673

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -23
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
@@ -7,11 +8,20 @@ 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(request: Request):
@@ -22,33 +32,18 @@ async def chat(request: Request):
22
  if not user_input:
23
  raise HTTPException(status_code=400, detail="Missing 'user_input' field in the request JSON.")
24
 
25
- os.system("cls")
26
- print("Chatbot by Aritra Roy & DVLH")
27
-
28
  ask = user_input
29
 
30
  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]"
31
 
32
- output_stream = llm(prompt, max_tokens=1024, echo=False, temperature=0.2, top_p=0.1, stream=True)
33
-
34
- # Remove the response_text variable and directly print the chunks
35
- while True:
36
- try:
37
- chunk = next(output_stream)
38
- if chunk.get('choices') and chunk['choices'][0].get('text'):
39
- text = chunk['choices'][0]['text']
40
- typewrite(text, delay=0.00)
41
- except StopIteration:
42
- break
43
-
44
  if ask == 'clear':
45
- os.system("cls")
46
 
47
- return {} # Since response_text is no longer used, return an empty dictionary
48
 
49
  except Exception as e:
50
  raise HTTPException(status_code=500, detail=str(e))
51
 
52
  if __name__ == "__main__":
53
  import uvicorn
54
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  from fastapi import FastAPI, HTTPException, Request
2
+ from fastapi.responses import StreamingResponse
3
  from llama_cpp import Llama
4
  import time
5
  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
 
11
+ def generate_output_stream(prompt):
12
+ output_stream = llm(prompt, max_tokens=1024, echo=False, temperature=0.2, top_p=0.1, stream=True)
13
+ while True:
14
+ try:
15
+ chunk = next(output_stream)
16
+ if chunk.get('choices') and chunk['choices'][0].get('text'):
17
+ text = chunk['choices'][0]['text']
18
+ yield text.encode('utf-8')
19
+ time.sleep(0.01) # Adjust the delay if needed
20
+ except StopIteration:
21
+ break
22
+
23
+ def clear_screen():
24
+ os.system("cls")
25
 
26
  @app.post("/chat")
27
  async def chat(request: Request):
 
32
  if not user_input:
33
  raise HTTPException(status_code=400, detail="Missing 'user_input' field in the request JSON.")
34
 
 
 
 
35
  ask = user_input
36
 
37
  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]"
38
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  if ask == 'clear':
40
+ clear_screen()
41
 
42
+ return StreamingResponse(generate_output_stream(prompt), media_type="text/plain")
43
 
44
  except Exception as e:
45
  raise HTTPException(status_code=500, detail=str(e))
46
 
47
  if __name__ == "__main__":
48
  import uvicorn
49
+ uvicorn.run(app, host="0.0.0.0", port=7860)