Spaces:
Running
Running
import gradio as gr | |
import os | |
from datetime import date | |
import json | |
import csv | |
import datetime | |
import smtplib | |
from email.mime.text import MIMEText | |
import requests | |
from transformers import AutoTokenizer, AutoModelWithLMHead | |
import gc | |
cwd = os.getcwd() | |
model_path = os.path.join(cwd) | |
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-emotion") | |
model_base = AutoModelWithLMHead.from_pretrained(model_path) | |
def get_emotion(text): | |
# input_ids = tokenizer.encode(text + '</s>', return_tensors='pt') | |
input_ids = tokenizer.encode(text, return_tensors='pt') | |
output = model_base.generate(input_ids=input_ids, | |
max_length=2) | |
dec = [tokenizer.decode(ids) for ids in output] | |
label = dec[0] | |
gc.collect() | |
return label | |
def generate_emotion(article): | |
sen_list = article | |
sen_list = sen_list.split('\r\n') | |
sen_list_temp = sen_list[0:] | |
results_dict = [] | |
results = [] | |
for sen in sen_list_temp: | |
if(sen.strip()): | |
log_sen_list.append(sen) | |
cur_result = get_emotion(sen) | |
results.append(cur_result) | |
results_dict.append( | |
{ | |
'sentence': sen, | |
'emotion': cur_result | |
} | |
) | |
result = { | |
'result': results_dict, | |
} | |
gc.collect() | |
print("LENGTH of results ====> ", results) | |
return result | |
inputs=gr.Textbox(lines=10, label="Sentences",elem_id="inp_div") | |
outputs=gr.Textbox(lines=10, label="Here is the Result",elem_id="inp_div") | |
demo = gr.Interface( | |
generate_emotion, | |
inputs, | |
outputs, | |
title="Emotion Detection", | |
description="Feel free to give your feedback", | |
css=".gradio-container {background-color: lightgray} #inp_div {background-color: [#7](https://www1.example.com/issues/7)FB3D5;" | |
) | |
demo.launch() |