Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,8 @@ import re
|
|
| 11 |
import json
|
| 12 |
import csv
|
| 13 |
import io
|
|
|
|
|
|
|
| 14 |
from datetime import datetime
|
| 15 |
|
| 16 |
# Configuration
|
|
@@ -259,28 +261,35 @@ def process_uploaded_file(file):
|
|
| 259 |
return content
|
| 260 |
|
| 261 |
def export_history_csv():
|
| 262 |
-
"""Export history to CSV"""
|
| 263 |
if not history:
|
| 264 |
-
return None
|
| 265 |
|
| 266 |
-
|
| 267 |
-
|
|
|
|
| 268 |
writer.writerow(['Timestamp', 'Text', 'Sentiment', 'Confidence', 'Positive_Prob', 'Negative_Prob'])
|
| 269 |
|
| 270 |
for entry in history:
|
| 271 |
writer.writerow([
|
| 272 |
entry['timestamp'], entry['text'], entry['sentiment'],
|
| 273 |
-
entry['confidence'], entry['pos_prob'], entry['neg_prob']
|
| 274 |
])
|
| 275 |
|
| 276 |
-
|
|
|
|
| 277 |
|
| 278 |
def export_history_json():
|
| 279 |
-
"""Export history to JSON"""
|
| 280 |
if not history:
|
| 281 |
-
return None
|
| 282 |
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
def keyword_heatmap():
|
| 286 |
"""Keyword sentiment heatmap"""
|
|
@@ -467,7 +476,7 @@ def tfidf_analysis():
|
|
| 467 |
def plot_history():
|
| 468 |
"""Analysis history visualization"""
|
| 469 |
if len(history) < 2:
|
| 470 |
-
return None
|
| 471 |
|
| 472 |
indices = list(range(len(history)))
|
| 473 |
pos_probs = [item['pos_prob'] for item in history]
|
|
@@ -491,13 +500,18 @@ def plot_history():
|
|
| 491 |
ax2.grid(True, alpha=0.3)
|
| 492 |
|
| 493 |
plt.tight_layout()
|
| 494 |
-
return fig
|
| 495 |
|
| 496 |
def clear_history():
|
| 497 |
"""Clear analysis history"""
|
| 498 |
global history
|
|
|
|
| 499 |
history.clear()
|
| 500 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
|
| 502 |
# Enhanced example data
|
| 503 |
EXAMPLE_REVIEWS = [
|
|
@@ -592,17 +606,18 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Movie Sentiment Analyzer") as demo
|
|
| 592 |
with gr.Row():
|
| 593 |
refresh_btn = gr.Button("Refresh History", variant="secondary")
|
| 594 |
clear_btn = gr.Button("Clear History", variant="stop")
|
|
|
|
| 595 |
|
| 596 |
with gr.Row():
|
| 597 |
export_csv_btn = gr.Button("Export CSV", variant="secondary")
|
| 598 |
export_json_btn = gr.Button("Export JSON", variant="secondary")
|
| 599 |
|
| 600 |
-
with gr.Row():
|
| 601 |
-
csv_download = gr.File(label="CSV Download", visible=False)
|
| 602 |
-
json_download = gr.File(label="JSON Download", visible=False)
|
| 603 |
-
|
| 604 |
history_status = gr.Textbox(label="Status", interactive=False)
|
| 605 |
history_plot = gr.Plot(label="Historical Analysis Trends")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 606 |
|
| 607 |
# Event handlers
|
| 608 |
analyze_btn.click(
|
|
@@ -627,20 +642,33 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Movie Sentiment Analyzer") as demo
|
|
| 627 |
network_btn.click(cooccurrence_network, outputs=network_plot)
|
| 628 |
tfidf_btn.click(tfidf_analysis, outputs=tfidf_plot)
|
| 629 |
|
| 630 |
-
refresh_btn.click(
|
| 631 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 632 |
|
| 633 |
export_csv_btn.click(
|
| 634 |
export_history_csv,
|
| 635 |
-
outputs=
|
| 636 |
)
|
| 637 |
|
| 638 |
export_json_btn.click(
|
| 639 |
export_history_json,
|
| 640 |
-
outputs=
|
| 641 |
)
|
| 642 |
|
| 643 |
-
|
|
|
|
| 644 |
|
| 645 |
|
| 646 |
|
|
|
|
| 11 |
import json
|
| 12 |
import csv
|
| 13 |
import io
|
| 14 |
+
import tempfile
|
| 15 |
+
import os
|
| 16 |
from datetime import datetime
|
| 17 |
|
| 18 |
# Configuration
|
|
|
|
| 261 |
return content
|
| 262 |
|
| 263 |
def export_history_csv():
|
| 264 |
+
"""Export history to CSV file"""
|
| 265 |
if not history:
|
| 266 |
+
return None, "No history to export"
|
| 267 |
|
| 268 |
+
# Create temporary file
|
| 269 |
+
temp_file = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.csv', encoding='utf-8')
|
| 270 |
+
writer = csv.writer(temp_file)
|
| 271 |
writer.writerow(['Timestamp', 'Text', 'Sentiment', 'Confidence', 'Positive_Prob', 'Negative_Prob'])
|
| 272 |
|
| 273 |
for entry in history:
|
| 274 |
writer.writerow([
|
| 275 |
entry['timestamp'], entry['text'], entry['sentiment'],
|
| 276 |
+
f"{entry['confidence']:.4f}", f"{entry['pos_prob']:.4f}", f"{entry['neg_prob']:.4f}"
|
| 277 |
])
|
| 278 |
|
| 279 |
+
temp_file.close()
|
| 280 |
+
return temp_file.name, f"Exported {len(history)} entries to CSV"
|
| 281 |
|
| 282 |
def export_history_json():
|
| 283 |
+
"""Export history to JSON file"""
|
| 284 |
if not history:
|
| 285 |
+
return None, "No history to export"
|
| 286 |
|
| 287 |
+
# Create temporary file
|
| 288 |
+
temp_file = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json', encoding='utf-8')
|
| 289 |
+
json.dump(history, temp_file, indent=2, ensure_ascii=False)
|
| 290 |
+
temp_file.close()
|
| 291 |
+
|
| 292 |
+
return temp_file.name, f"Exported {len(history)} entries to JSON"
|
| 293 |
|
| 294 |
def keyword_heatmap():
|
| 295 |
"""Keyword sentiment heatmap"""
|
|
|
|
| 476 |
def plot_history():
|
| 477 |
"""Analysis history visualization"""
|
| 478 |
if len(history) < 2:
|
| 479 |
+
return None, f"History contains {len(history)} entries. Need at least 2 for visualization."
|
| 480 |
|
| 481 |
indices = list(range(len(history)))
|
| 482 |
pos_probs = [item['pos_prob'] for item in history]
|
|
|
|
| 500 |
ax2.grid(True, alpha=0.3)
|
| 501 |
|
| 502 |
plt.tight_layout()
|
| 503 |
+
return fig, f"History contains {len(history)} analyses"
|
| 504 |
|
| 505 |
def clear_history():
|
| 506 |
"""Clear analysis history"""
|
| 507 |
global history
|
| 508 |
+
count = len(history)
|
| 509 |
history.clear()
|
| 510 |
+
return f"Cleared {count} entries from history"
|
| 511 |
+
|
| 512 |
+
def get_history_status():
|
| 513 |
+
"""Get current history status"""
|
| 514 |
+
return f"History contains {len(history)} entries"
|
| 515 |
|
| 516 |
# Enhanced example data
|
| 517 |
EXAMPLE_REVIEWS = [
|
|
|
|
| 606 |
with gr.Row():
|
| 607 |
refresh_btn = gr.Button("Refresh History", variant="secondary")
|
| 608 |
clear_btn = gr.Button("Clear History", variant="stop")
|
| 609 |
+
status_btn = gr.Button("Check Status", variant="secondary")
|
| 610 |
|
| 611 |
with gr.Row():
|
| 612 |
export_csv_btn = gr.Button("Export CSV", variant="secondary")
|
| 613 |
export_json_btn = gr.Button("Export JSON", variant="secondary")
|
| 614 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
history_status = gr.Textbox(label="Status", interactive=False)
|
| 616 |
history_plot = gr.Plot(label="Historical Analysis Trends")
|
| 617 |
+
|
| 618 |
+
# File downloads
|
| 619 |
+
csv_file_output = gr.File(label="Download CSV", visible=True)
|
| 620 |
+
json_file_output = gr.File(label="Download JSON", visible=True)
|
| 621 |
|
| 622 |
# Event handlers
|
| 623 |
analyze_btn.click(
|
|
|
|
| 642 |
network_btn.click(cooccurrence_network, outputs=network_plot)
|
| 643 |
tfidf_btn.click(tfidf_analysis, outputs=tfidf_plot)
|
| 644 |
|
| 645 |
+
refresh_btn.click(
|
| 646 |
+
plot_history,
|
| 647 |
+
outputs=[history_plot, history_status]
|
| 648 |
+
)
|
| 649 |
+
|
| 650 |
+
clear_btn.click(
|
| 651 |
+
clear_history,
|
| 652 |
+
outputs=history_status
|
| 653 |
+
)
|
| 654 |
+
|
| 655 |
+
status_btn.click(
|
| 656 |
+
get_history_status,
|
| 657 |
+
outputs=history_status
|
| 658 |
+
)
|
| 659 |
|
| 660 |
export_csv_btn.click(
|
| 661 |
export_history_csv,
|
| 662 |
+
outputs=[csv_file_output, history_status]
|
| 663 |
)
|
| 664 |
|
| 665 |
export_json_btn.click(
|
| 666 |
export_history_json,
|
| 667 |
+
outputs=[json_file_output, history_status]
|
| 668 |
)
|
| 669 |
|
| 670 |
+
if __name__ == "__main__":
|
| 671 |
+
demo.launch(share=True)
|
| 672 |
|
| 673 |
|
| 674 |
|