Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from fastapi.responses import StreamingResponse
|
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
|
| 9 |
# Configure logging
|
| 10 |
-
logging.basicConfig(level=logging.
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
app = FastAPI()
|
|
@@ -27,60 +27,81 @@ app.add_middleware(
|
|
| 27 |
|
| 28 |
# Load your API key from the environment (defaults to "change_me")
|
| 29 |
API_KEY = os.environ.get("API_KEY", "change_me")
|
| 30 |
-
|
| 31 |
-
print(f"API key={API_KEY}")
|
| 32 |
|
| 33 |
# URL of the running Ollama server (adjust as needed)
|
| 34 |
OLLAMA_SERVER_URL = "http://localhost:11434/api/generate"
|
|
|
|
| 35 |
|
| 36 |
@app.post("/api/generate")
|
| 37 |
async def generate(request: Request):
|
| 38 |
"""Endpoint that generates text based on the prompt."""
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
@app.get("/health")
|
| 80 |
async def health():
|
| 81 |
"""Health check endpoint."""
|
|
|
|
| 82 |
return {"status": "OK"}
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
| 85 |
import uvicorn
|
|
|
|
| 86 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
|
| 9 |
# Configure logging
|
| 10 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
app = FastAPI()
|
|
|
|
| 27 |
|
| 28 |
# Load your API key from the environment (defaults to "change_me")
|
| 29 |
API_KEY = os.environ.get("API_KEY", "change_me")
|
| 30 |
+
logger.debug(f"Loaded API key: {API_KEY}")
|
|
|
|
| 31 |
|
| 32 |
# URL of the running Ollama server (adjust as needed)
|
| 33 |
OLLAMA_SERVER_URL = "http://localhost:11434/api/generate"
|
| 34 |
+
logger.debug(f"Ollama server URL: {OLLAMA_SERVER_URL}")
|
| 35 |
|
| 36 |
@app.post("/api/generate")
|
| 37 |
async def generate(request: Request):
|
| 38 |
"""Endpoint that generates text based on the prompt."""
|
| 39 |
+
try:
|
| 40 |
+
# 1. Parse the incoming request
|
| 41 |
+
body = await request.json()
|
| 42 |
+
model = body.get("model", "hf.co/abanm/Dubs-Q8_0-GGUF:latest") # Default model
|
| 43 |
+
prompt_text = body.get("prompt", "")
|
| 44 |
+
|
| 45 |
+
if not prompt_text:
|
| 46 |
+
logger.error("No prompt provided in the request")
|
| 47 |
+
raise HTTPException(status_code=400, detail="No prompt provided")
|
| 48 |
+
|
| 49 |
+
logger.debug(f"Request body: {body}")
|
| 50 |
+
|
| 51 |
+
# 2. Validate API key
|
| 52 |
+
auth_header = request.headers.get("Authorization")
|
| 53 |
+
logger.debug(f"Received Authorization header: {auth_header}")
|
| 54 |
+
|
| 55 |
+
if not auth_header or not auth_header.startswith("Bearer "):
|
| 56 |
+
logger.error("Missing or invalid Authorization header")
|
| 57 |
+
raise HTTPException(status_code=401, detail="Missing or invalid Authorization header")
|
| 58 |
+
|
| 59 |
+
token = auth_header.split(" ")[1]
|
| 60 |
+
if token != API_KEY:
|
| 61 |
+
logger.error(f"Invalid API key provided: {token}")
|
| 62 |
+
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 63 |
+
|
| 64 |
+
# 3. Prepare request payload
|
| 65 |
+
payload = {"model": model, "prompt": prompt_text}
|
| 66 |
+
logger.debug(f"Payload prepared for Ollama: {payload}")
|
| 67 |
+
|
| 68 |
+
# 4. Stream response from Ollama
|
| 69 |
+
async def stream_response():
|
| 70 |
+
try:
|
| 71 |
+
async with httpx.AsyncClient() as client:
|
| 72 |
+
async with client.stream(
|
| 73 |
+
"POST", OLLAMA_SERVER_URL, json=payload, headers={"Content-Type": "application/json"}
|
| 74 |
+
) as response:
|
| 75 |
+
logger.info(f"Response status code from Ollama: {response.status_code}")
|
| 76 |
+
|
| 77 |
+
if response.status_code != 200:
|
| 78 |
+
logger.error(f"HTTP error: {response.status_code} - {await response.text()}")
|
| 79 |
+
yield json.dumps({"error": f"HTTP error: {response.status_code}"})
|
| 80 |
+
return
|
| 81 |
+
|
| 82 |
+
async for chunk in response.aiter_text():
|
| 83 |
+
logger.debug(f"Chunk received: {chunk}")
|
| 84 |
+
yield chunk
|
| 85 |
+
except httpx.RequestError as exc:
|
| 86 |
+
logger.error(f"Request error while communicating with Ollama: {str(exc)}")
|
| 87 |
+
yield json.dumps({"error": "Network error occurred while communicating with Ollama"})
|
| 88 |
+
except httpx.HTTPStatusError as exc:
|
| 89 |
+
logger.error(f"HTTP error from Ollama: {exc.response.status_code} - {exc.response.text}")
|
| 90 |
+
yield json.dumps({"error": f"HTTP error: {exc.response.text}"})
|
| 91 |
+
|
| 92 |
+
return StreamingResponse(stream_response(), media_type="application/json")
|
| 93 |
+
|
| 94 |
+
except Exception as e:
|
| 95 |
+
logger.exception(f"Unexpected error: {str(e)}")
|
| 96 |
+
raise HTTPException(status_code=500, detail="An unexpected error occurred")
|
| 97 |
|
| 98 |
@app.get("/health")
|
| 99 |
async def health():
|
| 100 |
"""Health check endpoint."""
|
| 101 |
+
logger.info("Health check endpoint accessed")
|
| 102 |
return {"status": "OK"}
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
| 105 |
import uvicorn
|
| 106 |
+
logger.info("Starting FastAPI application")
|
| 107 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|