Spaces:
Sleeping
Sleeping
File size: 20,049 Bytes
dce5185 660fff4 308eef3 4c1d6e7 660fff4 62090b1 7da623a 4ef11a1 ac9d833 84c0e52 1f79d8c 5345dd5 1f79d8c 660fff4 1f79d8c 81cf330 9986f92 62090b1 9986f92 62090b1 1f79d8c 5345dd5 407afee d459853 5345dd5 8fc5622 84c0e52 6a5f3c3 f5c3a62 6a5f3c3 e610257 78c6b24 7c6c69f 84c0e52 62090b1 84c0e52 8540440 9986f92 407afee 84c0e52 f5c3a62 9986f92 e610257 78c6b24 84c0e52 407afee 660fff4 e610257 660fff4 e610257 660fff4 a841cfc 660fff4 84c0e52 4ef11a1 1f79d8c 4ef11a1 ac9d833 1f79d8c ac9d833 1f79d8c 84c0e52 1f79d8c 84c0e52 ac9d833 660fff4 62090b1 660fff4 81cf330 ce4e79f 81cf330 660fff4 81cf330 660fff4 78c6b24 660fff4 78c6b24 660fff4 c6b218c 660fff4 78c6b24 c6b218c 78c6b24 660fff4 1f79d8c 660fff4 407afee 660fff4 4ef11a1 78c6b24 660fff4 78c6b24 660fff4 78c6b24 660fff4 78c6b24 660fff4 78c6b24 660fff4 78c6b24 660fff4 62090b1 660fff4 c6b218c b928855 660fff4 b928855 6978218 b928855 660fff4 7da623a 660fff4 f5c3a62 660fff4 a841cfc 660fff4 f5c3a62 660fff4 a841cfc 660fff4 a841cfc | 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 | # imports
import re
import plotly.graph_objects as go
import streamlit as st
import json
from openai import OpenAI
import textstat
import trafilatura
import requests
from bs4 import BeautifulSoup
# constants
MAX_WORDS = 400
ARTICLE_A = """In a watershed moment for global tech governance, international regulatory bodies have introduced the comprehensive Artificial Intelligence Safeguard Act. For too long, Silicon Valley titans have operated in a wild west environment, prioritizing unchecked corporate greed and rapid deployment over public safety. This landmark legislation aims to establish rigorous ethical boundaries and mandatory safety audits before any advanced generative models can be released to the public. Proponents argue that without these essential guardrails, society faces catastrophic risks ranging from massive, unmitigated job displacement to the proliferation of deepfake-fueled misinformation that threatens the very fabric of our democratic institutions. "We cannot allow a handful of unelected tech billionaires to play roulette with humanity's future," stated the coalition's lead ethicist. By prioritizing human welfare over blind technological acceleration, the Act serves as a vital moral firewall, ensuring that the development of artificial general intelligence benefits society as a whole rather than just enriching the elite few."""
ARTICLE_B = """Tech industry leaders and economists are sounding the alarm over the newly proposed Artificial Intelligence Safeguard Act, warning that the draconian legislation will severely cripple the nation’s economic engine. Critics argue that the bill is a masterclass in bureaucratic overreach, drowning agile tech startups in layers of punitive red tape and effectively stifling the very innovation that drives modern prosperity. By mandating arbitrary algorithmic audits and imposing heavy-handed restrictions on model training, the government is poised to surrender our global competitive edge to foreign adversaries who are not bound by such paralyzing regulations. "This isn't about safety; it's an innovation tax that penalizes success," argued a prominent venture capitalist. Analysts project that this short-sighted policy will force thousands of AI researchers to relocate overseas, draining billions of dollars in investment capital from the domestic market. Ultimately, framing technological progress as an inherent danger will only succeed in legislating the industry into obsolescence, destroying millions of future private-sector jobs in the process."""
URL_A = "https://www.foxnews.com/live-news/trump-iran-israel-war-updates-march-30"
URL_B = "https://edition.cnn.com/2026/03/30/world/live-news/iran-war-us-israel-trump"
st.set_page_config(page_title="FrameVis | Media Framing", layout="wide")
# Initialize the AI model
GROQ_API_KEY = st.secrets.get("GROQ_API_KEY")
if not GROQ_API_KEY:
st.warning("Groq API Token Missing.")
st.stop()
client = OpenAI(
api_key=GROQ_API_KEY,
base_url="https://api.groq.com/openai/v1",
)
def _truncate_to_words(text: str, limit: int) -> str:
"""Truncates text by word count."""
if not text:
return ""
words = text.split()
return " ".join(words[:limit])
def analyze_article(text: str) -> dict:
"""Analyzes framing using an LLM API and calculates readability."""
safe_text = _truncate_to_words(text, MAX_WORDS)
prompt = f"""
You are an expert media analyst. Analyze the following news snippet for framing, bias, and emotion.
Return ONLY a valid JSON object with the exact keys below. Do not include markdown formatting or explanations.
Keys to return:
"sentiment_score": A float between -1.0 (highly negative) and 1.0 (highly positive).
"primary_tone": The single dominant emotion.
"primary_theme": Choose ONE like: ["economic consequences", "moral and ethical fairness", "legal and bureaucratic", "public safety and health"].
"tone_scores": A dictionary scoring THESE EXACT 6 EMOTIONS from 0.0 to 1.0: {{"anger": 0.0, "fear": 0.0, "joy": 0.0, "sadness": 0.0, "surprise": 0.0, "trust": 0.0}}.
"framing_words": A list of dictionaries containing the 5 to 8 most emotionally charged or biased words, and the specific emotion they evoke. Format: [{{"word": "draconian", "emotion": "fear"}}, {{"word": "titans", "emotion": "awe"}}].
"subjectivity_score": A float between 0.0 (completely objective/factual) and 1.0 (highly opinionated/subjective).
Text to analyze:
"{safe_text}"
"""
try:
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
response_format={"type": "json_object"},
temperature=0.1,
messages=[
{"role": "system", "content": "You are a media analyst. You MUST respond with ONLY valid JSON matching the exact requested schema."},
{"role": "user", "content": prompt}
]
)
llm_data = json.loads(response.choices[0].message.content)
except Exception as e:
print(f"Error parsing LLM response: {e}")
llm_data = {}
raw_reading_ease = textstat.flesch_reading_ease(safe_text)
tones = llm_data.get("tone_scores", {})
standard_tones = {
"anger": tones.get("anger", 0.0),
"fear": tones.get("fear", 0.0),
"joy": tones.get("joy", 0.0),
"sadness": tones.get("sadness", 0.0),
"surprise": tones.get("surprise", 0.0),
"trust": tones.get("trust", 0.0),
}
return {
"sentiment_score": llm_data.get("sentiment_score", 0.0),
"primary_tone": llm_data.get("primary_tone", "neutral"),
"primary_theme": llm_data.get("primary_theme", "unclear"),
"tone_scores": standard_tones,
"framing_words": llm_data.get("framing_words", []),
"subjectivity_score": llm_data.get("subjectivity_score", 0.0),
"reading_ease": max(0.0, min(100.0, raw_reading_ease)),
}
def _create_sentiment_gauge(score: float, title: str) -> go.Figure:
"""Generates a Plotly gauge chart for sentiment visualization."""
fig = go.Figure(
go.Indicator(
mode="gauge+number",
value=score,
domain={"x": [0, 1], "y": [0, 1]},
title={"text": title, "font": {"size": 16}},
gauge={
"axis": {"range": [-1, 1], "tickwidth": 1, "tickcolor": "darkgrey"},
"bar": {"color": "#475569", "thickness": 0.2},
"bgcolor": "white",
"borderwidth": 0,
"steps": [
{"range": [-1, -0.1], "color": "#fee2e2"},
{"range": [-0.1, 0.1], "color": "#f1f5f9"},
{"range": [0.1, 1], "color": "#dcfce3"},
],
},
)
)
fig.update_layout(height=280, margin=dict(l=20, r=20, t=60, b=20))
return fig
def _create_comparison_radar_chart(results_a: dict, results_b: dict) -> go.Figure:
"""Generates an overlapping radar chart to compare emotions."""
categories = sorted(list(set(list(results_a["tone_scores"].keys()) + list(results_b["tone_scores"].keys()))))
val_a = [results_a["tone_scores"].get(c, 0) for c in categories]
val_b = [results_b["tone_scores"].get(c, 0) for c in categories]
categories.append(categories[0])
val_a.append(val_a[0])
val_b.append(val_b[0])
fig = go.Figure()
fig.add_trace(go.Scatterpolar(
r=val_a, theta=categories, fill='toself', name='Source A',
line=dict(color='#4f46e5', shape='spline', width=2),
fillcolor='rgba(79, 70, 229, 0.2)'
))
fig.add_trace(go.Scatterpolar(
r=val_b, theta=categories, fill='toself', name='Source B',
line=dict(color='#10b981', shape='spline', width=2),
fillcolor='rgba(16, 185, 129, 0.2)'
))
fig.update_layout(
polar=dict(
radialaxis=dict(visible=True, showticklabels=False, showline=False, gridcolor='rgba(0,0,0,0.1)'),
angularaxis=dict(gridcolor='rgba(0,0,0,0.1)', linecolor='rgba(0,0,0,0.1)')
),
showlegend=True,
legend=dict(orientation="h", yanchor="bottom", y=-0.2, xanchor="center", x=0.5),
title={"text": "Relative Emotion Profile", "font": {"size": 18, "family": "sans-serif"}},
height=400,
margin=dict(l=40, r=40, t=60, b=40),
paper_bgcolor='rgba(0,0,0,0)', # Transparent
plot_bgcolor='rgba(0,0,0,0)'
)
return fig
def _highlight_framing_words(text: str, target_words: list) -> str:
"""Highlights LLM-identified framing words and tags their specific emotion."""
display_text = _truncate_to_words(text, MAX_WORDS)
if not display_text:
return ""
highlighted_text = display_text + ("..." if len(text.split()) > MAX_WORDS else "")
for item in target_words:
if isinstance(item, dict):
word = item.get("word", "")
emotion = str(item.get("emotion", "charged")).upper()
else:
word = str(item)
emotion = "CHARGED"
if len(word) > 2:
pattern = r'\b(' + re.escape(word) + r')\b'
replacement = rf"""<span style='background-color: #f8fafc; border: 1px solid #cbd5e1; border-radius: 6px; padding: 0.15rem 0.3rem; font-weight: 600; color: #0f172a; box-shadow: 0 1px 2px rgba(0,0,0,0.02); margin: 0 0.1rem;'>\1 <span style='font-size: 0.65rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: #ffffff; background-color: #475569; padding: 0.15rem 0.4rem; border-radius: 12px; margin-left: 4px; vertical-align: text-bottom;'>{emotion}</span></span>"""
highlighted_text = re.sub(pattern, replacement, highlighted_text, flags=re.IGNORECASE)
return highlighted_text
@st.cache_data(ttl=3600, show_spinner=False)
def fetch_article_text(url: str) -> str:
"""Scrapes article text."""
downloaded = trafilatura.fetch_url(url)
if downloaded:
text = trafilatura.extract(downloaded)
if text and len(text) > 200:
return text
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
'Accept-Language': 'en-US,en;q=0.9',
}
response = requests.get(url, headers=headers, timeout=10)
soup = BeautifulSoup(response.content, 'html.parser')
for script in soup(["script", "style"]):
script.extract()
paragraphs = soup.find_all('p')
text = ' '.join([p.get_text() for p in paragraphs])
if text and len(text) > 200:
return text.strip()
except Exception as e:
return f"Error: Could not fetch URL. Connection failed."
return "Error: Could not extract text. The site may be protected by hard paywalls."
def check_contradiction(text_a: str, text_b: str) -> dict:
"""Uses the LLM to evaluate the stance between arguments."""
safe_a = _truncate_to_words(text_a, MAX_WORDS)
safe_b = _truncate_to_words(text_b, MAX_WORDS)
prompt = f"""
You are a fact-checking analyst. Compare these two news excerpts.
Return ONLY a valid JSON object with the exact keys below. Do not include markdown formatting.
Keys to return:
"relationship": Choose ONE from: ["CONTRADICTION", "ENTAILMENT", "NEUTRAL"]. (Contradiction = disputing facts, Entailment = agreeing on premise).
"confidence": A float between 0.0 and 1.0 representing how confident you are.
Text 1: "{safe_a}"
Text 2: "{safe_b}"
"""
try:
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
response_format={"type": "json_object"},
temperature=0.1,
messages=[
{"role": "system", "content": "You are a fact-checker. You MUST respond with ONLY valid JSON."},
{"role": "user", "content": prompt}
]
)
result = json.loads(response.choices[0].message.content)
except Exception as e:
print(f"Error in contradiction check: {e}")
result = {}
return {"relationship": result.get("relationship", "NEUTRAL"), "confidence": result.get("confidence", 0.0)}
@st.cache_data(show_spinner=False)
def get_cached_analysis(text: str) -> dict:
return analyze_article(text)
@st.cache_data(show_spinner=False)
def get_cached_contradiction(text_a: str, text_b: str) -> dict:
return check_contradiction(text_a, text_b)
st.markdown("""
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
header {visibility: hidden;}
.block-container {
padding-top: 2rem;
padding-bottom: 2rem;
max-width: 1200px;
}
[data-testid="stMetric"] {
background-color: #ffffff;
border: 1px solid #e2e8f0;
border-radius: 12px;
padding: 20px;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03);
transition: transform 0.2s ease-in-out;
}
[data-testid="stMetric"]:hover {
transform: translateY(-2px);
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}
[data-testid="stMetricValue"], [data-testid="stMetricValue"] * {
white-space: normal !important;
overflow-wrap: break-word !important;
word-wrap: break-word !important;
overflow: visible !important;
text-overflow: clip !important;
line-height: 1.25 !important;
}
[data-testid="stMetricValue"] > div {
color: #0f172a;
font-weight: 700 !important;
font-size: 1.3rem !important;
}
[data-testid="stMetricLabel"] > div {
color: #64748b;
font-weight: 500;
text-transform: uppercase;
letter-spacing: 0.05em;
font-size: 0.85rem;
}
</style>
""", unsafe_allow_html=True)
# STATE MANAGEMENT
if "results_a" not in st.session_state:
st.session_state.results_a = None
if "results_b" not in st.session_state:
st.session_state.results_b = None
if "nli_result" not in st.session_state:
st.session_state.nli_result = None
st.title("FrameVis")
st.markdown("##### Media bias and framing effects across global news sources.")
st.divider()
with st.container(border=True):
st.markdown("#### Configure Data Sources")
input_method = st.radio("Input Method", ["Paste Text", "Paste URL"], horizontal=True, index=0)
col1, col2 = st.columns(2)
with col1:
if input_method == "Paste Text":
user_article_a = st.text_area("Data Source A", value=ARTICLE_A.strip(), height=180)
else:
url_a = st.text_input("Source A URL", value=URL_A)
user_article_a = fetch_article_text(url_a) if url_a else ""
with col2:
if input_method == "Paste Text":
user_article_b = st.text_area("Data Source B", value=ARTICLE_B.strip(), height=180)
else:
url_b = st.text_input("Source B URL", value=URL_B)
user_article_b = fetch_article_text(url_b) if url_b else ""
st.write("")
execute_analysis = st.button("Analyze and Compare Sources", use_container_width=True, type="primary")
if execute_analysis:
text_a_clean = user_article_a.strip() if user_article_a else ""
text_b_clean = user_article_b.strip() if user_article_b else ""
if not text_a_clean or not text_b_clean:
st.warning("Please provide text or a valid URL for both Source A and Source B before analyzing.")
elif text_a_clean.startswith("Error:") or text_b_clean.startswith("Error:"):
st.error("One of the URLs could not be scraped. Please copy and paste the text directly.")
else:
with st.spinner("Analyzing both sources."):
try:
res_a = get_cached_analysis(text_a_clean)
res_b = get_cached_analysis(text_b_clean)
res_nli = get_cached_contradiction(text_a_clean, text_b_clean)
st.session_state.results_a = res_a
st.session_state.results_b = res_b
st.session_state.nli_result = res_nli
except Exception as e:
error_msg = str(e)
if "429" in error_msg or "RESOURCE_EXHAUSTED" in error_msg:
st.error("**Rate Limit Reached:** Too many requests too quickly.")
else:
st.error(f"API or Processing Error: {error_msg}")
st.session_state.results_a = None
st.session_state.results_b = None
st.session_state.nli_result = None
# Analysis Display
if st.session_state.results_a and st.session_state.results_b:
st.divider()
st.markdown("### Framing Analytics & Comparison")
# Display Contradictions
nli_result = st.session_state.nli_result
if nli_result:
if nli_result["relationship"].upper() == "CONTRADICTION":
st.error(f"**NARRATIVE CONTRADICTION** (Confidence: {nli_result['confidence']:.2f}) - These sources are disputing each other's facts.")
elif nli_result["relationship"].upper() == "ENTAILMENT":
st.success(f"**NARRATIVE ALIGNMENT** (Confidence: {nli_result['confidence']:.2f}) - These sources agree on the core premise.")
else:
st.info(f"**NEUTRAL RELATIONSHIP** - These sources are discussing the topic without direct contradiction or alignment.")
st.plotly_chart(_create_comparison_radar_chart(st.session_state.results_a, st.session_state.results_b), use_container_width=True)
res_col1, res_col2 = st.columns(2)
# Render Column A
with res_col1:
r_a = st.session_state.results_a
st.markdown("#### Source A Breakdown")
m1, m2 = st.columns(2)
m3, m4 = st.columns(2)
m1.metric("Subjectivity", f"{r_a['subjectivity_score']:.2f}", help="0 is objective, 1 is highly opinionated.")
m2.metric("Primary Emotion", str(r_a['primary_tone']).title())
m3.metric("Framing Lens", str(r_a['primary_theme']).title())
m4.metric("Reading Ease", f"{r_a['reading_ease']:.1f}", help="0-30 is college graduate level, 60-70 is 8th grade.")
st.plotly_chart(_create_sentiment_gauge(r_a["sentiment_score"], "Sentiment Bias"), use_container_width=True, key="gauge_a")
st.markdown("**Key Framing Language:**")
annotated_text = _highlight_framing_words(user_article_a, r_a['framing_words'])
st.markdown(f"<div style='background-color: #ffffff; padding: 1.5rem; border-radius: 12px; border: 1px solid #e2e8f0; line-height: 1.8; font-size: 1.05rem; box-shadow: 0 1px 3px rgba(0,0,0,0.05);'>{annotated_text}</div>", unsafe_allow_html=True)
# Render Column B
with res_col2:
r_b = st.session_state.results_b
st.markdown("#### Source B Breakdown")
m1, m2 = st.columns(2)
m3, m4 = st.columns(2)
m1.metric("Subjectivity", f"{r_b['subjectivity_score']:.2f}", help="0 is objective, 1 is highly opinionated.")
m2.metric("Primary Emotion", str(r_b['primary_tone']).title())
m3.metric("Framing Lens", str(r_b['primary_theme']).title())
m4.metric("Reading Ease", f"{r_b['reading_ease']:.1f}", help="0-30 is college graduate level, 60-70 is 8th grade.")
st.plotly_chart(_create_sentiment_gauge(r_b["sentiment_score"], "Sentiment Bias"), use_container_width=True, key="gauge_b")
st.markdown("**Key Framing Language:**")
annotated_text = _highlight_framing_words(user_article_b, r_b['framing_words'])
st.markdown(f"<div style='background-color: #ffffff; padding: 1.5rem; border-radius: 12px; border: 1px solid #e2e8f0; line-height: 1.8; font-size: 1.05rem; box-shadow: 0 1px 3px rgba(0,0,0,0.05);'>{annotated_text}</div>", unsafe_allow_html=True) |