Rename script/preprocess.py to scripts/preprocess.py
Browse files- {script → scripts}/preprocess.py +12 -11
{script → scripts}/preprocess.py
RENAMED
|
@@ -1,20 +1,20 @@
|
|
| 1 |
import json
|
| 2 |
import os
|
| 3 |
from datasets import Dataset, Image
|
| 4 |
-
from transformers import CLIPProcessor, CLIPModel, AutoTokenizer
|
| 5 |
from PIL import Image as PILImage
|
| 6 |
import torch
|
| 7 |
from tqdm import tqdm
|
| 8 |
|
| 9 |
-
def preprocess_multimodal_data(raw_json_path, image_dir, dataset_name,
|
| 10 |
"""
|
| 11 |
-
Preprocesses raw JSON
|
| 12 |
|
| 13 |
Args:
|
| 14 |
raw_json_path (str): Path to the raw JSON file.
|
| 15 |
image_dir (str): Directory containing the images.
|
| 16 |
dataset_name (str): Name of the Hugging Face Dataset.
|
| 17 |
-
|
| 18 |
clip_model_name (str): Name of the CLIP model from Hugging Face Transformers.
|
| 19 |
output_dir (str): Directory where the dataset repository will be saved.
|
| 20 |
"""
|
|
@@ -43,16 +43,17 @@ def preprocess_multimodal_data(raw_json_path, image_dir, dataset_name, tokenizer
|
|
| 43 |
sections = []
|
| 44 |
languages = []
|
| 45 |
|
| 46 |
-
# Load CLIP and Tokenizer
|
| 47 |
clip_processor = CLIPProcessor.from_pretrained(clip_model_name)
|
| 48 |
clip_model = CLIPModel.from_pretrained(clip_model_name).to("cuda")
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
# Process each data point
|
| 52 |
for i, item in enumerate(tqdm(data, desc="Processing data")):
|
| 53 |
question = item.get('question', '')
|
| 54 |
answer = item.get('answer', '')
|
| 55 |
-
image = None
|
| 56 |
image_path = item.get('image', None)
|
| 57 |
id_field = str(i)
|
| 58 |
|
|
@@ -115,11 +116,11 @@ def preprocess_multimodal_data(raw_json_path, image_dir, dataset_name, tokenizer
|
|
| 115 |
print(f"Dataset '{dataset_name}' saved to {os.path.join(output_dir, dataset_name)}")
|
| 116 |
|
| 117 |
# Example Usage:
|
| 118 |
-
raw_json_path = 'your_data.json'
|
| 119 |
-
image_dir = 'your_image_dir'
|
| 120 |
dataset_name = 'my_multimodal_dataset'
|
| 121 |
-
|
| 122 |
clip_model_name = 'openai/clip-vit-base-patch32'
|
| 123 |
output_dir = 'my_datasets'
|
| 124 |
|
| 125 |
-
preprocess_multimodal_data(raw_json_path, image_dir, dataset_name,
|
|
|
|
| 1 |
import json
|
| 2 |
import os
|
| 3 |
from datasets import Dataset, Image
|
| 4 |
+
from transformers import CLIPProcessor, CLIPModel, AutoTokenizer, AutoModel
|
| 5 |
from PIL import Image as PILImage
|
| 6 |
import torch
|
| 7 |
from tqdm import tqdm
|
| 8 |
|
| 9 |
+
def preprocess_multimodal_data(raw_json_path, image_dir, dataset_name, text_model_name, clip_model_name, output_dir):
|
| 10 |
"""
|
| 11 |
+
Preprocesses raw JSON and image data into a Hugging Face Dataset, using DeepSeek-R1 for text embeddings.
|
| 12 |
|
| 13 |
Args:
|
| 14 |
raw_json_path (str): Path to the raw JSON file.
|
| 15 |
image_dir (str): Directory containing the images.
|
| 16 |
dataset_name (str): Name of the Hugging Face Dataset.
|
| 17 |
+
text_model_name (str): Name of the text model (DeepSeek-R1) from Hugging Face Transformers.
|
| 18 |
clip_model_name (str): Name of the CLIP model from Hugging Face Transformers.
|
| 19 |
output_dir (str): Directory where the dataset repository will be saved.
|
| 20 |
"""
|
|
|
|
| 43 |
sections = []
|
| 44 |
languages = []
|
| 45 |
|
| 46 |
+
# Load CLIP, DeepSeek-R1, and Tokenizer
|
| 47 |
clip_processor = CLIPProcessor.from_pretrained(clip_model_name)
|
| 48 |
clip_model = CLIPModel.from_pretrained(clip_model_name).to("cuda")
|
| 49 |
+
text_model = AutoModel.from_pretrained(text_model_name).to("cuda")
|
| 50 |
+
tokenizer = AutoTokenizer.from_pretrained(text_model_name)
|
| 51 |
|
| 52 |
# Process each data point
|
| 53 |
for i, item in enumerate(tqdm(data, desc="Processing data")):
|
| 54 |
question = item.get('question', '')
|
| 55 |
answer = item.get('answer', '')
|
| 56 |
+
image = None
|
| 57 |
image_path = item.get('image', None)
|
| 58 |
id_field = str(i)
|
| 59 |
|
|
|
|
| 116 |
print(f"Dataset '{dataset_name}' saved to {os.path.join(output_dir, dataset_name)}")
|
| 117 |
|
| 118 |
# Example Usage:
|
| 119 |
+
raw_json_path = 'your_data.json'
|
| 120 |
+
image_dir = 'your_image_dir'
|
| 121 |
dataset_name = 'my_multimodal_dataset'
|
| 122 |
+
text_model_name = 'deepseek-ai/deepseek-rl-7b-chat' #Deepseek R1
|
| 123 |
clip_model_name = 'openai/clip-vit-base-patch32'
|
| 124 |
output_dir = 'my_datasets'
|
| 125 |
|
| 126 |
+
preprocess_multimodal_data(raw_json_path, image_dir, dataset_name, text_model_name, clip_model_name, output_dir)
|