Spaces:
Sleeping
Sleeping
File size: 10,372 Bytes
14d6a4f |
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 |
import os
import spacy
import stanza
# ===============================
# π§ Safe SpaCy + Stanza Downloads
# ===============================
def safe_load_spacy():
try:
return spacy.load("en_core_web_trf")
except OSError:
try:
return spacy.load("en_core_web_sm")
except OSError:
os.system("python -m spacy download en_core_web_sm")
return spacy.load("en_core_web_sm")
# β
Initialize English SpaCy safely
nlp_en = safe_load_spacy()
# Ensure Stanza models exist
stanza_dir = os.path.expanduser("~/.stanza_resources")
if not os.path.exists(stanza_dir):
stanza.download('hi')
stanza.download('ta')
# ===============================
# 1οΈβ£ Imports
# ===============================
import pandas as pd
import re
import docx
from collections import Counter
import stanza
from transformers import pipeline
import torch
from langdetect import detect
import streamlit as st
import io
# ===============================
# 2οΈβ£ Pre-download Stanza models
# ===============================
stanza.download('hi')
stanza.download('ta')
# ===============================
# 3οΈβ£ Initialize Stanza for Hindi/Tamil
# ===============================
nlp_hi = stanza.Pipeline('hi', processors='tokenize,pos', use_gpu=torch.cuda.is_available())
nlp_ta = stanza.Pipeline('ta', processors='tokenize,pos', use_gpu=torch.cuda.is_available())
# ===============================
# 4οΈβ£ Language-Aware Pipeline Loader
# ===============================
def load_pipelines(language_code):
lang = language_code.upper()
device = 0 if torch.cuda.is_available() else -1
st.write(f"π Language detected: {lang}")
st.write(f"Device set to use {'cuda:0' if device == 0 else 'cpu'}")
# Emotion model
if lang == "EN":
emo_model = "SamLowe/roberta-base-go_emotions"
elif lang in ["HI", "TA"]:
emo_model = "bhadresh-savani/bert-base-go-emotion"
else:
emo_model = "SamLowe/roberta-base-go_emotions"
emotion_pipeline = pipeline(
"text-classification",
model=emo_model,
tokenizer=emo_model,
return_all_scores=True,
device=device
)
# Sentiment model
if lang == "EN":
sent_model = "distilbert-base-uncased-finetuned-sst-2-english"
else:
sent_model = "cardiffnlp/twitter-xlm-roberta-base-sentiment-multilingual"
sentiment_pipeline = pipeline(
"text-classification",
model=sent_model,
tokenizer=sent_model,
return_all_scores=True,
device=device
)
return emotion_pipeline, sentiment_pipeline
# ===============================
# 5οΈβ£ Read DOCX and split articles
# ===============================
def read_and_split_articles(file_path):
doc = docx.Document(file_path)
paragraphs = [para.text.strip() for para in doc.paragraphs if para.text.strip()]
return paragraphs # β
Each docx paragraph separately
# ===============================
# 6οΈβ£ Utility β Filter Neutral
# ===============================
def filter_neutral(emotion_results, neutral_threshold=0.75):
scores = {r["label"]: round(r["score"], 3)
for r in sorted(emotion_results, key=lambda x: x["score"], reverse=True)}
if "neutral" in scores and scores["neutral"] > neutral_threshold:
scores.pop("neutral")
return scores
# ===============================
# 7οΈβ£ Sentence Splitter
# ===============================
def split_sentences(text, lang):
if lang == "hi":
sentences = re.split(r'ΰ₯€', text)
elif lang == "ta":
sentences = re.split(r'\.', text)
else:
doc = nlp_en(text)
sentences = [sent.text.strip() for sent in doc.sents]
return [s.strip() for s in sentences if s.strip()]
# ===============================
# 8οΈβ£ PoS Tagger
# ===============================
def get_pos_tags(sentence, lang):
if lang == "en":
doc = nlp_en(sentence)
return [(token.text, token.pos_) for token in doc]
elif lang == "hi":
doc = nlp_hi(sentence)
return [(word.text, word.upos) for sent in doc.sentences for word in sent.words]
elif lang == "ta":
doc = nlp_ta(sentence)
return [(word.text, word.upos) for sent in doc.sentences for word in sent.words]
else:
return []
# ===============================
# 9οΈβ£ Analysis Function
# ===============================
def analyze_article(article_text, lang, emotion_pipeline, sentiment_pipeline, normalize_paragraphs):
results_summary = []
export_rows = []
para_counters = []
article_counter = Counter()
paragraphs = [p.strip() for p in article_text.split("\n\n") if p.strip()]
# -------------------------------
# β
Weighted Overall results
weighted_scores = {}
total_length = 0
all_sentiments = []
for para in paragraphs:
sentences = split_sentences(para, lang[:2])
for sentence in sentences:
emo_results = emotion_pipeline(sentence[:512])[0]
filtered = filter_neutral(emo_results)
length = len(sentence.split())
total_length += length
for emo, score in filtered.items():
weighted_scores[emo] = weighted_scores.get(emo, 0) + score * length
sentiment_results = sentiment_pipeline(sentence[:512])[0]
all_sentiments.append(max(sentiment_results, key=lambda x: x["score"]))
if total_length > 0:
weighted_scores = {emo: round(val / total_length, 3) for emo, val in weighted_scores.items()}
overall_sentiment = max(all_sentiments, key=lambda x: x["score"]) if all_sentiments else {}
st.subheader("π OVERALL (Weighted)")
st.write("Emotions β", weighted_scores)
st.write("Sentiment β", overall_sentiment)
export_rows.append({
"Type": "Overall",
"Text": "Weighted across article",
"Emotions": weighted_scores,
"Sentiment": overall_sentiment
})
# -------------------------------
# Paragraph-level
for p_idx, para in enumerate(paragraphs, start=1):
para_counter = Counter()
sentences = split_sentences(para, lang[:2])
for sentence in sentences:
results = emotion_pipeline(sentence[:512])[0]
filtered = filter_neutral(results, neutral_threshold=0.75)
for emo, score in filtered.items():
para_counter[emo] += score
if normalize_paragraphs:
# β
Normalize scores so they sum β€ 1
total = sum(para_counter.values())
if total > 0:
para_counter = {emo: round(val / total, 3) for emo, val in para_counter.items()}
para_counters.append((para, dict(sorted(para_counter.items(), key=lambda x:x[1], reverse=True))))
st.write(f"\nπ Paragraph {p_idx}: {para}")
st.write("Emotions β", para_counters[-1][1])
export_rows.append({
"Type": "Paragraph",
"Text": para,
"Emotions": para_counters[-1][1],
"Sentiment": ""
})
# -------------------------------
# Sentence-level
st.subheader("π SENTENCES")
for para in paragraphs:
sentences = split_sentences(para, lang[:2])
for sentence in sentences:
pos_tags = get_pos_tags(sentence, lang[:2])
results = emotion_pipeline(sentence[:512])[0]
filtered = filter_neutral(results, neutral_threshold=0.75)
sentiment_results = sentiment_pipeline(sentence[:512])[0]
best_sentiment = max(sentiment_results, key=lambda x: x["score"])
results_summary.append({
"sentence": sentence,
"pos_tags": pos_tags,
"emotions": filtered,
"sentiment": best_sentiment
})
st.write(f"Sentence: {sentence}")
st.write(f"POS Tags β {pos_tags}")
st.write(f"Emotions β {filtered}")
st.write(f"Sentiment β {best_sentiment['label']} ({round(best_sentiment['score'],4)})\n")
export_rows.append({
"Type": "Sentence",
"Text": sentence,
"Emotions": filtered,
"Sentiment": best_sentiment
})
return results_summary, export_rows
# ===============================
# π Streamlit App
# ===============================
st.title("π Multilingual Text Emotion + Sentiment Analyzer")
uploaded_file = st.file_uploader("Upload a DOCX file", type=["docx"])
text_input = st.text_area("Or paste text here")
# β
Checkbox for paragraph normalization
normalize_paragraphs = st.checkbox("Normalize paragraph emotion scores", value=True)
# β
Placeholder for download buttons at the top
download_placeholder = st.empty()
if st.button("π Analyze"):
with st.spinner("Running analysis... β³"):
if uploaded_file:
articles = read_and_split_articles(uploaded_file)
text_to_analyze = "\n\n".join(articles) if articles else ""
elif text_input.strip():
text_to_analyze = text_input
else:
st.warning("Please upload a DOCX file or paste text to analyze.")
st.stop()
detected_lang = detect(text_to_analyze[:200]) if text_to_analyze else "en"
emotion_pipeline, sentiment_pipeline = load_pipelines(detected_lang)
results, export_rows = analyze_article(
text_to_analyze, detected_lang, emotion_pipeline, sentiment_pipeline, normalize_paragraphs
)
# β
Show download buttons at the TOP
df_export = pd.DataFrame(export_rows)
csv = df_export.to_csv(index=False).encode("utf-8")
with download_placeholder.container():
st.download_button(
label="β¬οΈ Download CSV",
data=csv,
file_name="analysis_results.csv",
mime="text/csv",
)
excel_buffer = io.BytesIO()
df_export.to_excel(excel_buffer, index=False, engine="xlsxwriter")
st.download_button(
label="β¬οΈ Download Excel",
data=excel_buffer,
file_name="analysis_results.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)
|