Manofem commited on
Commit
e8c98e5
·
1 Parent(s): 75407e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -17
app.py CHANGED
@@ -1,5 +1,4 @@
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
@@ -14,20 +13,6 @@ def typewrite(text, delay=0.01):
14
  time.sleep(delay)
15
  print(end='', flush=True) # Print newline to move to the next line
16
 
17
- async def generate_response(ask):
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
- 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
- response_text_chunk = chunk['choices'][0]['text']
26
- typewrite(response_text_chunk, delay=0.00)
27
- yield response_text_chunk
28
- except StopIteration:
29
- break
30
-
31
  @app.post("/chat")
32
  async def chat(request: Request):
33
  try:
@@ -37,11 +22,34 @@ async def chat(request: Request):
37
  if not user_input:
38
  raise HTTPException(status_code=400, detail="Missing 'user_input' field in the request JSON.")
39
 
40
- return StreamingResponse(generate_response(user_input), media_type="text/plain")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  except Exception as e:
43
  raise HTTPException(status_code=500, detail=str(e))
44
 
45
  if __name__ == "__main__":
46
  import uvicorn
47
- 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:
 
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
+ 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)