Spaces:
Running
Running
File size: 17,711 Bytes
876b12f a2624a3 5c3b4a6 876b12f 55cdb25 876b12f a2624a3 5c3b4a6 876b12f 5c3b4a6 a2624a3 5c3b4a6 a2624a3 5c3b4a6 a2624a3 5c3b4a6 a2624a3 5c3b4a6 876b12f 5c3b4a6 876b12f a2624a3 876b12f a2624a3 876b12f a2624a3 876b12f 5c3b4a6 1360e33 5c3b4a6 1360e33 a2624a3 1360e33 a2624a3 5c3b4a6 1360e33 5c3b4a6 1360e33 5c3b4a6 a2624a3 1360e33 5c3b4a6 a2624a3 1360e33 a2624a3 5c3b4a6 a2624a3 1360e33 a2624a3 1360e33 a2624a3 1360e33 a2624a3 1360e33 a2624a3 5c3b4a6 a2624a3 1360e33 a2624a3 1360e33 a2624a3 5c3b4a6 a2624a3 5c3b4a6 1360e33 5c3b4a6 a2624a3 5c3b4a6 1360e33 5c3b4a6 a2624a3 5c3b4a6 55cdb25 5c3b4a6 55cdb25 5c3b4a6 876b12f 5c3b4a6 876b12f 5c3b4a6 876b12f 5c3b4a6 876b12f 5c3b4a6 876b12f 5c3b4a6 876b12f 5c3b4a6 a2624a3 5c3b4a6 a2624a3 876b12f a2624a3 876b12f 5c3b4a6 876b12f 5c3b4a6 876b12f 5c3b4a6 876b12f 5c3b4a6 876b12f |
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 |
import logging
from typing import Dict, Any, List, Optional
from transformers import pipeline, AutoTokenizer
import numpy as np
import nltk
from nltk.tokenize import sent_tokenize
logger = logging.getLogger(__name__)
class HeadlineAnalyzer:
def __init__(self, use_ai: bool = True, model_registry: Optional[Any] = None):
"""
Initialize the analyzers for headline analysis.
Args:
use_ai: Boolean indicating whether to use AI-powered analysis (True) or traditional analysis (False)
model_registry: Optional shared model registry for better performance
"""
self.use_ai = use_ai
self.llm_available = False
self.model_registry = model_registry
if use_ai:
try:
if model_registry and model_registry.is_available:
# Use shared models
self.nli_pipeline = model_registry.nli
self.zero_shot = model_registry.zero_shot
self.tokenizer = AutoTokenizer.from_pretrained("roberta-large-mnli")
self.llm_available = True
logger.info("Using shared model pipelines for headline analysis")
else:
# Initialize own pipelines
self.nli_pipeline = pipeline(
"text-classification",
model="roberta-large-mnli",
batch_size=16
)
self.zero_shot = pipeline(
"zero-shot-classification",
model="facebook/bart-large-mnli",
device=-1,
batch_size=8
)
self.tokenizer = AutoTokenizer.from_pretrained("roberta-large-mnli")
self.llm_available = True
logger.info("Initialized dedicated model pipelines for headline analysis")
self.max_length = 512
except Exception as e:
logger.warning(f"Failed to initialize LLM pipelines: {str(e)}")
self.llm_available = False
else:
logger.info("Initializing headline analyzer in traditional mode")
def _split_content(self, headline: str, content: str) -> List[str]:
"""Split content into sections that fit within token limit."""
content_words = content.split()
sections = []
current_section = []
# Account for headline and [SEP] token in the max length
headline_tokens = len(self.tokenizer.encode(headline))
sep_tokens = len(self.tokenizer.encode("[SEP]")) - 2
max_content_tokens = self.max_length - headline_tokens - sep_tokens
# Process words into sections with 4000 character chunks
current_text = ""
for word in content_words:
if len(current_text) + len(word) + 1 <= 4000:
current_text += " " + word
else:
sections.append(current_text.strip())
current_text = word
if current_text:
sections.append(current_text.strip())
return sections
def _analyze_section(self, headline: str, section: str) -> Dict[str, Any]:
"""Analyze a single section for headline accuracy and sensationalism."""
try:
logger.info("\n" + "-"*30)
logger.info("ANALYZING SECTION")
logger.info("-"*30)
logger.info(f"Headline: {headline}")
logger.info(f"Section length: {len(section)} characters")
# Download NLTK data if needed
try:
nltk.data.find('tokenizers/punkt')
except LookupError:
nltk.download('punkt')
sentences = sent_tokenize(section)
logger.info(f"Found {len(sentences)} sentences in section")
if not sentences:
logger.warning("No sentences found in section")
return {
"accuracy_score": 50.0,
"flagged_phrases": [],
"detailed_scores": {
"nli": {"ENTAILMENT": 0.0, "CONTRADICTION": 0.0, "NEUTRAL": 1.0},
"sensationalism": {"factual reporting": 0.5, "accurate headline": 0.5}
}
}
# Categories for sensationalism check
sensationalism_categories = [
"clickbait",
"sensationalized",
"misleading",
"factual reporting",
"accurate headline"
]
logger.info("Checking headline for sensationalism...")
sensationalism_result = self.zero_shot(
headline,
sensationalism_categories,
multi_label=True
)
sensationalism_scores = {
label: score
for label, score in zip(sensationalism_result['labels'], sensationalism_result['scores'])
}
logger.info(f"Sensationalism scores: {sensationalism_scores}")
# Filter relevant sentences (longer than 20 chars)
relevant_sentences = [s.strip() for s in sentences if len(s.strip()) > 20]
logger.info(f"Found {len(relevant_sentences)} relevant sentences after filtering")
if not relevant_sentences:
logger.warning("No relevant sentences found in section")
return {
"accuracy_score": 50.0,
"flagged_phrases": [],
"detailed_scores": {
"nli": {"ENTAILMENT": 0.0, "CONTRADICTION": 0.0, "NEUTRAL": 1.0},
"sensationalism": sensationalism_scores
}
}
# Process sentences in batches for contradiction/support
nli_scores = []
flagged_phrases = []
batch_size = 8
logger.info("Processing sentences for contradictions...")
for i in range(0, len(relevant_sentences), batch_size):
batch = relevant_sentences[i:i+batch_size]
batch_inputs = [f"{headline} [SEP] {sentence}" for sentence in batch]
try:
# Get NLI scores for batch
batch_results = self.nli_pipeline(batch_inputs, top_k=None)
if not isinstance(batch_results, list):
batch_results = [batch_results]
for sentence, result in zip(batch, batch_results):
scores = {item['label']: item['score'] for item in result}
nli_scores.append(scores)
# Flag contradictory content with lower threshold
if scores.get('CONTRADICTION', 0) > 0.3: # Lowered threshold
logger.info(f"Found contradictory sentence (score: {scores['CONTRADICTION']:.2f}): {sentence}")
flagged_phrases.append({
'text': sentence,
'type': 'Contradiction',
'score': scores['CONTRADICTION'],
'highlight': f"[CONTRADICTION] (Score: {round(scores['CONTRADICTION'] * 100, 1)}%) \"{sentence}\""
})
# Flag highly sensationalized content
if sensationalism_scores.get('sensationalized', 0) > 0.6 or sensationalism_scores.get('clickbait', 0) > 0.6:
logger.info(f"Found sensationalized content: {sentence}")
flagged_phrases.append({
'text': sentence,
'type': 'Sensationalized',
'score': max(sensationalism_scores.get('sensationalized', 0), sensationalism_scores.get('clickbait', 0)),
'highlight': f"[SENSATIONALIZED] \"{sentence}\""
})
except Exception as batch_error:
logger.warning(f"Batch processing error: {str(batch_error)}")
continue
# Calculate aggregate scores with validation
if not nli_scores:
logger.warning("No NLI scores available")
avg_scores = {"ENTAILMENT": 0.0, "CONTRADICTION": 0.0, "NEUTRAL": 1.0}
else:
try:
avg_scores = {
label: float(np.mean([
score.get(label, 0.0)
for score in nli_scores
]))
for label in ['ENTAILMENT', 'CONTRADICTION', 'NEUTRAL']
}
logger.info(f"Average NLI scores: {avg_scores}")
except Exception as agg_error:
logger.error(f"Error aggregating NLI scores: {str(agg_error)}")
avg_scores = {"ENTAILMENT": 0.0, "CONTRADICTION": 0.0, "NEUTRAL": 1.0}
# Calculate headline accuracy score with validation
try:
accuracy_components = {
'entailment': avg_scores.get('ENTAILMENT', 0.0) * 0.4,
'non_contradiction': (1 - avg_scores.get('CONTRADICTION', 0.0)) * 0.3,
'non_sensational': (
sensationalism_scores.get('factual reporting', 0.0) +
sensationalism_scores.get('accurate headline', 0.0)
) * 0.15,
'non_clickbait': (
1 - sensationalism_scores.get('clickbait', 0.0) -
sensationalism_scores.get('sensationalized', 0.0)
) * 0.15
}
logger.info(f"Accuracy components: {accuracy_components}")
accuracy_score = sum(accuracy_components.values()) * 100
# Validate final score
if np.isnan(accuracy_score) or not np.isfinite(accuracy_score):
logger.warning("Invalid accuracy score calculated, using default")
accuracy_score = 50.0
else:
accuracy_score = float(accuracy_score)
logger.info(f"Final accuracy score: {accuracy_score:.1f}")
except Exception as score_error:
logger.error(f"Error calculating accuracy score: {str(score_error)}")
accuracy_score = 50.0
# Sort and limit flagged phrases
sorted_phrases = sorted(
flagged_phrases,
key=lambda x: x['score'],
reverse=True
)
unique_phrases = []
seen = set()
for phrase in sorted_phrases:
if phrase['text'] not in seen:
unique_phrases.append(phrase)
seen.add(phrase['text'])
if len(unique_phrases) >= 5:
break
logger.info(f"Final number of flagged phrases: {len(unique_phrases)}")
return {
"accuracy_score": accuracy_score,
"flagged_phrases": unique_phrases,
"detailed_scores": {
"nli": avg_scores,
"sensationalism": sensationalism_scores
}
}
except Exception as e:
logger.error(f"Section analysis failed: {str(e)}")
return {
"accuracy_score": 50.0,
"flagged_phrases": [],
"detailed_scores": {
"nli": {"ENTAILMENT": 0.0, "CONTRADICTION": 0.0, "NEUTRAL": 1.0},
"sensationalism": {}
}
}
def _analyze_traditional(self, headline: str, content: str) -> Dict[str, Any]:
"""Traditional headline analysis method."""
try:
# Download NLTK data if needed
try:
nltk.data.find('tokenizers/punkt')
except LookupError:
nltk.download('punkt')
# Basic metrics
headline_words = set(headline.lower().split())
content_words = set(content.lower().split())
# Calculate word overlap
overlap_words = headline_words.intersection(content_words)
overlap_score = len(overlap_words) / len(headline_words) if headline_words else 0
# Check for clickbait patterns
clickbait_patterns = [
"you won't believe",
"shocking",
"mind blowing",
"amazing",
"incredible",
"unbelievable",
"must see",
"click here",
"find out",
"what happens next"
]
clickbait_count = sum(1 for pattern in clickbait_patterns if pattern in headline.lower())
clickbait_penalty = clickbait_count * 10 # 10% penalty per clickbait phrase
# Calculate final score (0-100)
base_score = overlap_score * 100
final_score = max(0, min(100, base_score - clickbait_penalty))
# Find potentially misleading phrases
flagged_phrases = []
sentences = sent_tokenize(content)
for sentence in sentences:
# Flag sentences that directly contradict headline words
sentence_words = set(sentence.lower().split())
if len(headline_words.intersection(sentence_words)) > 2:
flagged_phrases.append(sentence.strip())
# Flag sentences with clickbait patterns
if any(pattern in sentence.lower() for pattern in clickbait_patterns):
flagged_phrases.append(sentence.strip())
return {
"headline_vs_content_score": round(final_score, 1),
"flagged_phrases": list(set(flagged_phrases))[:5] # Limit to top 5 unique phrases
}
except Exception as e:
logger.error(f"Traditional analysis failed: {str(e)}")
return {
"headline_vs_content_score": 0,
"flagged_phrases": []
}
def analyze(self, headline: str, content: str) -> Dict[str, Any]:
"""Analyze how well the headline matches the content."""
try:
logger.info("\n" + "="*50)
logger.info("HEADLINE ANALYSIS STARTED")
logger.info("="*50)
if not headline.strip() or not content.strip():
logger.warning("Empty headline or content provided")
return {
"headline_vs_content_score": 0,
"flagged_phrases": []
}
# Use LLM analysis if available and enabled
if self.use_ai and self.llm_available:
logger.info("Using LLM analysis for headline")
# Split content if needed
sections = self._split_content(headline, content)
section_results = []
# Analyze each section
for section in sections:
result = self._analyze_section(headline, section)
section_results.append(result)
# Aggregate results across sections
accuracy_scores = [r['accuracy_score'] for r in section_results]
final_score = np.mean(accuracy_scores)
# Combine and deduplicate flagged phrases
all_phrases = []
for result in section_results:
if 'flagged_phrases' in result:
all_phrases.extend(result['flagged_phrases'])
# Sort by score and get unique phrases
sorted_phrases = sorted(all_phrases, key=lambda x: x['score'], reverse=True)
unique_phrases = []
seen = set()
for phrase in sorted_phrases:
if phrase['text'] not in seen:
unique_phrases.append(phrase)
seen.add(phrase['text'])
if len(unique_phrases) >= 5:
break
return {
"headline_vs_content_score": round(final_score, 1),
"flagged_phrases": unique_phrases
}
else:
# Use traditional analysis
logger.info("Using traditional headline analysis")
return self._analyze_traditional(headline, content)
except Exception as e:
logger.error(f"Headline analysis failed: {str(e)}")
return {
"headline_vs_content_score": 0,
"flagged_phrases": []
} |