ShadowDominator commited on
Commit
4ac7334
1 Parent(s): 5aa21e6

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +85 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
2
+ import torch
3
+ import gradio as gr
4
+
5
+
6
+ tokenizer_sentence_analysis = AutoTokenizer.from_pretrained("finiteautomata/bertweet-base-sentiment-analysis")
7
+ model_sentence_analysis = AutoModelForSequenceClassification.from_pretrained("finiteautomata/bertweet-base-sentiment-analysis")
8
+
9
+ tokenizer_review_feedback_sentiment = AutoTokenizer.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')
10
+ model_review_feedback_sentiment = AutoModelForSequenceClassification.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')
11
+
12
+ classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
13
+
14
+ def sentence_sentiment_model(text, tokenizer, model):
15
+ inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
16
+ with torch.no_grad():
17
+ result = model(inputs['input_ids'], attention_mask=inputs['attention_mask'])
18
+ logits = result.logits.detach()
19
+ probs = torch.softmax(logits, dim=1)
20
+ pos_prob = probs[0][2].item()
21
+ neu_prob = probs[0][1].item()
22
+ neg_prob = probs[0][0].item()
23
+ return {'Positive': [round(float(pos_prob), 2)],"Neutural":[round(float(neu_prob), 2)], 'Negative': [round(float(neg_prob), 2)]}
24
+
25
+ def review_feedback_sentiment(text, tokenizer, model):
26
+ inputs = tokenizer.encode_plus(text, padding='max_length', max_length=512, return_tensors="pt")
27
+ with torch.no_grad():
28
+ result = model(inputs['input_ids'], attention_mask=inputs['attention_mask'])
29
+ logits = result.logits.detach()
30
+ probs = torch.softmax(logits, dim=1).detach().numpy()[0]
31
+ categories = ['Terrible', 'Poor', 'Average', 'Good', 'Excellent']
32
+ output_dict = {}
33
+ for i in range(len(categories)):
34
+ output_dict[categories[i]] = [round(float(probs[i]), 2)]
35
+ return output_dict
36
+
37
+
38
+
39
+ def emotion_sentiment(text):
40
+ results = classifier(text, padding='max_length', max_length=512)
41
+ return {label['label']: [label['score']] for label in results[0]}
42
+
43
+
44
+
45
+ def sentence_analysis(text):
46
+ result = sentence_sentiment_model(text,tokenizer_sentence_analysis,model_sentence_analysis)
47
+ return result
48
+ def emotion(text):
49
+ result = emotion_sentiment(text)
50
+ return result
51
+ def review_feed_back(text):
52
+ result = review_feedback_sentiment(text,tokenizer_review_feedback_sentiment,model_review_feedback_sentiment)
53
+ return result
54
+
55
+ def selection_model(model,text):
56
+ if text == "":
57
+ return "No Text Input"
58
+ if model=="Emotion Analysis":
59
+ return emotion(text)
60
+ if model == "Review Feedback Analysis":
61
+ return review_feed_back(text)
62
+ if model == "Sentence Analysis":
63
+ return sentence_analysis(text)
64
+ return "Please select model"
65
+
66
+
67
+ paragraph = """
68
+ I woke up this morning feeling refreshed and excited for the day ahead.
69
+ """
70
+
71
+ with gr.Blocks(title="Sentiment",css="footer {visibility: hidden}") as demo:
72
+ with gr.Row():
73
+ with gr.Column():
74
+ gr.Markdown("## Emotion, ReviewFeedback, Sentence Analysis")
75
+ with gr.Row():
76
+ with gr.Column():
77
+ drop_down_name = gr.Dropdown(choices=["Emotion Analysis", "Review Feedback Analysis", "Sentence Analysis"],label="Model")
78
+ inputs = gr.TextArea(label="sentence",value=paragraph,interactive=True)
79
+ btn = gr.Button(value="RUN")
80
+ with gr.Column():
81
+ output = gr.Label(label="output")
82
+ btn.click(fn=selection_model,inputs=[drop_down_name,inputs],outputs=[output])
83
+ demo.launch()
84
+
85
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio==3.32.0
2
+ torch==2.0.0
3
+ transformers==4.28.1