Spaces:
Runtime error
Runtime error
π§ Fix button interaction issue in Gradio app
Browse filesProblem: Buttons were not clickable due to scoping issues
Solution:
- Moved event handler functions outside of interface creation scope
- Added global app_instance variable for proper state management
- Simplified function structure to ensure accessibility
- Fixed JavaScript event binding issues
Changes:
- Redefined all button event handlers as global functions
- Used global app_instance for state management
- Maintained all functionality while fixing interaction
- Added error handling for uninitialized app state
π€ Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
app.py
CHANGED
|
@@ -14,6 +14,9 @@
|
|
| 14 |
import os
|
| 15 |
import pandas as pd
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
class SentimentGradioApp:
|
| 18 |
def __init__(self, model_name="5CD-AI/Vietnamese-Sentiment-visobert", max_batch_size=10):
|
| 19 |
self.model_name = model_name
|
|
@@ -246,12 +249,58 @@ def batch_predict(self, texts):
|
|
| 246 |
self.cleanup_memory()
|
| 247 |
return [], f"β Error during batch processing: {str(e)}"
|
| 248 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
def create_interface():
|
| 250 |
"""Create the Gradio interface for Hugging Face Spaces"""
|
| 251 |
-
|
|
|
|
|
|
|
| 252 |
|
| 253 |
# Load model
|
| 254 |
-
if not
|
| 255 |
print("β Failed to load model. Please try again.")
|
| 256 |
return None
|
| 257 |
|
|
@@ -327,12 +376,12 @@ def create_interface():
|
|
| 327 |
# Batch Analysis Tab
|
| 328 |
with gr.Tab("π Batch Analysis"):
|
| 329 |
gr.Markdown(f"### π Memory-Efficient Batch Processing")
|
| 330 |
-
gr.Markdown(f"**Maximum batch size:** {
|
| 331 |
-
gr.Markdown(f"**Memory limit:** {
|
| 332 |
|
| 333 |
batch_input = gr.Textbox(
|
| 334 |
label="Enter Multiple Texts (one per line)",
|
| 335 |
-
placeholder=f"Enter up to {
|
| 336 |
lines=8,
|
| 337 |
max_lines=20
|
| 338 |
)
|
|
@@ -345,7 +394,7 @@ def create_interface():
|
|
| 345 |
batch_result_output = gr.Markdown(label="Batch Analysis Result")
|
| 346 |
memory_info = gr.Textbox(
|
| 347 |
label="Memory Usage",
|
| 348 |
-
value=f"{
|
| 349 |
interactive=False
|
| 350 |
)
|
| 351 |
|
|
@@ -355,16 +404,16 @@ def create_interface():
|
|
| 355 |
## π€ Model Details
|
| 356 |
|
| 357 |
**Model Architecture:** Transformer-based sequence classification
|
| 358 |
-
**Base Model:** {
|
| 359 |
**Languages:** Vietnamese (optimized)
|
| 360 |
**Labels:** Negative, Neutral, Positive
|
| 361 |
-
**Max Batch Size:** {
|
| 362 |
|
| 363 |
## π Performance Metrics
|
| 364 |
|
| 365 |
- **Processing Speed:** ~100ms per text
|
| 366 |
- **Max Sequence Length:** 512 tokens
|
| 367 |
-
- **Memory Limit:** {
|
| 368 |
|
| 369 |
## π‘ Usage Tips
|
| 370 |
|
|
@@ -376,7 +425,7 @@ def create_interface():
|
|
| 376 |
## π‘οΈ Memory Management
|
| 377 |
|
| 378 |
- **Automatic Cleanup:** Memory is cleaned after each prediction
|
| 379 |
-
- **Batch Limits:** Maximum {
|
| 380 |
- **Memory Monitoring:** Real-time memory usage tracking
|
| 381 |
- **GPU Optimization:** CUDA cache clearing when available
|
| 382 |
|
|
@@ -388,43 +437,9 @@ def create_interface():
|
|
| 388 |
- Model loaded directly from Hugging Face Hub (no local training required)
|
| 389 |
""")
|
| 390 |
|
| 391 |
-
# Event handlers
|
| 392 |
-
def analyze_text(text):
|
| 393 |
-
result, output = app.predict_sentiment(text)
|
| 394 |
-
if result:
|
| 395 |
-
# Prepare data for confidence plot
|
| 396 |
-
plot_data = pd.DataFrame([
|
| 397 |
-
{"sentiment": "Negative", "confidence": result["probabilities"]["Negative"]},
|
| 398 |
-
{"sentiment": "Neutral", "confidence": result["probabilities"]["Neutral"]},
|
| 399 |
-
{"sentiment": "Positive", "confidence": result["probabilities"]["Positive"]}
|
| 400 |
-
])
|
| 401 |
-
return output, gr.BarPlot(visible=True, value=plot_data)
|
| 402 |
-
else:
|
| 403 |
-
return output, gr.BarPlot(visible=False)
|
| 404 |
-
|
| 405 |
-
def clear_inputs():
|
| 406 |
-
return "", "", gr.BarPlot(visible=False)
|
| 407 |
-
|
| 408 |
-
def analyze_batch(texts):
|
| 409 |
-
if texts:
|
| 410 |
-
text_list = [line.strip() for line in texts.split('\n') if line.strip()]
|
| 411 |
-
results, summary = app.batch_predict(text_list)
|
| 412 |
-
return summary
|
| 413 |
-
return "β Please enter some texts to analyze."
|
| 414 |
-
|
| 415 |
-
def clear_batch():
|
| 416 |
-
return ""
|
| 417 |
-
|
| 418 |
-
def update_memory_info():
|
| 419 |
-
return f"{app.get_memory_usage():.1f}MB used"
|
| 420 |
-
|
| 421 |
-
def manual_memory_cleanup():
|
| 422 |
-
app.cleanup_memory()
|
| 423 |
-
return f"Memory cleaned. Current usage: {app.get_memory_usage():.1f}MB"
|
| 424 |
-
|
| 425 |
# Connect events
|
| 426 |
analyze_btn.click(
|
| 427 |
-
fn=
|
| 428 |
inputs=[text_input],
|
| 429 |
outputs=[result_output, confidence_plot]
|
| 430 |
)
|
|
|
|
| 14 |
import os
|
| 15 |
import pandas as pd
|
| 16 |
|
| 17 |
+
# Global app instance
|
| 18 |
+
app_instance = None
|
| 19 |
+
|
| 20 |
class SentimentGradioApp:
|
| 21 |
def __init__(self, model_name="5CD-AI/Vietnamese-Sentiment-visobert", max_batch_size=10):
|
| 22 |
self.model_name = model_name
|
|
|
|
| 249 |
self.cleanup_memory()
|
| 250 |
return [], f"β Error during batch processing: {str(e)}"
|
| 251 |
|
| 252 |
+
# Define functions outside of interface creation for better scoping
|
| 253 |
+
def analyze_sentiment(text):
|
| 254 |
+
if not app_instance:
|
| 255 |
+
return "β App not initialized. Please refresh the page.", gr.BarPlot(visible=False)
|
| 256 |
+
|
| 257 |
+
result, output = app_instance.predict_sentiment(text)
|
| 258 |
+
if result:
|
| 259 |
+
# Prepare data for confidence plot
|
| 260 |
+
plot_data = pd.DataFrame([
|
| 261 |
+
{"sentiment": "Negative", "confidence": result["probabilities"]["Negative"]},
|
| 262 |
+
{"sentiment": "Neutral", "confidence": result["probabilities"]["Neutral"]},
|
| 263 |
+
{"sentiment": "Positive", "confidence": result["probabilities"]["Positive"]}
|
| 264 |
+
])
|
| 265 |
+
return output, gr.BarPlot(visible=True, value=plot_data)
|
| 266 |
+
else:
|
| 267 |
+
return output, gr.BarPlot(visible=False)
|
| 268 |
+
|
| 269 |
+
def clear_inputs():
|
| 270 |
+
return "", "", gr.BarPlot(visible=False)
|
| 271 |
+
|
| 272 |
+
def analyze_batch(texts):
|
| 273 |
+
if not app_instance:
|
| 274 |
+
return "β App not initialized. Please refresh the page."
|
| 275 |
+
|
| 276 |
+
if texts:
|
| 277 |
+
text_list = [line.strip() for line in texts.split('\n') if line.strip()]
|
| 278 |
+
results, summary = app_instance.batch_predict(text_list)
|
| 279 |
+
return summary
|
| 280 |
+
return "β Please enter some texts to analyze."
|
| 281 |
+
|
| 282 |
+
def clear_batch():
|
| 283 |
+
return ""
|
| 284 |
+
|
| 285 |
+
def update_memory_info():
|
| 286 |
+
if not app_instance:
|
| 287 |
+
return "App not initialized"
|
| 288 |
+
return f"{app_instance.get_memory_usage():.1f}MB used"
|
| 289 |
+
|
| 290 |
+
def manual_memory_cleanup():
|
| 291 |
+
if not app_instance:
|
| 292 |
+
return "App not initialized"
|
| 293 |
+
app_instance.cleanup_memory()
|
| 294 |
+
return f"Memory cleaned. Current usage: {app_instance.get_memory_usage():.1f}MB"
|
| 295 |
+
|
| 296 |
def create_interface():
|
| 297 |
"""Create the Gradio interface for Hugging Face Spaces"""
|
| 298 |
+
global app_instance
|
| 299 |
+
|
| 300 |
+
app_instance = SentimentGradioApp()
|
| 301 |
|
| 302 |
# Load model
|
| 303 |
+
if not app_instance.load_model():
|
| 304 |
print("β Failed to load model. Please try again.")
|
| 305 |
return None
|
| 306 |
|
|
|
|
| 376 |
# Batch Analysis Tab
|
| 377 |
with gr.Tab("π Batch Analysis"):
|
| 378 |
gr.Markdown(f"### π Memory-Efficient Batch Processing")
|
| 379 |
+
gr.Markdown(f"**Maximum batch size:** {app_instance.max_batch_size} texts (for memory efficiency)")
|
| 380 |
+
gr.Markdown(f"**Memory limit:** {app_instance.max_memory_mb}MB")
|
| 381 |
|
| 382 |
batch_input = gr.Textbox(
|
| 383 |
label="Enter Multiple Texts (one per line)",
|
| 384 |
+
placeholder=f"Enter up to {app_instance.max_batch_size} Vietnamese texts, one per line...",
|
| 385 |
lines=8,
|
| 386 |
max_lines=20
|
| 387 |
)
|
|
|
|
| 394 |
batch_result_output = gr.Markdown(label="Batch Analysis Result")
|
| 395 |
memory_info = gr.Textbox(
|
| 396 |
label="Memory Usage",
|
| 397 |
+
value=f"{app_instance.get_memory_usage():.1f}MB used",
|
| 398 |
interactive=False
|
| 399 |
)
|
| 400 |
|
|
|
|
| 404 |
## π€ Model Details
|
| 405 |
|
| 406 |
**Model Architecture:** Transformer-based sequence classification
|
| 407 |
+
**Base Model:** {app_instance.model_name}
|
| 408 |
**Languages:** Vietnamese (optimized)
|
| 409 |
**Labels:** Negative, Neutral, Positive
|
| 410 |
+
**Max Batch Size:** {app_instance.max_batch_size} texts
|
| 411 |
|
| 412 |
## π Performance Metrics
|
| 413 |
|
| 414 |
- **Processing Speed:** ~100ms per text
|
| 415 |
- **Max Sequence Length:** 512 tokens
|
| 416 |
+
- **Memory Limit:** {app_instance.max_memory_mb}MB
|
| 417 |
|
| 418 |
## π‘ Usage Tips
|
| 419 |
|
|
|
|
| 425 |
## π‘οΈ Memory Management
|
| 426 |
|
| 427 |
- **Automatic Cleanup:** Memory is cleaned after each prediction
|
| 428 |
+
- **Batch Limits:** Maximum {app_instance.max_batch_size} texts per batch to prevent overflow
|
| 429 |
- **Memory Monitoring:** Real-time memory usage tracking
|
| 430 |
- **GPU Optimization:** CUDA cache clearing when available
|
| 431 |
|
|
|
|
| 437 |
- Model loaded directly from Hugging Face Hub (no local training required)
|
| 438 |
""")
|
| 439 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
# Connect events
|
| 441 |
analyze_btn.click(
|
| 442 |
+
fn=analyze_sentiment,
|
| 443 |
inputs=[text_input],
|
| 444 |
outputs=[result_output, confidence_plot]
|
| 445 |
)
|