Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ from __future__ import annotations
|
|
| 2 |
|
| 3 |
import os, json, time, uuid, asyncio, logging
|
| 4 |
from typing import Any, AsyncGenerator
|
|
|
|
| 5 |
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from fastapi import FastAPI, HTTPException, Request, Depends
|
|
@@ -71,19 +72,37 @@ async def verify_key(request: Request) -> None:
|
|
| 71 |
if not auth.startswith("Bearer ") or auth[7:] != API_KEY:
|
| 72 |
raise HTTPException(status_code=401, detail="Invalid or missing API key")
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
# ---------------------------------------------------------------------------
|
| 75 |
# App
|
| 76 |
# ---------------------------------------------------------------------------
|
| 77 |
|
| 78 |
-
app = FastAPI(
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
)
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# ---------------------------------------------------------------------------
|
| 89 |
# Business logic - EXACTLY like the HTML chatbot
|
|
@@ -231,6 +250,18 @@ async def _stream_sse(text: str, req: ChatCompletionRequest) -> AsyncGenerator[s
|
|
| 231 |
# Routes
|
| 232 |
# ---------------------------------------------------------------------------
|
| 233 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
@app.get("/health")
|
| 235 |
async def health():
|
| 236 |
return {"status": "ok", "model": MODEL_ID, "space": HF_SPACE_URL}
|
|
|
|
| 2 |
|
| 3 |
import os, json, time, uuid, asyncio, logging
|
| 4 |
from typing import Any, AsyncGenerator
|
| 5 |
+
from contextlib import asynccontextmanager
|
| 6 |
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from fastapi import FastAPI, HTTPException, Request, Depends
|
|
|
|
| 72 |
if not auth.startswith("Bearer ") or auth[7:] != API_KEY:
|
| 73 |
raise HTTPException(status_code=401, detail="Invalid or missing API key")
|
| 74 |
|
| 75 |
+
# ---------------------------------------------------------------------------
|
| 76 |
+
# Lifespan context manager (modern FastAPI pattern)
|
| 77 |
+
# ---------------------------------------------------------------------------
|
| 78 |
+
|
| 79 |
+
@asynccontextmanager
|
| 80 |
+
async def lifespan(app: FastAPI):
|
| 81 |
+
# Startup
|
| 82 |
+
log.info("Starting up - connecting to Gradio client...")
|
| 83 |
+
await get_client()
|
| 84 |
+
log.info("Startup complete.")
|
| 85 |
+
yield
|
| 86 |
+
# Shutdown (if needed)
|
| 87 |
+
log.info("Shutting down.")
|
| 88 |
+
|
| 89 |
# ---------------------------------------------------------------------------
|
| 90 |
# App
|
| 91 |
# ---------------------------------------------------------------------------
|
| 92 |
|
| 93 |
+
app = FastAPI(
|
| 94 |
+
title="Falcon H1R API",
|
| 95 |
+
version="3.1.0",
|
| 96 |
+
lifespan=lifespan,
|
| 97 |
)
|
| 98 |
|
| 99 |
+
app.add_middleware(
|
| 100 |
+
CORSMiddleware,
|
| 101 |
+
allow_origins=["*"],
|
| 102 |
+
allow_credentials=True,
|
| 103 |
+
allow_methods=["*"],
|
| 104 |
+
allow_headers=["*"],
|
| 105 |
+
)
|
| 106 |
|
| 107 |
# ---------------------------------------------------------------------------
|
| 108 |
# Business logic - EXACTLY like the HTML chatbot
|
|
|
|
| 250 |
# Routes
|
| 251 |
# ---------------------------------------------------------------------------
|
| 252 |
|
| 253 |
+
@app.get("/")
|
| 254 |
+
async def root():
|
| 255 |
+
return {
|
| 256 |
+
"service": "Falcon H1R OpenAI-compatible API",
|
| 257 |
+
"version": "3.1.0",
|
| 258 |
+
"endpoints": {
|
| 259 |
+
"health": "/health",
|
| 260 |
+
"models": "/v1/models",
|
| 261 |
+
"chat": "/v1/chat/completions",
|
| 262 |
+
},
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
@app.get("/health")
|
| 266 |
async def health():
|
| 267 |
return {"status": "ok", "model": MODEL_ID, "space": HF_SPACE_URL}
|