sentiment-multi / main.py
entropy25's picture
Update main.py
d7f3d04 verified
import gradio as gr
import logging
from config import config
from app import SentimentApp
# Optimized Gradio Interface
def create_interface():
"""Create comprehensive Gradio interface with optimizations"""
app = SentimentApp()
with gr.Blocks(theme=gr.themes.Soft(), title="Multilingual Sentiment Analyzer") as demo:
gr.Markdown("# 🌍 Multilingual Sentiment Analyzer")
gr.Markdown("AI-powered sentiment analysis with SHAP & LIME explainable AI features")
with gr.Tab("Single Analysis"):
with gr.Row():
with gr.Column():
text_input = gr.Textbox(
label="Enter Text for Analysis",
placeholder="Enter your text in any supported language...",
lines=5
)
with gr.Row():
language_selector = gr.Dropdown(
choices=list(config.SUPPORTED_LANGUAGES.values()),
value="Auto Detect",
label="Language"
)
theme_selector = gr.Dropdown(
choices=list(config.THEMES.keys()),
value="default",
label="Theme"
)
with gr.Row():
clean_text_cb = gr.Checkbox(label="Clean Text", value=False)
remove_punct_cb = gr.Checkbox(label="Remove Punctuation", value=False)
remove_nums_cb = gr.Checkbox(label="Remove Numbers", value=False)
analyze_btn = gr.Button("Analyze", variant="primary", size="lg")
gr.Examples(
examples=app.examples,
inputs=text_input,
cache_examples=False
)
with gr.Column():
result_output = gr.Textbox(label="Analysis Results", lines=8)
with gr.Row():
gauge_plot = gr.Plot(label="Sentiment Gauge")
probability_plot = gr.Plot(label="Probability Distribution")
# FIXED Advanced Analysis Tab
with gr.Tab("Advanced Analysis"):
gr.Markdown("## Explainable AI Analysis")
gr.Markdown("**SHAP and LIME analysis with FIXED implementation** - now handles text input correctly!")
with gr.Row():
with gr.Column():
advanced_text_input = gr.Textbox(
label="Enter Text for Advanced Analysis",
placeholder="Enter text to analyze with SHAP and LIME...",
lines=6,
value="This movie is absolutely fantastic and amazing!"
)
with gr.Row():
advanced_language = gr.Dropdown(
choices=list(config.SUPPORTED_LANGUAGES.values()),
value="Auto Detect",
label="Language"
)
num_samples_slider = gr.Slider(
minimum=50,
maximum=300,
value=100,
step=25,
label="Number of Samples",
info="Lower = Faster, Higher = More Accurate"
)
with gr.Row():
shap_btn = gr.Button("SHAP Analysis", variant="primary")
lime_btn = gr.Button("LIME Analysis", variant="secondary")
gr.Markdown("""
**πŸ“Š Analysis Methods:**
- **SHAP**: Token-level importance scores using Text masker
- **LIME**: Feature importance through text perturbation
**⚑ Expected Performance:**
- 50 samples: ~10-20s | 100 samples: ~20-40s | 200+ samples: ~40-80s
""")
with gr.Column():
advanced_results = gr.Textbox(label="Analysis Summary", lines=12)
with gr.Row():
advanced_plot = gr.Plot(label="Feature Importance Visualization")
with gr.Tab("Batch Analysis"):
with gr.Row():
with gr.Column():
file_upload = gr.File(
label="Upload File (CSV/TXT)",
file_types=[".csv", ".txt"]
)
batch_input = gr.Textbox(
label="Batch Input (one text per line)",
placeholder="Enter multiple texts, one per line...",
lines=10
)
with gr.Row():
batch_language = gr.Dropdown(
choices=list(config.SUPPORTED_LANGUAGES.values()),
value="Auto Detect",
label="Language"
)
batch_theme = gr.Dropdown(
choices=list(config.THEMES.keys()),
value="default",
label="Theme"
)
with gr.Row():
batch_clean_cb = gr.Checkbox(label="Clean Text", value=False)
batch_punct_cb = gr.Checkbox(label="Remove Punctuation", value=False)
batch_nums_cb = gr.Checkbox(label="Remove Numbers", value=False)
with gr.Row():
load_file_btn = gr.Button("Load File")
analyze_batch_btn = gr.Button("Analyze Batch", variant="primary")
with gr.Column():
batch_summary = gr.Textbox(label="Batch Summary", lines=8)
batch_results_df = gr.Dataframe(
label="Detailed Results",
headers=["Index", "Text", "Sentiment", "Confidence", "Language", "Word_Count"],
datatype=["number", "str", "str", "str", "str", "number"]
)
with gr.Row():
batch_plot = gr.Plot(label="Batch Analysis Summary")
confidence_dist_plot = gr.Plot(label="Confidence Distribution")
with gr.Tab("History & Analytics"):
with gr.Row():
with gr.Column():
with gr.Row():
refresh_history_btn = gr.Button("Refresh History")
clear_history_btn = gr.Button("Clear History", variant="stop")
status_btn = gr.Button("Get Status")
history_theme = gr.Dropdown(
choices=list(config.THEMES.keys()),
value="default",
label="Dashboard Theme"
)
with gr.Row():
export_csv_btn = gr.Button("Export CSV")
export_json_btn = gr.Button("Export JSON")
with gr.Column():
history_status = gr.Textbox(label="History Status", lines=8)
history_dashboard = gr.Plot(label="History Analytics Dashboard")
with gr.Row():
csv_download = gr.File(label="CSV Download", visible=True)
json_download = gr.File(label="JSON Download", visible=True)
# Event Handlers
# Single Analysis
analyze_btn.click(
app.analyze_single,
inputs=[text_input, language_selector, theme_selector,
clean_text_cb, remove_punct_cb, remove_nums_cb],
outputs=[result_output, gauge_plot, probability_plot]
)
# FIXED Advanced Analysis with sample size control
shap_btn.click(
app.analyze_with_shap,
inputs=[advanced_text_input, advanced_language, num_samples_slider],
outputs=[advanced_results, advanced_plot]
)
lime_btn.click(
app.analyze_with_lime,
inputs=[advanced_text_input, advanced_language, num_samples_slider],
outputs=[advanced_results, advanced_plot]
)
# Batch Analysis
load_file_btn.click(
app.data_handler.process_file,
inputs=file_upload,
outputs=batch_input
)
analyze_batch_btn.click(
app.analyze_batch,
inputs=[batch_input, batch_language, batch_theme,
batch_clean_cb, batch_punct_cb, batch_nums_cb],
outputs=[batch_summary, batch_results_df, batch_plot, confidence_dist_plot]
)
# History & Analytics
refresh_history_btn.click(
app.plot_history,
inputs=history_theme,
outputs=[history_dashboard, history_status]
)
clear_history_btn.click(
lambda: f"Cleared {app.history.clear()} entries",
outputs=history_status
)
status_btn.click(
app.get_history_status,
outputs=history_status
)
export_csv_btn.click(
lambda: app.data_handler.export_data(app.history.get_all(), 'csv'),
outputs=[csv_download, history_status]
)
export_json_btn.click(
lambda: app.data_handler.export_data(app.history.get_all(), 'json'),
outputs=[json_download, history_status]
)
return demo
# Application Entry Point
if __name__ == "__main__":
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
try:
demo = create_interface()
demo.launch(
share=True,
server_name="0.0.0.0",
server_port=7860,
show_error=True
)
except Exception as e:
logging.error(f"Failed to launch application: {e}")
raise