File size: 1,503 Bytes
d4c6da4
 
 
889f3e5
f1eb360
889f3e5
d4c6da4
 
5dcb330
f1eb360
718e836
889f3e5
d4c6da4
 
 
 
f1eb360
d4c6da4
f1eb360
 
 
 
d4c6da4
f1eb360
 
d4c6da4
f1eb360
d4c6da4
 
 
f1eb360
d4c6da4
 
f1eb360
 
d4c6da4
f1eb360
 
d4c6da4
 
d43c09e
d4c6da4
 
 
 
889f3e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, StreamingResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from pmbl import PMBL
import os

app = FastAPI()
pmbl = PMBL("./PMB-7b.Q6_K.gguf")  # Replace with the path to your model

app.mount("/", StaticFiles(directory="static"), name="static")

templates = Jinja2Templates(directory=".")

@app.post("/chat")
async def chat(request: Request):
    try:
        data = await request.json()
        user_input = data["user_input"]
        mode = data["mode"]
        history = pmbl.get_chat_history(mode, user_input)
        response_generator = pmbl.generate_response(user_input, history, mode)
        return StreamingResponse(response_generator, media_type="text/plain")
    except Exception as e:
        print(f"[SYSTEM] Error: {str(e)}")
        return {"error": str(e)}

@app.get("/", response_class=HTMLResponse)
async def root(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})

@app.post("/sleep")
async def sleep():
    try:
        pmbl.sleep_mode()
        return {"message": "Sleep mode completed successfully"}
    except Exception as e:
        print(f"[SYSTEM] Error: {str(e)}")
        return {"error": str(e)}

if __name__ == "__main__":
    import uvicorn
    import asyncio

    loop = asyncio.get_event_loop()
    loop.run_until_complete(uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8000))))