-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLabelTextWithLocalModel.py
More file actions
146 lines (115 loc) · 5.54 KB
/
Copy pathLabelTextWithLocalModel.py
File metadata and controls
146 lines (115 loc) · 5.54 KB
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
import pandas as pd
import json
import torch
import time
import os
from transformers import BartForSequenceClassification, BartTokenizer
# Load model and tokenizer directly
model_name = "facebook/bart-large-mnli"
model = BartForSequenceClassification.from_pretrained(model_name)
tokenizer = BartTokenizer.from_pretrained(model_name)
# Set device for computation
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")
model.to(device)
print("Model loaded successfully")
# Define candidate labels for classification
topic_labels = ["Economy", "Foreign Policy", "Human Rights", "Environment", "Security", "Technology", "EU Governance"]
tone_labels = ["Neutral", "Urgent", "Optimistic", "Conflict-Oriented", "Critical", "Supportive"]
frame_labels = ["Humanitarian", "Security", "Legalistic", "Economic", "Nationalist", "Technocratic"]
# Check if the file exists before starting
file_path = "europarl_headlines_max_5000.csv"
if not os.path.exists(file_path):
raise FileNotFoundError(f"Data file '{file_path}' not found. Please ensure the file exists in the current directory.")
# Read your headlines
print(f"Reading data from {file_path}")
df = pd.read_csv(file_path)
headlines = df["Headline"].tolist()
print(f"Loaded {len(headlines)} headlines from CSV file")
# This will store the results
results = []
# Process headlines using local model
print("Starting classification process (using local model)...")
# Define how many headlines to process (e.g., first 100)
headlines_to_process = headlines[:100]
for i, headline in enumerate(headlines_to_process):
print(f"🔍 Classifying headline {i+1}/{len(headlines_to_process)}: {headline}")
try:
# TOPIC CLASSIFICATION
topic_scores = []
for label in topic_labels:
# Create hypothesis for zero-shot classification
hypothesis = f"This text is about {label}."
# Tokenize inputs
inputs = tokenizer(headline, hypothesis, return_tensors="pt", padding=True, truncation=True)
inputs = {k: v.to(device) for k, v in inputs.items()}
# Get predictions
with torch.no_grad():
outputs = model(**inputs)
# BART gives entailment vs contradiction scores (2 = entailment)
entailment_score = outputs.logits[0][2].item()
topic_scores.append((label, entailment_score))
# Sort by score in descending order
topic_scores.sort(key=lambda x: x[1], reverse=True)
top_topic = topic_scores[0][0]
top_topic_score = topic_scores[0][1]
# TONE CLASSIFICATION
tone_scores = []
for label in tone_labels:
hypothesis = f"The tone of this text is {label}."
inputs = tokenizer(headline, hypothesis, return_tensors="pt", padding=True, truncation=True)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
entailment_score = outputs.logits[0][2].item()
tone_scores.append((label, entailment_score))
tone_scores.sort(key=lambda x: x[1], reverse=True)
top_tone = tone_scores[0][0]
top_tone_score = tone_scores[0][1]
# FRAME CLASSIFICATION
frame_scores = []
for label in frame_labels:
hypothesis = f"This text uses a {label} frame."
inputs = tokenizer(headline, hypothesis, return_tensors="pt", padding=True, truncation=True)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
entailment_score = outputs.logits[0][2].item()
frame_scores.append((label, entailment_score))
frame_scores.sort(key=lambda x: x[1], reverse=True)
top_frame = frame_scores[0][0]
top_frame_score = frame_scores[0][1]
# Store the result
results.append({
"headline": headline,
"topic": top_topic,
"topic_confidence": round(top_topic_score, 4),
"tone": top_tone,
"tone_confidence": round(top_tone_score, 4),
"frame": top_frame,
"frame_confidence": round(top_frame_score, 4)
})
print(f" ✓ Classified as Topic: {top_topic} ({top_topic_score:.4f}), "
f"Tone: {top_tone} ({top_tone_score:.4f}), "
f"Frame: {top_frame} ({top_frame_score:.4f})")
except Exception as e:
print(f"× Error processing headline: {e}")
# Save results periodically to avoid losing progress
if results and (i % 10 == 0 or i == len(headlines_to_process) - 1):
with open("labeled_headlines_progress.json", "w", encoding="utf-8") as f:
json.dump(results, f, indent=2)
print(f"Progress saved: {len(results)} headlines processed so far")
# Save the final results
output_json = "labeled_headlines.json"
output_csv = "structured_labeled_headlines.csv"
print(f"Saving results to {output_json} and {output_csv}")
with open(output_json, "w", encoding="utf-8") as f:
json.dump(results, f, indent=2)
# Save as CSV for further analysis
if results:
df_labels = pd.DataFrame(results)
df_labels.to_csv(output_csv, index=False)
print(f"✅ Process completed. {len(results)} headlines classified and saved locally.")
else:
print("⚠️ No results were collected. Check the errors above.")
print("Classification process complete!")