Spaces:
Runtime error
Runtime error
Update transcription.py
Browse files- transcription.py +97 -92
transcription.py
CHANGED
|
@@ -1,93 +1,98 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from faster_whisper import WhisperModel
|
| 3 |
-
from pydub import AudioSegment
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
return output_dir
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from faster_whisper import WhisperModel
|
| 3 |
+
from pydub import AudioSegment
|
| 4 |
+
# Matplotlibのキャッシュディレクトリを変更
|
| 5 |
+
os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
|
| 6 |
+
|
| 7 |
+
# Hugging Faceのキャッシュディレクトリを変更
|
| 8 |
+
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 9 |
+
|
| 10 |
+
class TranscriptionMaker():
|
| 11 |
+
#書き起こしファイルを吐き出すディレクトリを指定
|
| 12 |
+
def __init__(self,output_dir="/tmp/data/transcriptions"):
|
| 13 |
+
self.model = WhisperModel("base", device="cpu")
|
| 14 |
+
self.output_dir = output_dir
|
| 15 |
+
try:
|
| 16 |
+
if not os.path.exists(self.output_dir):
|
| 17 |
+
os.makedirs(self.output_dir)
|
| 18 |
+
except OSError as e:
|
| 19 |
+
print(f"Error creating directory {self.output_dir}: {e}")
|
| 20 |
+
raise
|
| 21 |
+
|
| 22 |
+
#音声ファイルのディレクトリを受け取り、書き起こしファイルを作成する
|
| 23 |
+
def create_transcription(self,segments_directory):
|
| 24 |
+
results = []
|
| 25 |
+
#細切れ音声をくっつける
|
| 26 |
+
audio_directory = self.merge_segments(segments_directory)
|
| 27 |
+
#ディレクトリ内のファイルを全て取得
|
| 28 |
+
if not os.path.isdir(audio_directory):
|
| 29 |
+
raise ValueError(f"The specified path is not a valid directory: {audio_directory}")
|
| 30 |
+
audio_files = os.listdir(audio_directory)
|
| 31 |
+
for audio_file in audio_files:
|
| 32 |
+
if os.path.splitext(audio_file)[-1].lower() != '.wav':
|
| 33 |
+
continue
|
| 34 |
+
audio_path = os.path.join(audio_directory, audio_file)
|
| 35 |
+
try:
|
| 36 |
+
segments,info = list(self.model.transcribe(audio_path))
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"Error transcripting file {audio_path}: {e}")
|
| 39 |
+
raise
|
| 40 |
+
for segment in segments:
|
| 41 |
+
results.append({
|
| 42 |
+
"start": segment.start,
|
| 43 |
+
"end": segment.end,
|
| 44 |
+
"text": segment.text
|
| 45 |
+
})
|
| 46 |
+
#ファイルの書き込み。ファイル名は"読み込みディレクトリ名_transcription.txt"
|
| 47 |
+
output_file=os.path.join(self.output_dir,os.path.basename(audio_directory)+"_transcription.txt")
|
| 48 |
+
try:
|
| 49 |
+
with open(output_file,"w",encoding="utf-8") as f:
|
| 50 |
+
for result in results:
|
| 51 |
+
f.write(f"{result['text']}\n")
|
| 52 |
+
except OSError as e:
|
| 53 |
+
print(f"Error writing transcription file: {e}")
|
| 54 |
+
raise
|
| 55 |
+
return output_file
|
| 56 |
+
|
| 57 |
+
#ファイル名が連続しているならくっつける
|
| 58 |
+
def merge_segments(self,segments_dir,output_dir = "/tmp/data/merged_audio"):
|
| 59 |
+
if not os.path.exists(output_dir):
|
| 60 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 61 |
+
|
| 62 |
+
files = sorted([f for f in os.listdir(segments_dir) if f.endswith('.wav')])
|
| 63 |
+
|
| 64 |
+
merged_files = []
|
| 65 |
+
current_group = []
|
| 66 |
+
previous_index = None
|
| 67 |
+
|
| 68 |
+
for file in files:
|
| 69 |
+
# ファイル名から番号を抽出(例: "0.wav" -> 0)
|
| 70 |
+
file_index = int(file.split('.')[0])
|
| 71 |
+
|
| 72 |
+
# 番号が連続していない場合、新しいグループを作成
|
| 73 |
+
if previous_index is not None and file_index != previous_index + 1:
|
| 74 |
+
# 現在のグループを結合して保存
|
| 75 |
+
if current_group:
|
| 76 |
+
merged_files.append(current_group)
|
| 77 |
+
current_group = []
|
| 78 |
+
|
| 79 |
+
# 現在のファイルをグループに追加
|
| 80 |
+
current_group.append(file)
|
| 81 |
+
previous_index = file_index
|
| 82 |
+
|
| 83 |
+
# 最後のグループを追加
|
| 84 |
+
if current_group:
|
| 85 |
+
merged_files.append(current_group)
|
| 86 |
+
|
| 87 |
+
# グループごとに結合して保存
|
| 88 |
+
for i, group in enumerate(merged_files):
|
| 89 |
+
combined_audio = AudioSegment.empty()
|
| 90 |
+
for file in group:
|
| 91 |
+
file_path = os.path.join(segments_dir, file)
|
| 92 |
+
segment = AudioSegment.from_file(file_path)
|
| 93 |
+
combined_audio += segment
|
| 94 |
+
# 出力ファイル名を設定して保存
|
| 95 |
+
output_file = os.path.join(output_dir, f'merged_{i}.wav')
|
| 96 |
+
combined_audio.export(output_file, format='wav')
|
| 97 |
+
|
| 98 |
return output_dir
|