lunadebruyne commited on
Commit
e589d46
·
1 Parent(s): a069d53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -1,8 +1,13 @@
1
  import gradio as gr
 
 
2
 
3
- description1 = "<h3>Demo EmotioNL</h3>\nThis demo allows you to analyse the emotion in a sentence."
 
4
  description2 = "<h3>Demo EmotioNL</h3>\nThis demo allows you to analyse the emotions in a dataset.\nThe data should be in tsv-format with two named columns: the first column (id) should contain the sentence IDs, and the second column (text) should contain the actual texts. Optionally, there is a third column named 'date', which specifies the date associated with the text (e.g., tweet date). This column is necessary when the options 'emotion distribution over time' and 'peaks' are selected."
5
 
 
 
6
  def what_happened(text, file_object, option_list):
7
  if file_object:
8
  output = "You uploaded a file."
@@ -35,6 +40,16 @@ def what_happened2(file_object, option_list):
35
  if "topics" in option_list:
36
  output5 = "This option was selected."
37
  return [output1, output2, output3, output4, output5]
 
 
 
 
 
 
 
 
 
 
38
 
39
 
40
  iface0 = gr.Interface(
@@ -52,13 +67,12 @@ iface0 = gr.Interface(
52
  ],
53
  outputs="text")
54
 
55
- iface1 = gr.Interface(
56
- fn=what_happened1,
57
- description = description1,
58
  inputs = gr.Textbox(
59
  label="Enter a sentence",
60
- lines=1,
61
- value="Your name"),
62
  outputs="text")
63
 
64
  iface2 = gr.Interface(
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoConfig, AutoModel, AutoModelForSequenceClassification
4
 
5
+
6
+ description_sentence = "<h3>Demo EmotioNL</h3>\nThis demo allows you to analyse the emotion in a sentence."
7
  description2 = "<h3>Demo EmotioNL</h3>\nThis demo allows you to analyse the emotions in a dataset.\nThe data should be in tsv-format with two named columns: the first column (id) should contain the sentence IDs, and the second column (text) should contain the actual texts. Optionally, there is a third column named 'date', which specifies the date associated with the text (e.g., tweet date). This column is necessary when the options 'emotion distribution over time' and 'peaks' are selected."
8
 
9
+ inference_modelpath = "model/checkpoint-128"
10
+
11
  def what_happened(text, file_object, option_list):
12
  if file_object:
13
  output = "You uploaded a file."
 
40
  if "topics" in option_list:
41
  output5 = "This option was selected."
42
  return [output1, output2, output3, output4, output5]
43
+
44
+ def inference_sentence(text):
45
+ tokenizer = AutoTokenizer.from_pretrained(inference_modelpath)
46
+ model = AutoModelForSequenceClassification.from_pretrained(inference_modelpath)
47
+ inputs = tokenizer(text, return_tensors="pt")
48
+ with torch.no_grad(): # run model
49
+ logits = model(**inputs).logits
50
+ predicted_class_id = logits.argmax().item()
51
+ output = model.config.id2label[predicted_class_id]
52
+ return output
53
 
54
 
55
  iface0 = gr.Interface(
 
67
  ],
68
  outputs="text")
69
 
70
+ iface_sentence = gr.Interface(
71
+ fn=inference_sentence,
72
+ description = description_sentence,
73
  inputs = gr.Textbox(
74
  label="Enter a sentence",
75
+ lines=1),
 
76
  outputs="text")
77
 
78
  iface2 = gr.Interface(