lunadebruyne commited on
Commit
4bbe987
·
verified ·
1 Parent(s): 01e7b54

Delete app_old.py

Browse files
Files changed (1) hide show
  1. app_old.py +0 -105
app_old.py DELETED
@@ -1,105 +0,0 @@
1
- import gradio as gr
2
- import torch
3
- import numpy as np
4
-
5
- import pandas as pd
6
- from tqdm import tqdm
7
-
8
- import matplotlib.pyplot as plt
9
-
10
- from transformers import AutoTokenizer, AutoConfig, AutoModel, AutoModelForSequenceClassification
11
-
12
-
13
- description_sentence = "<h3>Demo EmotioNL</h3>\nThis demo allows you to analyse the emotion in a sentence."
14
- description_dataset = "<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."
15
-
16
- inference_modelpath = "model/checkpoint-128"
17
-
18
- def inference_sentence(text):
19
- tokenizer = AutoTokenizer.from_pretrained(inference_modelpath)
20
- model = AutoModelForSequenceClassification.from_pretrained(inference_modelpath)
21
- for text in tqdm([text]):
22
- inputs = tokenizer(text, return_tensors="pt")
23
- with torch.no_grad(): # run model
24
- logits = model(**inputs).logits
25
- predicted_class_id = logits.argmax().item()
26
- output = model.config.id2label[predicted_class_id]
27
- return output
28
-
29
- def frequencies(preds):
30
- preds_dict = {"neutral": 0, "anger": 0, "fear": 0, "joy": 0, "love": 0, "sadness": 0}
31
- for pred in preds:
32
- preds_dict[pred] = preds_dict[pred] + 1
33
- bars = list(preds_dict.keys())
34
- height = list(preds_dict.values())
35
-
36
- x_pos = np.arange(len(bars))
37
- plt.bar(x_pos, height, color=['lightgrey', 'firebrick', 'rebeccapurple', 'orange', 'palevioletred', 'cornflowerblue'])
38
- plt.xticks(x_pos, bars)
39
- return plt
40
-
41
- def inference_dataset(file_object, option_list):
42
- tokenizer = AutoTokenizer.from_pretrained(inference_modelpath)
43
- model = AutoModelForSequenceClassification.from_pretrained(inference_modelpath)
44
- data_path = open(file_object.name, 'r')
45
- df = pd.read_csv(data_path, delimiter='\t', header=0, names=['id', 'text'])
46
- ids = df["id"].tolist()
47
- texts = df["text"].tolist()
48
- preds = []
49
- for text in tqdm(texts): # progressbar
50
- inputs = tokenizer(text, return_tensors="pt")
51
- with torch.no_grad(): # run model
52
- logits = model(**inputs).logits
53
- predicted_class_id = logits.argmax().item()
54
- prediction = model.config.id2label[predicted_class_id]
55
- preds.append(prediction)
56
- predictions_content = list(zip(ids, texts, preds))
57
- # write predictions to file
58
- output = "output.txt"
59
- f = open(output, 'w')
60
- f.write("id\ttext\tprediction\n")
61
- for line in predictions_content:
62
- f.write(str(line[0]) + '\t' + str(line[1]) + '\t' + str(line[2]) + '\n')
63
- output1 = output
64
- output2 = output3 = output4 = output5 = "This option was not selected."
65
- if "emotion frequencies" in option_list:
66
- output2 = frequencies(preds)
67
- else:
68
- output2 = None
69
- if "emotion distribution over time" in option_list:
70
- output3 = "This option was selected."
71
- if "peaks" in option_list:
72
- output4 = "This option was selected."
73
- if "topics" in option_list:
74
- output5 = "This option was selected."
75
- return [output1, output2, output3, output4, output5]
76
-
77
- iface_sentence = gr.Interface(
78
- fn=inference_sentence,
79
- description = description_sentence,
80
- inputs = gr.Textbox(
81
- label="Enter a sentence",
82
- lines=1),
83
- outputs="text")
84
-
85
- inputs = [gr.File(
86
- label="Upload a dataset"),
87
- gr.CheckboxGroup(
88
- ["emotion frequencies", "emotion distribution over time", "peaks", "topics"],
89
- label = "Select options")]
90
-
91
- outputs = [gr.File(),
92
- gr.Plot(label="Emotion frequencies"),
93
- gr.Textbox(label="Emotion distribution over time"),
94
- gr.Textbox(label="Peaks"),
95
- gr.Textbox(label="Topics")]
96
-
97
- iface_dataset = gr.Interface(
98
- fn = inference_dataset,
99
- description = description_dataset,
100
- inputs=inputs,
101
- outputs = outputs)
102
-
103
- iface = gr.TabbedInterface([iface_sentence, iface_dataset], ["Sentence", "Dataset"])
104
-
105
- iface.queue().launch()