Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -37,7 +37,7 @@ sentences = [
|
|
37 |
]
|
38 |
|
39 |
# Function to map BERT labels
|
40 |
-
|
41 |
if label in ["1 star", "2 stars"]:
|
42 |
return "negative"
|
43 |
elif label == "3 stars":
|
@@ -45,18 +45,15 @@ sentences = [
|
|
45 |
elif label in ["4 stars", "5 stars"]:
|
46 |
return "positive"
|
47 |
|
48 |
-
|
49 |
# Function to map RoBERTa labels
|
50 |
def map_roberta_label(label):
|
51 |
label_mapping = {"LABEL_0": "negative", "LABEL_1": "neutral", "LABEL_2": "positive"}
|
52 |
return label_mapping[label]
|
53 |
|
54 |
-
|
55 |
# Function to analyze sentiment
|
56 |
def analyze_sentiment(sentence):
|
57 |
# Define model paths
|
58 |
model_paths = {
|
59 |
-
# "FinBert": "ProsusAI/finbert",
|
60 |
"BERT": "nlptown/bert-base-multilingual-uncased-sentiment",
|
61 |
"RoBERTa": "cardiffnlp/twitter-roberta-base-sentiment"
|
62 |
}
|
@@ -66,24 +63,22 @@ def analyze_sentiment(sentence):
|
|
66 |
for model_name, model_path in model_paths.items():
|
67 |
sentiment_analyzer = pipeline("sentiment-analysis", model=model_path)
|
68 |
result = sentiment_analyzer(sentence[:512])[0] # Analyze first 512 characters for brevity
|
69 |
-
|
70 |
if model_name == "BERT":
|
71 |
result['label'] = map_bert_label(result['label'])
|
72 |
elif model_name == "RoBERTa":
|
73 |
result['label'] = map_roberta_label(result['label'])
|
74 |
-
|
75 |
results[model_name] = result
|
76 |
|
77 |
# Analyze sentiment using sklearn models
|
78 |
results["Naive Bayes"] = {"label": nb_model.predict([sentence])[0],
|
79 |
"score": nb_model.predict_proba([sentence]).max()}
|
80 |
-
results["SVM"] = {"label": svm_model.predict([sentence])[0],
|
|
|
81 |
results["Random Forest"] = {"label": rf_model.predict([sentence])[0],
|
82 |
"score": rf_model.predict_proba([sentence]).max()}
|
83 |
|
84 |
return sentence, results
|
85 |
|
86 |
-
|
87 |
# Create Gradio interface
|
88 |
dropdown = gr.Dropdown(choices=sentences, label="Select Sentence")
|
89 |
text_output = gr.Textbox(label="Selected Sentence", lines=2)
|
@@ -95,4 +90,4 @@ gr.Interface(
|
|
95 |
outputs=[text_output, sentiment_output],
|
96 |
title="Compare Sentiment Analysis Across Models",
|
97 |
description="Select a sentence to see sentiment analysis results from multiple models."
|
98 |
-
).launch(share=True)
|
|
|
37 |
]
|
38 |
|
39 |
# Function to map BERT labels
|
40 |
+
def map_bert_label(label):
|
41 |
if label in ["1 star", "2 stars"]:
|
42 |
return "negative"
|
43 |
elif label == "3 stars":
|
|
|
45 |
elif label in ["4 stars", "5 stars"]:
|
46 |
return "positive"
|
47 |
|
|
|
48 |
# Function to map RoBERTa labels
|
49 |
def map_roberta_label(label):
|
50 |
label_mapping = {"LABEL_0": "negative", "LABEL_1": "neutral", "LABEL_2": "positive"}
|
51 |
return label_mapping[label]
|
52 |
|
|
|
53 |
# Function to analyze sentiment
|
54 |
def analyze_sentiment(sentence):
|
55 |
# Define model paths
|
56 |
model_paths = {
|
|
|
57 |
"BERT": "nlptown/bert-base-multilingual-uncased-sentiment",
|
58 |
"RoBERTa": "cardiffnlp/twitter-roberta-base-sentiment"
|
59 |
}
|
|
|
63 |
for model_name, model_path in model_paths.items():
|
64 |
sentiment_analyzer = pipeline("sentiment-analysis", model=model_path)
|
65 |
result = sentiment_analyzer(sentence[:512])[0] # Analyze first 512 characters for brevity
|
|
|
66 |
if model_name == "BERT":
|
67 |
result['label'] = map_bert_label(result['label'])
|
68 |
elif model_name == "RoBERTa":
|
69 |
result['label'] = map_roberta_label(result['label'])
|
|
|
70 |
results[model_name] = result
|
71 |
|
72 |
# Analyze sentiment using sklearn models
|
73 |
results["Naive Bayes"] = {"label": nb_model.predict([sentence])[0],
|
74 |
"score": nb_model.predict_proba([sentence]).max()}
|
75 |
+
results["SVM"] = {"label": svm_model.predict([sentence])[0],
|
76 |
+
"score": svm_model.predict_proba([sentence]).max()}
|
77 |
results["Random Forest"] = {"label": rf_model.predict([sentence])[0],
|
78 |
"score": rf_model.predict_proba([sentence]).max()}
|
79 |
|
80 |
return sentence, results
|
81 |
|
|
|
82 |
# Create Gradio interface
|
83 |
dropdown = gr.Dropdown(choices=sentences, label="Select Sentence")
|
84 |
text_output = gr.Textbox(label="Selected Sentence", lines=2)
|
|
|
90 |
outputs=[text_output, sentiment_output],
|
91 |
title="Compare Sentiment Analysis Across Models",
|
92 |
description="Select a sentence to see sentiment analysis results from multiple models."
|
93 |
+
).launch(share=True)
|