papluca's picture
Add predict function
e0ead87
raw
history blame
1.42 kB
"""Gradio app to showcase the language detector."""
import gradio as gr
from transformers import pipeline
# Get transformer model and set up a pipeline
model_ckpt = "papluca/xlm-roberta-base-language-detection"
pipe = pipeline("text-classification", model=model_ckpt)
def predict(text: str) -> dict:
"""Compute predictions for text."""
preds = pipe(text, return_all_scores=True)
if preds:
pred = preds[0]
return {p["label"]: float(p["score"]) for p in pred}
else:
return None
title = "Language detection with XLM-RoBERTa"
description = "Determine the language in which your text is written. Supported languages are (20): arabic (ar), bulgarian (bg), german (de), modern greek (el), english (en), spanish (es), french (fr), hindi (hi), italian (it), japanese (ja), dutch (nl), polish (pl), portuguese (pt), russian (ru), swahili (sw), thai (th), turkish (tr), urdu (ur), vietnamese (vi), and chinese (zh)."
examples = [
["Better late than never."],
["Tutto è bene ciò che finisce bene."],
["Donde hay humo, hay fuego."],
]
app = gr.Interface(
fn=predict,
inputs=gr.inputs.Textbox(
placeholder="What's the text you want to know the language for?",
label="Text",
lines=3,
),
outputs=gr.outputs.Label(num_top_classes=3, label="Your text is written in "),
description=description,
examples=examples,
)
app.launch()