Delete app_old.py
Browse files- app_old.py +0 -107
app_old.py
DELETED
|
@@ -1,107 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import logging
|
| 3 |
-
import json
|
| 4 |
-
import httpx
|
| 5 |
-
from fastapi import FastAPI, Request, HTTPException
|
| 6 |
-
from fastapi.responses import StreamingResponse
|
| 7 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
-
|
| 9 |
-
# Configure logging
|
| 10 |
-
logging.basicConfig(level=logging.INFO)
|
| 11 |
-
logger = logging.getLogger(__name__)
|
| 12 |
-
|
| 13 |
-
app = FastAPI()
|
| 14 |
-
|
| 15 |
-
# Optional: Configure CORS if needed
|
| 16 |
-
origins = [
|
| 17 |
-
# Add allowed origins if you implement a frontend later
|
| 18 |
-
]
|
| 19 |
-
|
| 20 |
-
app.add_middleware(
|
| 21 |
-
CORSMiddleware,
|
| 22 |
-
allow_origins=origins, # Adjust as needed
|
| 23 |
-
allow_credentials=True,
|
| 24 |
-
allow_methods=["*"],
|
| 25 |
-
allow_headers=["*"],
|
| 26 |
-
)
|
| 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.info(f"API key loaded: {API_KEY}")
|
| 31 |
-
|
| 32 |
-
# Since Ollama is working with /api/generate, set this accordingly
|
| 33 |
-
OLLAMA_SERVER_URL = "http://localhost:11434/api/generate"
|
| 34 |
-
|
| 35 |
-
@app.post("/api/generate")
|
| 36 |
-
async def generate(request: Request):
|
| 37 |
-
"""Endpoint that generates text based on the prompt."""
|
| 38 |
-
try:
|
| 39 |
-
# 1. Parse the incoming request
|
| 40 |
-
body = await request.json()
|
| 41 |
-
logger.info(f"Received request body: {body}")
|
| 42 |
-
|
| 43 |
-
model = body.get("model", "hf.co/abanm/Dubs-Q8_0-GGUF:latest") # Default model
|
| 44 |
-
prompt_text = body.get("prompt", "")
|
| 45 |
-
if not prompt_text:
|
| 46 |
-
logger.error("No prompt provided in the request body")
|
| 47 |
-
raise HTTPException(status_code=400, detail="No prompt provided")
|
| 48 |
-
|
| 49 |
-
# 2. Validate API key
|
| 50 |
-
auth_header = request.headers.get("Authorization")
|
| 51 |
-
logger.info(f"Received Authorization header: {auth_header}")
|
| 52 |
-
|
| 53 |
-
if not auth_header or not auth_header.startswith("Bearer "):
|
| 54 |
-
logger.error("Invalid or missing Authorization header")
|
| 55 |
-
raise HTTPException(status_code=401, detail="Missing or invalid Authorization header")
|
| 56 |
-
|
| 57 |
-
token = auth_header.split(" ")[1]
|
| 58 |
-
if token != API_KEY:
|
| 59 |
-
logger.error(f"Invalid API key: {token}")
|
| 60 |
-
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 61 |
-
|
| 62 |
-
# 3. Prepare request payload
|
| 63 |
-
payload = {"model": model, "prompt": prompt_text}
|
| 64 |
-
logger.info(f"Sending payload to Ollama: {payload}")
|
| 65 |
-
|
| 66 |
-
# 4. Stream response from Ollama
|
| 67 |
-
async def stream_response():
|
| 68 |
-
try:
|
| 69 |
-
async with httpx.AsyncClient() as client:
|
| 70 |
-
async with client.stream(
|
| 71 |
-
"POST",
|
| 72 |
-
OLLAMA_SERVER_URL,
|
| 73 |
-
json=payload,
|
| 74 |
-
headers={"Content-Type": "application/json"}
|
| 75 |
-
) as response:
|
| 76 |
-
# If we get 4xx or 5xx, raise_for_status() triggers HTTPStatusError
|
| 77 |
-
response.raise_for_status()
|
| 78 |
-
|
| 79 |
-
# Stream out the content as it arrives
|
| 80 |
-
async for chunk in response.aiter_text():
|
| 81 |
-
yield chunk
|
| 82 |
-
|
| 83 |
-
except httpx.RequestError:
|
| 84 |
-
logger.exception("Request error while communicating with Ollama")
|
| 85 |
-
yield json.dumps({"error": "Unable to communicate with Ollama"})
|
| 86 |
-
|
| 87 |
-
except httpx.HTTPStatusError as exc:
|
| 88 |
-
# Avoid reading the body since it's a streaming response and is closed on error
|
| 89 |
-
logger.error(f"HTTP error from Ollama: {exc.response.status_code}")
|
| 90 |
-
yield json.dumps({"error": f"HTTP error: {exc.response.status_code}"})
|
| 91 |
-
|
| 92 |
-
return StreamingResponse(stream_response(), media_type="application/json")
|
| 93 |
-
|
| 94 |
-
except Exception:
|
| 95 |
-
logger.exception("Unhandled exception in /api/generate")
|
| 96 |
-
raise HTTPException(status_code=500, detail="Internal server error")
|
| 97 |
-
|
| 98 |
-
@app.get("/health")
|
| 99 |
-
async def health():
|
| 100 |
-
"""Health check endpoint."""
|
| 101 |
-
logger.info("Health check endpoint called")
|
| 102 |
-
return {"status": "OK"}
|
| 103 |
-
|
| 104 |
-
if __name__ == "__main__":
|
| 105 |
-
import uvicorn
|
| 106 |
-
logger.info("Starting server on http://0.0.0.0:7860")
|
| 107 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|