Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,8 +6,9 @@ from fastapi.responses import JSONResponse, HTMLResponse
|
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
@app.get("/", response_class=HTMLResponse)
|
| 13 |
async def home():
|
|
@@ -19,7 +20,7 @@ async def ask_ai(request: Request):
|
|
| 19 |
data = await request.json()
|
| 20 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 21 |
|
| 22 |
-
# We
|
| 23 |
payload = {
|
| 24 |
"inputs": data.get("inputs", ""),
|
| 25 |
"options": {"wait_for_model": True}
|
|
@@ -27,8 +28,9 @@ async def ask_ai(request: Request):
|
|
| 27 |
|
| 28 |
response = requests.post(MODEL_URL, headers=headers, json=payload)
|
| 29 |
|
| 30 |
-
# This
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
return JSONResponse(content=response.json())
|
|
|
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 9 |
+
|
| 10 |
+
# This is the modern 'Router' URL that is more reliable
|
| 11 |
+
MODEL_URL = "https://router.huggingface.co/hf-inference/models/danosethrus/EthioDoc"
|
| 12 |
|
| 13 |
@app.get("/", response_class=HTMLResponse)
|
| 14 |
async def home():
|
|
|
|
| 20 |
data = await request.json()
|
| 21 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 22 |
|
| 23 |
+
# We send the question
|
| 24 |
payload = {
|
| 25 |
"inputs": data.get("inputs", ""),
|
| 26 |
"options": {"wait_for_model": True}
|
|
|
|
| 28 |
|
| 29 |
response = requests.post(MODEL_URL, headers=headers, json=payload)
|
| 30 |
|
| 31 |
+
# This checks if it's still 404 before trying to read JSON (to avoid the crash)
|
| 32 |
+
if response.status_code != 200:
|
| 33 |
+
print(f"FAILED: {response.status_code} - {response.text}")
|
| 34 |
+
return JSONResponse({"error": "AI is still booting up or URL is wrong."}, status_code=response.status_code)
|
| 35 |
|
| 36 |
return JSONResponse(content=response.json())
|