A-yum1 commited on
Commit
21b85ef
·
2 Parent(s): cf6a323 dda9dff

merge branch

Browse files
__pycache__/process.cpython-310.pyc CHANGED
Binary files a/__pycache__/process.cpython-310.pyc and b/__pycache__/process.cpython-310.pyc differ
 
__pycache__/transcription.cpython-310.pyc ADDED
Binary file (2.84 kB). View file
 
app.py CHANGED
@@ -4,11 +4,14 @@ from pydub import AudioSegment # 変換用にpydubをインポート
4
  import os
5
  import shutil
6
  from process import AudioProcessor
 
7
 
8
  process=AudioProcessor()
 
9
  app = Flask(__name__)
10
 
11
  users = []
 
12
 
13
  # トップページ(テンプレート: index.html)
14
  @app.route('/')
@@ -21,7 +24,6 @@ def index():
21
  def feedback():
22
  return render_template('feedback.html')
23
 
24
-
25
  # 会話詳細画面(テンプレート: talkDetail.html)
26
  @app.route('/talk_detail', methods=['GET', 'POST'])
27
  def talk_detail():
@@ -36,11 +38,17 @@ def userregister():
36
  def confirm():
37
  return jsonify({'members': users}), 200
38
 
39
-
 
 
 
 
 
40
 
41
  # 音声アップロード&解析エンドポイント
42
  @app.route('/upload_audio', methods=['POST'])
43
  def upload_audio():
 
44
  try:
45
  data = request.get_json()
46
  # name か users のいずれかが必須。どちらも無い場合はエラー
@@ -71,9 +79,9 @@ def upload_audio():
71
  # 複数人の場合は参照パスのリストを、1人の場合は単一のパスを渡す
72
  if len(users) > 1:
73
  print("複数人の場合の処理")
74
- matched_time, unmatched_time = process.process_multi_audio(reference_paths, audio_path, threshold=0.05)
75
  else:
76
- matched_time, unmatched_time = process.process_audio(reference_paths[0], audio_path, threshold=0.05)
77
 
78
  total_time = matched_time + unmatched_time
79
  rate = (matched_time / total_time) * 100 if total_time > 0 else 0
 
4
  import os
5
  import shutil
6
  from process import AudioProcessor
7
+ from transcription import TranscriptionMaker
8
 
9
  process=AudioProcessor()
10
+ transcription = TranscriptionMaker()
11
  app = Flask(__name__)
12
 
13
  users = []
14
+ segments_dir
15
 
16
  # トップページ(テンプレート: index.html)
17
  @app.route('/')
 
24
  def feedback():
25
  return render_template('feedback.html')
26
 
 
27
  # 会話詳細画面(テンプレート: talkDetail.html)
28
  @app.route('/talk_detail', methods=['GET', 'POST'])
29
  def talk_detail():
 
38
  def confirm():
39
  return jsonify({'members': users}), 200
40
 
41
+ # 書き起こし作成エンドポイント
42
+ @app.route('/transcription',methods =['GET','POST'])
43
+ def transcription():
44
+ global segments_dir
45
+ text = transcription.create_transcription(segments_dir)
46
+ return jsonify({'transcription': text}),200
47
 
48
  # 音声アップロード&解析エンドポイント
49
  @app.route('/upload_audio', methods=['POST'])
50
  def upload_audio():
51
+ global segments_dir
52
  try:
53
  data = request.get_json()
54
  # name か users のいずれかが必須。どちらも無い場合はエラー
 
79
  # 複数人の場合は参照パスのリストを、1人の場合は単一のパスを渡す
80
  if len(users) > 1:
81
  print("複数人の場合の処理")
82
+ matched_time, unmatched_time,segments_dir = process.process_multi_audio(reference_paths, audio_path, threshold=0.05)
83
  else:
84
+ matched_time, unmatched_time, segments_dir = process.process_audio(reference_paths[0], audio_path, threshold=0.05)
85
 
86
  total_time = matched_time + unmatched_time
87
  rate = (matched_time / total_time) * 100 if total_time > 0 else 0
process.py CHANGED
@@ -89,7 +89,7 @@ class AudioProcessor():
89
  matched_time_ms += len(AudioSegment.from_file(segment_file))
90
 
91
  unmatched_time_ms = total_duration_ms - matched_time_ms
92
- return matched_time_ms, unmatched_time_ms
93
 
94
 
