| import gradio as gr |
| import requests |
| import os |
|
|
| HF_TOKEN = os.getenv("HF_TOKEN") |
|
|
| API_URL = "https://router.huggingface.co/hf-inference/models/joeddav/xlm-roberta-large-xnli" |
|
|
|
|
| def classify(payload): |
| try: |
| |
| if isinstance(payload, list): |
| payload = payload[0] |
|
|
| invoice_text = payload.get("invoice_text", "") |
| categories = payload.get("categories", []) |
|
|
| if not invoice_text or not categories: |
| return "NO_INPUT" |
|
|
| |
| invoice_text = invoice_text.strip()[:3000] |
|
|
| candidate_labels = [c["cat_name"].strip() for c in categories] |
|
|
| if not candidate_labels: |
| return "NO_CATEGORIES" |
|
|
| response = requests.post( |
| API_URL, |
| headers={ |
| "Authorization": f"Bearer {HF_TOKEN}", |
| "Content-Type": "application/json" |
| }, |
| json={ |
| "inputs": invoice_text, |
| "parameters": { |
| "candidate_labels": candidate_labels, |
| "multi_label": False |
| } |
| }, |
| timeout=120 |
| ) |
|
|
| print("STATUS:", response.status_code) |
| print("RESPONSE:", response.text[:500]) |
|
|
| if response.status_code != 200: |
| return f"HTTP_ERROR_{response.status_code}" |
|
|
| result = response.json() |
|
|
| |
| |
| |
|
|
| if isinstance(result, list): |
| |
| if not result: |
| return "NO_RESULT" |
| result.sort(key=lambda x: x.get("score", 0), reverse=True) |
| return result[0].get("label", "NO_LABEL") |
|
|
| elif isinstance(result, dict): |
| labels = result.get("labels", []) |
| scores = result.get("scores", []) |
|
|
| if not labels: |
| return "NO_LABEL" |
|
|
| |
| |
| best_index = scores.index(max(scores)) if scores else 0 |
| return labels[best_index] |
|
|
| else: |
| return "UNEXPECTED_RESPONSE" |
|
|
| except requests.exceptions.Timeout: |
| return "TIMEOUT" |
| except Exception as e: |
| import traceback |
| traceback.print_exc() |
| return f"ERROR: {str(e)}" |
|
|
|
|
| gr.Interface( |
| fn=classify, |
| inputs="json", |
| outputs="text" |
| ).launch(ssr_mode=False) |