Spaces:
Sleeping
Sleeping
File size: 969 Bytes
386ee45 968f79d 386ee45 968f79d 386ee45 968f79d 386ee45 968f79d 386ee45 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import requests
import logging
import os
from dotenv import load_dotenv
load_dotenv()
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
log = logging.getLogger(__name__)
def hf_infer(model: str, text: str):
try:
resp = requests.post(
f"https://api-inference.huggingface.co/models/{model}",
headers={"Authorization": f"Bearer {HF_API_TOKEN}"},
json={"inputs": text},
timeout=15
)
resp.raise_for_status()
out = resp.json()
# Normalize API output from dict → list[]
if isinstance(out, dict):
out = [out]
elif isinstance(out, list) and len(out) == 1 and isinstance(out[0], list):
out = out[0]
# Filter invalid entries
out = [x for x in out if isinstance(x, dict) and "label" in x and "score" in x]
return out
except Exception as e:
log.warning(f"HF inference failed ({model}): {e}")
return []
|