Manofem commited on
Commit
645c249
·
1 Parent(s): 1740ae0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from fastapi import FastAPI, HTTPException, Request, StreamingResponse
2
  from llama_cpp import Llama
3
  import time
4
  import os
@@ -13,19 +13,6 @@ def typewrite(text, delay=0.01):
13
  time.sleep(delay)
14
  print(end='', flush=True) # Print newline to move to the next line
15
 
16
- async def generate_text_stream(ask):
17
- 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]"
18
-
19
- output_stream = llm(prompt, max_tokens=1024, echo=False, temperature=0.2, top_p=0.1, stream=True)
20
-
21
- while True:
22
- try:
23
- chunk = next(output_stream)
24
- if chunk.get('choices') and chunk['choices'][0].get('text'):
25
- yield chunk['choices'][0]['text']
26
- except StopIteration:
27
- break
28
-
29
  @app.post("/chat")
30
  async def chat(request: Request):
31
  try:
@@ -40,14 +27,29 @@ async def chat(request: Request):
40
 
41
  ask = user_input
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  if ask == 'clear':
44
  os.system("cls")
45
 
46
- return StreamingResponse(generate_text_stream(ask))
47
 
48
  except Exception as e:
49
  raise HTTPException(status_code=500, detail=str(e))
50
 
51
  if __name__ == "__main__":
52
  import uvicorn
53
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ from fastapi import FastAPI, HTTPException, Request
2
  from llama_cpp import Llama
3
  import time
4
  import os
 
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):
18
  try:
 
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
+ response_text_chunk = ''
35
+
36
+ while True:
37
+ try:
38
+ chunk = next(output_stream)
39
+ if chunk.get('choices') and chunk['choices'][0].get('text'):
40
+ response_text_chunk += chunk['choices'][0]['text']
41
+ typewrite(response_text_chunk, delay=0.00)
42
+ except StopIteration:
43
+ break
44
+
45
  if ask == 'clear':
46
  os.system("cls")
47
 
48
+ return {"response": response_text_chunk}
49
 
50
  except Exception as e:
51
  raise HTTPException(status_code=500, detail=str(e))
52
 
53
  if __name__ == "__main__":
54
  import uvicorn
55
+ uvicorn.run(app, host="0.0.0.0", port=7860)