j-hartmann commited on
Commit
284038e
1 Parent(s): 861be86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -10
app.py CHANGED
@@ -1,17 +1,103 @@
 
 
 
 
 
 
 
 
1
  import numpy as np
 
 
 
2
 
3
- import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
 
 
5
 
6
- def sepia(input_img):
7
- sepia_filter = np.array(
8
- [[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]]
9
- )
10
- sepia_img = input_img.dot(sepia_filter.T)
11
- sepia_img /= sepia_img.max()
12
- return sepia_img
13
 
14
 
15
- iface = gr.Interface(sepia, gr.inputs.Image(shape=(200, 200)), "image")
 
 
 
 
 
 
16
 
17
- iface.launch()
 
 
 
 
1
+ # imports
2
+ import gradio as gr
3
+ import pandas as pd
4
+ import tempfile
5
+ import itertools
6
+ # import required packages
7
+ import torch
8
+ import pandas as pd
9
  import numpy as np
10
+ from numpy import dot
11
+ from numpy.linalg import norm, multi_dot
12
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer
13
 
14
+ # load tokenizer and model, create trainer
15
+ model_name = "j-hartmann/emotion-english-distilroberta-base"
16
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
17
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
18
+ trainer = Trainer(model=model)
19
+
20
+ # compute dot product of inputs
21
+ # summary function - test for single gradio function interfrace
22
+ def gr_cosine_similarity(sentence1, sentence2):
23
+ # Create class for data preparation
24
+ class SimpleDataset:
25
+ def __init__(self, tokenized_texts):
26
+ self.tokenized_texts = tokenized_texts
27
+
28
+ def __len__(self):
29
+ return len(self.tokenized_texts["input_ids"])
30
+
31
+ def __getitem__(self, idx):
32
+ return {k: v[idx] for k, v in self.tokenized_texts.items()}
33
+
34
+ # sentences in list
35
+ lines_s = [sentence1, sentence2]
36
+ print(type(sentence1), type(sentence2))
37
+ print(sentence1, sentence2)
38
+ print(lines_s)
39
+
40
+ # Tokenize texts and create prediction data set
41
+ tokenized_texts = tokenizer(lines_s, truncation=True, padding=True)
42
+ pred_dataset = SimpleDataset(tokenized_texts)
43
+
44
+ # Run predictions -> predict whole df
45
+ predictions = trainer.predict(pred_dataset)
46
+
47
+ # Transform predictions to labels
48
+ preds = predictions.predictions.argmax(-1)
49
+ labels = pd.Series(preds).map(model.config.id2label)
50
+ scores = (np.exp(predictions[0])/np.exp(predictions[0]).sum(-1,keepdims=True)).max(1)
51
+ # scores raw
52
+ temp = (np.exp(predictions[0])/np.exp(predictions[0]).sum(-1, keepdims=True)).tolist()
53
+
54
+
55
+ # work in progress
56
+ # container
57
+ anger = []
58
+ disgust = []
59
+ fear = []
60
+ joy = []
61
+ neutral = []
62
+ sadness = []
63
+ surprise = []
64
+
65
+ print(temp)
66
+ # extract scores (as many entries as exist in pred_texts)
67
+ for i in range(len(lines_s)):
68
+ anger.append(temp[i][0])
69
+ disgust.append(temp[i][1])
70
+ fear.append(temp[i][2])
71
+ joy.append(temp[i][3])
72
+ neutral.append(temp[i][4])
73
+ sadness.append(temp[i][5])
74
+ surprise.append(temp[i][6])
75
+
76
+ # define both vectors for the dot product
77
+ # each include all values for both predictions
78
+ v1 = temp[0]
79
+ v2 = temp[1]
80
+ print(type(v1), type(v2))
81
+ # compute dot product of all
82
+ dot_product = dot(v1, v2)
83
 
84
+ # define df
85
+ df = pd.DataFrame(list(zip(lines_s,labels, anger, disgust, fear, joy, neutral, sadness, surprise)), columns=['text','label', 'anger', 'disgust', 'fear', 'joy', 'neutral', 'sadness', 'surprise'])
86
 
87
+ # compute cosine similarity
88
+ # is dot product of vectors n / norms 1*..*n vectors
89
+ cosine_similarity = dot_product / (norm(v1) * norm(v2))
 
 
 
 
90
 
91
 
92
+ # return dataframe for space output
93
+ return df, cosine_similarity
94
+
95
+ gr.Interface(gr_cosine_similarity,
96
+ [
97
+ gr.inputs.Textbox(lines=1, placeholder="This movie always makes me cry..", default="", label="Text 1"),
98
+ gr.inputs.Textbox(lines=1, placeholder="Her dog is sad.", default="", label="Text 2"),
99
 
100
+ #gr.outputs.Textbox(type="auto", label="Cosine similarity"),
101
+ ],
102
+ ["dataframe","text"]
103
+ ).launch(debug=True)