Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import re
|
|
| 3 |
import typing
|
| 4 |
import plotly.graph_objects as go
|
| 5 |
import streamlit as st
|
| 6 |
-
from keybert import KeyBERT
|
| 7 |
from textblob import TextBlob
|
| 8 |
from transformers import pipeline
|
| 9 |
import textstat
|
|
@@ -26,7 +25,7 @@ def _load_nlp_models() -> typing.Dict[str, typing.Any]:
|
|
| 26 |
"""
|
| 27 |
sentiment_analyzer = pipeline(
|
| 28 |
"text-classification",
|
| 29 |
-
model="
|
| 30 |
)
|
| 31 |
emotion_classifier = pipeline(
|
| 32 |
"text-classification",
|
|
@@ -38,21 +37,19 @@ def _load_nlp_models() -> typing.Dict[str, typing.Any]:
|
|
| 38 |
model="dslim/bert-base-NER",
|
| 39 |
aggregation_strategy="simple"
|
| 40 |
)
|
| 41 |
-
keyword_extractor = KeyBERT(model="all-mpnet-base-v2")
|
| 42 |
nli_classifier = pipeline(
|
| 43 |
"text-classification",
|
| 44 |
model="roberta-large-mnli"
|
| 45 |
)
|
| 46 |
theme_classifier = pipeline(
|
| 47 |
"zero-shot-classification",
|
| 48 |
-
model="
|
| 49 |
)
|
| 50 |
|
| 51 |
return {
|
| 52 |
"sentiment": sentiment_analyzer,
|
| 53 |
"emotion": emotion_classifier,
|
| 54 |
"ner": ner_extractor,
|
| 55 |
-
"keyword": keyword_extractor,
|
| 56 |
"nli": nli_classifier,
|
| 57 |
"theme": theme_classifier,
|
| 58 |
}
|
|
@@ -101,7 +98,6 @@ def analyze_article(text: str) -> dict:
|
|
| 101 |
diversity=0.6,
|
| 102 |
top_n=6
|
| 103 |
)
|
| 104 |
-
extracted_keywords = [kw[0] for kw in keyword_results]
|
| 105 |
|
| 106 |
# Thematic Framing
|
| 107 |
framing_dimensions = [
|
|
@@ -198,33 +194,21 @@ def _create_comparison_radar_chart(results_a: dict, results_b: dict) -> go.Figur
|
|
| 198 |
return fig
|
| 199 |
|
| 200 |
|
| 201 |
-
def
|
| 202 |
-
"""
|
| 203 |
-
|
|
|
|
| 204 |
|
| 205 |
-
|
| 206 |
-
sorted_keywords = sorted(keywords, key=len, reverse=True)
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
for kw in sorted_keywords:
|
| 213 |
-
pattern = re.compile(rf"(?i)\b({re.escape(kw)})\b")
|
| 214 |
-
if pattern.search(highlighted_sentence):
|
| 215 |
-
has_keyword = True
|
| 216 |
-
highlighted_sentence = pattern.sub(
|
| 217 |
-
r"<span style='background-color: #e0e7ff; color: #3730a3; font-weight: 600; padding: 0.15rem 0.4rem; border-radius: 12px; font-size: 0.9em;'>\1</span>",
|
| 218 |
-
highlighted_sentence,
|
| 219 |
-
)
|
| 220 |
-
|
| 221 |
-
if has_keyword:
|
| 222 |
-
relevant_sentences.append(highlighted_sentence)
|
| 223 |
-
|
| 224 |
-
if not relevant_sentences:
|
| 225 |
return text[:250] + "..."
|
| 226 |
|
| 227 |
-
|
|
|
|
| 228 |
|
| 229 |
|
| 230 |
def fetch_article_text(url: str) -> str:
|
|
@@ -423,5 +407,5 @@ if st.session_state.results_a and st.session_state.results_b:
|
|
| 423 |
st.markdown(f"**Extracted Entities:** `{', '.join(r_b['entities'])}`")
|
| 424 |
|
| 425 |
st.markdown("**Key Framing Language:**")
|
| 426 |
-
annotated_text =
|
| 427 |
st.markdown(f"<div style='background-color: #f8fafc; padding: 1rem; border-radius: 8px; border: 1px solid #e2e8f0;'>{annotated_text}</div>", unsafe_allow_html=True)
|
|
|
|
| 3 |
import typing
|
| 4 |
import plotly.graph_objects as go
|
| 5 |
import streamlit as st
|
|
|
|
| 6 |
from textblob import TextBlob
|
| 7 |
from transformers import pipeline
|
| 8 |
import textstat
|
|
|
|
| 25 |
"""
|
| 26 |
sentiment_analyzer = pipeline(
|
| 27 |
"text-classification",
|
| 28 |
+
model="mrm8488/distilroberta-finetuned-financial-news-sentiment",
|
| 29 |
)
|
| 30 |
emotion_classifier = pipeline(
|
| 31 |
"text-classification",
|
|
|
|
| 37 |
model="dslim/bert-base-NER",
|
| 38 |
aggregation_strategy="simple"
|
| 39 |
)
|
|
|
|
| 40 |
nli_classifier = pipeline(
|
| 41 |
"text-classification",
|
| 42 |
model="roberta-large-mnli"
|
| 43 |
)
|
| 44 |
theme_classifier = pipeline(
|
| 45 |
"zero-shot-classification",
|
| 46 |
+
model="facebook/bart-large-mnli"
|
| 47 |
)
|
| 48 |
|
| 49 |
return {
|
| 50 |
"sentiment": sentiment_analyzer,
|
| 51 |
"emotion": emotion_classifier,
|
| 52 |
"ner": ner_extractor,
|
|
|
|
| 53 |
"nli": nli_classifier,
|
| 54 |
"theme": theme_classifier,
|
| 55 |
}
|
|
|
|
| 98 |
diversity=0.6,
|
| 99 |
top_n=6
|
| 100 |
)
|
|
|
|
| 101 |
|
| 102 |
# Thematic Framing
|
| 103 |
framing_dimensions = [
|
|
|
|
| 194 |
return fig
|
| 195 |
|
| 196 |
|
| 197 |
+
def _highlight_subjective_sentences(text: str) -> str:
|
| 198 |
+
"""Highlights subjective/opinionated sentences in the text."""
|
| 199 |
+
raw_sentences = text.replace("?", ".").replace("!", ".").split(".")
|
| 200 |
+
sentences = [s.strip() + "." for s in raw_sentences if len(s.strip()) > 20]
|
| 201 |
|
| 202 |
+
scored_sentences = [(sentence, TextBlob(sentence).sentiment.subjectivity) for sentence in sentences]
|
|
|
|
| 203 |
|
| 204 |
+
top_subjective = sorted(scored_sentences, key=lambda x: x[1], reverse=True)[:3]
|
| 205 |
+
top_sentences = [item[0] for item in top_subjective if item[1] > 0.3]
|
| 206 |
+
|
| 207 |
+
if not top_sentences:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
return text[:250] + "..."
|
| 209 |
|
| 210 |
+
highlighted_text = " ... ".join(top_sentences)
|
| 211 |
+
return f"<span style='background-color: #e0e7ff; color: #3730a3; font-weight: 500; padding: 0.2rem 0.4rem; border-radius: 8px; font-size: 0.95em;'>{highlighted_text}</span>"
|
| 212 |
|
| 213 |
|
| 214 |
def fetch_article_text(url: str) -> str:
|
|
|
|
| 407 |
st.markdown(f"**Extracted Entities:** `{', '.join(r_b['entities'])}`")
|
| 408 |
|
| 409 |
st.markdown("**Key Framing Language:**")
|
| 410 |
+
annotated_text = _highlight_subjective_sentences(user_article_a)
|
| 411 |
st.markdown(f"<div style='background-color: #f8fafc; padding: 1rem; border-radius: 8px; border: 1px solid #e2e8f0;'>{annotated_text}</div>", unsafe_allow_html=True)
|