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)