Spaces:
Sleeping
Sleeping
File size: 1,246 Bytes
c1fb7b1 | 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 37 38 39 40 41 42 43 44 45 46 47 | from pathlib import Path
import gradio as gr
from language_classifier import DEFAULT_MODEL_PATH, LanguageDetector
MODEL_PATH = Path(DEFAULT_MODEL_PATH)
detector = LanguageDetector(model_path=MODEL_PATH)
detector.load_model()
def predict_language(text: str) -> dict[str, str | float]:
prediction = detector.predict_with_confidence(text or "")
if prediction["message"]:
return {
"language": prediction["language_name"],
"confidence": prediction["confidence"],
"status": prediction["message"],
}
status = "Confident" if prediction["is_confident"] else "Uncertain"
return {
"language": prediction["language_name"],
"confidence": round(prediction["confidence"], 4),
"status": status,
}
interface = gr.Interface(
fn=predict_language,
inputs=gr.Textbox(
lines=5,
placeholder="Type a question in any supported language...",
label="User Question",
),
outputs=gr.JSON(label="Detection Result"),
title="Module 1: Language Detection",
description="Character n-gram TF-IDF language classifier for routing chatbot queries.",
flagging_mode="never",
)
if __name__ == "__main__":
interface.launch()
|