Spaces:
Runtime error
Runtime error
| import os | |
| from faster_whisper import WhisperModel | |
| class TranscriptionMaker(): | |
| #書き起こしファイル(ファイル名_transcription.txt)を吐き出すディレクトリを指定 | |
| def __init__(self,output_dir=os.path.abspath("/tmp/data/transcriptions")): | |
| self.model = WhisperModel("base", device="cpu") | |
| self.output_dir = output_dir | |
| try: | |
| if not os.path.exists(self.output_dir): | |
| os.makedirs(self.output_dir) | |
| except OSError as e: | |
| print(f"Error creating directory {self.output_dir}: {e}") | |
| raise | |
| #音声ファイルのパスを受け取り、書き起こしファイルを作成する | |
| def create_transcription(self,audio_path): | |
| try: | |
| if not os.path.isfile(audio_path): | |
| raise FileNotFoundError(f"The specified audio file does not exist: {audio_path}") | |
| segments, info = self.model.transcribe(audio_path) | |
| results = [] | |
| for segment in segments: | |
| results.append({ | |
| "start": segment.start, | |
| "end": segment.end, | |
| "text": segment.text | |
| }) | |
| #ファイルの書き込み | |
| output_file=os.path.join(self.output_dir,os.path.basename(audio_path)+"_transcription.txt") | |
| try: | |
| with open(output_file,"w",encoding="utf-8") as f: | |
| for result in results: | |
| f.write(f"[{result['start']:.2f}s - {result['end']:.2f}s] {result['text']}\n") | |
| except OSError as e: | |
| print(f"Error writing transcription file: {e}") | |
| raise | |
| return output_file | |
| except FileNotFoundError as e: | |
| print(f"Error: {e}") | |
| raise | |
| except Exception as e: | |
| print(f"An unexpected error occurred: {e}") | |
| raise |