Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,33 @@
|
|
| 1 |
-
from fastapi import FastAPI, Request
|
| 2 |
from llama_cpp import Llama
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
-
# Load your 3.6GB model
|
| 6 |
-
llm = Llama(model_path="./mpt-7b-chat.ggmlv0.q4_0.bin")
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@app.get("/")
|
| 16 |
def home():
|
| 17 |
-
return {"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, HTTPException, Depends
|
| 2 |
from llama_cpp import Llama
|
| 3 |
|
| 4 |
app = FastAPI()
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Set your API Key here
|
| 7 |
+
API_KEY = "my-secret-key-123"
|
| 8 |
+
|
| 9 |
+
# Load the model we downloaded in the Dockerfile
|
| 10 |
+
# MPT models need the 'mpt' type specified for older llama-cpp
|
| 11 |
+
llm = Llama(model_path="./model.bin", model_type="mpt")
|
| 12 |
+
|
| 13 |
+
# Security check for your API Key
|
| 14 |
+
def verify_key(request: Request):
|
| 15 |
+
key = request.headers.get("Authorization")
|
| 16 |
+
if key != f"Bearer {API_KEY}":
|
| 17 |
+
raise HTTPException(status_code=403, detail="Invalid API Key")
|
| 18 |
|
| 19 |
@app.get("/")
|
| 20 |
def home():
|
| 21 |
+
return {"message": "Your MPT-7B Website is Online!", "api_endpoint": "/v1/chat"}
|
| 22 |
+
|
| 23 |
+
@app.post("/v1/chat", dependencies=[Depends(verify_key)])
|
| 24 |
+
async def chat(request: Request):
|
| 25 |
+
data = await request.json()
|
| 26 |
+
prompt = data.get("prompt", "Hello!")
|
| 27 |
+
|
| 28 |
+
# Generate the response
|
| 29 |
+
output = llm(f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n",
|
| 30 |
+
stop=["<|im_end|>"],
|
| 31 |
+
max_tokens=256)
|
| 32 |
+
|
| 33 |
+
return {"response": output["choices"][0]["text"]}
|