Update README.md
Browse files
README.md
CHANGED
|
@@ -19,3 +19,87 @@ language:
|
|
| 19 |
This mllama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
|
| 20 |
|
| 21 |
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
This mllama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
|
| 20 |
|
| 21 |
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
``` python
|
| 27 |
+
import pandas as pd
|
| 28 |
+
import os
|
| 29 |
+
from unsloth import FastVisionModel
|
| 30 |
+
import torch
|
| 31 |
+
from datasets import load_dataset
|
| 32 |
+
from transformers import TextStreamer
|
| 33 |
+
|
| 34 |
+
model_name = "NYUAD-ComNets/Llama3.2-MultiModal-Hate_Detector_Memes"
|
| 35 |
+
|
| 36 |
+
model, tokenizer = FastVisionModel.from_pretrained(model_name, token='xxxxxxxxx')
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
FastVisionModel.for_inference(model)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
dataset_test = load_dataset("QCRI/Prop2Hate-Meme", split = "test")
|
| 43 |
+
|
| 44 |
+
def add_labels_column(example):
|
| 45 |
+
example["labels"] = "no_hate" if example["hate_label"] == 0 else "hate"
|
| 46 |
+
return example
|
| 47 |
+
|
| 48 |
+
dataset_test = dataset_test.map(add_labels_column)
|
| 49 |
+
|
| 50 |
+
def convert_to_conversation(sample):
|
| 51 |
+
conversation = [
|
| 52 |
+
|
| 53 |
+
{ "role": "user",
|
| 54 |
+
"content" : [
|
| 55 |
+
{"type" : "text", "text" : sample["text"]},
|
| 56 |
+
{"type" : "image", "image" : sample["image"]} ]
|
| 57 |
+
},
|
| 58 |
+
{ "role" : "assistant",
|
| 59 |
+
"content" : [
|
| 60 |
+
{"type" : "text", "text" : sample["labels"]} ]
|
| 61 |
+
},
|
| 62 |
+
]
|
| 63 |
+
return { "messages" : conversation }
|
| 64 |
+
pass
|
| 65 |
+
|
| 66 |
+
dataset_test = [convert_to_conversation(sample) for sample in dataset_test]
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
pred=[]
|
| 70 |
+
|
| 71 |
+
for k in range(606):
|
| 72 |
+
image = dataset_test[k]["image"]
|
| 73 |
+
text = dataset_test[k]["text"]
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
messages = [
|
| 78 |
+
|
| 79 |
+
{"role": "user", "content": [
|
| 80 |
+
{"type": "image"},
|
| 81 |
+
{"type": "text", "text": text}
|
| 82 |
+
]}
|
| 83 |
+
]
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
input_text = tokenizer.apply_chat_template(messages,add_generation_prompt = True)
|
| 87 |
+
inputs = tokenizer(
|
| 88 |
+
image,
|
| 89 |
+
input_text,
|
| 90 |
+
add_special_tokens = False,
|
| 91 |
+
return_tensors = "pt",
|
| 92 |
+
).to("cuda")
|
| 93 |
+
|
| 94 |
+
text_streamer = TextStreamer(tokenizer, skip_prompt = True)
|
| 95 |
+
p = model.generate(**inputs, streamer = text_streamer, max_new_tokens = 128,
|
| 96 |
+
use_cache = True, temperature = 0.1, min_p = 0.1)
|
| 97 |
+
|
| 98 |
+
p = tokenizer.decode(p[0], skip_special_tokens=True)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
pred.append(p.split('assistant')[1])
|
| 103 |
+
|
| 104 |
+
print(pred)
|
| 105 |
+
```
|