Spaces:
Sleeping
Sleeping
File size: 6,047 Bytes
614d951 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
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) |