papluca commited on
Commit
e0ead87
1 Parent(s): 3ea797a

Add predict function

Browse files
Files changed (1) hide show
  1. app.py +26 -3
app.py CHANGED
@@ -1,17 +1,40 @@
1
  """Gradio app to showcase the language detector."""
2
 
3
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  title = "Language detection with XLM-RoBERTa"
6
- description = "Determine the language in which your text is written.\nSupported 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)."
7
  examples = [
8
  ["Better late than never."],
9
  ["Tutto è bene ciò che finisce bene."],
10
  ["Donde hay humo, hay fuego."],
11
  ]
12
 
13
- app = gr.Interface.load(
14
- "huggingface/papluca/xlm-roberta-base-language-detection",
 
 
 
 
 
 
15
  description=description,
16
  examples=examples,
17
  )
 
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)
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. 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)."
24
  examples = [
25
  ["Better late than never."],
26
  ["Tutto è bene ciò che finisce bene."],
27
  ["Donde hay humo, hay fuego."],
28
  ]
29
 
30
+ app = gr.Interface(
31
+ fn=predict,
32
+ inputs=gr.inputs.Textbox(
33
+ placeholder="What's the text you want to know the language for?",
34
+ label="Text",
35
+ lines=3,
36
+ ),
37
+ outputs=gr.outputs.Label(num_top_classes=3, label="Your text is written in "),
38
  description=description,
39
  examples=examples,
40
  )