Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,39 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
model_path = "./models/gpt-oss-20b-Q3_K_M.gguf"
|
| 19 |
-
llm = LLM(model_path=model_path, context_length=16384, flash_attention=True)
|
| 20 |
|
| 21 |
-
# Request body schema
|
| 22 |
class ChatRequest(BaseModel):
|
| 23 |
-
|
| 24 |
|
| 25 |
-
# Chat endpoint
|
| 26 |
@app.post("/chat")
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
MODEL_PATH = "./models/gpt-oss-20b-Q3_K_M.gguf"
|
| 9 |
+
|
| 10 |
+
print("🔄 Loading model… this may take a while")
|
| 11 |
+
|
| 12 |
+
llm = Llama(
|
| 13 |
+
model_path=MODEL_PATH,
|
| 14 |
+
n_ctx=16384,
|
| 15 |
+
n_threads=os.cpu_count(),
|
| 16 |
+
n_gpu_layers=0, # HF CPU-only unless paid GPU
|
| 17 |
+
verbose=False,
|
| 18 |
)
|
| 19 |
|
| 20 |
+
print("✅ Model loaded successfully")
|
|
|
|
|
|
|
| 21 |
|
|
|
|
| 22 |
class ChatRequest(BaseModel):
|
| 23 |
+
prompt: str
|
| 24 |
|
|
|
|
| 25 |
@app.post("/chat")
|
| 26 |
+
def chat(req: ChatRequest):
|
| 27 |
+
output = llm(
|
| 28 |
+
f"User: {req.prompt}\nAssistant:",
|
| 29 |
+
max_tokens=512,
|
| 30 |
+
stop=["User:"],
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
return {
|
| 34 |
+
"response": output["choices"][0]["text"].strip()
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
@app.get("/")
|
| 38 |
+
def root():
|
| 39 |
+
return {"status": "ChatGPT Open-Source 1.0 is running 🚀"}
|