File size: 11,177 Bytes
7c34c28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
from typing import Dict, List, Any, Union, Optional
from collections import Counter
from functools import cache
import contextlib
import tempfile
import shutil
import random
import subprocess
import json
import re
import io
import os

import torch
import requests
import transformers
import numpy as np
from datasets import load_dataset, Dataset
from PIL import Image

from multi_token.constants import IGNORE_INDEX


def encode_chat(
    item: Dict,
    tokenizer: transformers.PreTrainedTokenizer,
    modalities: List["Modality"],
) -> Dict:
    messages = list(item["messages"])
    chat_as_string = tokenizer.apply_chat_template(messages, tokenize=False)

    token_to_modality = {m.token: m for m in modalities}
    modality_token_counts = Counter()
    instruct_pattern = r"(\[INST\][\s\S]*?\[\/INST\])"
    pattern = "(" + "|".join(re.escape(m.token) for m in modalities) + ")"

    chat_part = re.split(instruct_pattern, chat_as_string)
    input_ids = []
    labels = []
    for part in chat_part:
        if "[INST]" in part:
            is_instruction = True
        else:
            is_instruction = False
        for subpart in re.split(pattern, part):
            if not subpart:
                continue
            if subpart in token_to_modality:
                assert (
                    is_instruction
                ), "There should be no modality tokens outside of instructions"
                m = token_to_modality[subpart]
                modality_token_counts[m.name] += 1
                input_ids.extend([m.token_idx] * m.token_width)
                labels.extend([IGNORE_INDEX] * m.token_width)
            elif is_instruction:
                part_ids = tokenizer(subpart, add_special_tokens=False).input_ids
                input_ids.extend(part_ids)
                labels.extend([IGNORE_INDEX] * len(part_ids))
            else:
                part_ids = tokenizer(subpart, add_special_tokens=False).input_ids
                input_ids.extend(part_ids)
                labels.extend(part_ids)

    input_ids = torch.tensor(input_ids, dtype=torch.long)
    labels = torch.tensor(labels, dtype=torch.long)

    data_dict = dict(
        input_ids=input_ids,
        labels=labels,
    )
    for m in modalities:
        data_dict[m.name] = m.preprocess_rows([item])[0]
    return data_dict

def encode_chat_multitask(
    item: Dict,
    tokenizer: transformers.PreTrainedTokenizer,
    modalities: List["Modality"],
) -> Dict:
    messages = list(item["messages"])
    chat_as_string = tokenizer.apply_chat_template(messages, tokenize=False)

    token_to_modality = {m.token: m for m in modalities}
    modality_token_counts = Counter()
    instruct_pattern = r"(\[INST\][\s\S]*?\[\/INST\])"
    pattern = "(" + "|".join(re.escape(m.token) for m in modalities) + ")"

    chat_part = re.split(instruct_pattern, chat_as_string)
    input_ids = []
    labels = []
    labels.append([])
    for part in chat_part:
        if "[INST]" in part:
            is_instruction = True
        else:
            is_instruction = False
        for subpart in re.split(pattern, part):
            if not subpart:
                continue
            if subpart in token_to_modality:
                assert (
                    is_instruction
                ), "There should be no modality tokens outside of instructions"
                m = token_to_modality[subpart]
                modality_token_counts[m.name] += 1
                input_ids.extend([m.token_idx] * m.token_width)
                labels[0].extend([IGNORE_INDEX] * m.token_width)
            elif is_instruction:
                part_ids = tokenizer(subpart, add_special_tokens=False).input_ids
                input_ids.extend(part_ids)
                labels[0].extend([IGNORE_INDEX] * len(part_ids))
            else:
                part_ids = tokenizer(subpart, add_special_tokens=False).input_ids
                input_ids.extend(part_ids)
                labels[0].extend(part_ids)

    input_ids = torch.tensor(input_ids, dtype=torch.long)
    labels[0] = torch.tensor(labels[0], dtype=torch.long)

    task_list = []
    for m in modalities:
        task_list += m.tasks["task_heads"].keys()
    # labels[task_specs["task_id"]] = load_tensor(item[task_name][0])

    for task_name in task_list:
        if task_name != "lmm_projector":
            labels.append(load_tensor(item[task_name][0]))

    # labels = torch.tensor(labels, dtype=torch.long)

    data_dict = dict(
        input_ids=input_ids,
        labels=labels,
    )
    for m in modalities:
        data_dict[m.name] = m.preprocess_rows([item])[0]
    return data_dict

def load_tensor(path: str) -> np.ndarray:
    return torch.tensor(np.load(path))


def load_image(value: Any) -> Image.Image:
    img = None
    if isinstance(value, str):
        if value.startswith("http://") or value.startswith("https://"):
            response = requests.get(value)
            img = Image.open(io.BytesIO(response.content))
        elif os.path.exists(value):
            img = Image.open(value)
    elif isinstance(value, Image.Image):
        img = value
    if img is None:
        raise ValueError(f"Could not load image from {value}")
    img = img.convert("RGB")
    return img


