anirudhmittal commited on
Commit
625657d
1 Parent(s): bf45a3e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import keras
3
+ import torch
4
+ import librosa
5
+ import numpy as np
6
+ import glob, os
7
+ from os import path
8
+ from pydub import AudioSegment
9
+ import pickle as pkl
10
+ from transformers import BertTokenizer, BertModel #RobertaTokenizer, RobertaModel, XLMRobertaTokenizer, TFXLMRobertaModel
11
+ import numpy as np
12
+ import numpy as np
13
+ import tensorflow as tf
14
+ import keras
15
+ import numpy as np
16
+ import torch
17
+ import os, glob
18
+ import pickle as pkl
19
+ from keras import layers
20
+ from tensorflow.keras.utils import to_categorical
21
+ from keras.models import Sequential
22
+ from keras.layers import Dense, Flatten, Dropout, Input, Bidirectional, LSTM, Activation, TimeDistributed, BatchNormalization
23
+ from sklearn.model_selection import train_test_split
24
+ import tensorflow.keras.backend as K
25
+ model = keras.models.load_model("/content/drive/Shareddrives/NLP_group/Project/Video data/Important Objects/Onlybert.h5")
26
+
27
+ tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
28
+
29
+ def text_features(input_text):
30
+ model_bert = BertModel.from_pretrained('bert-base-uncased', output_hidden_states = True,)
31
+ temp_text = input_text
32
+ lst_text = temp_text.strip().split(' ')
33
+ fin_lst_text = ["[CLS]"]
34
+ for word in lst_text:
35
+ fin_lst_text.append(word)
36
+ fin_text = " ".join(fin_lst_text)
37
+ # print(fin_text)
38
+ tokenized_text = tokenizer.tokenize(fin_text)
39
+ if len(tokenized_text) > 511:
40
+ tokenized_text = tokenized_text[:511]
41
+ tokenized_text.append('[SEP]')
42
+ indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
43
+ segments_ids = [1] * len(tokenized_text)
44
+ tokens_tensor = torch.tensor([indexed_tokens])
45
+ segments_tensors = torch.tensor([segments_ids])
46
+
47
+ model_bert = model_bert.eval()
48
+ with torch.no_grad():
49
+ outputs = model_bert(tokens_tensor, segments_tensors)
50
+ hidden_states = outputs[2]
51
+ token_embeddings = torch.stack(hidden_states, dim=0)
52
+ token_embeddings = torch.squeeze(token_embeddings, dim=1)
53
+ token_embeddings = token_embeddings.permute(1,0,2)
54
+ token_vecs_sum = []
55
+ for token in token_embeddings:
56
+ # `token` is a [12 x 768] tensor
57
+ # Sum the vectors from the last four layers.
58
+ sum_vec = torch.sum(token[-4:], dim=0)
59
+ sum_vec_np = sum_vec.numpy()
60
+ # Use `sum_vec` to represent `token`.
61
+ token_vecs_sum.append(sum_vec_np)
62
+ if len(token_vecs_sum) < 512:
63
+ for j in range(512-len(token_vecs_sum)):
64
+ padding_vec = np.zeros(768) #change to 1024 in large models
65
+ token_vecs_sum.append(padding_vec)
66
+ return token_vecs_sum
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+ def greet(name):
77
+
78
+ token_vecs_sums=[]
79
+ token_vecs_sums.append(text_features(name))
80
+ # sampleaud = K.constant(audioembeddingss)
81
+ samplebert = K.constant(token_vecs_sums)
82
+ samplescore = K.constant(0)
83
+ Z = model.predict([samplebert])
84
+ # print(Y)
85
+ # print(y_encoded_test)
86
+ Zclass = np.argmax(Z, axis=-1)
87
+ Zclasstrue = np.argmax(samplescore, axis=-1)
88
+ if Zclass == 0:
89
+ out= "Not funny"
90
+ elif Zclass == 1:
91
+ out= "Slightly funny only"
92
+ elif Zclass == 2:
93
+ out= "Midly funny :)"
94
+ elif Zclass == 3:
95
+ out= "Hahaha. Very funny."
96
+ elif Zclass == 4:
97
+ out= "Absolutely funny. LMAO."
98
+
99
+
100
+ return out
101
+
102
+
103
+ # print(greet("Does anybody else secretly hope that Kanye pulls, like a… Mrs. Doubtfire? They come home one day, and they’re like, “This is the new housekeeper,” and he’s like, “What’s up, fam?” Yeah, it’s a really weird thing to go through. There’s because you know, people try to give you advice, but even friends that are older, they’re like, “I don’t… know.” He’s like, “It looks pretty bad, bro, I hope… Good luck, sorry, here if you need,” but like, no advice. No one was like, “This is what you do.” Everyone was like, “I don’t know… yeah. You staying with your mom? You in a safe spot?” People ask me weird questions. So that’s the only thing I don’t like. They ask this weird stuff. Like the other day, somebody came up to me and was like, “I heard you’re friends with Jack Harlow!” And I am. He’s a very great, talented rapper. He’s a cool dude. Nice guy. So we’re pals, right? And Kanye put him on his new album. Even though I’ve been friends with him for like two, three years, you know, he did it anyway. People come up to me and they’re like, “How does that make you feel? Does that bother you? Does that get under your skin?” And I’m like, “No, he’s a rapper.” That’s his field, that’s what they do. That doesn’t hurt my feelings. It would hurt my feelings if I saw, like, Bill Burr at Sunday service. I’d be like, “What the fuck, Bill?” He’d be like, “Find God, Petey, go fuck yourself! Jesus!” Yeah, I don’t get it. A lot of people are very angry. It’s always 5050 when I go outside. Yeah, it is. It’s always 5050. Either someone’s just like, “Hey, man, you’re really cool, that’s great.” Or someone’s like, “Hey, yo! Fuck you! Fuck you! Yeah, you!” I always am like, “Can’t be me.”"))
104
+ demo = gr.Interface(fn=greet, inputs=gr.Textbox(lines=1, placeholder="Enter Root Word Here"), outputs="text")
105
+ demo.launch()