anirudhmittal's picture
Update app.py
79def53
import gradio as gr
import tensorflow
import keras
import torch
import numpy as np
import glob, os
from os import path
import pickle as pkl
from transformers import BertTokenizer, BertModel #RobertaTokenizer, RobertaModel, XLMRobertaTokenizer, TFXLMRobertaModel
from keras import layers
from tensorflow.keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Flatten, Dropout, Input, Bidirectional, LSTM, Activation, TimeDistributed, BatchNormalization
from sklearn.model_selection import train_test_split
import tensorflow.keras.backend as K
model = keras.models.load_model("Onlybert.h5")
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
def text_features(input_text):
model_bert = BertModel.from_pretrained('bert-base-uncased', output_hidden_states = True,)
temp_text = input_text
lst_text = temp_text.strip().split(' ')
fin_lst_text = ["[CLS]"]
for word in lst_text:
fin_lst_text.append(word)
fin_text = " ".join(fin_lst_text)
# print(fin_text)
tokenized_text = tokenizer.tokenize(fin_text)
if len(tokenized_text) > 511:
tokenized_text = tokenized_text[:511]
tokenized_text.append('[SEP]')
indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
segments_ids = [1] * len(tokenized_text)
tokens_tensor = torch.tensor([indexed_tokens])
segments_tensors = torch.tensor([segments_ids])
model_bert = model_bert.eval()
with torch.no_grad():
outputs = model_bert(tokens_tensor, segments_tensors)
hidden_states = outputs[2]
token_embeddings = torch.stack(hidden_states, dim=0)
token_embeddings = torch.squeeze(token_embeddings, dim=1)
token_embeddings = token_embeddings.permute(1,0,2)
token_vecs_sum = []
for token in token_embeddings:
# `token` is a [12 x 768] tensor
# Sum the vectors from the last four layers.
sum_vec = torch.sum(token[-4:], dim=0)
sum_vec_np = sum_vec.numpy()
# Use `sum_vec` to represent `token`.
token_vecs_sum.append(sum_vec_np)
if len(token_vecs_sum) < 512:
for j in range(512-len(token_vecs_sum)):
padding_vec = np.zeros(768) #change to 1024 in large models
token_vecs_sum.append(padding_vec)
return token_vecs_sum
def greet(name):
token_vecs_sums=[]
token_vecs_sums.append(text_features(name))
# sampleaud = K.constant(audioembeddingss)
samplebert = K.constant(token_vecs_sums)
samplescore = K.constant(0)
Z = model.predict([samplebert])
# print(Y)
# print(y_encoded_test)
Zclass = np.argmax(Z, axis=-1)
Zclasstrue = np.argmax(samplescore, axis=-1)
if Zclass == 0:
out= "Not funny (0)"
elif Zclass == 1:
out= "Slightly funny only (1)"
elif Zclass == 2:
out= "Midly funny :) (2)"
elif Zclass == 3:
out= "Hahaha. Very funny. (3)"
elif Zclass == 4:
out= "Absolutely funny. LMAO. (4)"
return out
examples=[["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."]]
# 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.”"))
demo = gr.Interface(fn=greet, inputs=gr.Textbox(lines=1, placeholder="Enter text here"),title="Automatic funniness checker (0 to 4)", description="This model helps you check if you're funny. Add text here and see how funny you're!", outputs="text")
demo.launch()