entropy25 commited on
Commit
614d951
Β·
verified Β·
1 Parent(s): c1f4a3d

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +135 -0
main.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import logging
3
+
4
+ from config import config
5
+ from app import SentimentApp
6
+
7
+ # Optimized Gradio Interface
8
+ def create_interface():
9
+ """Create comprehensive Gradio interface with optimizations"""
10
+ app = SentimentApp()
11
+
12
+ with gr.Blocks(theme=gr.themes.Soft(), title="Multilingual Sentiment Analyzer") as demo:
13
+ gr.Markdown("# 🌍 Multilingual Sentiment Analyzer")
14
+ gr.Markdown("AI-powered sentiment analysis with SHAP & LIME explainable AI features")
15
+
16
+ with gr.Tab("Single Analysis"):
17
+ with gr.Row():
18
+ with gr.Column():
19
+ text_input = gr.Textbox(
20
+ label="Enter Text for Analysis",
21
+ placeholder="Enter your text in any supported language...",
22
+ lines=5
23
+ )
24
+
25
+ with gr.Row():
26
+ language_selector = gr.Dropdown(
27
+ choices=list(config.SUPPORTED_LANGUAGES.values()),
28
+ value="Auto Detect",
29
+ label="Language"
30
+ )
31
+ theme_selector = gr.Dropdown(
32
+ choices=list(config.THEMES.keys()),
33
+ value="default",
34
+ label="Theme"
35
+ )
36
+
37
+ with gr.Row():
38
+ clean_text_cb = gr.Checkbox(label="Clean Text", value=False)
39
+ remove_punct_cb = gr.Checkbox(label="Remove Punctuation", value=False)
40
+ remove_nums_cb = gr.Checkbox(label="Remove Numbers", value=False)
41
+
42
+ analyze_btn = gr.Button("Analyze", variant="primary", size="lg")
43
+
44
+ gr.Examples(
45
+ examples=app.examples,
46
+ inputs=text_input,
47
+ cache_examples=False
48
+ )
49
+
50
+ with gr.Column():
51
+ result_output = gr.Textbox(label="Analysis Results", lines=8)
52
+
53
+ with gr.Row():
54
+ gauge_plot = gr.Plot(label="Sentiment Gauge")
55
+ probability_plot = gr.Plot(label="Probability Distribution")
56
+
57
+ # FIXED Advanced Analysis Tab
58
+ with gr.Tab("Advanced Analysis"):
59
+ gr.Markdown("## Explainable AI Analysis")
60
+ gr.Markdown("**SHAP and LIME analysis with FIXED implementation** - now handles text input correctly!")
61
+
62
+ with gr.Row():
63
+ with gr.Column():
64
+ advanced_text_input = gr.Textbox(
65
+ label="Enter Text for Advanced Analysis",
66
+ placeholder="Enter text to analyze with SHAP and LIME...",
67
+ lines=6,
68
+ value="This movie is absolutely fantastic and amazing!"
69
+ )
70
+
71
+ with gr.Row():
72
+ advanced_language = gr.Dropdown(
73
+ choices=list(config.SUPPORTED_LANGUAGES.values()),
74
+ value="Auto Detect",
75
+ label="Language"
76
+ )
77
+
78
+ num_samples_slider = gr.Slider(
79
+ minimum=50,
80
+ maximum=300,
81
+ value=100,
82
+ step=25,
83
+ label="Number of Samples",
84
+ info="Lower = Faster, Higher = More Accurate"
85
+ )
86
+
87
+ with gr.Row():
88
+ shap_btn = gr.Button("SHAP Analysis", variant="primary")
89
+ lime_btn = gr.Button("LIME Analysis", variant="secondary")
90
+
91
+ gr.Markdown("""
92
+
93
+ **πŸ“Š Analysis Methods:**
94
+ - **SHAP**: Token-level importance scores using Text masker
95
+ - **LIME**: Feature importance through text perturbation
96
+
97
+ **⚑ Expected Performance:**
98
+ - 50 samples: ~10-20s | 100 samples: ~20-40s | 200+ samples: ~40-80s
99
+ """)
100
+
101
+ with gr.Column():
102
+ advanced_results = gr.Textbox(label="Analysis Summary", lines=12)
103
+
104
+ with gr.Row():
105
+ advanced_plot = gr.Plot(label="Feature Importance Visualization")
106
+
107
+ with gr.Tab("Batch Analysis"):
108
+ with gr.Row():
109
+ with gr.Column():
110
+ file_upload = gr.File(
111
+ label="Upload File (CSV/TXT)",
112
+ file_types=[".csv", ".txt"]
113
+ )
114
+ batch_input = gr.Textbox(
115
+ label="Batch Input (one text per line)",
116
+ placeholder="Enter multiple texts, one per line...",
117
+ lines=10
118
+ )
119
+
120
+ with gr.Row():
121
+ batch_language = gr.Dropdown(
122
+ choices=list(config.SUPPORTED_LANGUAGES.values()),
123
+ value="Auto Detect",
124
+ label="Language"
125
+ )
126
+ batch_theme = gr.Dropdown(
127
+ choices=list(config.THEMES.keys()),
128
+ value="default",
129
+ label="Theme"
130
+ )
131
+
132
+ with gr.Row():
133
+ batch_clean_cb = gr.Checkbox(label="Clean Text", value=False)
134
+ batch_punct_cb = gr.Checkbox(label="Remove Punctuation", value=False)
135
+ batch_nums_cb = gr.Checkbox(label="Remove Numbers", value=False)