papluca commited on
Commit
a53e086
·
verified ·
1 Parent(s): 3fc6bb2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Gradio app to showcase the language detector."""
2
+
3
+ import gradio as gr
4
+ from transformers import pipeline
5
+
6
+
7
+ # Get transformer model and set up a pipeline
8
+ model_ckpt = "papluca/xlm-roberta-base-language-detection"
9
+ pipe = pipeline("text-classification", model=model_ckpt)
10
+
11
+
12
+ def predict(text: str) -> dict:
13
+ """Compute predictions for text."""
14
+ preds = pipe(text, return_all_scores=True, truncation=True, max_length=128)
15
+ if preds:
16
+ pred = preds[0]
17
+ return {p["label"]: float(p["score"]) for p in pred}
18
+ else:
19
+ return None
20
+
21
+
22
+ title = "Language detection with XLM-RoBERTa"
23
+ description = "Determine the language in which your text is written."
24
+ examples = [
25
+ ["Better late than never."],
26
+ ["Tutto è bene ciò che finisce bene."],
27
+ ["Donde hay humo, hay fuego."],
28
+ ]
29
+ explanation = "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)."
30
+
31
+ app = gr.Interface(
32
+ fn=predict,
33
+ inputs=gr.inputs.Textbox(
34
+ placeholder="What's the text you want to know the language for?",
35
+ label="Text",
36
+ lines=3,
37
+ ),
38
+ outputs=gr.outputs.Label(num_top_classes=3, label="Your text is written in "),
39
+ title=title,
40
+ description=description,
41
+ examples=examples,
42
+ article=explanation,
43
+ )
44
+
45
+ app.launch()