Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -114,7 +114,8 @@ def classify_sentence(sentence, progress=gr.Progress()):
|
|
| 114 |
|
| 115 |
if not sentence or sentence.strip() == "":
|
| 116 |
status_msg = "⚠️ Please enter a sentence to classify."
|
| 117 |
-
|
|
|
|
| 118 |
|
| 119 |
try:
|
| 120 |
# Step 1: Building prompt
|
|
@@ -169,44 +170,56 @@ def classify_sentence(sentence, progress=gr.Progress()):
|
|
| 169 |
# Step 4: Formatting output
|
| 170 |
progress(0.95, desc="📝 Formatting output...")
|
| 171 |
print("Step 4: Formatting output...")
|
| 172 |
-
status_4 = "⏳ **Step 4/4:** Formatting output..."
|
| 173 |
-
yield status_4, status_4
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
|
|
|
|
|
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
"""
|
| 186 |
|
| 187 |
progress(1.0, desc="✅ Complete!")
|
| 188 |
print(f"Classification complete! Label: {label_name}")
|
| 189 |
print(f"{'='*80}\n")
|
| 190 |
|
| 191 |
-
# Final result with success status
|
| 192 |
final_status = f"✅ **Classification Complete!** Result: {label_name}"
|
| 193 |
-
|
| 194 |
|
| 195 |
except Exception as e:
|
| 196 |
error_message = f"""
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
An error occurred during classification. Please try again
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
If the problem persists, please check the model configuration.
|
| 204 |
"""
|
| 205 |
print(f"Error in classify_sentence: {e}")
|
| 206 |
import traceback
|
| 207 |
traceback.print_exc()
|
| 208 |
error_status = f"❌ **Error:** {str(e)}"
|
| 209 |
-
|
| 210 |
|
| 211 |
|
| 212 |
# ============================================================================
|
|
@@ -234,111 +247,28 @@ custom_css = """
|
|
| 234 |
|
| 235 |
/* Status box styling */
|
| 236 |
#status_box {
|
| 237 |
-
padding: 15px;
|
| 238 |
-
border-radius: 10px;
|
| 239 |
-
margin: 15px 0;
|
| 240 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 241 |
-
color: white;
|
| 242 |
-
font-weight: 500;
|
| 243 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 244 |
-
min-height: 50px;
|
| 245 |
-
|
| 246 |
-
align-items: center;
|
| 247 |
-
justify-content: center;
|
| 248 |
}
|
| 249 |
|
| 250 |
/* Result box container */
|
| 251 |
#output_box {
|
| 252 |
-
padding: 0;
|
| 253 |
-
margin-top: 20px;
|
| 254 |
}
|
| 255 |
|
| 256 |
-
/*
|
| 257 |
#output_box .prose {
|
| 258 |
max-width: 100% !important;
|
| 259 |
}
|
| 260 |
|
| 261 |
-
#output_box h2 {
|
| 262 |
-
font-size: 1.1em !important;
|
| 263 |
-
margin-top: 0 !important;
|
| 264 |
-
margin-bottom: 15px !important;
|
| 265 |
-
padding: 20px;
|
| 266 |
-
border-radius: 12px 12px 0 0;
|
| 267 |
-
color: white !important;
|
| 268 |
-
text-align: center;
|
| 269 |
-
font-weight: 600 !important;
|
| 270 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 271 |
-
}
|
| 272 |
-
|
| 273 |
-
#output_box h3 {
|
| 274 |
-
font-size: 2em !important;
|
| 275 |
-
margin: 0 !important;
|
| 276 |
-
padding: 30px 20px;
|
| 277 |
-
text-align: center;
|
| 278 |
-
border-radius: 0 !important;
|
| 279 |
-
font-weight: bold !important;
|
| 280 |
-
}
|
| 281 |
-
|
| 282 |
-
/* Color coding for different labels */
|
| 283 |
-
#output_box h3:contains("🔵") {
|
| 284 |
-
background-color: #e3f2fd !important;
|
| 285 |
-
color: #1976d2 !important;
|
| 286 |
-
border-left: 6px solid #1976d2;
|
| 287 |
-
}
|
| 288 |
-
|
| 289 |
-
#output_box h3:contains("🟡") {
|
| 290 |
-
background-color: #fff9c4 !important;
|
| 291 |
-
color: #f57f17 !important;
|
| 292 |
-
border-left: 6px solid #f57f17;
|
| 293 |
-
}
|
| 294 |
-
|
| 295 |
-
#output_box h3:contains("🟢") {
|
| 296 |
-
background-color: #e8f5e9 !important;
|
| 297 |
-
color: #388e3c !important;
|
| 298 |
-
border-left: 6px solid #388e3c;
|
| 299 |
-
}
|
| 300 |
-
|
| 301 |
-
#output_box h3:contains("🔴") {
|
| 302 |
-
background-color: #ffebee !important;
|
| 303 |
-
color: #d32f2f !important;
|
| 304 |
-
border-left: 6px solid #d32f2f;
|
| 305 |
-
}
|
| 306 |
-
|
| 307 |
-
#output_box p strong {
|
| 308 |
-
color: #333 !important;
|
| 309 |
-
}
|
| 310 |
-
|
| 311 |
-
#output_box hr {
|
| 312 |
-
margin: 0 !important;
|
| 313 |
-
border: none !important;
|
| 314 |
-
border-top: 2px solid #e0e0e0 !important;
|
| 315 |
-
}
|
| 316 |
-
|
| 317 |
-
/* Category description styling */
|
| 318 |
-
#output_box > div > p:first-of-type {
|
| 319 |
-
padding: 20px;
|
| 320 |
-
background-color: #f8f9fa;
|
| 321 |
-
margin: 0 !important;
|
| 322 |
-
font-size: 1.1em;
|
| 323 |
-
}
|
| 324 |
-
|
| 325 |
-
/* Model explanation section */
|
| 326 |
-
#output_box > div > p:last-of-type {
|
| 327 |
-
padding: 20px;
|
| 328 |
-
background-color: white;
|
| 329 |
-
margin: 0 !important;
|
| 330 |
-
line-height: 1.6;
|
| 331 |
-
border-radius: 0 0 12px 12px;
|
| 332 |
-
}
|
| 333 |
-
|
| 334 |
-
/* Add shadow to entire result card */
|
| 335 |
-
#output_box > div {
|
| 336 |
-
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
|
| 337 |
-
border-radius: 12px;
|
| 338 |
-
overflow: hidden;
|
| 339 |
-
border: 1px solid #e0e0e0;
|
| 340 |
-
}
|
| 341 |
-
|
| 342 |
footer {
|
| 343 |
display: none !important;
|
| 344 |
}
|
|
@@ -359,8 +289,6 @@ with gr.Blocks(css=custom_css, title="Populism Detector") as demo:
|
|
| 359 |
- **🟢 People-Centrism**: Positive references to "the people" or ordinary citizens
|
| 360 |
- **🔴 Both**: Combines anti-elite rhetoric with people-centric language
|
| 361 |
|
| 362 |
-
|
| 363 |
-
* Note: Predictions may take a couple minutes to generate, given this space is hosted on a free cpu and i am poor :(
|
| 364 |
---
|
| 365 |
""")
|
| 366 |
|
|
|
|
| 114 |
|
| 115 |
if not sentence or sentence.strip() == "":
|
| 116 |
status_msg = "⚠️ Please enter a sentence to classify."
|
| 117 |
+
yield status_msg, status_msg
|
| 118 |
+
return # End generator
|
| 119 |
|
| 120 |
try:
|
| 121 |
# Step 1: Building prompt
|
|
|
|
| 170 |
# Step 4: Formatting output
|
| 171 |
progress(0.95, desc="📝 Formatting output...")
|
| 172 |
print("Step 4: Formatting output...")
|
|
|
|
|
|
|
| 173 |
|
| 174 |
+
# Determine background color based on label
|
| 175 |
+
color_map = {
|
| 176 |
+
'a': {'bg': '#e3f2fd', 'text': '#1976d2', 'border': '#1976d2'},
|
| 177 |
+
'b': {'bg': '#fff9c4', 'text': '#f57f17', 'border': '#f57f17'},
|
| 178 |
+
'c': {'bg': '#e8f5e9', 'text': '#388e3c', 'border': '#388e3c'},
|
| 179 |
+
'd': {'bg': '#ffebee', 'text': '#d32f2f', 'border': '#d32f2f'}
|
| 180 |
+
}
|
| 181 |
+
colors = color_map.get(label, color_map['a'])
|
| 182 |
|
| 183 |
+
classification_output = f"""
|
| 184 |
+
<div style="border: 1px solid #e0e0e0; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 12px rgba(0,0,0,0.1); margin: 20px 0;">
|
| 185 |
+
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 15px; text-align: center;">
|
| 186 |
+
<h3 style="margin: 0; font-size: 1.2em;">Classification Result</h3>
|
| 187 |
+
</div>
|
| 188 |
+
<div style="background-color: {colors['bg']}; color: {colors['text']}; padding: 30px 20px; text-align: center; border-left: 6px solid {colors['border']};">
|
| 189 |
+
<h2 style="margin: 0; font-size: 2em; font-weight: bold;">{label_name}</h2>
|
| 190 |
+
</div>
|
| 191 |
+
<div style="background-color: #f8f9fa; padding: 20px; border-top: 2px solid #e0e0e0;">
|
| 192 |
+
<p style="margin: 0; font-size: 1.1em;"><strong>Category:</strong> {label_desc}</p>
|
| 193 |
+
</div>
|
| 194 |
+
<div style="background-color: white; padding: 20px; border-top: 2px solid #e0e0e0;">
|
| 195 |
+
<p style="margin: 0; line-height: 1.6;"><strong>Model Explanation:</strong></p>
|
| 196 |
+
<p style="margin: 10px 0 0 0; line-height: 1.6;">{response_text}</p>
|
| 197 |
+
</div>
|
| 198 |
+
</div>
|
| 199 |
"""
|
| 200 |
|
| 201 |
progress(1.0, desc="✅ Complete!")
|
| 202 |
print(f"Classification complete! Label: {label_name}")
|
| 203 |
print(f"{'='*80}\n")
|
| 204 |
|
| 205 |
+
# Final result with success status - YIELD instead of return
|
| 206 |
final_status = f"✅ **Classification Complete!** Result: {label_name}"
|
| 207 |
+
yield final_status, classification_output
|
| 208 |
|
| 209 |
except Exception as e:
|
| 210 |
error_message = f"""
|
| 211 |
+
<div style="border: 1px solid #f44336; border-radius: 12px; padding: 20px; background-color: #ffebee; margin: 20px 0;">
|
| 212 |
+
<h3 style="color: #d32f2f; margin-top: 0;">⚠️ Error</h3>
|
| 213 |
+
<p>An error occurred during classification. Please try again.</p>
|
| 214 |
+
<p><strong>Error details:</strong> {str(e)}</p>
|
| 215 |
+
<p style="margin-bottom: 0;">If the problem persists, please check the model configuration.</p>
|
| 216 |
+
</div>
|
|
|
|
| 217 |
"""
|
| 218 |
print(f"Error in classify_sentence: {e}")
|
| 219 |
import traceback
|
| 220 |
traceback.print_exc()
|
| 221 |
error_status = f"❌ **Error:** {str(e)}"
|
| 222 |
+
yield error_status, error_message
|
| 223 |
|
| 224 |
|
| 225 |
# ============================================================================
|
|
|
|
| 247 |
|
| 248 |
/* Status box styling */
|
| 249 |
#status_box {
|
| 250 |
+
padding: 15px !important;
|
| 251 |
+
border-radius: 10px !important;
|
| 252 |
+
margin: 15px 0 !important;
|
| 253 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 254 |
+
color: white !important;
|
| 255 |
+
font-weight: 500 !important;
|
| 256 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1) !important;
|
| 257 |
+
min-height: 50px !important;
|
| 258 |
+
text-align: center !important;
|
|
|
|
|
|
|
| 259 |
}
|
| 260 |
|
| 261 |
/* Result box container */
|
| 262 |
#output_box {
|
| 263 |
+
padding: 0 !important;
|
| 264 |
+
margin-top: 20px !important;
|
| 265 |
}
|
| 266 |
|
| 267 |
+
/* Make sure HTML renders properly */
|
| 268 |
#output_box .prose {
|
| 269 |
max-width: 100% !important;
|
| 270 |
}
|
| 271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
footer {
|
| 273 |
display: none !important;
|
| 274 |
}
|
|
|
|
| 289 |
- **🟢 People-Centrism**: Positive references to "the people" or ordinary citizens
|
| 290 |
- **🔴 Both**: Combines anti-elite rhetoric with people-centric language
|
| 291 |
|
|
|
|
|
|
|
| 292 |
---
|
| 293 |
""")
|
| 294 |
|