Views
No views yet
openai/clip-vit-base-patch32, fine-tuned for misinformation detectiontransformerstabulate.1pip install transformers torch pillow tabulate
2
3from transformers import CLIPModel, CLIPProcessor
4from PIL import Image
5import torch
6import torch.nn.functional as F
7from tabulate import tabulate
8
9# Load your fine-tuned model
10model = CLIPModel.from_pretrained("jarif/Multimodal-BNEN-Fake-News-Scanner-Model")
11processor = CLIPProcessor.from_pretrained("jarif/Multimodal-BNEN-Fake-News-Scanner-Model")
12
13# Define class prompts in Bangla
14class_texts = ["এটি ফেক নিউজ", "এটি রিয়েল নিউজ"] # ["This is fake news", "This is real news"]
15
16# --- Image Classification ---
17image = Image.open("your_image.jpg").convert("RGB") # Replace with your image path
18image_inputs = processor(images=image, return_tensors="pt")
19image_emb = model.get_image_features(**image_inputs)
20
21# --- Text Classification ---
22text = "পদ্মা নদীর প্রবল স্রোতে লঞ্চঘাট বিলীন হয়েছে।"
23text_inputs = processor(text=text, return_tensors="pt", padding=True, truncation=True)
24text_emb = model.get_text_features(**text_inputs)
25
26# Get embeddings for class prompts
27class_inputs = processor(text=class_texts, return_tensors="pt", padding=True, truncation=True)
28class_embs = model.get_text_features(**class_inputs)
29
30# Normalize embeddings (cosine similarity)
31image_emb = F.normalize(image_emb, p=2, dim=-1)
32text_emb = F.normalize(text_emb, p=2, dim=-1)
33class_embs = F.normalize(class_embs, p=2, dim=-1)
34
35# Compute similarity
36image_sims = (image_emb @ class_embs.T).squeeze(0)
37text_sims = (text_emb @ class_embs.T).squeeze(0)
38
39# Predict
40image_pred = image_sims.argmax().item()
41text_pred = text_sims.argmax().item()
42
43image_label = "🛑 Fake" if image_pred == 0 else "✅ Real"
44text_label = "🛑 Fake" if text_pred == 0 else "✅ Real"
45
46# Create result table
47table = [
48 ["ImageRelation", image_label],
49 ["Text Relation", text_label]
50]
51
52# Print formatted table
53print(tabulate(table, headers=["Modality", "Prediction"], tablefmt="fancy_grid"))╒════════════════════════════════════════════════════════════════════╕
│ Modality │ Prediction │
╞════════════════════════════════════════════════════════════════════╡
│ Image Relation │ ✅ Real │
│ Text Relation │ 🛑 Fake │
╘════════════════════════════════════════════════════════════════════╛