Instructions to use NYUAD-ComNets/Gemma3_meme_classification with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use NYUAD-ComNets/Gemma3_meme_classification with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="NYUAD-ComNets/Gemma3_meme_classification") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("NYUAD-ComNets/Gemma3_meme_classification") model = AutoModelForMultimodalLM.from_pretrained("NYUAD-ComNets/Gemma3_meme_classification", device_map="auto") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use NYUAD-ComNets/Gemma3_meme_classification with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "NYUAD-ComNets/Gemma3_meme_classification" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "NYUAD-ComNets/Gemma3_meme_classification", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/NYUAD-ComNets/Gemma3_meme_classification
- SGLang
How to use NYUAD-ComNets/Gemma3_meme_classification with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "NYUAD-ComNets/Gemma3_meme_classification" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "NYUAD-ComNets/Gemma3_meme_classification", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "NYUAD-ComNets/Gemma3_meme_classification" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "NYUAD-ComNets/Gemma3_meme_classification", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Unsloth Studio
How to use NYUAD-ComNets/Gemma3_meme_classification with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for NYUAD-ComNets/Gemma3_meme_classification to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for NYUAD-ComNets/Gemma3_meme_classification to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for NYUAD-ComNets/Gemma3_meme_classification to start chatting
Load model with FastModel
pip install unsloth from unsloth import FastModel model, tokenizer = FastModel.from_pretrained( model_name="NYUAD-ComNets/Gemma3_meme_classification", max_seq_length=2048, ) - Docker Model Runner
How to use NYUAD-ComNets/Gemma3_meme_classification with Docker Model Runner:
docker model run hf.co/NYUAD-ComNets/Gemma3_meme_classification
Update README.md
Browse files
README.md
CHANGED
|
@@ -30,7 +30,7 @@ The proposed solutions offer a more nuanced understanding of memes for accurate
|
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
-
# Finetuned Gemma 3 Embedding Model
|
| 34 |
|
| 35 |
``` python
|
| 36 |
|
|
@@ -39,13 +39,17 @@ import torch
|
|
| 39 |
import torch._dynamo
|
| 40 |
from tqdm import tqdm # Progress bar library
|
| 41 |
from unsloth import FastVisionModel
|
|
|
|
| 42 |
from datasets import load_dataset
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
lis=[]
|
| 46 |
lis.append({"type": "text", "text": sample["text"]})
|
| 47 |
lis.append({"type": "image", "image": sample["image"]})
|
| 48 |
-
|
| 49 |
conversation = [
|
| 50 |
{
|
| 51 |
"role": "system",
|
|
@@ -61,11 +65,12 @@ def convert_to_conversation(sample):
|
|
| 61 |
pass
|
| 62 |
|
| 63 |
dataset = load_dataset("QCRI/ArGuard-Task1", split="dev_test")
|
|
|
|
| 64 |
converted_dataset = [convert_to_conversation(sample) for sample in dataset]
|
| 65 |
|
| 66 |
# 1. Disable compiler optimization conflicts
|
| 67 |
-
torch._dynamo.config.disable = True
|
| 68 |
-
torch._dynamo.reset()
|
| 69 |
|
| 70 |
# 2. Load your fine-tuned model and processor
|
| 71 |
model_path = "NYUAD-ComNets/Gemma3_meme_classification"
|
|
@@ -81,7 +86,7 @@ instruction = "classify meme into Hateful or Not"
|
|
| 81 |
all_embeddings = []
|
| 82 |
labels_list = []
|
| 83 |
|
| 84 |
-
num_iterations =
|
| 85 |
|
| 86 |
print(f"Starting embedding extraction for {num_iterations} items...")
|
| 87 |
|
|
@@ -104,24 +109,26 @@ for idx in tqdm(range(num_iterations), desc="Extracting Embeddings"):
|
|
| 104 |
templated_text = processor.apply_chat_template(conversation, tokenize=False)
|
| 105 |
inputs = processor(text=templated_text, images=sample_image, return_tensors="pt").to("cuda")
|
| 106 |
|
| 107 |
-
|
|
|
|
| 108 |
with torch.no_grad():
|
| 109 |
with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
|
| 110 |
-
outputs = model
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
|
|
|
| 125 |
labels_list.append(sample_label)
|
| 126 |
|
| 127 |
except Exception as e:
|
|
@@ -131,7 +138,8 @@ for idx in tqdm(range(num_iterations), desc="Extracting Embeddings"):
|
|
| 131 |
embedding_matrix = np.vstack(all_embeddings)
|
| 132 |
|
| 133 |
print("Final Concatenated Array Shape:", embedding_matrix.shape)
|
| 134 |
-
|
|
|
|
| 135 |
```
|
| 136 |
|
| 137 |
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
+
# Finetuned Gemma 3 Embedding Model with mean pooling
|
| 34 |
|
| 35 |
``` python
|
| 36 |
|
|
|
|
| 39 |
import torch._dynamo
|
| 40 |
from tqdm import tqdm # Progress bar library
|
| 41 |
from unsloth import FastVisionModel
|
| 42 |
+
|
| 43 |
from datasets import load_dataset
|
| 44 |
|
| 45 |
+
instruction = "classify meme into Hateful or Not"
|
| 46 |
+
|
| 47 |
+
def convert_to_conversation(sample):
|
| 48 |
+
|
| 49 |
lis=[]
|
| 50 |
lis.append({"type": "text", "text": sample["text"]})
|
| 51 |
lis.append({"type": "image", "image": sample["image"]})
|
| 52 |
+
|
| 53 |
conversation = [
|
| 54 |
{
|
| 55 |
"role": "system",
|
|
|
|
| 65 |
pass
|
| 66 |
|
| 67 |
dataset = load_dataset("QCRI/ArGuard-Task1", split="dev_test")
|
| 68 |
+
|
| 69 |
converted_dataset = [convert_to_conversation(sample) for sample in dataset]
|
| 70 |
|
| 71 |
# 1. Disable compiler optimization conflicts
|
| 72 |
+
#torch._dynamo.config.disable = True
|
| 73 |
+
#torch._dynamo.reset()
|
| 74 |
|
| 75 |
# 2. Load your fine-tuned model and processor
|
| 76 |
model_path = "NYUAD-ComNets/Gemma3_meme_classification"
|
|
|
|
| 86 |
all_embeddings = []
|
| 87 |
labels_list = []
|
| 88 |
|
| 89 |
+
num_iterations = len(converted_dataset)
|
| 90 |
|
| 91 |
print(f"Starting embedding extraction for {num_iterations} items...")
|
| 92 |
|
|
|
|
| 109 |
templated_text = processor.apply_chat_template(conversation, tokenize=False)
|
| 110 |
inputs = processor(text=templated_text, images=sample_image, return_tensors="pt").to("cuda")
|
| 111 |
|
| 112 |
+
|
| 113 |
+
# 5. Forward Pass
|
| 114 |
with torch.no_grad():
|
| 115 |
with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
|
| 116 |
+
outputs = model(**inputs, output_hidden_states=True, return_dict=True)
|
| 117 |
+
|
| 118 |
+
# 6. Extract final layer and attention mask
|
| 119 |
+
last_hidden_states = outputs.hidden_states[-1] # [batch_size, seq_len, hidden_dim]
|
| 120 |
+
attention_mask = inputs["attention_mask"] # [batch_size, seq_len]
|
| 121 |
+
|
| 122 |
+
# 7. Masked Mean Pooling (Ignores padding tokens entirely)
|
| 123 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(last_hidden_states.size()).float()
|
| 124 |
+
sum_embeddings = torch.sum(last_hidden_states * input_mask_expanded, dim=1)
|
| 125 |
+
sum_mask = torch.clamp(input_mask_expanded.sum(dim=1), min=1e-9)
|
| 126 |
+
|
| 127 |
+
all_embedding = (sum_embeddings / sum_mask).squeeze(0).float()
|
| 128 |
|
| 129 |
+
all_embeddings.append(all_embedding.cpu().numpy())
|
| 130 |
+
|
| 131 |
+
|
| 132 |
labels_list.append(sample_label)
|
| 133 |
|
| 134 |
except Exception as e:
|
|
|
|
| 138 |
embedding_matrix = np.vstack(all_embeddings)
|
| 139 |
|
| 140 |
print("Final Concatenated Array Shape:", embedding_matrix.shape)
|
| 141 |
+
|
| 142 |
+
np.save("test_gemma3_mean_embeddings.npy", embedding_matrix)
|
| 143 |
```
|
| 144 |
|
| 145 |
|