Spaces:
Running
Running
pragnakalp
commited on
Commit
•
3c82b91
1
Parent(s):
e8cab2c
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,61 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
|
|
|
|
|
|
|
|
|
|
3 |
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
return result
|
8 |
|
9 |
-
inputs=gr.Textbox(lines=
|
10 |
-
outputs=gr.Textbox(lines=
|
11 |
|
12 |
demo = gr.Interface(
|
13 |
generate_emotion,
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
from datetime import date
|
4 |
+
import json
|
5 |
+
import csv
|
6 |
+
import datetime
|
7 |
+
import smtplib
|
8 |
+
from email.mime.text import MIMEText
|
9 |
+
import requests
|
10 |
+
from transformers import AutoTokenizer, AutoModelWithLMHead
|
11 |
+
import gc
|
12 |
|
13 |
+
cwd = os.getcwd()
|
14 |
+
model_path = os.path.join(cwd)
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-emotion")
|
16 |
+
model_base = AutoModelWithLMHead.from_pretrained(model_path)
|
17 |
+
def get_emotion(text):
|
18 |
|
19 |
+
# input_ids = tokenizer.encode(text + '</s>', return_tensors='pt')
|
20 |
+
input_ids = tokenizer.encode(text, return_tensors='pt')
|
21 |
+
output = model_base.generate(input_ids=input_ids,
|
22 |
+
max_length=2)
|
23 |
|
24 |
+
dec = [tokenizer.decode(ids) for ids in output]
|
25 |
+
label = dec[0]
|
26 |
+
gc.collect()
|
27 |
+
return label
|
28 |
+
|
29 |
+
def generate_emotion(article):
|
30 |
+
sen_list = article
|
31 |
+
sen_list = sen_list.split('\r\n')
|
32 |
+
sen_list_temp = sen_list[0:]
|
33 |
+
results_dict = []
|
34 |
+
results = []
|
35 |
+
|
36 |
+
for sen in sen_list_temp:
|
37 |
+
if(sen.strip()):
|
38 |
+
log_sen_list.append(sen)
|
39 |
+
cur_result = get_emotion(sen)
|
40 |
+
|
41 |
+
results.append(cur_result)
|
42 |
+
results_dict.append(
|
43 |
+
{
|
44 |
+
'sentence': sen,
|
45 |
+
'emotion': cur_result
|
46 |
+
}
|
47 |
+
)
|
48 |
+
|
49 |
+
result = {
|
50 |
+
'result': results_dict,
|
51 |
+
}
|
52 |
+
gc.collect()
|
53 |
+
print("LENGTH of results ====> ", results)
|
54 |
+
|
55 |
return result
|
56 |
|
57 |
+
inputs=gr.Textbox(lines=10, label="Sentences",elem_id="inp_div")
|
58 |
+
outputs=gr.Textbox(lines=10, label="Here is the Result",elem_id="inp_div")
|
59 |
|
60 |
demo = gr.Interface(
|
61 |
generate_emotion,
|