File size: 43,095 Bytes
4e25610 285b200 4e25610 d7cd2ec 4e25610 d7cd2ec 4e25610 d7cd2ec 4e25610 866f669 4e25610 866f669 4e25610 866f669 4e25610 866f669 4e25610 866f669 4e25610 866f669 4e25610 d7cd2ec 4e25610 866f669 4e25610 d7cd2ec 4e25610 d7cd2ec 4e25610 866f669 4e25610 866f669 4e25610 d7cd2ec 4e25610 708f019 285b200 708f019 4e25610 708f019 4e25610 866f669 4e25610 d7cd2ec 4e25610 d7cd2ec 4e25610 866f669 4e25610 866f669 4e25610 866f669 4e25610 866f669 4e25610 866f669 4e25610 866f669 4e25610 866f669 4e25610 866f669 4e25610 866f669 4e25610 285b200 4e25610 866f669 285b200 4e25610 866f669 4e25610 866f669 708f019 4e25610 866f669 4e25610 866f669 4e25610 866f669 4e25610 866f669 4e25610 d7cd2ec 4e25610 866f669 4e25610 866f669 4e25610 866f669 4e25610 |
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 |
import torch
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import numpy as np
from wordcloud import WordCloud
from collections import Counter, defaultdict
import re
import json
import csv
import io
import tempfile
from datetime import datetime
import logging
from functools import lru_cache
from dataclasses import dataclass
from typing import List, Dict, Optional, Tuple
import nltk
from nltk.corpus import stopwords
import langdetect
import pandas as pd
# Configuration
@dataclass
class Config:
MAX_HISTORY_SIZE: int = 500
BATCH_SIZE_LIMIT: int = 30
MAX_TEXT_LENGTH: int = 512
CACHE_SIZE: int = 64
# Supported languages and models
SUPPORTED_LANGUAGES = {
'auto': 'Auto Detect',
'en': 'English',
'zh': 'Chinese',
'es': 'Spanish',
'fr': 'French',
'de': 'German',
'sv': 'Swedish'
}
MODELS = {
'en': "cardiffnlp/twitter-roberta-base-sentiment-latest",
'multilingual': "cardiffnlp/twitter-xlm-roberta-base-sentiment"
}
# Color themes
THEMES = {
'default': {'pos': '#4CAF50', 'neg': '#F44336', 'neu': '#FF9800'},
'ocean': {'pos': '#0077BE', 'neg': '#FF6B35', 'neu': '#00BCD4'},
'dark': {'pos': '#66BB6A', 'neg': '#EF5350', 'neu': '#FFA726'},
'rainbow': {'pos': '#9C27B0', 'neg': '#E91E63', 'neu': '#FF5722'}
}
config = Config()
# Logging setup
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Initialize NLTK
try:
nltk.download('stopwords', quiet=True)
nltk.download('punkt', quiet=True)
STOP_WORDS = set(stopwords.words('english'))
except:
STOP_WORDS = {'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by'}
class ModelManager:
"""Manages multiple language models"""
def __init__(self):
self.models = {}
self.tokenizers = {}
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self._load_default_model()
def _load_default_model(self):
"""Load the default English model"""
try:
model_name = config.MODELS['multilingual'] # Use multilingual as default
self.tokenizers['default'] = AutoTokenizer.from_pretrained(model_name)
self.models['default'] = AutoModelForSequenceClassification.from_pretrained(model_name)
self.models['default'].to(self.device)
logger.info(f"Default model loaded: {model_name}")
except Exception as e:
logger.error(f"Failed to load default model: {e}")
raise
def get_model(self, language='en'):
"""Get model for specific language"""
if language in ['en', 'auto'] or language not in config.SUPPORTED_LANGUAGES:
return self.models['default'], self.tokenizers['default']
return self.models['default'], self.tokenizers['default'] # Use multilingual for all
@staticmethod
def detect_language(text: str) -> str:
"""Detect text language properly"""
try:
# Use langdetect for all languages
detected = langdetect.detect(text)
# Map some common langdetect codes to our supported languages
language_mapping = {
'zh-cn': 'zh',
'zh-tw': 'zh'
}
detected = language_mapping.get(detected, detected)
return detected if detected in config.SUPPORTED_LANGUAGES else 'en'
except:
return 'en'
model_manager = ModelManager()
class HistoryManager:
"""Enhanced history manager with more features"""
def __init__(self):
self._history = []
def add_entry(self, entry: Dict):
self._history.append(entry)
if len(self._history) > config.MAX_HISTORY_SIZE:
self._history = self._history[-config.MAX_HISTORY_SIZE:]
def add_batch_entries(self, entries: List[Dict]):
"""Add multiple entries at once"""
for entry in entries:
self.add_entry(entry)
def get_history(self) -> List[Dict]:
return self._history.copy()
def get_recent_history(self, n: int = 10) -> List[Dict]:
"""Get n most recent entries"""
return self._history[-n:] if self._history else []
def filter_history(self, sentiment: str = None, language: str = None,
min_confidence: float = None) -> List[Dict]:
"""Filter history by criteria"""
filtered = self._history
if sentiment:
filtered = [h for h in filtered if h['sentiment'] == sentiment]
if language:
filtered = [h for h in filtered if h.get('language', 'en') == language]
if min_confidence:
filtered = [h for h in filtered if h['confidence'] >= min_confidence]
return filtered
def clear(self) -> int:
count = len(self._history)
self._history.clear()
return count
def get_stats(self) -> Dict:
if not self._history:
return {}
sentiments = [item['sentiment'] for item in self._history]
confidences = [item['confidence'] for item in self._history]
languages = [item.get('language', 'en') for item in self._history]
return {
'total_analyses': len(self._history),
'positive_count': sentiments.count('Positive'),
'negative_count': sentiments.count('Negative'),
'neutral_count': sentiments.count('Neutral'),
'avg_confidence': np.mean(confidences),
'max_confidence': np.max(confidences),
'min_confidence': np.min(confidences),
'languages_detected': len(set(languages)),
'most_common_language': Counter(languages).most_common(1)[0][0] if languages else 'en',
'avg_text_length': np.mean([len(item.get('full_text', '')) for item in self._history])
}
history_manager = HistoryManager()
class TextProcessor:
"""Enhanced text processing"""
@staticmethod
@lru_cache(maxsize=config.CACHE_SIZE)
def clean_text(text: str, remove_punctuation: bool = True, remove_numbers: bool = False) -> str:
"""Clean text with options"""
text = text.lower().strip()
if remove_numbers:
text = re.sub(r'\d+', '', text)
if remove_punctuation:
text = re.sub(r'[^\w\s]', '', text)
words = text.split()
cleaned_words = [w for w in words if w not in STOP_WORDS and len(w) > 2]
return ' '.join(cleaned_words)
@staticmethod
def extract_keywords(text: str, top_k: int = 5) -> List[str]:
"""Extract key words from text"""
# For Chinese text, extract characters
if re.search(r'[\u4e00-\u9fff]', text):
words = re.findall(r'[\u4e00-\u9fff]+', text)
all_chars = ''.join(words)
char_freq = Counter(all_chars)
return [char for char, _ in char_freq.most_common(top_k)]
else:
# For other languages, use word-based extraction
cleaned = TextProcessor.clean_text(text)
words = cleaned.split()
word_freq = Counter(words)
return [word for word, _ in word_freq.most_common(top_k)]
@staticmethod
def parse_batch_input(text: str) -> List[str]:
"""Parse batch input from textarea"""
lines = text.strip().split('\n')
return [line.strip() for line in lines if line.strip()]
class SentimentAnalyzer:
"""Enhanced sentiment analysis"""
@staticmethod
def analyze_text(text: str, language: str = 'auto', preprocessing_options: Dict = None) -> Dict:
"""Analyze single text with language support"""
if not text.strip():
raise ValueError("Empty text provided")
# Detect language if auto
if language == 'auto':
detected_lang = model_manager.detect_language(text)
else:
detected_lang = language
# Get appropriate model
model, tokenizer = model_manager.get_model(detected_lang)
# Preprocessing options - don't clean Chinese text
options = preprocessing_options or {}
processed_text = text
if options.get('clean_text', False) and not re.search(r'[\u4e00-\u9fff]', text):
processed_text = TextProcessor.clean_text(
text,
options.get('remove_punctuation', True),
options.get('remove_numbers', False)
)
try:
# Tokenize and analyze
inputs = tokenizer(processed_text, return_tensors="pt", padding=True,
truncation=True, max_length=config.MAX_TEXT_LENGTH).to(model_manager.device)
with torch.no_grad():
outputs = model(**inputs)
probs = torch.nn.functional.softmax(outputs.logits, dim=-1).cpu().numpy()[0]
# Handle different model outputs
if len(probs) == 3: # negative, neutral, positive
sentiment_idx = np.argmax(probs)
sentiment_labels = ['Negative', 'Neutral', 'Positive']
sentiment = sentiment_labels[sentiment_idx]
confidence = float(probs[sentiment_idx])
result = {
'sentiment': sentiment,
'confidence': confidence,
'neg_prob': float(probs[0]),
'neu_prob': float(probs[1]),
'pos_prob': float(probs[2]),
'has_neutral': True
}
else: # negative, positive
pred = np.argmax(probs)
sentiment = "Positive" if pred == 1 else "Negative"
confidence = float(probs[pred])
result = {
'sentiment': sentiment,
'confidence': confidence,
'neg_prob': float(probs[0]),
'pos_prob': float(probs[1]),
'neu_prob': 0.0,
'has_neutral': False
}
# Add metadata
result.update({
'language': detected_lang,
'keywords': TextProcessor.extract_keywords(text),
'word_count': len(text.split()),
'char_count': len(text)
})
return result
except Exception as e:
logger.error(f"Analysis failed: {e}")
raise
@staticmethod
def analyze_batch(texts: List[str], language: str = 'auto',
preprocessing_options: Dict = None) -> List[Dict]:
"""Analyze multiple texts"""
results = []
for i, text in enumerate(texts):
try:
result = SentimentAnalyzer.analyze_text(text, language, preprocessing_options)
result['batch_index'] = i
results.append(result)
except Exception as e:
# Add error result
results.append({
'sentiment': 'Error',
'confidence': 0.0,
'error': str(e),
'batch_index': i,
'text': text
})
return results
class PlotlyVisualizer:
"""Enhanced visualizations with Plotly"""
@staticmethod
def create_sentiment_gauge(result: Dict, theme: str = 'default') -> go.Figure:
"""Create an animated sentiment gauge"""
colors = config.THEMES[theme]
if result['has_neutral']:
# Three-way gauge
fig = go.Figure(go.Indicator(
mode = "gauge+number+delta",
value = result['pos_prob'] * 100,
domain = {'x': [0, 1], 'y': [0, 1]},
title = {'text': f"Sentiment: {result['sentiment']}"},
delta = {'reference': 50},
gauge = {
'axis': {'range': [None, 100]},
'bar': {'color': colors['pos'] if result['sentiment'] == 'Positive' else colors['neg']},
'steps': [
{'range': [0, 33], 'color': colors['neg']},
{'range': [33, 67], 'color': colors['neu']},
{'range': [67, 100], 'color': colors['pos']}
],
'threshold': {
'line': {'color': "red", 'width': 4},
'thickness': 0.75,
'value': 90
}
}
))
else:
# Two-way gauge
fig = go.Figure(go.Indicator(
mode = "gauge+number",
value = result['confidence'] * 100,
domain = {'x': [0, 1], 'y': [0, 1]},
title = {'text': f"Confidence: {result['sentiment']}"},
gauge = {
'axis': {'range': [None, 100]},
'bar': {'color': colors['pos'] if result['sentiment'] == 'Positive' else colors['neg']},
'steps': [
{'range': [0, 50], 'color': "lightgray"},
{'range': [50, 100], 'color': "gray"}
]
}
))
fig.update_layout(height=400, font={'size': 16})
return fig
@staticmethod
def create_probability_bars(result: Dict, theme: str = 'default') -> go.Figure:
"""Create probability bar chart"""
colors = config.THEMES[theme]
if result['has_neutral']:
labels = ['Negative', 'Neutral', 'Positive']
values = [result['neg_prob'], result['neu_prob'], result['pos_prob']]
bar_colors = [colors['neg'], colors['neu'], colors['pos']]
else:
labels = ['Negative', 'Positive']
values = [result['neg_prob'], result['pos_prob']]
bar_colors = [colors['neg'], colors['pos']]
fig = go.Figure(data=[
go.Bar(x=labels, y=values, marker_color=bar_colors, text=[f'{v:.3f}' for v in values])
])
fig.update_traces(texttemplate='%{text}', textposition='outside')
fig.update_layout(
title="Sentiment Probabilities",
yaxis_title="Probability",
height=400,
showlegend=False
)
return fig
@staticmethod
def create_batch_summary(results: List[Dict], theme: str = 'default') -> go.Figure:
"""Create batch analysis summary"""
colors = config.THEMES[theme]
# Count sentiments
sentiments = [r['sentiment'] for r in results if 'sentiment' in r]
sentiment_counts = Counter(sentiments)
# Create pie chart
fig = go.Figure(data=[go.Pie(
labels=list(sentiment_counts.keys()),
values=list(sentiment_counts.values()),
marker_colors=[colors.get(s.lower()[:3], '#999999') for s in sentiment_counts.keys()],
textinfo='label+percent',
hole=0.3
)])
fig.update_layout(
title=f"Batch Analysis Summary ({len(results)} texts)",
height=400
)
return fig
@staticmethod
def create_confidence_distribution(results: List[Dict]) -> go.Figure:
"""Create confidence distribution plot"""
confidences = [r['confidence'] for r in results if 'confidence' in r and r['sentiment'] != 'Error']
if not confidences:
return go.Figure()
fig = go.Figure(data=[go.Histogram(
x=confidences,
nbinsx=20,
marker_color='skyblue',
opacity=0.7
)])
fig.update_layout(
title="Confidence Distribution",
xaxis_title="Confidence Score",
yaxis_title="Frequency",
height=400
)
return fig
@staticmethod
def create_history_dashboard(history: List[Dict]) -> go.Figure:
"""Create comprehensive history dashboard"""
if len(history) < 2:
return go.Figure()
# Create subplots
fig = make_subplots(
rows=2, cols=2,
subplot_titles=['Sentiment Timeline', 'Confidence Distribution',
'Language Distribution', 'Sentiment Summary'],
specs=[[{"secondary_y": False}, {"secondary_y": False}],
[{"type": "pie"}, {"type": "bar"}]]
)
# Extract data
indices = list(range(len(history)))
pos_probs = [item['pos_prob'] for item in history]
confidences = [item['confidence'] for item in history]
sentiments = [item['sentiment'] for item in history]
languages = [item.get('language', 'en') for item in history]
# Sentiment timeline
colors = ['#4CAF50' if s == 'Positive' else '#F44336' for s in sentiments]
fig.add_trace(
go.Scatter(x=indices, y=pos_probs, mode='lines+markers',
marker=dict(color=colors, size=8),
name='Positive Probability'),
row=1, col=1
)
# Confidence distribution
fig.add_trace(
go.Histogram(x=confidences, nbinsx=10, name='Confidence'),
row=1, col=2
)
# Language distribution
lang_counts = Counter(languages)
fig.add_trace(
go.Pie(labels=list(lang_counts.keys()), values=list(lang_counts.values()),
name="Languages"),
row=2, col=1
)
# Sentiment summary
sent_counts = Counter(sentiments)
fig.add_trace(
go.Bar(x=list(sent_counts.keys()), y=list(sent_counts.values()),
marker_color=['#4CAF50' if k == 'Positive' else '#F44336' for k in sent_counts.keys()]),
row=2, col=2
)
fig.update_layout(height=800, showlegend=False)
return fig
# Main application functions
def analyze_single_text(text: str, language: str, theme: str, clean_text: bool,
remove_punct: bool, remove_nums: bool):
"""Enhanced single text analysis"""
try:
if not text.strip():
return "Please enter text", None, None
# Map display names back to language codes
language_map = {
'Auto Detect': 'auto',
'English': 'en',
'Chinese': 'zh',
'Spanish': 'es',
'French': 'fr',
'German': 'de',
'Swedish': 'sv'
}
language_code = language_map.get(language, 'auto')
preprocessing_options = {
'clean_text': clean_text,
'remove_punctuation': remove_punct,
'remove_numbers': remove_nums
}
result = SentimentAnalyzer.analyze_text(text, language_code, preprocessing_options)
# Add to history
history_entry = {
'text': text[:100] + '...' if len(text) > 100 else text,
'full_text': text,
'sentiment': result['sentiment'],
'confidence': result['confidence'],
'pos_prob': result['pos_prob'],
'neg_prob': result['neg_prob'],
'neu_prob': result.get('neu_prob', 0),
'language': result['language'],
'timestamp': datetime.now().isoformat(),
'analysis_type': 'single'
}
history_manager.add_entry(history_entry)
# Create visualizations
gauge_fig = PlotlyVisualizer.create_sentiment_gauge(result, theme)
bars_fig = PlotlyVisualizer.create_probability_bars(result, theme)
# Create info text
info_text = f"""
**Analysis Results:**
- **Sentiment:** {result['sentiment']} ({result['confidence']:.3f} confidence)
- **Language:** {result['language'].upper()}
- **Keywords:** {', '.join(result['keywords'])}
- **Stats:** {result['word_count']} words, {result['char_count']} characters
"""
return info_text, gauge_fig, bars_fig
except Exception as e:
logger.error(f"Analysis failed: {e}")
return f"Error: {str(e)}", None, None
def analyze_batch_texts(batch_text: str, language: str, theme: str,
clean_text: bool, remove_punct: bool, remove_nums: bool):
"""Batch text analysis"""
try:
if not batch_text.strip():
return "Please enter texts (one per line)", None, None, None
# Parse batch input
texts = TextProcessor.parse_batch_input(batch_text)
if len(texts) > config.BATCH_SIZE_LIMIT:
return f"Too many texts. Maximum {config.BATCH_SIZE_LIMIT} allowed.", None, None, None
if not texts:
return "No valid texts found", None, None, None
# Map display names back to language codes
language_map = {
'Auto Detect': 'auto',
'English': 'en',
'Chinese': 'zh',
'Spanish': 'es',
'French': 'fr',
'German': 'de',
'Swedish': 'sv'
}
language_code = language_map.get(language, 'auto')
preprocessing_options = {
'clean_text': clean_text,
'remove_punctuation': remove_punct,
'remove_numbers': remove_nums
}
# Analyze all texts
results = SentimentAnalyzer.analyze_batch(texts, language_code, preprocessing_options)
# Add to history
batch_entries = []
for i, (text, result) in enumerate(zip(texts, results)):
if 'error' not in result:
entry = {
'text': text[:100] + '...' if len(text) > 100 else text,
'full_text': text,
'sentiment': result['sentiment'],
'confidence': result['confidence'],
'pos_prob': result['pos_prob'],
'neg_prob': result['neg_prob'],
'neu_prob': result.get('neu_prob', 0),
'language': result['language'],
'timestamp': datetime.now().isoformat(),
'analysis_type': 'batch',
'batch_index': i
}
batch_entries.append(entry)
history_manager.add_batch_entries(batch_entries)
# Create visualizations
summary_fig = PlotlyVisualizer.create_batch_summary(results, theme)
confidence_fig = PlotlyVisualizer.create_confidence_distribution(results)
# Create results table
df_data = []
for i, (text, result) in enumerate(zip(texts, results)):
if 'error' in result:
df_data.append({
'Index': i+1,
'Text': text[:50] + '...' if len(text) > 50 else text,
'Sentiment': 'Error',
'Confidence': 0.0,
'Language': 'Unknown',
'Error': result['error']
})
else:
df_data.append({
'Index': i+1,
'Text': text[:50] + '...' if len(text) > 50 else text,
'Sentiment': result['sentiment'],
'Confidence': f"{result['confidence']:.3f}",
'Language': result['language'].upper(),
'Keywords': ', '.join(result['keywords'][:3])
})
df = pd.DataFrame(df_data)
# Summary info
successful_results = [r for r in results if 'error' not in r]
error_count = len(results) - len(successful_results)
if successful_results:
sentiment_counts = Counter([r['sentiment'] for r in successful_results])
avg_confidence = np.mean([r['confidence'] for r in successful_results])
summary_text = f"""
**Batch Analysis Summary:**
- **Total Texts:** {len(texts)}
- **Successful:** {len(successful_results)}
- **Errors:** {error_count}
- **Average Confidence:** {avg_confidence:.3f}
- **Sentiments:** {dict(sentiment_counts)}
"""
else:
summary_text = f"All {len(texts)} texts failed to analyze."
return summary_text, df, summary_fig, confidence_fig
except Exception as e:
logger.error(f"Batch analysis failed: {e}")
return f"Error: {str(e)}", None, None, None
def analyze_advanced_text(text: str, language: str, theme: str, include_keywords: bool,
keyword_count: int, min_confidence: float):
"""Advanced analysis with additional features"""
try:
if not text.strip():
return "Please enter text", None, None
# Map display names back to language codes
language_map = {
'Auto Detect': 'auto',
'English': 'en',
'Chinese': 'zh',
'Spanish': 'es',
'French': 'fr',
'German': 'de',
'Swedish': 'sv'
}
language_code = language_map.get(language, 'auto')
result = SentimentAnalyzer.analyze_text(text, language_code)
# Advanced keyword extraction
if include_keywords:
result['keywords'] = TextProcessor.extract_keywords(text, keyword_count)
# Confidence filtering
meets_confidence = result['confidence'] >= min_confidence
# Add to history
history_entry = {
'text': text[:100] + '...' if len(text) > 100 else text,
'full_text': text,
'sentiment': result['sentiment'],
'confidence': result['confidence'],
'pos_prob': result['pos_prob'],
'neg_prob': result['neg_prob'],
'neu_prob': result.get('neu_prob', 0),
'language': result['language'],
'timestamp': datetime.now().isoformat(),
'analysis_type': 'advanced',
'meets_confidence_threshold': meets_confidence
}
history_manager.add_entry(history_entry)
# Create visualizations
gauge_fig = PlotlyVisualizer.create_sentiment_gauge(result, theme)
bars_fig = PlotlyVisualizer.create_probability_bars(result, theme)
# Create detailed info text
confidence_status = "✅ High Confidence" if meets_confidence else "⚠️ Low Confidence"
info_text = f"""
**Advanced Analysis Results:**
- **Sentiment:** {result['sentiment']} ({result['confidence']:.3f} confidence)
- **Confidence Status:** {confidence_status}
- **Language:** {result['language'].upper()}
- **Text Statistics:**
- Words: {result['word_count']}
- Characters: {result['char_count']}
- Average word length: {result['char_count']/max(result['word_count'], 1):.1f}
"""
if include_keywords:
info_text += f"\n- **Top Keywords:** {', '.join(result['keywords'])}"
if not meets_confidence:
info_text += f"\n\n⚠️ **Note:** Confidence ({result['confidence']:.3f}) is below threshold ({min_confidence})"
return info_text, gauge_fig, bars_fig
except Exception as e:
logger.error(f"Advanced analysis failed: {e}")
return f"Error: {str(e)}", None, None
def get_history_stats():
"""Get enhanced history statistics"""
stats = history_manager.get_stats()
if not stats:
return "No analysis history available"
return f"""
**Comprehensive History Statistics:**
**Analysis Counts:**
- Total Analyses: {stats['total_analyses']}
- Positive: {stats['positive_count']}
- Negative: {stats['negative_count']}
- Neutral: {stats['neutral_count']}
**Confidence Metrics:**
- Average Confidence: {stats['avg_confidence']:.3f}
- Highest Confidence: {stats['max_confidence']:.3f}
- Lowest Confidence: {stats['min_confidence']:.3f}
**Language Statistics:**
- Languages Detected: {stats['languages_detected']}
- Most Common Language: {stats['most_common_language'].upper()}
**Text Statistics:**
- Average Text Length: {stats['avg_text_length']:.1f} characters
"""
def filter_history_display(sentiment_filter: str, language_filter: str, min_confidence: float):
"""Display filtered history"""
# Convert filters
sentiment = sentiment_filter if sentiment_filter != "All" else None
language = language_filter.lower() if language_filter != "All" else None
filtered_history = history_manager.filter_history(
sentiment=sentiment,
language=language,
min_confidence=min_confidence if min_confidence > 0 else None
)
if not filtered_history:
return "No entries match the filter criteria", None
# Create DataFrame for display
df_data = []
for entry in filtered_history[-20:]: # Show last 20 entries
df_data.append({
'Timestamp': entry['timestamp'][:16], # YYYY-MM-DD HH:MM
'Text': entry['text'],
'Sentiment': entry['sentiment'],
'Confidence': f"{entry['confidence']:.3f}",
'Language': entry['language'].upper(),
'Type': entry.get('analysis_type', 'single')
})
df = pd.DataFrame(df_data)
summary = f"""
**Filtered Results:**
- Found {len(filtered_history)} entries matching criteria
- Showing most recent {min(20, len(filtered_history))} entries
"""
return summary, df
def plot_history_dashboard():
"""Create history dashboard"""
history = history_manager.get_history()
if len(history) < 2:
return None, "Need at least 2 analyses for dashboard"
fig = PlotlyVisualizer.create_history_dashboard(history)
return fig, f"Dashboard showing {len(history)} analyses"
def export_history_csv():
"""Export history to CSV"""
history = history_manager.get_history()
if not history:
return None, "No history to export"
try:
df = pd.DataFrame(history)
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.csv', mode='w')
df.to_csv(temp_file.name, index=False)
return temp_file.name, f"Exported {len(history)} entries to CSV"
except Exception as e:
return None, f"Export failed: {str(e)}"
def export_history_excel():
"""Export history to Excel"""
history = history_manager.get_history()
if not history:
return None, "No history to export"
try:
df = pd.DataFrame(history)
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.xlsx')
df.to_excel(temp_file.name, index=False)
return temp_file.name, f"Exported {len(history)} entries to Excel"
except Exception as e:
return None, f"Export failed: {str(e)}"
def clear_all_history():
"""Clear analysis history"""
count = history_manager.clear()
return f"Cleared {count} entries from history"
def get_recent_analyses():
"""Get recent analysis summary"""
recent = history_manager.get_recent_history(10)
if not recent:
return "No recent analyses available"
summary_text = "**Recent Analyses (Last 10):**\n\n"
for i, entry in enumerate(recent, 1):
summary_text += f"{i}. **{entry['sentiment']}** ({entry['confidence']:.3f}) - {entry['text']}\n"
return summary_text
# Sample data
SAMPLE_TEXTS = [
# Auto Detect
["The film had its moments, but overall it felt a bit too long and lacked emotional depth."],
# English
["I was completely blown away by the movie — the performances were raw and powerful, and the story stayed with me long after the credits rolled."],
# Chinese
["这部电影节奏拖沓,剧情老套,完全没有让我产生任何共鸣,是一次失望的观影体验。"],
# Spanish
["Una obra maestra del cine contemporáneo, con actuaciones sobresalientes, un guion bien escrito y una dirección impecable."],
# French
["Je m'attendais à beaucoup mieux. Le scénario était confus, les dialogues ennuyeux, et je me suis presque endormi au milieu du film."],
# German
["Der Film war ein emotionales Erlebnis mit großartigen Bildern, einem mitreißenden Soundtrack und einer Geschichte, die zum Nachdenken anregt."],
# Swedish
["Filmen var en besvikelse – tråkig handling, överdrivet skådespeleri och ett slut som inte gav något avslut alls."]
]
BATCH_SAMPLE = """I love this product! It works perfectly.
The service was terrible and slow.
Not sure if I like it or not.
Amazing quality and fast delivery!
Could be better, but it's okay."""
# Gradio Interface
with gr.Blocks(theme=gr.themes.Soft(), title="Advanced Multilingual Sentiment Analyzer") as demo:
gr.Markdown("# 🎭 Advanced Multilingual Sentiment Analyzer")
gr.Markdown("Comprehensive sentiment analysis with batch processing, advanced analytics, and multilingual support")
with gr.Tab("📝 Single Analysis"):
with gr.Row():
with gr.Column(scale=2):
text_input = gr.Textbox(
label="Text to Analyze",
placeholder="Enter your text here... (supports multiple languages)",
lines=4
)
with gr.Row():
language_select = gr.Dropdown(
choices=['Auto Detect', 'English', 'Chinese', 'Spanish', 'French', 'German', 'Swedish'],
value='Auto Detect',
label="Language"
)
theme_select = gr.Dropdown(
choices=list(config.THEMES.keys()),
value='default',
label="Theme"
)
with gr.Row():
clean_text = gr.Checkbox(label="Clean Text", value=False)
remove_punct = gr.Checkbox(label="Remove Punctuation", value=True)
remove_nums = gr.Checkbox(label="Remove Numbers", value=False)
analyze_btn = gr.Button("🔍 Analyze", variant="primary", size="lg")
gr.Examples(
examples=SAMPLE_TEXTS,
inputs=text_input,
label="Sample Texts (Multiple Languages)"
)
with gr.Column(scale=1):
result_info = gr.Markdown("Enter text and click Analyze")
with gr.Row():
gauge_plot = gr.Plot(label="Sentiment Gauge")
bars_plot = gr.Plot(label="Probability Distribution")
with gr.Tab("📊 Batch Analysis"):
with gr.Row():
with gr.Column(scale=2):
batch_input = gr.Textbox(
label="Batch Text Input (One text per line)",
placeholder="Enter multiple texts, one per line...",
lines=8
)
with gr.Row():
batch_language = gr.Dropdown(
choices=['Auto Detect', 'English', 'Chinese', 'Spanish', 'French', 'German', 'Swedish'],
value='Auto Detect',
label="Language"
)
batch_theme = gr.Dropdown(
choices=list(config.THEMES.keys()),
value='default',
label="Theme"
)
with gr.Row():
batch_clean = gr.Checkbox(label="Clean Text", value=False)
batch_remove_punct = gr.Checkbox(label="Remove Punctuation", value=True)
batch_remove_nums = gr.Checkbox(label="Remove Numbers", value=False)
batch_analyze_btn = gr.Button("🔍 Analyze Batch", variant="primary", size="lg")
gr.Examples(
examples=[[BATCH_SAMPLE]],
inputs=batch_input,
label="Sample Batch Input"
)
with gr.Column(scale=1):
batch_summary = gr.Markdown("Enter texts and click Analyze Batch")
with gr.Row():
batch_results_table = gr.DataFrame(
label="Detailed Results",
interactive=False
)
with gr.Row():
batch_summary_plot = gr.Plot(label="Sentiment Summary")
batch_confidence_plot = gr.Plot(label="Confidence Distribution")
with gr.Tab("🔬 Advanced Analysis"):
with gr.Row():
with gr.Column(scale=2):
advanced_input = gr.Textbox(
label="Text for Advanced Analysis",
placeholder="Enter text for detailed analysis...",
lines=4
)
with gr.Row():
advanced_language = gr.Dropdown(
choices=['Auto Detect', 'English', 'Chinese', 'Spanish', 'French', 'German', 'Swedish'],
value='Auto Detect',
label="Language"
)
advanced_theme = gr.Dropdown(
choices=list(config.THEMES.keys()),
value='default',
label="Theme"
)
with gr.Row():
include_keywords = gr.Checkbox(label="Extract Keywords", value=True)
keyword_count = gr.Slider(
minimum=3,
maximum=10,
value=5,
step=1,
label="Number of Keywords"
)
min_confidence_slider = gr.Slider(
minimum=0.0,
maximum=1.0,
value=0.7,
step=0.1,
label="Minimum Confidence Threshold"
)
advanced_analyze_btn = gr.Button("🔬 Advanced Analyze", variant="primary", size="lg")
with gr.Column(scale=1):
advanced_result_info = gr.Markdown("Configure settings and click Advanced Analyze")
with gr.Row():
advanced_gauge_plot = gr.Plot(label="Sentiment Gauge")
advanced_bars_plot = gr.Plot(label="Probability Distribution")
with gr.Tab("📈 History & Analytics"):
with gr.Row():
with gr.Column():
gr.Markdown("### 📊 Statistics")
stats_btn = gr.Button("📈 Get Statistics")
recent_btn = gr.Button("🕒 Recent Analyses")
stats_output = gr.Markdown("Click 'Get Statistics' to view analysis history")
with gr.Column():
gr.Markdown("### 🔍 Filter History")
with gr.Row():
sentiment_filter = gr.Dropdown(
choices=["All", "Positive", "Negative", "Neutral"],
value="All",
label="Filter by Sentiment"
)
language_filter = gr.Dropdown(
choices=["All", "English", "Chinese", "Spanish", "French", "German", "Swedish"],
value="All",
label="Filter by Language"
)
confidence_filter = gr.Slider(
minimum=0.0,
maximum=1.0,
value=0.0,
step=0.1,
label="Minimum Confidence"
)
filter_btn = gr.Button("🔍 Filter History")
with gr.Row():
dashboard_btn = gr.Button("📊 View Dashboard")
clear_btn = gr.Button("🗑️ Clear History", variant="stop")
with gr.Row():
export_csv_btn = gr.Button("📄 Export CSV")
export_excel_btn = gr.Button("📊 Export Excel")
dashboard_plot = gr.Plot(label="Analytics Dashboard")
with gr.Row():
filtered_results = gr.Markdown("Use filters to view specific entries")
filtered_table = gr.DataFrame(label="Filtered History", interactive=False)
csv_file = gr.File(label="Download CSV Report")
excel_file = gr.File(label="Download Excel Report")
history_status = gr.Textbox(label="Status", interactive=False)
# Event handlers
# Single Analysis
analyze_btn.click(
analyze_single_text,
inputs=[text_input, language_select, theme_select, clean_text, remove_punct, remove_nums],
outputs=[result_info, gauge_plot, bars_plot]
)
# Batch Analysis
batch_analyze_btn.click(
analyze_batch_texts,
inputs=[batch_input, batch_language, batch_theme, batch_clean, batch_remove_punct, batch_remove_nums],
outputs=[batch_summary, batch_results_table, batch_summary_plot, batch_confidence_plot]
)
# Advanced Analysis
advanced_analyze_btn.click(
analyze_advanced_text,
inputs=[advanced_input, advanced_language, advanced_theme, include_keywords, keyword_count, min_confidence_slider],
outputs=[advanced_result_info, advanced_gauge_plot, advanced_bars_plot]
)
# History & Analytics
stats_btn.click(
get_history_stats,
outputs=stats_output
)
recent_btn.click(
get_recent_analyses,
outputs=stats_output
)
filter_btn.click(
filter_history_display,
inputs=[sentiment_filter, language_filter, confidence_filter],
outputs=[filtered_results, filtered_table]
)
dashboard_btn.click(
plot_history_dashboard,
outputs=[dashboard_plot, history_status]
)
export_csv_btn.click(
export_history_csv,
outputs=[csv_file, history_status]
)
export_excel_btn.click(
export_history_excel,
outputs=[excel_file, history_status]
)
clear_btn.click(
clear_all_history,
outputs=history_status
)
if __name__ == "__main__":
demo.launch(share=True) |