Update app.py
Browse files
app.py
CHANGED
|
@@ -33,11 +33,12 @@ logger.debug(f"Loaded API key: {API_KEY}")
|
|
| 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 |
-
#
|
| 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", "")
|
|
@@ -48,7 +49,7 @@ async def generate(request: Request):
|
|
| 48 |
|
| 49 |
logger.debug(f"Request body: {body}")
|
| 50 |
|
| 51 |
-
#
|
| 52 |
auth_header = request.headers.get("Authorization")
|
| 53 |
logger.debug(f"Received Authorization header: {auth_header}")
|
| 54 |
|
|
@@ -61,14 +62,14 @@ async def generate(request: Request):
|
|
| 61 |
logger.error(f"Invalid API key provided: {token}")
|
| 62 |
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 63 |
|
| 64 |
-
#
|
| 65 |
payload = {"model": model, "prompt": prompt_text}
|
| 66 |
logger.debug(f"Payload prepared for Ollama: {payload}")
|
| 67 |
|
| 68 |
-
#
|
| 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:
|
|
@@ -82,12 +83,15 @@ async def generate(request: Request):
|
|
| 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
|
| 89 |
-
logger.
|
| 90 |
-
yield json.dumps({"error":
|
| 91 |
|
| 92 |
return StreamingResponse(stream_response(), media_type="application/json")
|
| 93 |
|
|
@@ -95,12 +99,14 @@ async def generate(request: Request):
|
|
| 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")
|
|
|
|
| 33 |
OLLAMA_SERVER_URL = "http://localhost:11434/api/generate"
|
| 34 |
logger.debug(f"Ollama server URL: {OLLAMA_SERVER_URL}")
|
| 35 |
|
| 36 |
+
|
| 37 |
@app.post("/api/generate")
|
| 38 |
async def generate(request: Request):
|
| 39 |
"""Endpoint that generates text based on the prompt."""
|
| 40 |
try:
|
| 41 |
+
# Parse the incoming request
|
| 42 |
body = await request.json()
|
| 43 |
model = body.get("model", "hf.co/abanm/Dubs-Q8_0-GGUF:latest") # Default model
|
| 44 |
prompt_text = body.get("prompt", "")
|
|
|
|
| 49 |
|
| 50 |
logger.debug(f"Request body: {body}")
|
| 51 |
|
| 52 |
+
# Validate API key
|
| 53 |
auth_header = request.headers.get("Authorization")
|
| 54 |
logger.debug(f"Received Authorization header: {auth_header}")
|
| 55 |
|
|
|
|
| 62 |
logger.error(f"Invalid API key provided: {token}")
|
| 63 |
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 64 |
|
| 65 |
+
# Prepare request payload
|
| 66 |
payload = {"model": model, "prompt": prompt_text}
|
| 67 |
logger.debug(f"Payload prepared for Ollama: {payload}")
|
| 68 |
|
| 69 |
+
# Stream response from Ollama
|
| 70 |
async def stream_response():
|
| 71 |
try:
|
| 72 |
+
async with httpx.AsyncClient(timeout=httpx.Timeout(60.0)) as client:
|
| 73 |
async with client.stream(
|
| 74 |
"POST", OLLAMA_SERVER_URL, json=payload, headers={"Content-Type": "application/json"}
|
| 75 |
) as response:
|
|
|
|
| 83 |
async for chunk in response.aiter_text():
|
| 84 |
logger.debug(f"Chunk received: {chunk}")
|
| 85 |
yield chunk
|
| 86 |
+
except httpx.ReadTimeout:
|
| 87 |
+
logger.error("ReadTimeout while waiting for response chunks")
|
| 88 |
+
yield json.dumps({"error": "Server response timeout. Try again later."})
|
| 89 |
except httpx.RequestError as exc:
|
| 90 |
logger.error(f"Request error while communicating with Ollama: {str(exc)}")
|
| 91 |
yield json.dumps({"error": "Network error occurred while communicating with Ollama"})
|
| 92 |
+
except Exception as exc:
|
| 93 |
+
logger.exception(f"Unexpected error during streaming: {str(exc)}")
|
| 94 |
+
yield json.dumps({"error": "An unexpected error occurred during streaming."})
|
| 95 |
|
| 96 |
return StreamingResponse(stream_response(), media_type="application/json")
|
| 97 |
|
|
|
|
| 99 |
logger.exception(f"Unexpected error: {str(e)}")
|
| 100 |
raise HTTPException(status_code=500, detail="An unexpected error occurred")
|
| 101 |
|
| 102 |
+
|
| 103 |
@app.get("/health")
|
| 104 |
async def health():
|
| 105 |
"""Health check endpoint."""
|
| 106 |
logger.info("Health check endpoint accessed")
|
| 107 |
return {"status": "OK"}
|
| 108 |
|
| 109 |
+
|
| 110 |
if __name__ == "__main__":
|
| 111 |
import uvicorn
|
| 112 |
logger.info("Starting FastAPI application")
|