danosethrus commited on
Commit
479b8fc
·
verified ·
1 Parent(s): 7ad9903

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -6
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
- # The standard address for a text-generation model
10
- MODEL_URL = "https://api-inference.huggingface.co/models/danosethrus/EthioDoc"
 
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 add 'wait_for_model' so it doesn't error out while loading the 4GB file
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 will show the real answer or error in your Logs
31
- print(f"DEBUG Status: {response.status_code}")
32
- print(f"DEBUG Response: {response.text}")
 
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())