@contextlib.contextmanager
def with_local_files(fn_or_urls: List[Any]):
    local_fns = []
    fps = []
    for fn_or_url in fn_or_urls:
        if isinstance(fn_or_url, Image.Image):
            fp = tempfile.NamedTemporaryFile(suffix=".png", mode="wb")
            fn_or_url.convert("RGB").save(fp)
            fps.append(fp)
            local_fns.append(fp.name)
        elif fn_or_url.startswith("http://") or fn_or_url.startswith("https://"):
            suffix = os.path.splitext(fn_or_url)[-1]
            with requests.get(fn_or_url, stream=True) as r:
                fp = tempfile.NamedTemporaryFile(suffix=suffix, mode="wb")
                shutil.copyfileobj(r.raw, fp)
                fps.append(fp)
                local_fns.append(fp.name)
        else:
            local_fns.append(fn_or_url)
    try:
        yield local_fns
    finally:
        for fp in fps:
            fp.close()


@cache
def _get_dataset(dataset_args: str) -> Dataset:
    return load_dataset(**json.loads(dataset_args))


def get_dataset_cached(dataset_args: Dict) -> Dataset:
    return _get_dataset(json.dumps(dataset_args))


def load_audio_signal(input_: Union[Dict, str]) -> Dict:
    from audiotools import AudioSignal

    if isinstance(input_, dict) and "array" in input_:
        array = input_["array"]
    elif isinstance(input_, dict) and "dataset_args" in input_:
        item = get_dataset_cached(input_["dataset_args"])[input_["idx"]]
        array = item["audio"]["array"]
    elif isinstance(input_, dict) and "path" in input_:
        with with_local_files([input_["path"]]) as local_fns:
            array =  AudioSignal(local_fns[0])
    elif isinstance(input_, str):
        with with_local_files([input_]) as local_fns:
            array =  AudioSignal(local_fns[0])
    else:
        raise ValueError(f"Could not load audio from {input_}")

    return {"array": list(array)}


def load_audio(input_: Union[Dict, str], target_sampling_rate: int = None) -> Dict:
    import soundfile as sf
    import librosa

    if isinstance(input_, dict) and "array" in input_ and "sampling_rate" in input_:
        array = input_["array"]
        sampling_rate = input_["sampling_rate"]
    elif isinstance(input_, dict) and "dataset_args" in input_:
        item = get_dataset_cached(input_["dataset_args"])[input_["idx"]]
        array = item["audio"]["array"]
        sampling_rate = item["audio"]["sampling_rate"]
    elif isinstance(input_, dict) and "path" in input_:
        with with_local_files([input_["path"]]) as local_fns:
            array, sampling_rate = sf.read(local_fns[0])
    elif isinstance(input_, str):
        with with_local_files([input_]) as local_fns:
            array, sampling_rate = sf.read(local_fns[0])
    else:
        raise ValueError(f"Could not load audio from {input_}")

    if array.ndim == 2:
        array = array.mean(axis=1)

    if target_sampling_rate is not None and sampling_rate != target_sampling_rate:
        array = librosa.resample(
            array, orig_sr=sampling_rate, target_sr=target_sampling_rate
        )
        sampling_rate = target_sampling_rate

    return {"array": list(array), "sampling_rate": sampling_rate}


def _download_yt_video(url: str) -> str:
    from pytube import YouTube

    youtube = YouTube(url)
    video = youtube.streams.first()

    fn = "".join(random.choices("abcdefghijklmnopqrstuvwxyz", k=10))
    file_path = video.download(output_path=tempfile.gettempdir(), filename=fn)

    return file_path


def _read_video_pyav(container, indices):
    frames = []
    container.seek(0)
    start_index = indices[0]
    end_index = indices[-1]
    for i, frame in enumerate(container.decode(video=0)):
        if i > end_index:
            break
        if i >= start_index and i in indices:
            frames.append(frame)
    return np.stack([x.to_ndarray(format="rgb24") for x in frames])


def _sample_frame_indices(clip_len, frame_sample_rate, seg_len):
    converted_len = int(clip_len * frame_sample_rate)
    end_idx = np.random.randint(converted_len, seg_len)
    start_idx = end_idx - converted_len
    indices = np.linspace(start_idx, end_idx, num=clip_len)
    indices = np.clip(indices, start_idx, end_idx - 1).astype(np.int64)
    return indices


def load_video(
    input_: str,
    frames: int = 8,
    frame_sample_rate: int = 1,
    start_time: Optional[int] = None,
    end_time: Optional[int] = None,
) -> np.ndarray:
    import av

    delete_file = False

    if isinstance(input_, dict) and "youtube.com" and input_.get("url", ""):
        file_path = _download_yt_video(input_["url"])
        delete_file = True
        # start_time = input_.get("start_time", None)
        # end_time = input_.get("end_time", None)
    elif isinstance(input_, str) and "youtube.com" in input_:
        file_path = _download_yt_video(input_)
        delete_file = True
    elif isinstance(input_, str):
        file_path = input_
    else:
        raise ValueError(f"Could not load video from {input_}")

    if start_time is not None or end_time is not None:
        start_time = start_time if start_time is not None else 0
        end_time = end_time if end_time is not None else "end"
        trim_file_path = f"{file_path.rsplit('.', 1)[0]}_trim.mp4"
        subprocess.run(
            [
                "ffmpeg",
                "-i",
                file_path,
                "-ss",
                str(start_time),
                "-to",
                str(end_time),
                "-c",
                "copy",
                trim_file_path,
            ]
        )
        file_path = trim_file_path

    container = av.open(file_path)
    indices = _sample_frame_indices(
        clip_len=frames,
        frame_sample_rate=frame_sample_rate,
        seg_len=container.streams.video[0].frames,
    )
    video = _read_video_pyav(container, indices)

    if delete_file:
        os.remove(file_path)

    return video