shainaraza commited on
Commit
f7febd7
·
verified ·
1 Parent(s): a016be0

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +279 -49
README.md CHANGED
@@ -1,76 +1,306 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
- # Llama3.2 NLP Bias Classifier
3
 
4
- This model merges the base Llama-3.2 architecture with a custom adapter to classify text for disinformation likelihood, leveraging NLP techniques for high accuracy in distinguishing manipulative content from unbiased sources. It focuses on detecting rhetorical techniques commonly used in disinformation, offering both 'Likely' and 'Unlikely' classifications based on structured indicators.
 
 
 
 
 
5
 
6
  ## Model Details
7
 
8
- - **Base Model**: [meta-llama/Llama-3.2-1B-Instruct](https://huggingface.co/meta-llama/Lla
9
- - **Deployment Environment**: Configured for GPU (CUDA) support.
10
- - **Training Data**: [vector-institute/newsmediabias-plus](https://huggingface.co/datasets/vector-institute/newsmediabias-plus)
11
- - **Sampled Data to test inference**: [sample data](https://huggingface.co/vector-institute/Llama3.2-Multimodal-Newsmedia-Bias-Detector/blob/main/sampled-data/sample_dataset.csv)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- ## Model Usage
 
 
 
 
 
 
 
 
14
 
15
- ```python
16
  import torch
17
- from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  import pandas as pd
 
 
19
 
20
- # Load tokenizer and model
21
- tokenizer = AutoTokenizer.from_pretrained("save_directory")
22
- model = AutoModelForCausalLM.from_pretrained("save_directory", torch_dtype=torch.float16).to(device)
23
- ```
24
 
25
- ### Generating Predictions
 
26
 
27
- The model evaluates text for disinformation by identifying rhetorical techniques. To classify input text, use the `generate_response` function:
 
 
 
 
 
28
 
29
- ```python
30
- def generate_response(model, prompt):
31
- inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=1024).to(device)
32
- outputs = model.generate(inputs['input_ids'], max_new_tokens=50, temperature=0.7, top_p=0.95)
33
- return tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
34
- ```
 
 
35
 
36
- ## Dataset and Evaluation
 
 
37
 
38
- - **Input Dataset**: Sample data from `sample_dataset.csv` containing balanced examples of 'Likely' and 'Unlikely' disinformation.
39
- - **Labeling Criteria**: Text classified as "Likely" or "Unlikely" disinformation based on the presence of rhetorical techniques (e.g., exaggeration, emotional appeal).
40
- - **Metrics**: Precision, recall, F1 score, and accuracy, computed with `sklearn.metrics`.
41
 
42
- ### Model Performance
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- | Label | Precision | Recall | F1 Score |
46
- |----------------|-----------|--------|----------|
47
- | Unlikely (0) | 78% | 82% | 79.95% |
48
- | Likely (1) | 81% | 85% | 82.95% |
49
- | **Accuracy** | | | 87% |
50
 
51
- ### Example Classification
52
 
53
- ```plaintext
54
- Example 1:
55
- Text: "This new vaccine causes severe side effects in a majority of patients, which is something the authorities don’t want you to know."
56
- Actual Label: Likely (1)
57
- Model Prediction: Likely (1)
58
- ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
- ## Limitations and Future Work
 
 
61
 
62
- - **False Positives**: May misclassify subjective statements lacking explicit disinformation techniques.
63
- - **Inference Speed**: Optimization for deployment on different devices could improve real-time applicability.
64
 
65
- ## Citation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
- If you use this model, please cite our work as follows:
68
  ```
69
- @inproceedings{Raza2024LlamaBiasClassifier,
70
- title={Llama3.2 NLP Bias Classifier for Disinformation Detection},
71
- author={Shaina Raza},
72
- year={2024}
73
- }
74
  ```
75
 
76
- For more information, contact Shaina Raza, PhD at [email protected]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: cc
3
+ datasets:
4
+ - vector-institute/newsmediabias-plus
5
+ language:
6
+ - en
7
+ metrics:
8
+ - accuracy
9
+ - precision
10
+ base_model:
11
+ - meta-llama/Llama-3.2-11B-Vision-Instruct
12
+ ---
13
 
 
14
 
15
+ # LLaMA 3.2 Multimodal News Media Bias Detector
16
+
17
+
18
+ ## Model Overview
19
+
20
+ The LLaMA 3.2 Multimodal News Media Bias Detector is a fine-tuned version of the LLaMA 2 Vision-Instruct model, designed to assess potential disinformation in news media articles by analyzing both text and image content. The model identifies the presence of rhetorical techniques commonly associated with disinformation and classifies articles as 'Likely' or 'Unlikely' to be disinformation.
21
 
22
  ## Model Details
23
 
24
+ - **Base Model**: [meta-llama/Llama-3.2-11B-Vision-Instruct](https://huggingface.co/meta-llama/Llama-3.2-11B-Vision-Instruct)
25
+ - **Architecture**: Multimodal (Text and Image)
26
+ - **Model Size**: 11B parameters
27
+ - **Quantization**: 4-bit quantization (`nf4`) using `bitsandbytes` for efficient inference
28
+ - **Framework**: [Hugging Face Transformers](https://github.com/huggingface/transformers)
29
+ - **Fine-tuning**: The model was fine-tuned using the [PEFT](https://github.com/huggingface/peft) library with LoRA adapters
30
+
31
+ ## Intended Use
32
+
33
+ ### Primary Use Cases
34
+
35
+ - **Disinformation Detection**: Analyze news articles and accompanying images to detect potential disinformation.
36
+ - **Media Analysis**: Assist researchers and analysts in studying bias and rhetorical techniques in media content.
37
+ - **Educational Tool**: Serve as a resource for learning about multimodal models and disinformation detection.
38
+
39
+ ### Limitations
40
+
41
+ - **Not a Fact-Checker**: The model does not verify factual accuracy but assesses rhetorical techniques.
42
+ - **Bias in Training Data**: The model's judgments may reflect biases present in the training data.
43
+ - **Language and Domain Limitations**: Primarily trained on English-language news articles; performance may degrade on other languages or domains.
44
+
45
+ ## How to Use
46
+ First access through huggingface cli
47
+
48
+ ``` ! huggingface-cli login ```
49
+
50
+ **Sampled Data Usage**
51
+
52
+ Please use the sampled data available at either of the following repositories for testing or development purposes with the LLaMA 3.2 Multimodal News Media Bias Detector:
53
+
54
+ - [Sampled Data on Hugging Face - LLaMA 3.2 Multimodal News Media Bias Detector](https://huggingface.co/vector-institute/Llama3.2-Multimodal-Newsmedia-Bias-Detector/tree/main/sampled-data)
55
+ - [NewsMediaBias-Plus Dataset on Hugging Face](https://huggingface.co/datasets/vector-institute/newsmediabias-plus)
56
+
57
+ These datasets are pre-configured to be compatible with the model and include a variety of news articles and images that have been annotated for potential disinformation, allowing for effective model evaluation and demonstration.
58
+
59
 
60
+ ### Installation
61
+
62
+ Ensure you have the required libraries installed:
63
+
64
+ ```
65
+ pip install transformers accelerate bitsandbytes
66
+ ```
67
+ ```
68
+ ### Loading the Model
69
 
 
70
  import torch
71
+ from transformers import AutoModelForVision2Seq, AutoProcessor, BitsAndBytesConfig
72
+
73
+ merged_model_save_path= "vector-institute/Llama3.2-Multimodal-Newsmedia-Bias-Detector"
74
+ # Load quantization config if used during saving
75
+ bnb_config = BitsAndBytesConfig(
76
+ load_in_4bit=True,
77
+ bnb_4bit_use_double_quant=True,
78
+ bnb_4bit_quant_type="nf4",
79
+ bnb_4bit_compute_dtype=torch.bfloat16
80
+ )
81
+
82
+ # Load the merged model
83
+ model = AutoModelForVision2Seq.from_pretrained(
84
+ merged_model_save_path,
85
+ device_map="auto",
86
+ torch_dtype=torch.bfloat16,
87
+ quantization_config=bnb_config
88
+ )
89
+ model.eval()
90
+
91
+ # Load the processor
92
+ processor = AutoProcessor.from_pretrained(merged_model_save_path)
93
+
94
  import pandas as pd
95
+ import os
96
+ from PIL import Image
97
 
98
+ # Path to your dataset CSV and image folder
99
+ dataset_csv_path = 'sample_dataset.csv'
100
+ image_folder_path = 'sampled_images'
 
101
 
102
+ # Load the DataFrame
103
+ df = pd.read_csv(dataset_csv_path)
104
 
105
+ # Function to prepare samples from the DataFrame
106
+ def prepare_samples_from_dataframe(df, image_folder_path):
107
+ samples = []
108
+ for index, row in df.iterrows():
109
+ unique_id = row['unique_id'] # Replace with the column that contains the image identifier
110
+ text_content = row['first_paragraph'] # Replace with your text column name
111
 
112
+ # Find the image file
113
+ possible_extensions = ['jpg', 'jpeg', 'png']
114
+ image_path = None
115
+ for ext in possible_extensions:
116
+ img_path = os.path.join(image_folder_path, f"{unique_id}.{ext}")
117
+ if os.path.exists(img_path):
118
+ image_path = img_path
119
+ break
120
 
121
+ if image_path is None:
122
+ print(f"No image found for ID {unique_id}")
123
+ continue
124
 
125
+ image = Image.open(image_path).convert("RGB")
126
+ max_size = (224, 224)
127
+ image.thumbnail(max_size, Image.Resampling.LANCZOS)
128
 
129
+ # Prepare the prompt text
130
+ sample_text = (
131
+ "Assess the text and image below for potential disinformation (try finding deliberately misleading or biased information) by identifying the presence of rhetorical techniques listed.\n"
132
+ "If you find any of the listed rhetorical techniques, then the article is likely disinformation; if not, it is likely not disinformation.\n\n"
133
+ "Rhetorical Techniques Checklist:\n"
134
+ "- Emotional Appeal: Uses language that intentionally invokes extreme emotions like fear or anger, aiming to distract from lack of factual backing.\n"
135
+ "- Exaggeration and Hyperbole: Makes claims that are unsupported by evidence, or presents normal situations as extraordinary to manipulate perceptions.\n"
136
+ "- Bias and Subjectivity: Presents information in a way that unreasonably favors one perspective, omitting key facts that might provide balance.\n"
137
+ "- Repetition: Uses repeated messaging of specific points or misleading statements to embed a biased viewpoint in the reader's mind.\n"
138
+ "- Specific Word Choices: Employs emotionally charged or misleading terms to sway opinions subtly, often in a manipulative manner.\n"
139
+ "- Appeals to Authority: References authorities who lack relevant expertise or cites sources that do not have the credentials to be considered authoritative in the context.\n"
140
+ "- Lack of Verifiable Sources: Relies on sources that either cannot be verified or do not exist, suggesting a fabrication of information.\n"
141
+ "- Logical Fallacies: Engages in flawed reasoning such as circular reasoning, strawman arguments, or ad hominem attacks that undermine logical debate.\n"
142
+ "- Conspiracy Theories: Propagates theories that lack proof and often contain elements of paranoia or implausible scenarios as facts.\n"
143
+ "- Inconsistencies and Factual Errors: Contains multiple contradictions or factual inaccuracies that are easily disprovable, indicating a lack of concern for truth.\n"
144
+ "- Selective Omission: Deliberately leaves out crucial information that is essential for a fair understanding of the topic, skewing perception.\n"
145
+ "- Manipulative Framing: Frames issues in a way that leaves out alternative perspectives or possible explanations, focusing only on aspects that support a biased narrative.\n\n"
146
+ f"{text_content}\n\n"
147
+ "Please **only** provide your answer in the format: 'Classification: Likely' or 'Classification: Unlikely'. Do not include any additional text or explanation."
148
+ )
149
 
150
+ sample = {
151
+ "unique_id": unique_id,
152
+ "messages": [
153
+ {
154
+ "role": "user",
155
+ "content": [
156
+ {
157
+ "type": "text",
158
+ "text": sample_text,
159
+ },
160
+ {
161
+ "type": "image",
162
+ "image": image,
163
+ }
164
+ ],
165
+ }
166
+ ]
167
+ }
168
 
169
+ samples.append(sample)
 
 
 
 
170
 
171
+ return samples
172
 
173
+ import re
174
+
175
+ def extract_assistant_reply(generated_text):
176
+ lines = generated_text.strip().split('\n')
177
+ for idx, line in enumerate(lines):
178
+ if line.strip().lower() == 'assistant':
179
+ assistant_reply = '\n'.join(lines[idx+1:]).strip()
180
+ return assistant_reply
181
+ return lines[-1].strip()
182
+
183
+ def extract_classification(assistant_reply):
184
+ match = re.search(r'Classification:\s*(Likely|Unlikely)', assistant_reply, re.IGNORECASE)
185
+ if match:
186
+ return match.group(1).capitalize()
187
+ else:
188
+ # Attempt to find 'Likely' or 'Unlikely' anywhere in the reply
189
+ match = re.search(r'\b(Likely|Unlikely)\b', assistant_reply, re.IGNORECASE)
190
+ if match:
191
+ return match.group(1).capitalize()
192
+ else:
193
+ return 'Unknown'
194
+
195
+ def generate_prediction(sample):
196
+ # Remove the model.to(device) line
197
+ # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
198
+ # model.to(device)
199
 
200
+ # Prepare the input texts and images
201
+ texts = processor.apply_chat_template(sample["messages"], tokenize=False)
202
+ image_input = sample["messages"][0]['content'][1]['image']
203
 
204
+ # Prepare the inputs without moving them to a device
205
+ inputs = processor(text=texts, images=image_input, return_tensors="pt", padding=True)
206
 
207
+ # Ensure that the inputs are on the same device as the model's embeddings
208
+ # This is handled internally by the model when using device_map="auto"
209
+
210
+ with torch.no_grad():
211
+ outputs = model.generate(
212
+ **inputs,
213
+ max_new_tokens=20,
214
+ do_sample=False,
215
+ num_beams=1,
216
+ )
217
+
218
+ generated_texts = processor.batch_decode(outputs, skip_special_tokens=True)
219
+ assistant_reply = extract_assistant_reply(generated_texts[0])
220
+
221
+ return assistant_reply
222
+
223
+ # Prepare the samples from the DataFrame
224
+ samples = prepare_samples_from_dataframe(df, image_folder_path)
225
+
226
+ # Limit to a subset if desired
227
+ samples_to_infer = samples[:5] # For example, take the first 5 samples
228
+
229
+ # Run inference and collect results
230
+ results = []
231
+
232
+ for sample in samples_to_infer:
233
+ assistant_reply = generate_prediction(sample)
234
+ predicted_label = extract_classification(assistant_reply)
235
+
236
+ # Collect results
237
+ result = {
238
+ 'unique_id': sample['unique_id'],
239
+ 'assistant_reply': assistant_reply,
240
+ 'predicted_label': predicted_label,
241
+ # Add any other fields you need from the sample or DataFrame
242
+ }
243
+ results.append(result)
244
+
245
+ # Display the results
246
+ print(f"Sample ID: {sample['unique_id']}")
247
+ print("Assistant's Reply:")
248
+ print(assistant_reply)
249
+ print(f"Predicted Label: {predicted_label}")
250
+ print("-" * 50)
251
+
252
+ results_df = pd.DataFrame(results)
253
+ # Save to CSV if desired
254
+ results_df.to_csv('inference_results.csv', index=False)
255
+ ```
256
+ ### Example Output
257
 
 
258
  ```
259
+ Assistant's Reply:
260
+ Classification: Likely
 
 
 
261
  ```
262
 
263
+ ## Training Data
264
+
265
+ The model is fine-tuned on [NewsMediaBias-Plus Dataset on Hugging Face](https://huggingface.co/datasets/vector-institute/newsmediabias-plus)
266
+
267
+
268
+ ## Training Procedure
269
+
270
+ - **Fine-Tuning**: The model was fine-tuned using the PEFT library with LoRA adapters to efficiently adapt the base model to the disinformation detection task.
271
+ - **Quantization**: 4-bit quantization was applied using `bitsandbytes` to optimize inference performance.
272
+ - **Hyperparameters**: Standard hyperparameters were used, with adjustments made for optimal performance on the custom dataset.
273
+
274
+ ## Evaluation
275
+
276
+ The model was evaluated on a balanced test set with the following metrics:
277
+
278
+ - **Accuracy**: *88%*
279
+ - **Precision**: *78%*
280
+ - **Recall**: *75%*
281
+
282
+ *Note: Replace the above metrics with actual results from your evaluations.*
283
+
284
+ ## Limitations and Biases
285
+
286
+ - **Biases in Data**: The model's performance and judgments are influenced by the biases present in the training data.
287
+ - **False Positives/Negatives**: The model may incorrectly classify some articles due to subtle rhetorical techniques or lack thereof.
288
+ - **Ethical Considerations**: Users should be cautious when interpreting the model's outputs and consider them as part of a broader analysis.
289
+
290
+ ## Ethical Considerations
291
+
292
+ - **User Responsibility**: The model should not be used as the sole basis for making critical decisions.
293
+ - **Data Privacy**: Ensure compliance with data privacy laws when using the model with user data.
294
+ - **Misuse Prevention**: The model should not be used to unjustly label or discriminate against individuals or groups.
295
+
296
+
297
+
298
+ ## License
299
+
300
+ This model is licensed under CC BY-NC-SA 4.0.
301
+
302
+ Developed by Dr. Shaina Raza at the Vector Institute. Contact: [[email protected]](mailto:[email protected])
303
+
304
+ **Disclaimer**: The model is provided "as is" without any warranty. The developers are not responsible for any consequences arising from the use of this model.
305
+
306
+ ```