File size: 3,063 Bytes
fd31e5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389f817
 
fd31e5f
 
389f817
 
 
 
 
 
28e566d
fd31e5f
389f817
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c20f8d9
389f817
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# from transformers import AutoTokenizer, AutoModelForSequenceClassification

# download = False
# save_model_locally= False
# if download:
#     tokenizer = AutoTokenizer.from_pretrained("MilaNLProc/feel-it-italian-sentiment", cache_dir="data/")
#     model_sent = AutoModelForSequenceClassification.from_pretrained("MilaNLProc/feel-it-italian-sentiment", cache_dir="data/")
#     model_sent.eval()
#     tokenizer_emo = AutoTokenizer.from_pretrained("MilaNLProc/feel-it-italian-emotion", cache_dir="data/")
#     model_emo = AutoModelForSequenceClassification.from_pretrained("MilaNLProc/feel-it-italian-emotion", cache_dir="data/")
#     model_emo.eval()
#     if save_model_locally:
#         model_sent.save_pretrained('./local_models/sentiment_ITA')
#         tokenizer.save_pretrained('./local_models/sentiment_ITA')
#         model_emo.save_pretrained('./local_models/emotion_ITA')
#         tokenizer_emo.save_pretrained('./local_models/emotion_ITA')
# else:
#     tokenizer = AutoTokenizer.from_pretrained("./local_models/sentiment_ITA/")
#     model_sent = AutoModelForSequenceClassification.from_pretrained("./local_models/sentiment_ITA/", num_labels=2)
#     model_sent.eval()

#     tokenizer_emo = AutoTokenizer.from_pretrained("./local_models/emotion_ITA/")
#     model_emo = AutoModelForSequenceClassification.from_pretrained("./local_models/emotion_ITA/", num_labels=4)
#     model_emo.eval()


# #%%generator_sent

from transformers import pipeline
generator_sent = pipeline(task="text-classification", model='./local_models/sentiment_ITA/', top_k=None)
generator_emo = pipeline(task="text-classification", model='./local_models/emotion_ITA/', top_k=None)

def sentiment_emoji(input_abs):

    if(input_abs ==""):
        return "πŸ€·β€β™‚οΈ"
        
    res = generator_sent(input_abs)[0]
    print("res: ", res)
    res = {res[x]["label"]: res[x]["score"] for x in range(len(res))}
    res["πŸ™‚ positive"] = res.pop("positive")
    res["πŸ™ negative"] = res.pop("negative")
    return res


def emotion_emoji(input_abs):
    if(input_abs ==""):
        return "πŸ€·β€β™‚οΈ"

    res = generator_emo(input_abs)[0]
    res = {res[x]["label"]: res[x]["score"] for x in range(len(res))}
    res["πŸ˜ƒ joy"] = res.pop("joy")
    res["😑 anger"] = res.pop("anger")
    res["😨 fear"] = res.pop("fear")
    res["😟 sadness"] = res.pop("sadness")
   
    return res
#%%

import gradio as gr
demo = gr.Blocks()
with demo:
   gr.Markdown("# Analisi sentimento/emozioni del testo italiano")
   with gr.Row():
      with gr.Column():
         text_input = gr.Textbox(placeholder="Scrivi qui")
         button_1 = gr.Button("Invia")
      with gr.Column():
         label_sem = gr.Label()
         label_emo = gr.Label()
      #   gr.Interface(fn=emotion_emoji, inputs=text_input, outputs="label")
   button_1.click(sentiment_emoji, inputs=text_input, outputs=label_sem, api_name="sentiment")
   button_1.click(emotion_emoji, inputs=text_input, outputs=label_emo, api_name="emotion")
            

demo.launch()
print("Running is terminated")