504 Server Error: Gateway Time-out for url

Hi everyone,

My script is having the following error:

504 Server Error: Gateway Time-out for url: https://router.huggingface.co/hf-inference/models/facebook/bart-large-mnli

there’s something not working properly

1 Like

Seems working now?

import os, json, time, random, requests

API_URL = "https://router.huggingface.co/hf-inference/models/facebook/bart-large-mnli"
HF_TOKEN = os.getenv("HF_TOKEN")  # set: export HF_TOKEN=hf_...
TEXT = "I bought a device from your company. It does not work and I want a refund."
LABELS = ["refund", "legal", "faq"]

TIMEOUT = float(os.getenv("HF_TIMEOUT", "90"))
RETRIES = int(os.getenv("HF_RETRIES", "3"))
BASE = float(os.getenv("HF_BACKOFF_BASE", "1.5"))

def backoff(i): return BASE * (2 ** (i-1)) + random.uniform(0, 0.3)

headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
payload = {"inputs": TEXT, "parameters": {"candidate_labels": LABELS}}

for attempt in range(1, RETRIES+1):
    try:
        r = requests.post(API_URL, headers=headers, json=payload, timeout=TIMEOUT)
        meta = {
            "attempt": attempt,
            "status": r.status_code,
            "elapsed_s": r.elapsed.total_seconds(),
            "date": r.headers.get("date"),
            "x-request-id": r.headers.get("x-request-id") or r.headers.get("x-amzn-trace-id"),
            "server": r.headers.get("server"),
        }
        if r.ok:
            data = r.json()
            print("[hf-debug] success", json.dumps(meta, indent=2))
            print(json.dumps(data, indent=2))
            break
        print("[hf-debug] http_error", json.dumps({**meta, "body": r.text[:500]}, indent=2))
        # Retry only on transient statuses
        if r.status_code not in (429, 503, 504) or attempt == RETRIES:
            break
    except requests.Timeout as e:
        print("[hf-debug] timeout", {"attempt": attempt, "error": str(e)})
        if attempt == RETRIES:
            break
    time.sleep(backoff(attempt))

"""
[hf-debug] success {
  "attempt": 1,
  "status": 200,
  "elapsed_s": 5.310831,
  "date": "Thu, 06 Nov 2025 15:53:39 GMT",
  "x-request-id": "Root=1-690cc47e-339aa87e2f2b99bb582b1c8b",
  "server": "uvicorn"
}
[
  {
    "label": "refund",
    "score": 0.9196358323097229
  },
  {
    "label": "faq",
    "score": 0.06989368051290512
  },
  {
    "label": "legal",
    "score": 0.010470455512404442
  }
]
"""