95
  def process_multi_audio(self, reference_pathes, input_path, output_folder='/tmp/data/matched_multi_segments', seg_duration=1.0, threshold=0.5):
@@ -141,7 +141,7 @@ class AudioProcessor():
141
  if match is not None:
142
  matched_time[match] += seg_duration
143
 
144
- return matched_time
145
 
146
 
147
  def save_audio_from_base64(self,base64_audio,output_dir,output_filename,temp_format='webm'):
 
89
  matched_time_ms += len(AudioSegment.from_file(segment_file))
90
 
91
  unmatched_time_ms = total_duration_ms - matched_time_ms
92
+ return matched_time_ms, unmatched_time_ms, output_folder
93
 
94
 
95
  def process_multi_audio(self, reference_pathes, input_path, output_folder='/tmp/data/matched_multi_segments', seg_duration=1.0, threshold=0.5):
 
141
  if match is not None:
142
  matched_time[match] += seg_duration
143
 
144
+ return matched_time,segment_file,segmented_path
145
 
146
 
147
  def save_audio_from_base64(self,base64_audio,output_dir,output_filename,temp_format='webm'):
transcription.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  from faster_whisper import WhisperModel
 
3
 
4
  class TranscriptionMaker():
5
  #書き起こしファイルを吐き出すディレクトリを指定
@@ -14,8 +15,10 @@ class TranscriptionMaker():
14
  raise
15
 
16
  #音声ファイルのディレクトリを受け取り、書き起こしファイルを作成する
17
- def create_transcription(self,audio_directory):
18
  results = []
 
 
19
  #ディレクトリ内のファイルを全て取得
20
  if not os.path.isdir(audio_directory):
21
  raise ValueError(f"The specified path is not a valid directory: {audio_directory}")
@@ -44,4 +47,47 @@ class TranscriptionMaker():
44
  except OSError as e:
45
  print(f"Error writing transcription file: {e}")
46
  raise
47
- return output_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  from faster_whisper import WhisperModel
3
+ from pydub import AudioSegment
4
 
5
  class TranscriptionMaker():
6
  #書き起こしファイルを吐き出すディレクトリを指定
 
15
  raise
16
 
17
  #音声ファイルのディレクトリを受け取り、書き起こしファイルを作成する
18
+ def create_transcription(self,segments_directory):
19
  results = []
20
+ #細切れ音声をくっつける
21
+ audio_directory = self.merge_segments(segments_directory)
22
  #ディレクトリ内のファイルを全て取得
23
  if not os.path.isdir(audio_directory):
24
  raise ValueError(f"The specified path is not a valid directory: {audio_directory}")
 
47
  except OSError as e:
48
  print(f"Error writing transcription file: {e}")
49
  raise
50
+ return output_file
51
+
52
+ #ファイル名が連続しているならくっつける
53
+ def merge_segments(self,segments_dir,output_dir = "/tmp/data/merged_audio"):
54
+ if not os.path.exists(output_dir):
55
+ os.makedirs(output_dir, exist_ok=True)
56
+
57
+ files = sorted([f for f in os.listdir(segments_dir) if f.endswith('.wav')])
58
+
59
+ merged_files = []
60
+ current_group = []
61
+ previous_index = None
62
+
63
+ for file in files:
64
+ # ファイル名から番号を抽出(例: "0.wav" -> 0)
65
+ file_index = int(file.split('.')[0])
66
+
67
+ # 番号が連続していない場合、新しいグループを作成
68
+ if previous_index is not None and file_index != previous_index + 1:
69
+ # 現在のグループを結合して保存
70
+ if current_group:
71
+ merged_files.append(current_group)
72
+ current_group = []
73
+
74
+ # 現在のファイルをグループに追加
75
+ current_group.append(file)
76
+ previous_index = file_index
77
+
78
+ # 最後のグループを追加
79
+ if current_group:
80
+ merged_files.append(current_group)
81
+
82
+ # グループごとに結合して保存
83
+ for i, group in enumerate(merged_files):
84
+ combined_audio = AudioSegment.empty()
85
+ for file in group:
86
+ file_path = os.path.join(segments_dir, file)
87
+ segment = AudioSegment.from_file(file_path)
88
+ combined_audio += segment
89
+ # 出力ファイル名を設定して保存
90
+ output_file = os.path.join(output_dir, f'merged_{i}.wav')
91
+ combined_audio.export(output_file, format='wav')
92
+
93
+ return output_dir