-
Notifications
You must be signed in to change notification settings - Fork 592
Expand file tree
/
Copy pathgemini_api.py
More file actions
288 lines (244 loc) · 11.1 KB
/
gemini_api.py
File metadata and controls
288 lines (244 loc) · 11.1 KB
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
import io
import os
import pathlib
import re
import time
from typing import List, Tuple
from accelerate import Accelerator, DistributedType
from loguru import logger as eval_logger
from PIL import Image
from tqdm import tqdm
from lmms_eval.api.instance import GenerationResult, Instance, TokenCounts
from lmms_eval.api.model import lmms
from lmms_eval.api.registry import register_model
from lmms_eval.models.model_utils.usage_metrics import is_budget_exceeded, log_usage
try:
import google.generativeai as genai
from google.generativeai.types import HarmBlockThreshold, HarmCategory
NUM_SECONDS_TO_SLEEP = 30
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
genai.configure(api_key=GOOGLE_API_KEY)
except Exception as e:
eval_logger.error(f"Error importing generativeai: {str(e)}")
genai = None
try:
import soundfile as sf
except Exception as e:
eval_logger.warning(f"Error importing soundfile, audio generation will not work: {str(e)}")
@register_model("gemini_api")
class GeminiAPI(lmms):
def __init__(
self,
model_version: str = "gemini-1.5-pro",
# modality: str = "image",
timeout: int = 120,
interleave: bool = False,
**kwargs,
) -> None:
super().__init__()
self.model_version = model_version
self.timeout = timeout
self.model = genai.GenerativeModel(model_version)
self.interleave = interleave
accelerator = Accelerator()
if accelerator.num_processes > 1:
assert accelerator.distributed_type in [DistributedType.FSDP, DistributedType.MULTI_GPU, DistributedType.DEEPSPEED], "Unsupported distributed type provided. Only DDP and FSDP are supported."
self.accelerator = accelerator
if self.accelerator.is_local_main_process:
eval_logger.info(f"Using {accelerator.num_processes} devices with data parallelism")
self._rank = self.accelerator.local_process_index
self._world_size = self.accelerator.num_processes
else:
self.accelerator = accelerator
self._rank = self.accelerator.local_process_index
self._world_size = self.accelerator.num_processes
self.device = self.accelerator.device
# self.modality = modality
self.video_pool = []
def free_video(self):
for video in self.video_pool:
video.delete()
self.video_pool = []
def flatten(self, input):
new_list = []
for i in input:
for j in i:
new_list.append(j)
return new_list
def get_image_size(self, image):
# Create a BytesIO object to store the image bytes
img_byte_array = io.BytesIO()
# Save the image to the BytesIO object
image.save(img_byte_array, format="PNG")
# Get the size of the BytesIO object
img_size = img_byte_array.tell()
return img_size
def encode_video(self, video_path):
uploaded_obj = genai.upload_file(path=video_path)
time.sleep(5)
self.video_pool.append(uploaded_obj)
return uploaded_obj
def encode_audio(self, audio):
audio_io = io.BytesIO()
sf.write(audio_io, audio["array"], audio["sampling_rate"], format="WAV")
return genai.upload_file(audio_io, mime_type="audio/wav")
def convert_modality(self, images):
for idx, img in enumerate(images):
if isinstance(img, dict) and "sampling_rate" in img: # audio
audio = self.encode_audio(img)
images[idx] = audio
elif isinstance(img, str): # video
try:
images[idx] = self.encode_video(img)
except Exception as e:
eval_logger.error(f"Error converting video: {str(e)}")
return images
def construct_interleaved_input(self, content, media):
pattern = r"<media_(\d+)>"
parts = re.split(pattern, content)
result = []
for i, part in enumerate(parts):
if i % 2 == 0:
if part == "":
continue
result.append(part)
else:
result.append(media[int(part)])
return result
def generate_until(self, requests) -> List[GenerationResult]:
res = []
pbar = tqdm(total=len(requests), disable=(self.rank != 0), desc="Model Responding")
def get_uuid(task, split, doc_id):
return f"{task}___{split}___{doc_id}"
for contexts, gen_kwargs, doc_to_visual, doc_id, task, split in [reg.args for reg in requests]:
if is_budget_exceeded():
res.append(GenerationResult(text="", token_counts=None))
pbar.update(1)
continue
if "max_new_tokens" not in gen_kwargs:
gen_kwargs["max_new_tokens"] = 1024
if "temperature" not in gen_kwargs:
gen_kwargs["temperature"] = 0
config = genai.GenerationConfig(
max_output_tokens=gen_kwargs["max_new_tokens"],
temperature=gen_kwargs["temperature"],
)
visuals = [doc_to_visual(self.task_dict[task][split][doc_id])]
visuals = self.flatten(visuals)
visuals = self.convert_modality(visuals)
if self.interleave:
message = self.construct_interleaved_input(contexts, visuals)
else:
message = [contexts] + visuals
token_counts = None
for attempt in range(5):
try:
content = self.model.generate_content(
message,
generation_config=config,
safety_settings={
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
},
)
if hasattr(content, "usage_metadata") and content.usage_metadata:
log_usage(
model_name=self.model_version,
task_name=task,
input_tokens=getattr(content.usage_metadata, "prompt_token_count", 0) or 0,
output_tokens=getattr(content.usage_metadata, "candidates_token_count", 0) or 0,
reasoning_tokens=0,
source="model",
)
token_counts = TokenCounts(
input_tokens=getattr(content.usage_metadata, "prompt_token_count", 0) or 0,
output_tokens=getattr(content.usage_metadata, "candidates_token_count", 0) or 0,
)
content = content.text
break
except Exception as e:
eval_logger.info(f"Attempt {attempt + 1} failed with error: {str(e)}")
if isinstance(e, ValueError):
try:
eval_logger.info(f"Prompt feed_back: {content.prompt_feedback}")
content = ""
break
except Exception:
pass
if attempt < 5 - 1: # If we have retries left, sleep and then continue to next attempt
time.sleep(NUM_SECONDS_TO_SLEEP)
else: # If this was the last attempt, log and return empty
eval_logger.error(f"All 5 attempts failed. Last error message: {str(e)}")
content = ""
token_counts = None
res.append(GenerationResult(text=content, token_counts=token_counts))
pbar.update(1)
self.free_video()
pbar.close()
return res
def generate_until_multi_round(self, requests) -> List[str]:
raise NotImplementedError("TODO: Implement multi-round generation for Gemini API")
def loglikelihood(self, requests: List[Instance]) -> List[Tuple[float, bool]]:
# TODO
assert False, "Gemini API not support"
def get_image_audio_text_interleaved_messsage(self, image_path, audio_path, question):
# image_path for list of image path
# audio_path for list of audio path
# question for question
# fixed image token and no audio in text
for index in range(1, 1 + len(image_path)):
question = question.replace(f"[img{index}]", "<image>")
for index in range(1, 1 + len(audio_path)):
question = question.replace(f"[audio{index}]", "<audio>")
text = question
info_list = []
image_counter = 0
audio_counter = 0
for part in re.split(r"(<image>|<audio>)", text):
if part == "<image>":
info_list.append(Image.open(image_path[image_counter]))
image_counter += 1
elif part == "<audio>":
info_list.append({"mime_type": "audio/wav", "data": pathlib.Path(audio_path[audio_counter]).read_bytes()})
audio_counter += 1
else:
if part == " ":
continue
info_list.append(part)
return info_list
def get_video_audio_text_interleaved_message(self, video_path, audio_path, question):
# image_path for list of image path
# audio_path for list of audio path
# question for question
# fixed video token and no audio in text
for index in range(1, 1 + len(video_path)):
question = question.replace(f"[video{index}]", "<video>")
for index in range(1, 1 + len(audio_path)):
question = question.replace(f"[audio{index}]", "<audio>")
text = question
info_list = []
video_counter = 0
audio_counter = 0
for part in re.split(r"(<video>|<audio>)", text):
if part == "<video>":
current_video_file_name = video_path[video_counter]
current_video_file = genai.upload_file(path=current_video_file_name)
while current_video_file.state.name == "processing":
print("uploading file")
time.sleep(5)
current_video_file = genai.get_file(current_video_file.name)
if current_video_file.state.name == "FAILED":
print("uploading file failed, next question")
return 0
info_list.append(current_video_file)
video_counter += 1
elif part == "<audio>":
info_list.append({"mime_type": "audio/wav", "data": pathlib.Path(audio_path[audio_counter]).read_bytes()})
audio_counter += 1
else:
if part == " ":
continue
info_list.append(part)
return info_list