Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| import torch | |
| import torch.nn.functional as F | |
| # Modèle de détection de langue | |
| model_name = "papluca/xlm-roberta-base-language-detection" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForSequenceClassification.from_pretrained(model_name) | |
| # Récupération des étiquettes de langue depuis la config | |
| id2label = model.config.id2label | |
| # Fonction de détection de langue | |
| def detect_language(text): | |
| inputs = tokenizer(text, return_tensors="pt", truncation=True) | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| probs = F.softmax(outputs.logits, dim=1) | |
| confidence, predicted_class = torch.max(probs, dim=1) | |
| label = id2label[predicted_class.item()] | |
| return f"{label} ({confidence.item():.2%} confidence)" | |
| # Interface Gradio | |
| demo = gr.Interface(fn=detect_language, inputs="text", outputs="text", title="Language Detection") | |
| demo.launch() | |