Update app.py
Browse files
app.py
CHANGED
|
@@ -29,7 +29,11 @@ app.add_middleware(
|
|
| 29 |
API_KEY = os.environ.get("API_KEY", "change_me")
|
| 30 |
logger.info(f"API key loaded: {API_KEY}")
|
| 31 |
|
| 32 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
OLLAMA_SERVER_URL = "http://localhost:11434/generate"
|
| 34 |
|
| 35 |
@app.post("/api/generate")
|
|
@@ -73,7 +77,7 @@ async def generate(request: Request):
|
|
| 73 |
json=payload,
|
| 74 |
headers={"Content-Type": "application/json"}
|
| 75 |
) as response:
|
| 76 |
-
# If we get
|
| 77 |
response.raise_for_status()
|
| 78 |
|
| 79 |
# Stream out the content as it arrives
|
|
@@ -85,12 +89,10 @@ async def generate(request: Request):
|
|
| 85 |
yield json.dumps({"error": "Unable to communicate with Ollama"})
|
| 86 |
|
| 87 |
except httpx.HTTPStatusError as exc:
|
| 88 |
-
#
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
logger.error(f"HTTP error from Ollama: {exc.response.status_code} - {error_text}")
|
| 93 |
-
yield json.dumps({"error": f"HTTP error: {error_text}"})
|
| 94 |
|
| 95 |
return StreamingResponse(stream_response(), media_type="application/json")
|
| 96 |
|
|
@@ -108,4 +110,3 @@ if __name__ == "__main__":
|
|
| 108 |
import uvicorn
|
| 109 |
logger.info("Starting server on http://0.0.0.0:7860")
|
| 110 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 111 |
-
|
|
|
|
| 29 |
API_KEY = os.environ.get("API_KEY", "change_me")
|
| 30 |
logger.info(f"API key loaded: {API_KEY}")
|
| 31 |
|
| 32 |
+
# IMPORTANT: Choose the correct endpoint for your Ollama version.
|
| 33 |
+
# - If your Ollama logs say "POST /generate" => "http://localhost:11434/generate"
|
| 34 |
+
# - If it says "POST /api/generate" => "http://localhost:11434/api/generate"
|
| 35 |
+
# Double-check with a direct curl: curl -X POST -d '{}' http://localhost:11434/generate
|
| 36 |
+
# If that returns 404, try /api/generate instead.
|
| 37 |
OLLAMA_SERVER_URL = "http://localhost:11434/generate"
|
| 38 |
|
| 39 |
@app.post("/api/generate")
|
|
|
|
| 77 |
json=payload,
|
| 78 |
headers={"Content-Type": "application/json"}
|
| 79 |
) as response:
|
| 80 |
+
# If we get a 4xx/5xx, raise_for_status() triggers HTTPStatusError
|
| 81 |
response.raise_for_status()
|
| 82 |
|
| 83 |
# Stream out the content as it arrives
|
|
|
|
| 89 |
yield json.dumps({"error": "Unable to communicate with Ollama"})
|
| 90 |
|
| 91 |
except httpx.HTTPStatusError as exc:
|
| 92 |
+
# The stream is already closed if we get 404 or another 4xx/5xx error
|
| 93 |
+
# so we can't call aread() safely. Let's just log the status code.
|
| 94 |
+
logger.error(f"HTTP error from Ollama: {exc.response.status_code}")
|
| 95 |
+
yield json.dumps({"error": f"HTTP error: {exc.response.status_code}"})
|
|
|
|
|
|
|
| 96 |
|
| 97 |
return StreamingResponse(stream_response(), media_type="application/json")
|
| 98 |
|
|
|
|
| 110 |
import uvicorn
|
| 111 |
logger.info("Starting server on http://0.0.0.0:7860")
|
| 112 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|