-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
175 lines (147 loc) · 5.13 KB
/
models.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
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
import torch
from transformers import AutoProcessor, Qwen2VLForConditionalGeneration, MllamaForConditionalGeneration, MllamaProcessor, AutoTokenizer, AutoModelForCausalLM
import os
from dotenv import load_dotenv
load_dotenv()
MM_LLAMA_MODELS = {
'Llama 3.2 11B': '',
'Llama 3.2 90B': '',
}
MM_QWEN_MODELS = {
'Qwen2 VL 2B': '',
'Qwen2 VL 7B': '',
'Qwen2 VL 72B': '',
}
LLAMA_MODELS = {
'Llama 3.2 1B': '',
'Llama 3.2 3B': '',
'Llama 3.2 11B': '',
'Llama 3.2 90B': '',
'Llama 3.1 8B': '',
'Llama 3.1 90B': '',
'Llama 3.1 405B': '',
'Llama 3 8B': '',
'Llama 3 70B': '',
'Llama 2 7B': '',
'Llama 2 13B': '',
'Llama 2 70B': '',
}
def get_llama_mm(model_path=LLAMA_MODELS['Llama 3.2 11B']):
model = MllamaForConditionalGeneration.from_pretrained(
model_path,
torch_dtype=torch.bfloat16,
device_map="auto",
)
processor = AutoProcessor.from_pretrained(
model_path,
)
return model, processor
def get_llama(model_path=LLAMA_MODELS['Llama 3.1 8B']):
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.bfloat16,
device_map="auto",
)
tokenizer = AutoTokenizer.from_pretrained(
model_path,
)
return model, tokenizer
def get_qwen2_vl(model_path=MM_QWEN_MODELS['Qwen2 VL 7B']):
model = Qwen2VLForConditionalGeneration.from_pretrained(
model_path, torch_dtype="auto", device_map="auto"
)
processor = AutoProcessor.from_pretrained(model_path)
return model, processor
def generate_openai(client, openai_model, messages: list[dict[str, str]], max_tokens=128):
return client.chat.completions.create(
model=openai_model,
messages=messages,
max_tokens=max_tokens,
stream=True,
)
def generate_llama(
model: MllamaForConditionalGeneration,
processor: MllamaProcessor,
messages,
max_tokens=128,
):
input_text = processor.apply_chat_template(
messages, add_generation_prompt=True)
inputs = processor(text=input_text, return_tensors="pt").to(model.device)
output_ids = model.generate(
**inputs, max_new_tokens=max_tokens, pad_token_id=0)
generated_ids = [
output_ids[len(input_ids):]
for input_ids, output_ids in zip(inputs.input_ids, output_ids)
]
output_text = processor.batch_decode(
generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
)
return output_text[0]
def generate_llama_mm(
model: MllamaForConditionalGeneration,
processor: MllamaProcessor,
messages,
max_tokens=128,
):
input_text = processor.apply_chat_template(
messages, add_generation_prompt=True)
inputs = processor(text=input_text, return_tensors="pt").to(model.device)
output_ids = model.generate(
**inputs, max_new_tokens=max_tokens, pad_token_id=0)
generated_ids = [
output_ids[len(input_ids):]
for input_ids, output_ids in zip(inputs.input_ids, output_ids)
]
output_text = processor.batch_decode(
generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
)
return output_text[0]
def generate_llama_with_image(model, processor, messages, images, max_tokens=128):
text_prompt = processor.apply_chat_template(
messages, add_generation_prompt=True)
inputs = processor(
text=[text_prompt], images=images, padding=True, return_tensors="pt"
)
inputs = inputs.to(model.device)
output_ids = model.generate(**inputs, max_new_tokens=max_tokens)
generated_ids = [
output_ids[len(input_ids):]
for input_ids, output_ids in zip(inputs.input_ids, output_ids)
]
output_text = processor.batch_decode(
generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
)
return output_text[0]
def generate_qwen2_vl(model, processor, messages, max_tokens=128):
text_prompt = processor.apply_chat_template(
messages, add_generation_prompt=True)
inputs = processor(
text=[text_prompt], padding=True, return_tensors="pt"
)
inputs = inputs.to(model.device)
output_ids = model.generate(**inputs, max_new_tokens=max_tokens)
generated_ids = [
output_ids[len(input_ids):]
for input_ids, output_ids in zip(inputs.input_ids, output_ids)
]
output_text = processor.batch_decode(
generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
)
return output_text[0]
def generate_qwen2_vl_with_image(model, processor, messages, images, max_tokens=128):
text_prompt = processor.apply_chat_template(
messages, add_generation_prompt=True)
inputs = processor(
text=[text_prompt], images=images, padding=True, return_tensors="pt"
)
inputs = inputs.to(model.device)
output_ids = model.generate(**inputs, max_new_tokens=max_tokens)
generated_ids = [
output_ids[len(input_ids):]
for input_ids, output_ids in zip(inputs.input_ids, output_ids)
]
output_text = processor.batch_decode(
generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
)
return output_text[0]