Ubuntu commited on
Commit
c530b86
1 Parent(s): 1185541

Add model selection

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -4,32 +4,40 @@ from optimum.intel.openvino import OVModelForSequenceClassification
4
  from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
5
 
6
  model_id = "juliensimon/xlm-v-base-language-id"
 
 
 
7
  ov_model = OVModelForSequenceClassification.from_pretrained(
8
  model_id, from_transformers=True
9
  )
10
  tokenizer = AutoTokenizer.from_pretrained(model_id)
11
 
12
- p = pipeline("text-classification", model=ov_model, tokenizer=tokenizer)
13
 
14
  # Warmum
15
  for i in range(100):
16
- p("Hello world")
17
 
18
 
19
- def process(text, top_k=5):
 
 
 
 
20
  tick = time.time()
21
- pred = p(text, top_k=top_k)
22
  tock = time.time()
23
  scores = {x["label"]: x["score"] for x in pred}
24
- msg = f'{(tock-tick)*1000:.2f} milliseconds'
25
  return scores, msg
26
 
27
 
28
  # Gradio inputs
29
  input_text = gr.Text(label="Enter text")
 
30
 
31
  # Gradio outputs
32
- labels = gr.Label(label="Languages", num_top_classes=5)
33
  output_text = gr.Text(label="Prediction time")
34
 
35
  description = "This Space lets you perform language identification on the 102 languages present in the google/fleurs dataset. The underlying model scores 99.3% accuracy on the validation set. Inference is optimized with Optimum Intel and OpenVINO."
@@ -37,8 +45,8 @@ description = "This Space lets you perform language identification on the 102 la
37
  iface = gr.Interface(
38
  description=description,
39
  fn=process,
40
- inputs=input_text,
41
- outputs=[labels, output_text],
42
  examples=[
43
  "Kila mtu ana haki ya kuelimishwa. Elimu yapasa itolewe bure hasa ile ya madarasa ya chini. Elimu ya masarasa ya chini ihudhuriwe kwa lazima. Elimu ya ufundi na ustadi iwe wazi kwa wote. Na elimu ya juu iwe wazi kwa wote kwa kutegemea sifa ya mtu",
44
  "Ang bawat tao'y may karapatan sa edukasyon. Ang edukasyon ay walang bayad, doon man lamang sa elementarya at pangunahing antas. Ang edukasyong elementarya ay magiging sapilitan. Ang edukasyong teknikal at propesyonal ay gagawing maabot ng lahat at ang lalong mataas na edukasyon ay ipagkakaloob nang pantay-pantay sa lahat batay sa pagiging karapat-dapat.",
 
4
  from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
5
 
6
  model_id = "juliensimon/xlm-v-base-language-id"
7
+
8
+ p = pipeline("text-classification", model=model_id)
9
+
10
  ov_model = OVModelForSequenceClassification.from_pretrained(
11
  model_id, from_transformers=True
12
  )
13
  tokenizer = AutoTokenizer.from_pretrained(model_id)
14
 
15
+ ov_p = pipeline("text-classification", model=ov_model, tokenizer=tokenizer)
16
 
17
  # Warmum
18
  for i in range(100):
19
+ ov_p("Hello world")
20
 
21
 
22
+ def process(text, model, top_k=5):
23
+ if model == "Vanilla":
24
+ pipe = p
25
+ else:
26
+ pipe = ov_p
27
  tick = time.time()
28
+ pred = pipe(text, top_k=top_k)
29
  tock = time.time()
30
  scores = {x["label"]: x["score"] for x in pred}
31
+ msg = f"{(tock-tick)*1000:.2f} milliseconds"
32
  return scores, msg
33
 
34
 
35
  # Gradio inputs
36
  input_text = gr.Text(label="Enter text")
37
+ input_model = gr.Radio(label="Pick a model", choices=["Vanilla", "OpenVINO"])
38
 
39
  # Gradio outputs
40
+ output_labels = gr.Label(label="Languages", num_top_classes=5)
41
  output_text = gr.Text(label="Prediction time")
42
 
43
  description = "This Space lets you perform language identification on the 102 languages present in the google/fleurs dataset. The underlying model scores 99.3% accuracy on the validation set. Inference is optimized with Optimum Intel and OpenVINO."
 
45
  iface = gr.Interface(
46
  description=description,
47
  fn=process,
48
+ inputs=[input_text, input_model],
49
+ outputs=[output_labels, output_text],
50
  examples=[
51
  "Kila mtu ana haki ya kuelimishwa. Elimu yapasa itolewe bure hasa ile ya madarasa ya chini. Elimu ya masarasa ya chini ihudhuriwe kwa lazima. Elimu ya ufundi na ustadi iwe wazi kwa wote. Na elimu ya juu iwe wazi kwa wote kwa kutegemea sifa ya mtu",
52
  "Ang bawat tao'y may karapatan sa edukasyon. Ang edukasyon ay walang bayad, doon man lamang sa elementarya at pangunahing antas. Ang edukasyong elementarya ay magiging sapilitan. Ang edukasyong teknikal at propesyonal ay gagawing maabot ng lahat at ang lalong mataas na edukasyon ay ipagkakaloob nang pantay-pantay sa lahat batay sa pagiging karapat-dapat.",