Sergidev commited on
Commit
d4c6da4
1 Parent(s): 969f920

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -17
app.py CHANGED
@@ -1,36 +1,47 @@
1
- from flask import Flask, request, Response, render_template
 
 
2
  from pmbl import PMBL
3
- import os
4
- import json
5
 
6
- app = Flask(__name__, template_folder='.')
 
 
 
7
  pmbl = PMBL("./PMB-7b.Q6_K.gguf") # Replace with the path to your model
8
 
9
- @app.route("/chat", methods=['POST'])
10
- def chat():
 
 
11
  try:
12
- data = request.get_json()
13
  user_input = data["user_input"]
14
  mode = data["mode"]
15
  history = pmbl.get_chat_history(mode, user_input)
16
  response_generator = pmbl.generate_response(user_input, history, mode)
17
- return Response(response_generator, mimetype='text/plain')
18
  except Exception as e:
19
  print(f"[SYSTEM] Error: {str(e)}")
20
- return json.dumps({"error": str(e)})
21
 
22
- @app.route("/", methods=['GET'])
23
- def root():
24
- return render_template("index.html")
25
 
26
- @app.route("/sleep", methods=['POST'])
27
- def sleep():
28
  try:
29
  pmbl.sleep_mode()
30
- return json.dumps({"message": "Sleep mode completed successfully"})
31
  except Exception as e:
32
  print(f"[SYSTEM] Error: {str(e)}")
33
- return json.dumps({"error": str(e)})
 
 
34
 
35
  if __name__ == "__main__":
36
- app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.responses import HTMLResponse, StreamingResponse
3
+ from fastapi.templating import Jinja2Templates
4
  from pmbl import PMBL
 
 
5
 
6
+
7
+
8
+
9
+ app = FastAPI()
10
  pmbl = PMBL("./PMB-7b.Q6_K.gguf") # Replace with the path to your model
11
 
12
+ templates = Jinja2Templates(directory=".")
13
+
14
+ @app.post("/chat")
15
+ async def chat(request: Request):
16
  try:
17
+ data = await request.json()
18
  user_input = data["user_input"]
19
  mode = data["mode"]
20
  history = pmbl.get_chat_history(mode, user_input)
21
  response_generator = pmbl.generate_response(user_input, history, mode)
22
+ return StreamingResponse(response_generator, media_type="text/plain")
23
  except Exception as e:
24
  print(f"[SYSTEM] Error: {str(e)}")
25
+ return {"error": str(e)}
26
 
27
+ @app.get("/", response_class=HTMLResponse)
28
+ async def root(request: Request):
29
+ return templates.TemplateResponse("index.html", {"request": request})
30
 
31
+ @app.post("/sleep")
32
+ async def sleep():
33
  try:
34
  pmbl.sleep_mode()
35
+ return {"message": "Sleep mode completed successfully"}
36
  except Exception as e:
37
  print(f"[SYSTEM] Error: {str(e)}")
38
+ return {"error": str(e)}
39
+
40
+ # Remove the main function as Hugging Face Spaces will automatically run the app. Using here to test
41
 
42
  if __name__ == "__main__":
43
+ import uvicorn
44
+ import asyncio
45
+
46
+ loop = asyncio.get_event_loop()
47
+ loop.run_until_complete(uvicorn.run(app, host="0.0.0.0", port=1771))