sergeyfeldman commited on
Commit
cef4787
·
verified ·
1 Parent(s): e208e60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -16
app.py CHANGED
@@ -13,35 +13,33 @@ tokenizer.do_lower_case = True
13
  model = AutoModelForSequenceClassification.from_pretrained("guidecare/feelings_and_issues_large_v2", token=authtoken, use_safetensors=True)
14
  all_label_names = list(model.config.id2label.values())
15
 
16
-
17
  def predict(text):
18
  probs = expit(model(**tokenizer([text], return_tensors="pt", padding=True)).logits.detach().numpy())
19
- # can't use numpy for whatever reason
20
  probs = [float(np.round(i, 2)) for i in probs[0]]
21
- # break out issue, harm, sentiment, feeling
22
  zipped_list = list(zip(all_label_names, probs))
23
  print(text, zipped_list)
 
24
  issues = [(i, j) for i, j in zipped_list if i.startswith('issue')]
25
  feelings = [(i, j) for i, j in zipped_list if i.startswith('feeling')]
26
  harm = [(i, j) for i, j in zipped_list if i.startswith('harm')]
27
  sentiment = [(i, j) for i, j in zipped_list if i.startswith('sentiment')]
28
- # keep tops for each one
29
- issues = sorted(issues, key=lambda x: x[1])[::-1]
30
- feelings = sorted(feelings, key=lambda x: x[1])[::-1]
31
- harm = sorted(harm, key=lambda x: x[1])[::-1]
32
- sentiment = sorted(sentiment, key=lambda x: x[1])[::-1]
33
- # top is the combo of these
34
  top = issues + feelings + harm + sentiment
35
-
36
  d = {i: j for i, j in top}
37
  return d
38
 
39
-
40
  iface = gr.Interface(
41
- fn=predict,
42
- inputs="text",
43
- outputs="label",
44
- #examples=["This test tomorrow is really freaking me out."]
 
45
  )
46
 
47
- iface.launch()
 
 
13
  model = AutoModelForSequenceClassification.from_pretrained("guidecare/feelings_and_issues_large_v2", token=authtoken, use_safetensors=True)
14
  all_label_names = list(model.config.id2label.values())
15
 
 
16
  def predict(text):
17
  probs = expit(model(**tokenizer([text], return_tensors="pt", padding=True)).logits.detach().numpy())
 
18
  probs = [float(np.round(i, 2)) for i in probs[0]]
 
19
  zipped_list = list(zip(all_label_names, probs))
20
  print(text, zipped_list)
21
+
22
  issues = [(i, j) for i, j in zipped_list if i.startswith('issue')]
23
  feelings = [(i, j) for i, j in zipped_list if i.startswith('feeling')]
24
  harm = [(i, j) for i, j in zipped_list if i.startswith('harm')]
25
  sentiment = [(i, j) for i, j in zipped_list if i.startswith('sentiment')]
26
+
27
+ issues = sorted(issues, key=lambda x: x[1], reverse=True)
28
+ feelings = sorted(feelings, key=lambda x: x[1], reverse=True)
29
+ harm = sorted(harm, key=lambda x: x[1], reverse=True)
30
+ sentiment = sorted(sentiment, key=lambda x: x[1], reverse=True)
31
+
32
  top = issues + feelings + harm + sentiment
 
33
  d = {i: j for i, j in top}
34
  return d
35
 
 
36
  iface = gr.Interface(
37
+ fn=predict,
38
+ inputs=gr.Textbox(label="Enter text"),
39
+ outputs=gr.Label(label="Predictions"),
40
+ title="Emotion and Issue Predictor",
41
+ description="Enter a text to predict emotions and issues.",
42
  )
43
 
44
+ if __name__ == "__main__":
45
+ iface.launch()