ruanchaves commited on
Commit
34cde4f
1 Parent(s): 82306f4

simplify interface

Browse files
Files changed (2) hide show
  1. app.py +36 -22
  2. requirements.txt +2 -1
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
4
  from collections import Counter
 
5
 
6
  article_string = "Author: <a href=\"https://huggingface.co/ruanchaves\">Ruan Chaves Rodrigues</a>. Read more about our <a href=\"https://github.com/ruanchaves/eplm\">research on the evaluation of Portuguese language models</a>."
7
 
@@ -58,6 +59,10 @@ user_friendly_name = {
58
  "ruanchaves/bert-large-portuguese-cased-hatebr": "BERTimbau large (HateBR)",
59
  }
60
 
 
 
 
 
61
  model_array = []
62
 
63
  for model_name in model_list:
@@ -71,39 +76,48 @@ def most_frequent(array):
71
  occurence_count = Counter(array)
72
  return occurence_count.most_common(1)[0][0]
73
 
74
- def predict(s1):
 
 
 
75
  scores = {}
 
76
  for row in model_array:
77
- name = user_friendly_name[row["name"]]
78
- tokenizer = row["tokenizer"]
79
- model = row["model"]
80
- model_input = tokenizer(*([s1],), padding=True, return_tensors="pt")
81
- with torch.no_grad():
82
- output = model(**model_input)
83
- score = output[0][0].argmax().item()
84
- scores[name] = score
85
- average_score = most_frequent(list(scores.values()))
86
- description = score_descriptions[average_score]
87
- description_pt = score_descriptions_pt[average_score]
88
- final_description = description + "\n \n" + description_pt
89
-
90
- for key, value in scores.items():
91
- scores[key] = score_descriptions[value]
92
-
93
- return final_description, scores
 
 
 
 
 
94
 
95
 
96
  inputs = [
97
- gr.inputs.Textbox(label="Text"),
 
98
  ]
99
 
100
  outputs = [
101
- gr.Textbox(label="Evaluation", value=output_textbox_component_description),
102
- gr.JSON(label="Results by model", value=output_json_component_description)
103
  ]
104
 
105
 
106
  gr.Interface(fn=predict, inputs=inputs, outputs=outputs, title=app_title,
107
  description=app_description,
108
  examples=app_examples,
109
- article = article_string).launch()
 
2
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
4
  from collections import Counter
5
+ from scipy.special import softmax
6
 
7
  article_string = "Author: <a href=\"https://huggingface.co/ruanchaves\">Ruan Chaves Rodrigues</a>. Read more about our <a href=\"https://github.com/ruanchaves/eplm\">research on the evaluation of Portuguese language models</a>."
8
 
 
59
  "ruanchaves/bert-large-portuguese-cased-hatebr": "BERTimbau large (HateBR)",
60
  }
61
 
62
+ reverse_user_friendly_name = { v:k for k,v in user_friendly_name.items() }
63
+
64
+ user_friendly_name_list = list(user_friendly_name.values())
65
+
66
  model_array = []
67
 
68
  for model_name in model_list:
 
76
  occurence_count = Counter(array)
77
  return occurence_count.most_common(1)[0][0]
78
 
79
+
80
+ def predict(s1, chosen_model):
81
+ if not chosen_model:
82
+ chosen_model = user_friendly_name_list[0]
83
  scores = {}
84
+ full_chosen_model_name = reverse_user_friendly_name[chosen_model]
85
  for row in model_array:
86
+ name = row["name"]
87
+ if name != full_chosen_model_name:
88
+ continue
89
+ else:
90
+ tokenizer = row["tokenizer"]
91
+ model = row["model"]
92
+ model_input = tokenizer(*([s1],), padding=True, return_tensors="pt")
93
+ with torch.no_grad():
94
+ output = model(**model_input)
95
+ logits = output[0][0].detach().numpy()
96
+ logits = softmax(logits).tolist()
97
+ print(f"logits: {logits}")
98
+ break
99
+ def get_description(idx):
100
+ description = score_descriptions[idx]
101
+ description_pt = score_descriptions_pt[idx]
102
+ final_description = description + "\n \n" + description_pt
103
+ return final_description
104
+
105
+ scores = { get_description(k):v for k,v in enumerate(logits) }
106
+
107
+ return scores
108
 
109
 
110
  inputs = [
111
+ gr.Textbox(label="Text"),
112
+ gr.Dropdown(label="Model", choices=user_friendly_name_list, default=user_friendly_name_list[0])
113
  ]
114
 
115
  outputs = [
116
+ gr.Label(label="Result")
 
117
  ]
118
 
119
 
120
  gr.Interface(fn=predict, inputs=inputs, outputs=outputs, title=app_title,
121
  description=app_description,
122
  examples=app_examples,
123
+ article = article_string).launch(share=True, debug=True)
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  torch
2
  gradio
3
- transformers
 
 
1
  torch
2
  gradio
3
+ transformers
4
+ scipy