|
| 1 | +# Copyright 2025 Tencent Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import json |
| 16 | +import os |
| 17 | +from pathlib import Path |
| 18 | +from typing import Dict, List, Union |
| 19 | + |
| 20 | +from transformers import ProcessorMixin |
| 21 | + |
| 22 | +from ..utils.lazy_imports import qwen_omni_utils |
| 23 | +from .base_dataset import BaseDataset |
| 24 | + |
| 25 | + |
| 26 | +class OmniDataset(BaseDataset): |
| 27 | + """Dataset for multimodal (text + image) data""" |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + processor: ProcessorMixin, |
| 32 | + device: str = "cpu", |
| 33 | + max_length: int = 4096, |
| 34 | + num_samples: int = -1, |
| 35 | + data_source: Union[str, Dict] = None, |
| 36 | + is_hf_dataset: bool = False, |
| 37 | + use_audio_in_video: bool = False, |
| 38 | + ): |
| 39 | + super().__init__(processor, device, max_length) |
| 40 | + self.is_hf_dataset = is_hf_dataset |
| 41 | + self.use_audio_in_video = use_audio_in_video |
| 42 | + |
| 43 | + self._load_file_based_dataset(data_source, num_samples) |
| 44 | + |
| 45 | + def _load_file_based_dataset(self, data_path: str, num_samples: int): |
| 46 | + """Load dataset from local file system""" |
| 47 | + path_obj = Path(data_path) |
| 48 | + data_dir = path_obj.parent |
| 49 | + |
| 50 | + line_count = 0 |
| 51 | + with open(data_path, "r") as f: |
| 52 | + for line in f: |
| 53 | + if num_samples > 0 and line_count >= num_samples: |
| 54 | + break |
| 55 | + data = json.loads(line.strip()) |
| 56 | + video_path = None |
| 57 | + audio_path = None |
| 58 | + image_path = None |
| 59 | + |
| 60 | + if "video_path" in data: |
| 61 | + video_path = os.path.normpath( |
| 62 | + os.path.join(data_dir, data["video_path"]) |
| 63 | + ) |
| 64 | + if "audio_path" in data: |
| 65 | + audio_path = os.path.normpath( |
| 66 | + os.path.join(data_dir, data["audio_path"]) |
| 67 | + ) |
| 68 | + if "image_path" in data: |
| 69 | + image_path = os.path.normpath( |
| 70 | + os.path.join(data_dir, data["image_path"]) |
| 71 | + ) |
| 72 | + |
| 73 | + ms = data.get("messages") |
| 74 | + |
| 75 | + conversation = [] |
| 76 | + for m in ms: |
| 77 | + if m["role"] == "system": |
| 78 | + conversation.append( |
| 79 | + { |
| 80 | + "role": "system", |
| 81 | + "content": [{"type": "text", "text": m["content"]}], |
| 82 | + } |
| 83 | + ) |
| 84 | + elif m["role"] == "user": |
| 85 | + content = [] |
| 86 | + text_content = m["content"] |
| 87 | + text_content = ( |
| 88 | + text_content.replace("<video>", "") |
| 89 | + .replace("<audio>", "") |
| 90 | + .replace("<image>", "") |
| 91 | + ) |
| 92 | + content.append({"type": "text", "text": text_content}) |
| 93 | + if video_path: |
| 94 | + content.append({"type": "video", "video": video_path}) |
| 95 | + if audio_path: |
| 96 | + content.append({"type": "audio", "audio": audio_path}) |
| 97 | + if image_path: |
| 98 | + content.append({"type": "image", "image": image_path}) |
| 99 | + conversation.append( |
| 100 | + { |
| 101 | + "role": "user", |
| 102 | + "content": content, |
| 103 | + } |
| 104 | + ) |
| 105 | + self._process_and_append(conversation) |
| 106 | + line_count += 1 |
| 107 | + |
| 108 | + def _process_and_append(self, messages: List[Dict]): |
| 109 | + """Process messages and append to dataset""" |
| 110 | + text = self.processor.apply_chat_template( |
| 111 | + messages, tokenize=False, add_generation_prompt=True |
| 112 | + ) |
| 113 | + audios, images, videos = qwen_omni_utils.process_mm_info( |
| 114 | + messages, use_audio_in_video=self.use_audio_in_video |
| 115 | + ) |
| 116 | + |
| 117 | + # Process inputs |
| 118 | + inputs = self.processor( |
| 119 | + text=text, |
| 120 | + images=images, |
| 121 | + audios=audios, |
| 122 | + videos=videos, |
| 123 | + padding=True, |
| 124 | + return_tensors="pt", |
| 125 | + use_audio_in_video=self.use_audio_in_video, |
| 126 | + ) |
| 127 | + self.data.append(inputs) |
0 commit comments