Update app.py
Browse files
app.py
CHANGED
@@ -3,14 +3,9 @@ from transformers import pipeline
|
|
3 |
import os
|
4 |
|
5 |
# --- Model Loading ---
|
6 |
-
# Using the model ID you've been working with, which is fine-tuned on an English dataset (IMDB).
|
7 |
-
# It's crucial that this model is indeed fine-tuned for sentiment analysis in English.
|
8 |
MODEL_ID = "Light-Dav/sentiment-analysis-full-project"
|
9 |
|
10 |
try:
|
11 |
-
# Attempt to load the pipeline. This needs to be outside the function for efficiency.
|
12 |
-
# Using top_k=None to get all scores (equivalent to return_all_scores=True in older versions).
|
13 |
-
# This also addresses the UserWarning about `return_all_scores` deprecation.
|
14 |
sentiment_analyzer = pipeline("sentiment-analysis", model=MODEL_ID, top_k=None)
|
15 |
model_loaded_successfully = True
|
16 |
except Exception as e:
|
@@ -123,23 +118,16 @@ def analyze_sentiment(text):
|
|
123 |
}
|
124 |
|
125 |
try:
|
126 |
-
|
127 |
-
# e.g., [[{'label': 'LABEL_0', 'score': 0.9}, {'label': 'LABEL_1', 'score': 0.05}, ...]]
|
128 |
-
results = sentiment_analyzer(text)[0] # Get the first (and only) list of results
|
129 |
|
130 |
-
# Sort results by score in descending order
|
131 |
results_sorted = sorted(results, key=lambda x: x['score'], reverse=True)
|
132 |
|
133 |
-
# Get the top sentiment
|
134 |
top_sentiment = results_sorted[0]
|
135 |
label = top_sentiment['label']
|
136 |
score = top_sentiment['score']
|
137 |
|
138 |
-
# Format for Gradio Label component (Dictionary {label: score})
|
139 |
-
# This is for the 'Confidence Scores' output
|
140 |
confidence_scores_output = {item['label']: item['score'] for item in results}
|
141 |
|
142 |
-
# Interpret sentiment for the 'Overall Sentiment' output
|
143 |
overall_sentiment_display = interpret_sentiment(label, score)
|
144 |
|
145 |
return {
|
@@ -155,12 +143,12 @@ def analyze_sentiment(text):
|
|
155 |
}
|
156 |
|
157 |
# --- Gradio Interface ---
|
158 |
-
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
159 |
gr.Markdown("# β¨ Sentiment Spark β¨")
|
160 |
gr.Markdown("---")
|
161 |
gr.Markdown("### Uncover the emotional tone of your English text instantly!")
|
162 |
|
163 |
-
with gr.Row():
|
164 |
with gr.Column(scale=2):
|
165 |
text_input = gr.Textbox(
|
166 |
lines=7,
|
@@ -169,7 +157,15 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo: # Using gr.Block
|
|
169 |
interactive=True,
|
170 |
value="This movie was absolutely brilliant! A masterpiece of storytelling and emotion."
|
171 |
)
|
172 |
-
analyze_btn = gr.Button("Analyze Sentiment", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
gr.Markdown("---")
|
175 |
gr.Markdown("### Try some examples:")
|
@@ -183,29 +179,19 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo: # Using gr.Block
|
|
183 |
["The meeting concluded without any major decisions."]
|
184 |
],
|
185 |
inputs=text_input,
|
186 |
-
fn=analyze_sentiment, #
|
187 |
-
outputs=[overall_sentiment_output, confidence_scores_output, raw_output], #
|
188 |
cache_examples=True # Caching examples for faster loading
|
189 |
)
|
190 |
|
191 |
-
with gr.Column(scale=3):
|
192 |
-
gr.Markdown("## π Analysis Results π")
|
193 |
-
# Using gr.HTML for "Overall Sentiment" to allow custom CSS and rich content
|
194 |
-
overall_sentiment_output = gr.HTML(label="Overall Sentiment")
|
195 |
-
# Using gr.Label for "Confidence Scores" as it's designed for classification outputs
|
196 |
-
confidence_scores_output = gr.Label(num_top_classes=3, label="Confidence Scores")
|
197 |
-
# Using gr.JSON for raw output, useful for debugging, but hidden by default
|
198 |
-
raw_output = gr.JSON(label="Raw Model Output", visible=False)
|
199 |
|
200 |
# --- Event Listeners ---
|
201 |
-
# Trigger analysis when text input changes (optional, can be resource intensive if live=True)
|
202 |
text_input.change(
|
203 |
fn=analyze_sentiment,
|
204 |
inputs=text_input,
|
205 |
outputs=[overall_sentiment_output, confidence_scores_output, raw_output],
|
206 |
-
# live=True
|
207 |
)
|
208 |
-
# Trigger analysis when the button is clicked
|
209 |
analyze_btn.click(
|
210 |
fn=analyze_sentiment,
|
211 |
inputs=text_input,
|
|
|
3 |
import os
|
4 |
|
5 |
# --- Model Loading ---
|
|
|
|
|
6 |
MODEL_ID = "Light-Dav/sentiment-analysis-full-project"
|
7 |
|
8 |
try:
|
|
|
|
|
|
|
9 |
sentiment_analyzer = pipeline("sentiment-analysis", model=MODEL_ID, top_k=None)
|
10 |
model_loaded_successfully = True
|
11 |
except Exception as e:
|
|
|
118 |
}
|
119 |
|
120 |
try:
|
121 |
+
results = sentiment_analyzer(text)[0]
|
|
|
|
|
122 |
|
|
|
123 |
results_sorted = sorted(results, key=lambda x: x['score'], reverse=True)
|
124 |
|
|
|
125 |
top_sentiment = results_sorted[0]
|
126 |
label = top_sentiment['label']
|
127 |
score = top_sentiment['score']
|
128 |
|
|
|
|
|
129 |
confidence_scores_output = {item['label']: item['score'] for item in results}
|
130 |
|
|
|
131 |
overall_sentiment_display = interpret_sentiment(label, score)
|
132 |
|
133 |
return {
|
|
|
143 |
}
|
144 |
|
145 |
# --- Gradio Interface ---
|
146 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
147 |
gr.Markdown("# β¨ Sentiment Spark β¨")
|
148 |
gr.Markdown("---")
|
149 |
gr.Markdown("### Uncover the emotional tone of your English text instantly!")
|
150 |
|
151 |
+
with gr.Row():
|
152 |
with gr.Column(scale=2):
|
153 |
text_input = gr.Textbox(
|
154 |
lines=7,
|
|
|
157 |
interactive=True,
|
158 |
value="This movie was absolutely brilliant! A masterpiece of storytelling and emotion."
|
159 |
)
|
160 |
+
analyze_btn = gr.Button("Analyze Sentiment", variant="primary")
|
161 |
+
|
162 |
+
# --- Define outputs BEFORE gr.Examples ---
|
163 |
+
# These variables need to exist before they are referenced in gr.Examples
|
164 |
+
gr.Markdown("## π Analysis Results π")
|
165 |
+
overall_sentiment_output = gr.HTML(label="Overall Sentiment")
|
166 |
+
confidence_scores_output = gr.Label(num_top_classes=3, label="Confidence Scores")
|
167 |
+
raw_output = gr.JSON(label="Raw Model Output", visible=False)
|
168 |
+
# ----------------------------------------
|
169 |
|
170 |
gr.Markdown("---")
|
171 |
gr.Markdown("### Try some examples:")
|
|
|
179 |
["The meeting concluded without any major decisions."]
|
180 |
],
|
181 |
inputs=text_input,
|
182 |
+
fn=analyze_sentiment, # Function to run for examples
|
183 |
+
outputs=[overall_sentiment_output, confidence_scores_output, raw_output], # Outputs for examples
|
184 |
cache_examples=True # Caching examples for faster loading
|
185 |
)
|
186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
|
188 |
# --- Event Listeners ---
|
|
|
189 |
text_input.change(
|
190 |
fn=analyze_sentiment,
|
191 |
inputs=text_input,
|
192 |
outputs=[overall_sentiment_output, confidence_scores_output, raw_output],
|
193 |
+
# live=True
|
194 |
)
|
|
|
195 |
analyze_btn.click(
|
196 |
fn=analyze_sentiment,
|
197 |
inputs=text_input,
|