Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from lmstudio import LLM # LM Studio Python interface
|
| 4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
|
| 6 |
+
# FastAPI app
|
| 7 |
+
app = FastAPI(title="ChatGPT OS 1.0", description="Local AI Chat API", version="1.0")
|
| 8 |
+
|
| 9 |
+
# Enable CORS so frontend can connect from any origin
|
| 10 |
+
app.add_middleware(
|
| 11 |
+
CORSMiddleware,
|
| 12 |
+
allow_origins=["*"],
|
| 13 |
+
allow_methods=["*"],
|
| 14 |
+
allow_headers=["*"]
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Load the GPT-OSS model
|
| 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 |
+
message: str
|
| 24 |
+
|
| 25 |
+
# Chat endpoint
|
| 26 |
+
@app.post("/chat")
|
| 27 |
+
async def chat(req: ChatRequest):
|
| 28 |
+
user_message = req.message
|
| 29 |
+
# Generate response from the model
|
| 30 |
+
response = llm.generate(user_message, max_new_tokens=256)
|
| 31 |
+
return {"response": response}
|
| 32 |
+
|
| 33 |
+
# Optional: health check
|
| 34 |
+
@app.get("/health")
|
| 35 |
+
def health():
|
| 36 |
+
return {"status": "ok", "model_loaded": True}
|