-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.py
37 lines (30 loc) · 2.16 KB
/
random.py
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
import re
import random
import numpy as np
from dataset import VidHalDataset
from pipelines.inference.base import (
VidHalInferencePipeline,
VidHalMCQAInferencePipeline,
VidHalNaiveOrderingInferencePipeline,
VidHalRelativeOrderingInferencePipeline
)
class RandomInferencePipeline(VidHalInferencePipeline):
def __init__(self, dataset: VidHalDataset, model=None, num_captions=3, option_display_order: dict = None, generation_config=..., *args, **kwargs):
super().__init__(model, dataset, num_captions, option_display_order, generation_config, *args, **kwargs)
def format_prompt(self, main_prompt, options_prompt, system_prompt=None, *args, **kwargs):
return f"{main_prompt}\n\n{options_prompt}", system_prompt
def generate_response(self, video, main_prompt, system_prompt=None, generation_config=..., *args, **kwargs):
if "choose" in main_prompt:
options = list(set(re.findall(r'\b[A-Z]\b', main_prompt)))
return random.choice(options)
else:
return ", ".join(np.random.permutation(["A", "B", "C"]).tolist())
class RandomMCQAInferencePipeline(RandomInferencePipeline, VidHalMCQAInferencePipeline):
def __init__(self, dataset: VidHalDataset, model=None, num_captions=3, option_display_order: dict = None, generation_config=..., *args, **kwargs):
super().__init__(dataset, model, num_captions, option_display_order, generation_config, *args, **kwargs)
class RandomNaiveOrderingInferencePipeline(RandomInferencePipeline, VidHalNaiveOrderingInferencePipeline):
def __init__(self, dataset: VidHalDataset, model=None, num_captions=3, option_display_order: dict = None, generation_config=..., *args, **kwargs):
super().__init__(dataset, model, num_captions, option_display_order, generation_config, *args, **kwargs)
class RandomRelativeOrderingInferencePipeline(RandomInferencePipeline, VidHalRelativeOrderingInferencePipeline):
def __init__(self, dataset: VidHalDataset, model=None, num_captions=3, option_display_order: dict = None, generation_config=..., *args, **kwargs):
super().__init__(dataset, model, num_captions, option_display_order, generation_config, *args, **kwargs)