Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,41 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
|
|
|
|
3 |
|
4 |
-
# Load
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
"LABEL_0": "Very Negative",
|
10 |
-
"LABEL_1": "Negative",
|
11 |
-
"LABEL_2": "Neutral",
|
12 |
-
"LABEL_3": "Positive",
|
13 |
-
"LABEL_4": "Very Positive"
|
14 |
-
}
|
15 |
|
16 |
def advanced_sentiment_analysis(text):
|
17 |
-
#
|
18 |
-
|
19 |
|
20 |
-
#
|
21 |
-
|
|
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
for entry in result:
|
26 |
-
label = label_map.get(entry['label'], entry['label'])
|
27 |
-
percentage = (entry['score'] / total_score) * 100
|
28 |
-
formatted_output += f"{label}: {percentage:.2f}%\n"
|
29 |
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# Gradio UI
|
33 |
with gr.Blocks() as demo:
|
34 |
-
gr.Markdown("### Welcome, please enter a sample of what you may respond or tell a customer,let's tell you how cool it is")
|
35 |
-
|
36 |
-
text_input = gr.Textbox(lines=4, placeholder="Type your message here...", label="Customer Message")
|
37 |
output = gr.Textbox(label="Sentiment Analysis Result")
|
38 |
analyze_button = gr.Button("Analyze Sentiment")
|
39 |
-
|
40 |
analyze_button.click(fn=advanced_sentiment_analysis, inputs=text_input, outputs=output)
|
41 |
|
42 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import torch
|
4 |
+
import torch.nn.functional as F
|
5 |
|
6 |
+
# Load model and tokenizer
|
7 |
+
model_name = "cardiffnlp/twitter-roberta-base-sentiment"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
10 |
|
11 |
+
# Label map for 3-level sentiment
|
12 |
+
labels = ['Negative', 'Neutral', 'Positive']
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def advanced_sentiment_analysis(text):
|
15 |
+
# Tokenize input
|
16 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True)
|
17 |
|
18 |
+
# Get model logits
|
19 |
+
with torch.no_grad():
|
20 |
+
logits = model(**inputs).logits
|
21 |
|
22 |
+
# Convert logits to probabilities
|
23 |
+
probs = F.softmax(logits, dim=1)[0]
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# Format result
|
26 |
+
results = ""
|
27 |
+
for i, prob in enumerate(probs):
|
28 |
+
results += f"{labels[i]}: {prob.item() * 100:.2f}%\n"
|
29 |
+
|
30 |
+
return results.strip()
|
31 |
|
32 |
# Gradio UI
|
33 |
with gr.Blocks() as demo:
|
34 |
+
gr.Markdown("### Welcome, please enter a sample of what you may respond or tell a customer, let's tell you how cool it is")
|
35 |
+
text_input = gr.Textbox(lines=4, placeholder="Type your message here...", label="Customer Message")
|
|
|
36 |
output = gr.Textbox(label="Sentiment Analysis Result")
|
37 |
analyze_button = gr.Button("Analyze Sentiment")
|
38 |
+
|
39 |
analyze_button.click(fn=advanced_sentiment_analysis, inputs=text_input, outputs=output)
|
40 |
|
41 |
demo.launch()
|