2525)
2626from manim_voiceover .tracker import AUDIO_OFFSET_RESOLUTION
2727
28- if t .TYPE_CHECKING :
29- PathLike = t .Union [str , os .PathLike [str ]]
30- else :
31- PathLike = t .Union [str , os .PathLike ]
28+ PathLike = str | os .PathLike [str ]
3229
3330
3431class TranscriptionResult (t .Protocol ):
3532 text : str
3633
37- def segments_to_dicts (self ) -> t . List [TranscriptionSegment ]: ...
34+ def segments_to_dicts (self ) -> list [TranscriptionSegment ]: ...
3835
3936
4037class WhisperModel (t .Protocol ):
4138 def transcribe (self , audio_path : str , ** kwargs : object ) -> TranscriptionResult : ...
4239
4340
44- def _pop_optional_path (kwargs : t .MutableMapping [str , object ], key : str ) -> t . Optional [ PathLike ] :
41+ def _pop_optional_path (kwargs : t .MutableMapping [str , object ], key : str ) -> PathLike | None :
4542 value = kwargs .pop (key , None )
4643 if value is None :
4744 return None
@@ -59,7 +56,7 @@ def path_to_string(path: PathLike) -> str:
5956 raise TypeError ("path must resolve to a string path" )
6057
6158
62- def _pop_optional_str (kwargs : t .MutableMapping [str , object ], key : str ) -> t . Optional [ str ] :
59+ def _pop_optional_str (kwargs : t .MutableMapping [str , object ], key : str ) -> str | None :
6360 value = kwargs .pop (key , None )
6461 if value is None or isinstance (value , str ):
6562 return value
@@ -73,7 +70,7 @@ def _pop_float(kwargs: t.MutableMapping[str, object], key: str, default: float)
7370 raise TypeError (f"{ key } must be a number" )
7471
7572
76- def _pop_optional_dict (kwargs : t .MutableMapping [str , object ], key : str ) -> t . Optional [ t . Dict [ str , object ]] :
73+ def _pop_optional_dict (kwargs : t .MutableMapping [str , object ], key : str ) -> dict [ str , object ] | None :
7774 value = kwargs .pop (key , None )
7875 if value is None :
7976 return None
@@ -85,7 +82,7 @@ def _pop_optional_dict(kwargs: t.MutableMapping[str, object], key: str) -> t.Opt
8582def initialize_speech_service (
8683 service : "SpeechService" ,
8784 kwargs : t .MutableMapping [str , object ],
88- transcription_model : t . Optional [ str ] = None ,
85+ transcription_model : str | None = None ,
8986) -> None :
9087 model = _pop_optional_str (kwargs , "transcription_model" )
9188 SpeechService .__init__ (
@@ -98,8 +95,8 @@ def initialize_speech_service(
9895 service .additional_kwargs .update (kwargs )
9996
10097
101- def timestamps_to_word_boundaries (segments : t .Sequence [TranscriptionSegment ]) -> t . List [WordBoundary ]:
102- word_boundaries : t . List [WordBoundary ] = []
98+ def timestamps_to_word_boundaries (segments : t .Sequence [TranscriptionSegment ]) -> list [WordBoundary ]:
99+ word_boundaries : list [WordBoundary ] = []
103100 current_text_offset = 0
104101 for segment in segments :
105102 for dict_ in segment ["words" ]:
@@ -128,9 +125,9 @@ class SpeechService(ABC):
128125 def __init__ (
129126 self ,
130127 global_speed : float = 1.00 ,
131- cache_dir : t . Optional [ PathLike ] = None ,
132- transcription_model : t . Optional [ str ] = None ,
133- transcription_kwargs : t . Optional [ t . Dict [ str , object ]] = None ,
128+ cache_dir : PathLike | None = None ,
129+ transcription_model : str | None = None ,
130+ transcription_kwargs : dict [ str , object ] | None = None ,
134131 ** kwargs : object ,
135132 ) -> None :
136133 """
@@ -155,8 +152,8 @@ def __init__(
155152 if not os .path .exists (self .cache_dir ):
156153 os .makedirs (self .cache_dir )
157154
158- self .transcription_model : t . Optional [ str ] = None
159- self ._whisper_model : t . Optional [ WhisperModel ] = None
155+ self .transcription_model : str | None = None
156+ self ._whisper_model : WhisperModel | None = None
160157 self .set_transcription (
161158 model = transcription_model ,
162159 kwargs = {} if transcription_kwargs is None else transcription_kwargs ,
@@ -172,7 +169,7 @@ def _wrap_generate_from_text(
172169 # Replace newlines with lines, reduce multiple consecutive spaces to single
173170 text = " " .join (text .split ())
174171 raw_path = kwargs .pop ("path" , None )
175- path : t . Optional [ PathLike ] = None
172+ path : PathLike | None = None
176173 if raw_path is not None :
177174 if not isinstance (raw_path , (str , os .PathLike )):
178175 raise TypeError ("path must be a string or path-like object" )
@@ -218,7 +215,7 @@ def _wrap_generate_from_text(
218215 append_voiceover_cache_entry (Path (self .cache_dir ) / DEFAULT_VOICEOVER_CACHE_JSON_FILENAME , dict_ )
219216 return dict_
220217
221- def set_transcription (self , model : t . Optional [ str ] = None , kwargs : t . Optional [ t . Dict [ str , object ]] = None ) -> None :
218+ def set_transcription (self , model : str | None = None , kwargs : dict [ str , object ] | None = None ) -> None :
222219 """Set the transcription model and keyword arguments to be passed
223220 to the transcribe() function.
224221
@@ -264,8 +261,8 @@ def get_audio_basename(self, data: t.Mapping[str, JsonValue]) -> str:
264261 def generate_from_text (
265262 self ,
266263 text : str ,
267- cache_dir : t . Optional [ PathLike ] = None ,
268- path : t . Optional [ PathLike ] = None ,
264+ cache_dir : PathLike | None = None ,
265+ path : PathLike | None = None ,
269266 ** kwargs : object ,
270267 ) -> VoiceoverData :
271268 """Implement this method for each speech service. Refer to `AzureService` for an example.
@@ -284,7 +281,7 @@ def get_cached_result(
284281 self ,
285282 input_data : t .Mapping [str , JsonValue ],
286283 cache_dir : PathLike ,
287- ) -> t . Optional [ VoiceoverData ] :
284+ ) -> VoiceoverData | None :
288285 json_path = Path (cache_dir ) / DEFAULT_VOICEOVER_CACHE_JSON_FILENAME
289286 requested_input_data = json_object (input_data )
290287 for entry in load_voiceover_cache (json_path ):
0 commit comments