|
|
|
"""Emotion Recognition_Fine Tuning |
|
|
|
Automatically generated by Colab. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/1pZgt5n6943GB5oq_h43LjAYoA4yi-EST |
|
""" |
|
|
|
|
|
"""Our Application""" |
|
|
|
from transformers import TFBertForSequenceClassification,AutoTokenizer |
|
|
|
import numpy as np |
|
|
|
import tensorflow as tf |
|
|
|
|
|
loaded_model = TFBertForSequenceClassification.from_pretrained("https://huggingface.co/spaces/dhruvsaxena11/Emotion_Recognition_in_Text/blob/main/tf_model.h5") |
|
loaded_tokenizer=AutoTokenizer.from_pretrained("google-bert/bert-base-uncased") |
|
|
|
def predict_emotion(text): |
|
|
|
text_token=loaded_tokenizer(text,padding=True,return_tensors="np") |
|
outputs=loaded_model(text_token) |
|
probabilities = tf.nn.softmax(outputs.logits) |
|
final=probabilities.numpy() |
|
labels=["sadness","joy","love","anger","fear","surprise"] |
|
final=final.tolist() |
|
result_dict = {k: v for k, v in zip(labels,final[0])} |
|
return result_dict |
|
|
|
predict_emotion("dhruv") |
|
|
|
my_labels=["sadness","joy","love","anger","fear","surprise"] |
|
|
|
|
|
import gradio as gr |
|
inputs = gr.Textbox(lines=1, label="Input Text") |
|
outputs = gr.Label(num_top_classes=6) |
|
interface = gr.Interface(fn=predict_emotion, inputs=inputs, outputs=outputs,title="Emotion Recognition in Text - NLP") |
|
interface.launch() |