import fasttext import gradio as gr from huggingface_hub import hf_hub_download # Télécharger le modèle depuis Hugging Face model_path = hf_hub_download( repo_id="facebook/fasttext-language-identification", filename="model.bin" ) # Charger le modèle avec FastText model = fasttext.load_model(model_path) def predict_language(text): if not text.strip(): return "", 0.0 # Nettoyage du texte text = text.replace('\n', ' ').strip() # Prédiction labels, probs = model.predict(text) # Formatage du résultat lang_code = labels[0].replace("__label__", "") probability = round(float(probs[0]), 4) return lang_code, probability # Interface Gradio interface = gr.Interface( fn=predict_language, inputs=gr.Textbox( label="Texte à analyser", placeholder="Écrivez ou collez votre texte ici..." ), outputs=[ gr.Text(label="Code langue"), gr.Number(label="Confiance", precision=4) ], examples=[ ["Bonjour, comment ça va ?"], ["Hello, how are you?"], ["Hola, ¿cómo estás?"] ], title="Détection de Langue avec FastText", description="Identifie la langue d'un texte entré. Supporte 176 langues différentes." ) # Configuration de déploiement pour Hugging Face Spaces if __name__ == "__main__": interface.launch( server_name="0.0.0.0", server_port=7860, share=True # Désactiver le partage public )