@@ -87,17 +87,37 @@ def predict(
8787 Returns:
8888 PwganResult: The predicted pwgan results, support str and json output.
8989 """
90- if type (input ) == str :
91- if input .endswith ("txt" ):
92- if os .path .exists (input ):
90+ sentences = []
91+ if isinstance (input , str ):
92+ if input .endswith (".txt" ):
93+ if not os .path .exists (input ):
94+ raise FileNotFoundError (f"The specified text file does not exist: { input } " )
95+ try :
9396 with open (input , "r" , encoding = "utf-8" ) as f :
94- sentences = f .readlines ()
97+ sentences = [line .strip () for line in f .readlines ()]
98+ except IOError as e :
99+ raise IOError (f"An error occurred while reading the file { input } : { e } " )
95100 else :
96101 sentences = [input ]
97- elif type (input ) == list :
98- sentences = input
102+ elif isinstance (input , list ):
103+ sentences = []
104+ for item in input :
105+ if isinstance (item , str ):
106+ if item .endswith (".txt" ):
107+ if not os .path .exists (item ):
108+ raise FileNotFoundError (f"The specified text file in the list does not exist: { item } " )
109+ try :
110+ with open (item , "r" , encoding = "utf-8" ) as f :
111+ sentences .extend ([line .strip () for line in f .readlines ()])
112+ except IOError as e :
113+ raise IOError (f"An error occurred while reading the file { item } : { e } " )
114+ else :
115+ sentences .append (item )
99116 else :
100- raise TypeError ("Input must be string or list of strings." )
117+ raise TypeError (f"Unsupported input type: { type (input )} . Expected str, list, or np.ndarray." )
118+ if not sentences :
119+ raise ValueError ("The input resulted in an empty list of sentences to process." )
120+
101121 for sentence in sentences :
102122 text_to_pinyin_res = [self .get_text_to_pinyin_result (sentence )['result' ]['phone_ids' ]]
103123 text_to_speech_acoustic_res = [self .get_text_to_speech_acoustic_result (text_to_pinyin_res )['result' ]]
0 commit comments