Sergidev commited on
Commit
7551230
1 Parent(s): b0c5708

Update app.py

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