11from __future__ import annotations
22
3+ from abc import ABCMeta
34from typing import Any
45
56from guidellm .data .preprocessors .preprocessor import (
67 DatasetPreprocessor ,
78 PreprocessorRegistry ,
89)
910from guidellm .data .schemas import GenerativeDatasetColumnType
10- from guidellm .data .utils import encode_audio , encode_image , encode_video , text_stats
11+ from guidellm .data .utils import text_stats
1112from guidellm .schemas import GenerationRequest , GenerationRequestArguments , UsageMetrics
1213
1314__all__ = [
1819]
1920
2021
22+ class RequestFormatter (DatasetPreprocessor , metaclass = ABCMeta ):
23+ @staticmethod
24+ def encode_audio (* args , ** kwargs ):
25+ from guidellm .extras .multimodal import encode_audio
26+
27+ return encode_audio (* args , ** kwargs )
28+
29+ @staticmethod
30+ def encode_image (* args , ** kwargs ):
31+ from guidellm .extras .multimodal import encode_image
32+
33+ return encode_image (* args , ** kwargs )
34+
35+ @staticmethod
36+ def encode_video (* args , ** kwargs ):
37+ from guidellm .extras .multimodal import encode_video
38+
39+ return encode_video (* args , ** kwargs )
40+
41+
2142@PreprocessorRegistry .register ("text_completions" )
22- class GenerativeTextCompletionsRequestFormatter (DatasetPreprocessor ):
43+ class GenerativeTextCompletionsRequestFormatter (RequestFormatter ):
2344 def __init__ (
2445 self ,
2546 model : str ,
@@ -92,7 +113,7 @@ def __call__(
92113
93114
94115@PreprocessorRegistry .register ("chat_completions" )
95- class GenerativeChatCompletionsRequestFormatter (DatasetPreprocessor ):
116+ class GenerativeChatCompletionsRequestFormatter (RequestFormatter ):
96117 def __init__ (
97118 self ,
98119 model : str ,
@@ -120,7 +141,7 @@ def __init__(
120141 encode_kwargs .get ("audio" , {}) if encode_kwargs else {}
121142 )
122143
123- def __call__ (
144+ def __call__ ( # noqa: C901, PLR0912, PLR0915
124145 self , columns : dict [GenerativeDatasetColumnType , list [Any ]]
125146 ) -> GenerationRequest :
126147 arguments = GenerationRequestArguments (body = {})
@@ -200,7 +221,7 @@ def __call__(
200221 if not image :
201222 continue
202223
203- image_dict = encode_image (image , ** self .encode_image_kwargs )
224+ image_dict = self . encode_image (image , ** self .encode_image_kwargs )
204225 if (image_pixels := image_dict .get ("image_pixels" )) is not None :
205226 input_metrics .image_pixels = (
206227 input_metrics .image_pixels or 0
@@ -223,7 +244,7 @@ def __call__(
223244 if not video :
224245 continue
225246
226- video_dict = encode_video (video , ** self .encode_video_kwargs )
247+ video_dict = self . encode_video (video , ** self .encode_video_kwargs )
227248 if (video_frames := video_dict .get ("video_frames" )) is not None :
228249 input_metrics .video_frames = (
229250 input_metrics .video_frames or 0
@@ -250,7 +271,9 @@ def __call__(
250271 if not audio :
251272 continue
252273
253- audio_dict = encode_audio (audio , b64encode = True , ** self .encode_audio_kwargs )
274+ audio_dict = self .encode_audio (
275+ audio , b64encode = True , ** self .encode_audio_kwargs
276+ )
254277 if (audio_samples := audio_dict .get ("audio_samples" )) is not None :
255278 input_metrics .audio_samples = (
256279 input_metrics .audio_samples or 0
@@ -288,7 +311,7 @@ def __call__(
288311
289312
290313@PreprocessorRegistry .register ("audio_transcriptions" )
291- class GenerativeAudioTranscriptionRequestFormatter (DatasetPreprocessor ):
314+ class GenerativeAudioTranscriptionRequestFormatter (RequestFormatter ):
292315 def __init__ (
293316 self ,
294317 model : str ,
@@ -345,7 +368,7 @@ def __call__( # noqa: C901
345368 f"one audio column, but got { len (audio_columns )} ."
346369 )
347370
348- audio_dict = encode_audio (
371+ audio_dict = self . encode_audio (
349372 audio_columns [0 ], b64encode = False , ** self .encode_audio_kwargs
350373 )
351374 input_metrics .audio_samples = audio_dict .get ("audio_samples" )
0 commit comments