Views
No views yet

pip install catboost torchvision transformers numpy pandas pillow joblib peft huggingface-hubpip install -r requirements.txt1# Download best_model.pkl
2wget https://github.com/asShidqi/return-refund-prediction/raw/main/best_model.pkl1# 1. Install dependencies
2# !pip install catboost torchvision transformers numpy pandas pillow
3
4import torch
5import numpy as np
6import pandas as pd
7from PIL import Image
8from joblib import load
9from catboost import CatBoostClassifier
10from transformers import AutoProcessor, AutoModel, AutoTokenizer
11
12# 2. Load pretrained models
13device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
15# DINOv2 for image
16dino_model_name = "facebook/dinov2-large"
17dino_processor = AutoProcessor.from_pretrained(dino_model_name)
18dino_model = AutoModel.from_pretrained(dino_model_name).to(device)
19dino_model.eval()
20
21# Qwen for text
22text_model_name = "Qwen/Qwen3-Embedding-0.6B" # adjust to your use case
23text_tokenizer = AutoTokenizer.from_pretrained(text_model_name)
24text_model = AutoModel.from_pretrained(text_model_name).to(device)
25text_model.eval()
26
27# CatBoost model
28model = load("best_model.pkl") # path to your trained model
29
30# 3. Define embedding functions
31def embed_image(image_path):
32 image = Image.open(image_path).convert("RGB").resize((600, 600))
33 inputs = dino_processor(images=image, return_tensors="pt").to(device)
34 with torch.no_grad():
35 features = dino_model(**inputs).last_hidden_state.mean(dim=1)
36 return features.cpu().numpy().squeeze()
37
38def embed_text(text):
39 inputs = text_tokenizer(text, return_tensors="pt", padding=True, truncation=True).to(device)
40 with torch.no_grad():
41 outputs = text_model(**inputs)
42 embeddings = outputs.last_hidden_state.mean(dim=1)
43 return embeddings.cpu().numpy().squeeze()
44
45# 4. Prepare your input
46img_main_path = "example_main.jpg"
47img_review_path = "example_review.jpg"
48caption = "This is the product description from the user."
49
50# 5. Embed each input
51embed_main = embed_image(img_main_path)
52embed_review = embed_image(img_review_path)
53embed_caption = embed_text(caption)
54
55# 6. Convert each to DataFrame columns
56df_img_main_embed = pd.DataFrame(embed_main.reshape(1, -1), columns=[f"img_main_{i}" for i in range(embed_main.shape[0])])
57df_img_review_embed = pd.DataFrame(embed_review.reshape(1, -1), columns=[f"img_review_{i}" for i in range(embed_review.shape[0])])
58df_text_embed = pd.DataFrame(embed_caption.reshape(1, -1), columns=[f"text_feat_{i}" for i in range(embed_caption.shape[0])])
59
60# 7. Combine all into one dataset
61combine_dataset = pd.concat([df_img_main_embed, df_img_review_embed, df_text_embed], axis=1)
62
63# 8. Predict
64prediction = model.predict(combine_dataset)
65print("Prediction:", prediction)0: Klaim palsu1: Klaim valid1import torch
2import torch.nn as nn
3from transformers import AutoModel, AutoTokenizer, AutoImageProcessor
4from peft import get_peft_model, PeftModel, LoraConfig
5from PIL import Image
6import numpy as np
7import joblib
8import catboost
9from huggingface_hub import hf_hub_download
10
11device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
13# -------------------------
14# 1. Helper load image
15# -------------------------
16def load_image(path):
17 return Image.open(path).convert("RGB")
18
19# -------------------------
20# 2. Define SiameseModel (projection head + backbone)
21# -------------------------
22class SiameseModel(nn.Module):
23 def __init__(self, backbone, emb_dim=256, pool="cls", normalize=True):
24 super().__init__()
25 self.backbone = backbone
26 self.pool = pool
27 self.normalize = normalize
28 hidden_size = getattr(backbone.config, "hidden_size", 1024)
29 self.proj = nn.Sequential(
30 nn.Linear(hidden_size, max(hidden_size // 2, emb_dim)),
31 nn.ReLU(),
32 nn.Dropout(0.1),
33 nn.Linear(max(hidden_size // 2, emb_dim), emb_dim)
34 )
35
36 def _safe_forward(self, module, **kwargs):
37 return module.forward(**kwargs)
38
39 def encode(self, pixel: torch.Tensor):
40 out = self._safe_forward(self.backbone, pixel_values=pixel, return_dict=True)
41 if hasattr(out, "pooler_output") and out.pooler_output is not None:
42 h = out.pooler_output
43 else:
44 h = out.last_hidden_state[:, 0, :]
45 emb = self.proj(h)
46 if self.normalize:
47 emb = nn.functional.normalize(emb, dim=-1)
48 return emb
49
50# -------------------------
51# 3. Load DINO backbone + LoRA + projection head
52# -------------------------
53backbone_dino = AutoModel.from_pretrained("facebook/dinov2-large")
54lora_cfg = LoraConfig(
55 r=8, lora_alpha=16, target_modules=["query","value"], lora_dropout=0.1,
56 bias="none", task_type="FEATURE_EXTRACTION"
57)
58backbone_dino = get_peft_model(backbone_dino, lora_cfg)
59backbone_dino.to(device)
60
61# Load projection head + backbone weights
62model_dino = SiameseModel(backbone_dino).to(device)
63path_proj = hf_hub_download(
64 repo_id="shidqii/dino-siamese-full",
65 filename="siamese_model.pt"
66)
67state_dict = torch.load(path_proj, map_location=device)
68model_dino.load_state_dict(state_dict)
69model_dino.eval()
70
71processor_dino = AutoImageProcessor.from_pretrained("facebook/dinov2-large")
72
73# -------------------------
74# 4. Load Qwen LoRA from HuggingFace
75# -------------------------
76tokenizer_qwen = AutoTokenizer.from_pretrained("shidqii/qwen-embed-lora")
77base_model_qwen = AutoModel.from_pretrained("Qwen/Qwen3-Embedding-0.6B")
78model_qwen_lora = PeftModel.from_pretrained(base_model_qwen, "shidqii/qwen-embed-lora")
79model_qwen_lora.to(device)
80model_qwen_lora.eval()
81
82# -------------------------
83# 5. Load CatBoost
84# -------------------------
85cat_model = joblib.load("best_model_finetune.pkl")
86
87# -------------------------
88# 6. Helpers embed image & text
89# -------------------------
90@torch.no_grad()
91def embed_image(paths):
92 imgs = [load_image(p) for p in paths]
93 inputs = processor_dino(images=imgs, return_tensors="pt", padding=True).to(device)
94 emb = model_dino.encode(inputs["pixel_values"])
95 return emb.cpu().numpy()
96
97@torch.no_grad()
98def embed_caption(texts):
99 embs = []
100 for i in range(0, len(texts), 32):
101 batch = texts[i:i+32]
102 tokens = tokenizer_qwen(batch, padding=True, truncation=True, return_tensors="pt").to(device)
103 out = model_qwen_lora.base_model(**tokens)
104 last_hidden = out.last_hidden_state
105 mask = tokens["attention_mask"].unsqueeze(-1).expand(last_hidden.size())
106 mean_pooled = torch.sum(last_hidden * mask, 1) / torch.clamp(mask.sum(1), min=1e-9)
107 embs.append(mean_pooled.cpu())
108 return np.vstack(embs)
109
110# -------------------------
111# 7. Predict function
112# -------------------------
113def predict(img_main_path, img_review_path, caption):
114 img_emb = embed_image([img_main_path, img_review_path])
115 caption_emb = embed_caption([caption])[0]
116
117 features = np.concatenate([img_emb[0], img_emb[1], caption_emb])
118 return cat_model.predict([features])[0]
119
120# -------------------------
121# 8. Example usage
122# -------------------------
123result = predict(
124 "image-main.png",
125 "image-review.png",
126 "caption"
127)
128print("Predicted label:", result)peft: Untuk Parameter-Efficient Fine-Tuninghuggingface_hub: Untuk mengunduh model terlatih dari repositori HuggingFacepip install peft huggingface_hub