Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,47 @@
|
|
1 |
-
from
|
|
|
|
|
2 |
from pmbl import PMBL
|
3 |
-
import os
|
4 |
-
import json
|
5 |
|
6 |
-
|
|
|
|
|
|
|
7 |
pmbl = PMBL("./PMB-7b.Q6_K.gguf") # Replace with the path to your model
|
8 |
|
9 |
-
|
10 |
-
|
|
|
|
|
11 |
try:
|
12 |
-
data = request.
|
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
|
18 |
except Exception as e:
|
19 |
print(f"[SYSTEM] Error: {str(e)}")
|
20 |
-
return
|
21 |
|
22 |
-
@app.
|
23 |
-
def root():
|
24 |
-
return
|
25 |
|
26 |
-
@app.
|
27 |
-
def sleep():
|
28 |
try:
|
29 |
pmbl.sleep_mode()
|
30 |
-
return
|
31 |
except Exception as e:
|
32 |
print(f"[SYSTEM] Error: {str(e)}")
|
33 |
-
return
|
|
|
|
|
34 |
|
35 |
if __name__ == "__main__":
|
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 |
+
|
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))
|