mobrown commited on
Commit
7e815f0
·
verified ·
1 Parent(s): 2dae8e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -47
app.py CHANGED
@@ -12,7 +12,6 @@ from transformers.utils import logging
12
 
13
  logging.set_verbosity("ERROR")
14
 
15
-
16
  # Load the provided dataset
17
  file_path = 'data.csv'
18
  df = pd.read_csv(file_path)
@@ -34,7 +33,6 @@ rf_model.fit(X_train, y_train)
34
  sentences = [
35
  "The announced restructuring will erase the company's indebtedness.",
36
  "UPM-Kymmene upgraded to `in-line' from `underperform' by Goldman Sachs.",
37
- # "$AAPL shares are breaking above the recent resistance level.",
38
  "Profitability (in EBIT %) was not impressive due to expenses rising by 14.3%.",
39
  "The Finnish bank has issued a profit warning.",
40
  "TeliaSonera's underlying results however included 457 mln SKr in positive one-offs, hence the adjusted underlying EBITDA actually amounts to 7.309 bln SKr, clearly below expectations, analysts said."
@@ -49,19 +47,13 @@ def map_bert_label(label):
49
  elif label in ["4 stars", "5 stars"]:
50
  return "positive"
51
 
52
- # Function to map RoBERTa labels
53
- def map_roberta_label(label):
54
- label_mapping = {"LABEL_0": "negative", "LABEL_1": "neutral", "LABEL_2": "positive"}
55
- return label_mapping[label]
56
-
57
  # Function to analyze sentiment
58
  def analyze_sentiment(sentence):
59
  # Define model paths
60
  model_paths = {
61
  "BERT": "nlptown/bert-base-multilingual-uncased-sentiment",
62
- # "RoBERTa": "cardiffnlp/twitter-roberta-base-sentiment"
63
  }
64
-
65
  # Analyze sentiment using transformers models
66
  results = {}
67
  for model_name, model_path in model_paths.items():
@@ -69,50 +61,33 @@ def analyze_sentiment(sentence):
69
  result = sentiment_analyzer(sentence[:512])[0] # Analyze first 512 characters for brevity
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
  results[model_name] = result
75
-
76
  # Analyze sentiment using sklearn models
77
  results["Naive Bayes"] = {"label": nb_model.predict([sentence])[0],
78
  "score": nb_model.predict_proba([sentence]).max()}
79
- results["SVM"] = {"label": svm_model.predict([sentence])[0],
80
  "score": svm_model.predict_proba([sentence]).max()}
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
- # Create Gradio interface
87
- dropdown = gr.Dropdown(choices=sentences, label="Select Sentence")
88
- text_output = gr.Textbox(label="Selected Sentence", lines=2)
89
- sentiment_output = gr.JSON(label="Sentiment Scores")
90
-
91
-
92
- # Custom CSS to reduce font size
93
- custom_css = """
94
- body, .gradio-container {
95
- font-size: 8px !important;
96
- }
97
- .output-markdown, .input-markdown {
98
- font-size: 8px !important;
99
- }
100
- """
101
-
102
-
103
- gr.Interface(
104
- fn=analyze_sentiment,
105
- inputs=[dropdown],
106
- outputs=[text_output, sentiment_output],
107
- title="Compare Sentiment Analysis Across Models",
108
- description="Select a sentence to see sentiment analysis results from multiple models.",
109
- css=custom_css # Add this line to apply the custom CSS
110
- ).launch()
111
-
112
- # gr.Interface(
113
- # fn=analyze_sentiment,
114
- # inputs=[dropdown],
115
- # outputs=[text_output, sentiment_output],
116
- # title="Compare Sentiment Analysis Across Models",
117
- # description="Select a sentence to see sentiment analysis results from multiple models."
118
- # ).launch()
 
12
 
13
  logging.set_verbosity("ERROR")
14
 
 
15
  # Load the provided dataset
16
  file_path = 'data.csv'
17
  df = pd.read_csv(file_path)
 
33
  sentences = [
34
  "The announced restructuring will erase the company's indebtedness.",
35
  "UPM-Kymmene upgraded to `in-line' from `underperform' by Goldman Sachs.",
 
36
  "Profitability (in EBIT %) was not impressive due to expenses rising by 14.3%.",
37
  "The Finnish bank has issued a profit warning.",
38
  "TeliaSonera's underlying results however included 457 mln SKr in positive one-offs, hence the adjusted underlying EBITDA actually amounts to 7.309 bln SKr, clearly below expectations, analysts said."
 
47
  elif label in ["4 stars", "5 stars"]:
48
  return "positive"
49
 
 
 
 
 
 
50
  # Function to analyze sentiment
51
  def analyze_sentiment(sentence):
52
  # Define model paths
53
  model_paths = {
54
  "BERT": "nlptown/bert-base-multilingual-uncased-sentiment",
 
55
  }
56
+
57
  # Analyze sentiment using transformers models
58
  results = {}
59
  for model_name, model_path in model_paths.items():
 
61
  result = sentiment_analyzer(sentence[:512])[0] # Analyze first 512 characters for brevity
62
  if model_name == "BERT":
63
  result['label'] = map_bert_label(result['label'])
 
 
64
  results[model_name] = result
65
+
66
  # Analyze sentiment using sklearn models
67
  results["Naive Bayes"] = {"label": nb_model.predict([sentence])[0],
68
  "score": nb_model.predict_proba([sentence]).max()}
69
+ results["SVM"] = {"label": svm_model.predict([sentence])[0],
70
  "score": svm_model.predict_proba([sentence]).max()}
71
  results["Random Forest"] = {"label": rf_model.predict([sentence])[0],
72
  "score": rf_model.predict_proba([sentence]).max()}
73
+
74
  return sentence, results
75
 
76
+ # Create a custom theme with smaller font size
77
+ custom_theme = gr.themes.Default(
78
+ text_size=gr.themes.sizes.text_sm, # Set smaller text size
79
+ font=[gr.themes.GoogleFont("Source Sans Pro"), "Arial", "sans-serif"] # Optional: change font
80
+ )
81
+
82
+ # Create Gradio interface with the custom theme
83
+ with gr.Blocks(theme=custom_theme) as demo:
84
+ gr.Markdown("# Compare Sentiment Analysis Across Models")
85
+ gr.Markdown("Select a sentence to see sentiment analysis results from multiple models.")
86
+
87
+ dropdown = gr.Dropdown(choices=sentences, label="Select Sentence")
88
+ text_output = gr.Textbox(label="Selected Sentence", lines=2)
89
+ sentiment_output = gr.JSON(label="Sentiment Scores")
90
+
91
+ dropdown.change(analyze_sentiment, inputs=[dropdown], outputs=[text_output, sentiment_output])
92
+
93
+ demo.launch()