Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -132,4 +132,130 @@ def create_interface():
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
136 |
+
|
137 |
+
with gr.Row():
|
138 |
+
load_file_btn = gr.Button("Load File")
|
139 |
+
analyze_batch_btn = gr.Button("Analyze Batch", variant="primary")
|
140 |
+
|
141 |
+
with gr.Column():
|
142 |
+
batch_summary = gr.Textbox(label="Batch Summary", lines=8)
|
143 |
+
batch_results_df = gr.Dataframe(
|
144 |
+
label="Detailed Results",
|
145 |
+
headers=["Index", "Text", "Sentiment", "Confidence", "Language", "Word_Count"],
|
146 |
+
datatype=["number", "str", "str", "str", "str", "number"]
|
147 |
+
)
|
148 |
+
|
149 |
+
with gr.Row():
|
150 |
+
batch_plot = gr.Plot(label="Batch Analysis Summary")
|
151 |
+
confidence_dist_plot = gr.Plot(label="Confidence Distribution")
|
152 |
+
|
153 |
+
with gr.Tab("History & Analytics"):
|
154 |
+
with gr.Row():
|
155 |
+
with gr.Column():
|
156 |
+
with gr.Row():
|
157 |
+
refresh_history_btn = gr.Button("Refresh History")
|
158 |
+
clear_history_btn = gr.Button("Clear History", variant="stop")
|
159 |
+
status_btn = gr.Button("Get Status")
|
160 |
+
|
161 |
+
history_theme = gr.Dropdown(
|
162 |
+
choices=list(config.THEMES.keys()),
|
163 |
+
value="default",
|
164 |
+
label="Dashboard Theme"
|
165 |
+
)
|
166 |
+
|
167 |
+
with gr.Row():
|
168 |
+
export_csv_btn = gr.Button("Export CSV")
|
169 |
+
export_json_btn = gr.Button("Export JSON")
|
170 |
+
|
171 |
+
with gr.Column():
|
172 |
+
history_status = gr.Textbox(label="History Status", lines=8)
|
173 |
+
|
174 |
+
history_dashboard = gr.Plot(label="History Analytics Dashboard")
|
175 |
+
|
176 |
+
with gr.Row():
|
177 |
+
csv_download = gr.File(label="CSV Download", visible=True)
|
178 |
+
json_download = gr.File(label="JSON Download", visible=True)
|
179 |
+
|
180 |
+
# Event Handlers
|
181 |
+
|
182 |
+
# Single Analysis
|
183 |
+
analyze_btn.click(
|
184 |
+
app.analyze_single,
|
185 |
+
inputs=[text_input, language_selector, theme_selector,
|
186 |
+
clean_text_cb, remove_punct_cb, remove_nums_cb],
|
187 |
+
outputs=[result_output, gauge_plot, probability_plot]
|
188 |
+
)
|
189 |
+
|
190 |
+
# FIXED Advanced Analysis with sample size control
|
191 |
+
shap_btn.click(
|
192 |
+
app.analyze_with_shap,
|
193 |
+
inputs=[advanced_text_input, advanced_language, num_samples_slider],
|
194 |
+
outputs=[advanced_results, advanced_plot]
|
195 |
+
)
|
196 |
+
|
197 |
+
lime_btn.click(
|
198 |
+
app.analyze_with_lime,
|
199 |
+
inputs=[advanced_text_input, advanced_language, num_samples_slider],
|
200 |
+
outputs=[advanced_results, advanced_plot]
|
201 |
+
)
|
202 |
+
|
203 |
+
# Batch Analysis
|
204 |
+
load_file_btn.click(
|
205 |
+
app.data_handler.process_file,
|
206 |
+
inputs=file_upload,
|
207 |
+
outputs=batch_input
|
208 |
+
)
|
209 |
+
|
210 |
+
analyze_batch_btn.click(
|
211 |
+
app.analyze_batch,
|
212 |
+
inputs=[batch_input, batch_language, batch_theme,
|
213 |
+
batch_clean_cb, batch_punct_cb, batch_nums_cb],
|
214 |
+
outputs=[batch_summary, batch_results_df, batch_plot, confidence_dist_plot]
|
215 |
+
)
|
216 |
+
|
217 |
+
# History & Analytics
|
218 |
+
refresh_history_btn.click(
|
219 |
+
app.plot_history,
|
220 |
+
inputs=history_theme,
|
221 |
+
outputs=[history_dashboard, history_status]
|
222 |
+
)
|
223 |
+
clear_history_btn.click(
|
224 |
+
lambda: f"Cleared {app.history.clear()} entries",
|
225 |
+
outputs=history_status
|
226 |
+
)
|
227 |
+
|
228 |
+
status_btn.click(
|
229 |
+
app.get_history_status,
|
230 |
+
outputs=history_status
|
231 |
+
)
|
232 |
+
|
233 |
+
export_csv_btn.click(
|
234 |
+
lambda: app.data_handler.export_data(app.history.get_all(), 'csv'),
|
235 |
+
outputs=[csv_download, history_status]
|
236 |
+
)
|
237 |
+
export_json_btn.click(
|
238 |
+
lambda: app.data_handler.export_data(app.history.get_all(), 'json'),
|
239 |
+
outputs=[json_download, history_status]
|
240 |
+
)
|
241 |
+
|
242 |
+
return demo
|
243 |
+
|
244 |
+
# Application Entry Point
|
245 |
+
if __name__ == "__main__":
|
246 |
+
logging.basicConfig(
|
247 |
+
level=logging.INFO,
|
248 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
249 |
+
)
|
250 |
+
|
251 |
+
try:
|
252 |
+
demo = create_interface()
|
253 |
+
demo.launch(
|
254 |
+
share=True,
|
255 |
+
server_name="0.0.0.0",
|
256 |
+
server_port=7860,
|
257 |
+
show_error=True
|
258 |
+
)
|
259 |
+
except Exception as e:
|
260 |
+
logging.error(f"Failed to launch application: {e}")
|
261 |
+
raise
|