STron
commited on
Commit
·
43d0a4a
1
Parent(s):
7df2acb
Changed issues due to pull
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ from transformers import RobertaTokenizer, ViTImageProcessor
|
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
import torch
|
7 |
-
<<<<<<< HEAD
|
8 |
import os
|
9 |
import time
|
10 |
import logging
|
@@ -92,46 +91,10 @@ def predict_news(text, image):
|
|
92 |
|
93 |
# Process text input
|
94 |
inputs = tokenizer.encode_plus(text, add_special_tokens = True, return_tensors='np', max_length=80, truncation=True, padding='max_length')
|
95 |
-
=======
|
96 |
-
from torchvision.transforms import v2
|
97 |
-
import os
|
98 |
-
import time
|
99 |
-
|
100 |
-
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
101 |
-
model_path = "./multimodal_model_optimized.onnx"
|
102 |
-
ort_session = ort.InferenceSession(model_path)
|
103 |
-
|
104 |
-
transform = v2.Compose([
|
105 |
-
v2.Resize((256, 256)),
|
106 |
-
v2.ToImage(),
|
107 |
-
v2.ToDtype(torch.float32, scale=True),
|
108 |
-
v2.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
109 |
-
])
|
110 |
-
|
111 |
-
labels = ["Fake", "Real"]
|
112 |
-
|
113 |
-
def predict_news(text, image):
|
114 |
-
if text is None or text.strip() == "":
|
115 |
-
return {labels[0]: 0.0, labels[1]: 0.0}, None, "Please enter some text to analyze."
|
116 |
-
|
117 |
-
if image is None:
|
118 |
-
return {labels[0]: 0.0, labels[1]: 0.0}, None, "Please upload an image to analyze."
|
119 |
-
|
120 |
-
try:
|
121 |
-
inputs = tokenizer.encode_plus(
|
122 |
-
text,
|
123 |
-
add_special_tokens=True,
|
124 |
-
return_tensors='np',
|
125 |
-
max_length=80,
|
126 |
-
truncation=True,
|
127 |
-
padding='max_length'
|
128 |
-
)
|
129 |
-
>>>>>>> 585173c095c709c00b2ab290bb8d69553911f0d5
|
130 |
|
131 |
input_ids = inputs['input_ids']
|
132 |
attention_mask = inputs['attention_mask']
|
133 |
|
134 |
-
<<<<<<< HEAD
|
135 |
logger.info(f"Input IDs shape: {input_ids.shape}")
|
136 |
logger.info(f"Attention mask shape: {attention_mask.shape}")
|
137 |
|
@@ -198,97 +161,17 @@ def predict_news(text, image):
|
|
198 |
except Exception as e:
|
199 |
logger.error(f"Error during analysis: {str(e)}", exc_info=True)
|
200 |
return {labels[0]: 0.0, labels[1]: 0.0, labels[2]: 0.0}, None, f"Error during analysis: {str(e)}"
|
201 |
-
=======
|
202 |
-
image_tensor = transform(image).numpy()
|
203 |
-
|
204 |
-
ort_inputs = {
|
205 |
-
"input_ids": input_ids,
|
206 |
-
"attention_mask": attention_mask,
|
207 |
-
"image": image_tensor.reshape(1, 3, 256, 256) # Ensure correct shape
|
208 |
-
}
|
209 |
-
|
210 |
-
start_time = time.time()
|
211 |
-
outputs = ort_session.run(None, ort_inputs)
|
212 |
-
inference_time = time.time() - start_time
|
213 |
-
|
214 |
-
logits = outputs[0]
|
215 |
-
probs = softmax(logits)[0]
|
216 |
-
|
217 |
-
pred_idx = int(np.argmax(probs))
|
218 |
-
confidence = float(probs[pred_idx])
|
219 |
-
|
220 |
-
if pred_idx == 1: # Real
|
221 |
-
color = "green"
|
222 |
-
message = f"This content appears to be **REAL** with {confidence:.1%} confidence."
|
223 |
-
else: # Fake
|
224 |
-
color = "red"
|
225 |
-
message = f"This content appears to be **FAKE** with {confidence:.1%} confidence."
|
226 |
-
|
227 |
-
analysis = f"""
|
228 |
-
<div style='text-align: center; padding: 10px; background-color: {color}15; border-radius: 5px; margin-top: 10px;'>
|
229 |
-
<span style='font-size: 18px; color: {color}; font-weight: bold;'>{message}</span>
|
230 |
-
<p>Inference time: {inference_time:.3f} seconds</p>
|
231 |
-
</div>
|
232 |
-
"""
|
233 |
-
|
234 |
-
result = {labels[0]: float(probs[0]), labels[1]: float(probs[1])}
|
235 |
-
|
236 |
-
interpretation = image_with_prediction(image, labels[pred_idx], confidence)
|
237 |
-
|
238 |
-
return result, interpretation, analysis
|
239 |
-
|
240 |
-
except Exception as e:
|
241 |
-
return {labels[0]: 0.0, labels[1]: 0.0}, None, f"Error during analysis: {str(e)}"
|
242 |
-
|
243 |
-
def softmax(x):
|
244 |
-
"""Compute softmax values for each sets of scores in x."""
|
245 |
-
e_x = np.exp(x - np.max(x, axis=1, keepdims=True))
|
246 |
-
return e_x / e_x.sum(axis=1, keepdims=True)
|
247 |
-
|
248 |
-
def image_with_prediction(img, label, confidence):
|
249 |
-
"""Return the original image with an overlay showing the prediction"""
|
250 |
-
from PIL import Image, ImageDraw, ImageFont
|
251 |
-
import io
|
252 |
-
|
253 |
-
img_copy = img.copy()
|
254 |
-
draw = ImageDraw.Draw(img_copy)
|
255 |
-
|
256 |
-
width, height = img_copy.size
|
257 |
-
|
258 |
-
overlay = Image.new('RGBA', (width, 40), (0, 0, 0, 150))
|
259 |
-
img_copy.paste(overlay, (0, height-40), overlay)
|
260 |
-
|
261 |
-
text = f"{label}: {confidence:.1%}"
|
262 |
-
|
263 |
-
try:
|
264 |
-
font = ImageFont.truetype("arial.ttf", 20)
|
265 |
-
except IOError:
|
266 |
-
font = ImageFont.load_default()
|
267 |
-
|
268 |
-
text_width = draw.textlength(text, font=font)
|
269 |
-
text_position = ((width - text_width) // 2, height - 35)
|
270 |
-
draw.text(text_position, text, fill=(255, 255, 255), font=font)
|
271 |
-
|
272 |
-
return img_copy
|
273 |
-
>>>>>>> 585173c095c709c00b2ab290bb8d69553911f0d5
|
274 |
|
275 |
examples = [
|
276 |
["COVID-19 vaccine causes severe side effects in 80% of recipients", "https://images.unsplash.com/photo-1605289982774-9a6fef564df8?q=80&w=1000&auto=format&fit=crop"],
|
277 |
["Scientists discover new species of deep-sea fish", "https://images.unsplash.com/photo-1524704796725-9fc3044a58b2?q=80&w=1000&auto=format&fit=crop"],
|
278 |
]
|
279 |
-
|
280 |
-
<<<<<<< HEAD
|
281 |
# Build Gradio interface
|
282 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
283 |
gr.Markdown(
|
284 |
"""
|
285 |
-
# 📰 Fake News Detector (
|
286 |
-
=======
|
287 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
288 |
-
gr.Markdown(
|
289 |
-
"""
|
290 |
-
# 📰 Fake News Detector (BERT + ResNet)
|
291 |
-
>>>>>>> 585173c095c709c00b2ab290bb8d69553911f0d5
|
292 |
|
293 |
This multimodal AI system analyzes both text and images to detect potentially fake news content.
|
294 |
Upload an image and enter a news headline to see if the combination is likely to be real or fake news.
|
@@ -321,22 +204,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
321 |
|
322 |
gr.Markdown(
|
323 |
"""
|
324 |
-
### How it works
|
325 |
-
|
326 |
This system combines:
|
327 |
-
<<<<<<< HEAD
|
328 |
- **RoBERTa**: Analyzes the textual content
|
329 |
- **ViT**: Processes the image data
|
330 |
- **Multimodal Fusion**: Combines both signals to make a prediction
|
331 |
|
332 |
The model was trained on the Fakeddit dataset containing real and fake news pairs with both text and images.
|
333 |
-
=======
|
334 |
-
- **BERT**: Analyzes the textual content
|
335 |
-
- **ResNet**: Processes the image data
|
336 |
-
- **Multimodal Fusion**: Combines both signals to make a prediction
|
337 |
-
|
338 |
-
The model was trained on a dataset of real and fake news pairs containing both text and images.
|
339 |
-
>>>>>>> 585173c095c709c00b2ab290bb8d69553911f0d5
|
340 |
"""
|
341 |
)
|
342 |
|
@@ -347,8 +220,5 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
347 |
)
|
348 |
|
349 |
if __name__ == "__main__":
|
350 |
-
<<<<<<< HEAD
|
351 |
logger.info("Starting Gradio application")
|
352 |
-
=======
|
353 |
-
>>>>>>> 585173c095c709c00b2ab290bb8d69553911f0d5
|
354 |
demo.launch()
|
|
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
import torch
|
|
|
7 |
import os
|
8 |
import time
|
9 |
import logging
|
|
|
91 |
|
92 |
# Process text input
|
93 |
inputs = tokenizer.encode_plus(text, add_special_tokens = True, return_tensors='np', max_length=80, truncation=True, padding='max_length')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
input_ids = inputs['input_ids']
|
96 |
attention_mask = inputs['attention_mask']
|
97 |
|
|
|
98 |
logger.info(f"Input IDs shape: {input_ids.shape}")
|
99 |
logger.info(f"Attention mask shape: {attention_mask.shape}")
|
100 |
|
|
|
161 |
except Exception as e:
|
162 |
logger.error(f"Error during analysis: {str(e)}", exc_info=True)
|
163 |
return {labels[0]: 0.0, labels[1]: 0.0, labels[2]: 0.0}, None, f"Error during analysis: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
|
165 |
examples = [
|
166 |
["COVID-19 vaccine causes severe side effects in 80% of recipients", "https://images.unsplash.com/photo-1605289982774-9a6fef564df8?q=80&w=1000&auto=format&fit=crop"],
|
167 |
["Scientists discover new species of deep-sea fish", "https://images.unsplash.com/photo-1524704796725-9fc3044a58b2?q=80&w=1000&auto=format&fit=crop"],
|
168 |
]
|
169 |
+
|
|
|
170 |
# Build Gradio interface
|
171 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
172 |
gr.Markdown(
|
173 |
"""
|
174 |
+
# 📰 Fake News Detector (RoBERTa + ViT)
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
This multimodal AI system analyzes both text and images to detect potentially fake news content.
|
177 |
Upload an image and enter a news headline to see if the combination is likely to be real or fake news.
|
|
|
204 |
|
205 |
gr.Markdown(
|
206 |
"""
|
|
|
|
|
207 |
This system combines:
|
|
|
208 |
- **RoBERTa**: Analyzes the textual content
|
209 |
- **ViT**: Processes the image data
|
210 |
- **Multimodal Fusion**: Combines both signals to make a prediction
|
211 |
|
212 |
The model was trained on the Fakeddit dataset containing real and fake news pairs with both text and images.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
"""
|
214 |
)
|
215 |
|
|
|
220 |
)
|
221 |
|
222 |
if __name__ == "__main__":
|
|
|
223 |
logger.info("Starting Gradio application")
|
|
|
|
|
224 |
demo.launch()
|