Download app.py from invocai/llm: direct link, hf CLI and curl.
- Browser
- Download file 2.69 kB
-
https://huggingface.co/spaces/invocai/llm/resolve/main/app.py
- Command line
-
hf download hf://spaces/invocai/llm/app.py
-
curl -L -o app.py https://huggingface.co/spaces/invocai/llm/resolve/main/app.py
2.69 kB
| 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: | |
| # Gradio JSON input bazen list içinde gelir | |
| 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" | |
| # Metni kırp ama Türkçe karakterleri bozmadan | |
| 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() | |
| # xlm-roberta ve deberta her ikisi de dict döner: | |
| # {"sequence": "...", "labels": [...], "scores": [...]} | |
| # Ama HF bazen list of dict döner, ikisini de handle ediyoruz. | |
| if isinstance(result, list): | |
| # list of {"label": ..., "score": ...} formatı | |
| 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" | |
| # En yüksek skorlu kategoriyi direkt döndür | |
| # (random veya penalty yok — model kararına güven) | |
| 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) |