3030from gradio .context import LocalContext
3131from gradio .data_classes import FileData , GradioModel , GradioRootModel , JsonData
3232from gradio .exceptions import Error , InvalidPathError
33+ from gradio .profiling import traced_sync
3334from gradio .route_utils import API_PREFIX
3435from gradio .utils import abspath , get_hash_seed , get_upload_folder , is_in_or_equal
3536
@@ -155,6 +156,7 @@ def hash_base64(base64_encoding: str, chunk_num_blocks: int = 128) -> str:
155156 return sha .hexdigest ()
156157
157158
159+ @traced_sync ("postprocess_save_pil_to_cache" )
158160def save_pil_to_cache (
159161 img : Image .Image ,
160162 cache_dir : str ,
@@ -169,13 +171,15 @@ def save_pil_to_cache(
169171 return filename
170172
171173
174+ @traced_sync ("postprocess_save_img_array_to_cache" )
172175def save_img_array_to_cache (
173176 arr : np .ndarray , cache_dir : str , format : str = "webp"
174177) -> str :
175178 pil_image = Image .fromarray (_convert (arr , np .uint8 , force_copy = False ))
176179 return save_pil_to_cache (pil_image , cache_dir , format = format )
177180
178181
182+ @traced_sync ("postprocess_save_audio_to_cache" )
179183def save_audio_to_cache (
180184 data : np .ndarray , sample_rate : int , format : str , cache_dir : str
181185) -> str :
@@ -207,6 +211,7 @@ def detect_audio_format(data: bytes) -> str:
207211 return ""
208212
209213
214+ @traced_sync ("postprocess_save_bytes_to_cache" )
210215def save_bytes_to_cache (data : bytes , file_name : str , cache_dir : str ) -> str :
211216 path = Path (cache_dir ) / hash_bytes (data )
212217 path .mkdir (exist_ok = True , parents = True )
@@ -218,6 +223,7 @@ def save_bytes_to_cache(data: bytes, file_name: str, cache_dir: str) -> str:
218223 return str (path .resolve ())
219224
220225
226+ @traced_sync ("save_file_to_cache" )
221227def save_file_to_cache (file_path : str | Path , cache_dir : str ) -> str :
222228 """Returns a temporary file path for a copy of the given file path if it does
223229 not already exist. Otherwise returns the path to the existing temp file."""
@@ -644,28 +650,31 @@ def resize_and_crop(img, size, crop_type="center"):
644650def audio_from_file (
645651 filename : str , crop_min : float = 0 , crop_max : float = 100
646652) -> tuple [int , np .ndarray ]:
647- try :
648- audio = AudioSegment .from_file (filename )
649- except FileNotFoundError as e :
650- isfile = Path (filename ).is_file ()
651- msg = (
652- f"Cannot load audio from file: `{ 'ffprobe' if isfile else filename } ` not found."
653- + " Please install `ffmpeg` in your system to use non-WAV audio file formats"
654- " and make sure `ffprobe` is in your PATH."
655- if isfile
656- else ""
657- )
658- raise RuntimeError (msg ) from e
659- except OSError as e :
660- raise e
661- if crop_min != 0 or crop_max != 100 :
662- audio_start = len (audio ) * crop_min / 100
663- audio_end = len (audio ) * crop_max / 100
664- audio = audio [audio_start :audio_end ]
665- data = np .array (audio .get_array_of_samples ())
666- if audio .channels > 1 :
667- data = data .reshape (- 1 , audio .channels )
668- return audio .frame_rate , data
653+ from gradio .profiling import trace_phase_sync
654+
655+ with trace_phase_sync ("preprocess_audio_from_file" ):
656+ try :
657+ audio = AudioSegment .from_file (filename )
658+ except FileNotFoundError as e :
659+ isfile = Path (filename ).is_file ()
660+ msg = (
661+ f"Cannot load audio from file: `{ 'ffprobe' if isfile else filename } ` not found."
662+ + " Please install `ffmpeg` in your system to use non-WAV audio file formats"
663+ " and make sure `ffprobe` is in your PATH."
664+ if isfile
665+ else ""
666+ )
667+ raise RuntimeError (msg ) from e
668+ except OSError as e :
669+ raise e
670+ if crop_min != 0 or crop_max != 100 :
671+ audio_start = len (audio ) * crop_min / 100
672+ audio_end = len (audio ) * crop_max / 100
673+ audio = audio [audio_start :audio_end ]
674+ data = np .array (audio .get_array_of_samples ())
675+ if audio .channels > 1 :
676+ data = data .reshape (- 1 , audio .channels )
677+ return audio .frame_rate , data
669678
670679
671680def audio_to_file (sample_rate , data , filename , format = "wav" ):
0 commit comments