Spaces:
Runtime error
Runtime error
Upload 10 files
Browse files- README.md +10 -329
- app.py +299 -112
- transcription.py +108 -89
README.md
CHANGED
|
@@ -1,329 +1,10 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: JusTalk
|
| 3 |
-
emoji: ⚡
|
| 4 |
-
colorFrom: gray
|
| 5 |
-
colorTo: blue
|
| 6 |
-
sdk: docker
|
| 7 |
-
pinned: false
|
| 8 |
-
---
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
---
|
| 12 |
-
|
| 13 |
-
## グローバル変数と主要な設定
|
| 14 |
-
|
| 15 |
-
- **`users`**
|
| 16 |
-
登録されたユーザー名のリスト。
|
| 17 |
-
- **`transcription_text`**
|
| 18 |
-
音声から作成された書き起こしのファイルパスまたは内容。
|
| 19 |
-
- **`total_audio`**
|
| 20 |
-
音声セグメントをマージした結果の音声ファイルパス。
|
| 21 |
-
- **`harassment_keywords`**
|
| 22 |
-
ハラスメントとして検出するキーワードのリスト(例:"バカ", "死ね" など)。
|
| 23 |
-
|
| 24 |
-
また、外部ライブラリとして `pydub`、および独自の `AudioProcessor`、`TranscriptionMaker`、`TextAnalyzer` を用いて音声処理や解析を行います。
|
| 25 |
-
|
| 26 |
-
---
|
| 27 |
-
|
| 28 |
-
## 各エンドポイントの概要
|
| 29 |
-
|
| 30 |
-
### 1. トップページ/ユーザー登録画面
|
| 31 |
-
|
| 32 |
-
- **URL:** `/index`
|
| 33 |
-
**メソッド:** GET, POST
|
| 34 |
-
**機能:**
|
| 35 |
-
- テンプレート `index.html` を表示。
|
| 36 |
-
- 現在の `users` リストをテンプレートに渡す。
|
| 37 |
-
|
| 38 |
-
- **URL:** `/` または `/userregister`
|
| 39 |
-
**メソッド:** GET, POST
|
| 40 |
-
**機能:**
|
| 41 |
-
- ユーザー登録用の画面を表示するため、`userRegister.html` テンプレートを返す。
|
| 42 |
-
|
| 43 |
-
---
|
| 44 |
-
|
| 45 |
-
### 2. フィードバックおよび会話詳細画面
|
| 46 |
-
|
| 47 |
-
- **URL:** `/feedback`
|
| 48 |
-
**メソッド:** GET, POST
|
| 49 |
-
**機能:**
|
| 50 |
-
- フィードバック画面(テンプレート:`feedback.html`)を表示。
|
| 51 |
-
|
| 52 |
-
- **URL:** `/talk_detail`
|
| 53 |
-
**メソッド:** GET, POST
|
| 54 |
-
**機能:**
|
| 55 |
-
- 会話の詳細を表示する画面(テンプレート:`talkDetail.html`)を返す。
|
| 56 |
-
|
| 57 |
-
---
|
| 58 |
-
|
| 59 |
-
### 3. ユーザーとファイルのリセット関連
|
| 60 |
-
|
| 61 |
-
- **URL:** `/reset_html`
|
| 62 |
-
**メソッド:** GET, POST
|
| 63 |
-
**機能:**
|
| 64 |
-
- リセット画面(テンプレート:`reset.html`)を表示。
|
| 65 |
-
|
| 66 |
-
- **URL:** `/reset_member`
|
| 67 |
-
**メソッド:** GET, POST
|
| 68 |
-
**機能:**
|
| 69 |
-
- 指定されたメンバー(ユーザー)とその関連の参照音声ファイルを削除する。
|
| 70 |
-
- リクエストボディ内で `"names"` キーが必須。
|
| 71 |
-
- `/tmp/data/base_audio` 内の各ユーザーの音声ファイル(`{name}.wav`)を削除。
|
| 72 |
-
- 削除後、`users` リストから対象ユーザーを除外する。
|
| 73 |
-
- 加えて、累積音声ファイル(`total_audio`)や、書き起こしファイル(`transcription_text`)も削除する処理を実施。
|
| 74 |
-
|
| 75 |
-
- **URL:** `/reset`
|
| 76 |
-
**メソッド:** GET
|
| 77 |
-
**機能:**
|
| 78 |
-
- `users` リストを空にリセットし、成功レスポンスを返す。
|
| 79 |
-
- `/reset_member`同様、累積音声ファイル(`total_audio`)と、書き起こしファイル(`transcription_text`)も削除する。
|
| 80 |
-
---
|
| 81 |
-
|
| 82 |
-
### 4. 状態確認エンドポイント
|
| 83 |
-
|
| 84 |
-
- **URL:** `/confirm`
|
| 85 |
-
**メソッド:** GET
|
| 86 |
-
**機能:**
|
| 87 |
-
- 現在登録されているメンバー(`users`)を JSON 形式で返す。
|
| 88 |
-
|
| 89 |
-
---
|
| 90 |
-
|
| 91 |
-
### 5. 音声アップロード・解析関連
|
| 92 |
-
|
| 93 |
-
#### a. 基本の音声アップロードと参照音声との照合
|
| 94 |
-
|
| 95 |
-
- **URL:** `/upload_audio`
|
| 96 |
-
**メソッド:** POST
|
| 97 |
-
**機能:**
|
| 98 |
-
- クライアントから受け取った Base64 形式の音声データをデコードし、一時ファイル(`/tmp/data/tmp.wav`)として保存。
|
| 99 |
-
- リクエストボディ内には、`audio_data` とともに、`name` または `users`(登録済みユーザーの情報)が必要。
|
| 100 |
-
- 各ユーザーの参照音声ファイル(`/tmp/data/base_audio/{user}.wav`)とアップロードされた音声を比較し、
|
| 101 |
-
- 複数ユーザーの場合:`process_multi_audio` を使用して各ユーザー毎の一致時間を計算し、その比率(パーセンテージ)を返す。
|
| 102 |
-
- 単一ユーザーの場合:`process_audio` を用いて一致時間と非一致時間から比率(パーセンテージ)を計算して返す。
|
| 103 |
-
- また、処理後に複数の音声セグメントをマージし、その結果を `total_audio` に格納する。
|
| 104 |
-
|
| 105 |
-
#### b. 基準音声のアップロード(ユーザー登録時に使用)
|
| 106 |
-
|
| 107 |
-
- **URL:** `/upload_base_audio`
|
| 108 |
-
**メソッド:** POST
|
| 109 |
-
**機能:**
|
| 110 |
-
- リクエストボディ内の `audio_data`(Base64形式)と `name` を受け取り、
|
| 111 |
-
- `users` リストに名前を追加(重複排除を実施)。
|
| 112 |
-
- `/tmp/data/base_audio/` 内に、`{name}.wav` という名前で保存。
|
| 113 |
-
- 登録成功時に、状態とファイルパスを返す。
|
| 114 |
-
|
| 115 |
-
---
|
| 116 |
-
|
| 117 |
-
### 6. 書き起こし生成およびテキスト解析
|
| 118 |
-
|
| 119 |
-
#### a. 書き起こし作成
|
| 120 |
-
|
| 121 |
-
- **URL:** `/transcription`
|
| 122 |
-
**メソッド:** GET, POST
|
| 123 |
-
**機能:**
|
| 124 |
-
- グローバル変数 `transcription_text` に書き起こし済みのファイルが存在しない場合、
|
| 125 |
-
- `total_audio`(マージ済み音声ファイル)が存在しているかをチェック。
|
| 126 |
-
- `TranscriptionMaker` の `merge_segments` メソッドでセグメントをマー���し、`create_transcription` で書き起こしファイルを生成。
|
| 127 |
-
- 書き起こしファイルを読み込み、内容を JSON 形式で返す。
|
| 128 |
-
- エラー発生時には適切な HTTP ステータスコード(400, 404, 500)とエラーメッセージを返す。
|
| 129 |
-
|
| 130 |
-
#### b. AI によるテキスト解析
|
| 131 |
-
|
| 132 |
-
- **URL:** `/analyze`
|
| 133 |
-
**メソッド:** GET, POST
|
| 134 |
-
**機能:**
|
| 135 |
-
- `/transcription` と同様に、書き起こしファイルの存在を確認し、必要なら再生成する。
|
| 136 |
-
- `TextAnalyzer` クラスを使って、書き起こしテキストとハラスメントキーワードをもとに解析を実施。
|
| 137 |
-
- 環境変数 `DEEPSEEK` を API キーとして取得し、DeepSeek 解析を呼び出す。
|
| 138 |
-
- 解析結果(会話レベル、ハラスメントの有無や種類、繰り返しの程度、会話の心地よさ、非難やハラスメントの程度など)をコンソール出力し、JSON 形式で返す。
|
| 139 |
-
|
| 140 |
-
---
|
| 141 |
-
|
| 142 |
-
## エンドポイント間の流れとポイント
|
| 143 |
-
|
| 144 |
-
- **ユーザー登録:**
|
| 145 |
-
`/upload_base_audio` でユーザーごとの基準音声を登録し、`users` リストに名前を追加。
|
| 146 |
-
|
| 147 |
-
- **音声解析:**
|
| 148 |
-
`/upload_audio` によりアップロードされた音声を、登録された参照音声と照合。
|
| 149 |
-
- 複数ユーザーの場合、各ユーザーの一致時間を計算し、パーセンテージとして返す。
|
| 150 |
-
- 単一ユーザーの場合、一致時間と非一致時間から比率を返す。
|
| 151 |
-
- この段階で、音声セグメントをマージし、後続の書き起こし・解析に利用するために `total_audio` に格納。
|
| 152 |
-
|
| 153 |
-
- **書き起こしの生成と利用:**
|
| 154 |
-
`/transcription` で `total_audio` から書き起こしファイルを生成し、その内容を取得可能。
|
| 155 |
-
`/analyze` では、生成済みの書き起こしテキストをもとに、DeepSeek API を用いた AI 解析が実行される。
|
| 156 |
-
|
| 157 |
-
- **リセット機能:**
|
| 158 |
-
`/reset_member` で指定ユーザーの音声ファイル、累積音声、書き起こしファイルを削除。
|
| 159 |
-
`/reset` で `users` リストを完全にクリアする。
|
| 160 |
-
|
| 161 |
-
---
|
| 162 |
-
|
| 163 |
-
## <process.py>
|
| 164 |
-
---
|
| 165 |
-
|
| 166 |
-
## 全体概要
|
| 167 |
-
|
| 168 |
-
- **目的:**
|
| 169 |
-
音声ファイルの前処理、セグメント分割、音声特徴量(エンベディング)計算、類似度算出、さらに Base64 形式の音声データの保存など、音声データの様々な操作を行うための機能を提供します。
|
| 170 |
-
|
| 171 |
-
- **利用ライブラリ:**
|
| 172 |
-
- `pydub`:音声ファイルの読み込み、書き出し、セグメント分割に使用
|
| 173 |
-
- `pyannote.audio`:事前学習済みモデルを用いて音声のエンベディング(特徴量)を計算
|
| 174 |
-
- `numpy`:数値計算およびベクトル演算(コサイン類似度計算など)
|
| 175 |
-
- その他、`os`, `shutil`, `base64`, `datetime` など、ファイル操作や乱数生成に利用
|
| 176 |
-
|
| 177 |
-
- **初期設定:**
|
| 178 |
-
コンストラクタ (`__init__`) では、環境変数 `HF` から Hugging Face のトークンを取得し、`pyannote` のモデルをロード。標準の音声長さ(秒)も設定します。
|
| 179 |
-
|
| 180 |
-
---
|
| 181 |
-
|
| 182 |
-
## 各関数の詳細
|
| 183 |
-
|
| 184 |
-
### 1. `normalize_audio_duration`
|
| 185 |
-
- **目的:**
|
| 186 |
-
指定された音声ファイルの長さを、ターゲットの秒数に合わせて正規化(短い場合は無音でパディング、長い場合は切り詰め)します。
|
| 187 |
-
|
| 188 |
-
- **引数:**
|
| 189 |
-
- `input_path`:入力音声ファイルのパス
|
| 190 |
-
- `target_duration_seconds`:目標とする音声の長さ(秒)。未指定の場合は標準値(`self.standard_duration`)を使用
|
| 191 |
-
- `output_path`:出力先のパス(未指定の場合は一時ファイルとして生成)
|
| 192 |
-
|
| 193 |
-
- **戻り値:**
|
| 194 |
-
正規化された音声ファイルのパス
|
| 195 |
-
|
| 196 |
-
---
|
| 197 |
-
|
| 198 |
-
### 2. `batch_normalize_audio_duration`
|
| 199 |
-
- **目的:**
|
| 200 |
-
指定したディレクトリ内のすべての音声ファイルに対して、`normalize_audio_duration` を適用し、ファイルの長さを統一します。
|
| 201 |
-
|
| 202 |
-
- **引数:**
|
| 203 |
-
- `input_directory`:入力音声ファイルが格納されているディレクトリ
|
| 204 |
-
- `target_duration_seconds`:目標とする音声の長さ(秒)
|
| 205 |
-
- `output_directory`:出力先ディレクトリ(未指定の場合は入力ディレクトリと同じ場所)
|
| 206 |
-
|
| 207 |
-
- **戻り値:**
|
| 208 |
-
処理後の音声ファイルパスのリスト
|
| 209 |
-
|
| 210 |
-
---
|
| 211 |
-
|
| 212 |
-
### 3. `cosine_similarity`
|
| 213 |
-
- **目的:**
|
| 214 |
-
2つのベクトル間のコサイン類似度を計算します。
|
| 215 |
-
- **引数:**
|
| 216 |
-
- `vec1`, `vec2`:比較対象となる numpy 配列
|
| 217 |
-
- **処理:**
|
| 218 |
-
次元が一致しているかを確認し、各ベクトルを正規化した後、内積を計算
|
| 219 |
-
- **戻り値:**
|
| 220 |
-
コサイン類似度(-1~1の範囲)
|
| 221 |
-
|
| 222 |
-
---
|
| 223 |
-
|
| 224 |
-
### 4. `segment_audio`
|
| 225 |
-
- **目的:**
|
| 226 |
-
入力音声ファイルを指定した秒数のセグメントに分割し、各セグメントを出力ディレクトリに保存します。
|
| 227 |
-
- **��数:**
|
| 228 |
-
- `path`:入力音声ファイルのパス
|
| 229 |
-
- `target_path`:分割されたセグメントの保存先ディレクトリ
|
| 230 |
-
- `seg_duration`:各セグメントの長さ(秒)
|
| 231 |
-
- **戻り値:**
|
| 232 |
-
分割されたセグメントのディレクトリパスと、元の音声の総時間(ミリ秒)
|
| 233 |
-
|
| 234 |
-
---
|
| 235 |
-
|
| 236 |
-
### 5. `calculate_embedding`
|
| 237 |
-
- **目的:**
|
| 238 |
-
指定した音声ファイルからエンベディング(特徴量)を計算します。
|
| 239 |
-
- **引数:**
|
| 240 |
-
- `audio_path`:音声ファイルのパス
|
| 241 |
-
- **処理:**
|
| 242 |
-
- まず音声の長さを `normalize_audio_duration` で標準化
|
| 243 |
-
- `pyannote.audio` の推論機能を使い、エンベディングを計算
|
| 244 |
-
- 処理後に一時ファイルを削除(必要に応じて)
|
| 245 |
-
- **戻り値:**
|
| 246 |
-
エンベディング(flattened な numpy 配列)
|
| 247 |
-
|
| 248 |
-
---
|
| 249 |
-
|
| 250 |
-
### 6. `calculate_similarity`
|
| 251 |
-
- **目的:**
|
| 252 |
-
2つの音声ファイル間の類似度(コサイン類似度)を計算します。
|
| 253 |
-
- **引数:**
|
| 254 |
-
- `path1`, `path2`:比較対象となる2つの音声ファイルのパス
|
| 255 |
-
- **処理:**
|
| 256 |
-
- 各音声のエンベディングを計算し、次元チェックを実施
|
| 257 |
-
- `cosine_similarity` を利用して類似度を求める
|
| 258 |
-
- **戻り値:**
|
| 259 |
-
類似度(float 値、エラー時は None)
|
| 260 |
-
|
| 261 |
-
---
|
| 262 |
-
|
| 263 |
-
### 7. `process_audio`
|
| 264 |
-
- **目的:**
|
| 265 |
-
リファレンス音声と入力音声を比較し、リファレンスに類似したセグメントを抽出します。
|
| 266 |
-
- **引数:**
|
| 267 |
-
- `reference_path`:リファレンス音声のパス
|
| 268 |
-
- `input_path`:入力音声のパス
|
| 269 |
-
- `output_folder`:マッチしたセグメントの出力先ディレクトリ
|
| 270 |
-
- `seg_duration`:セグメントの長さ(秒)
|
| 271 |
-
- `threshold`:類似度の閾値
|
| 272 |
-
- **処理:**
|
| 273 |
-
- リファレンス音声のエンベディングを計算
|
| 274 |
-
- 入力音声をセグメントに分割し、各セグメントのエンベディングを計算
|
| 275 |
-
- 各セグメントとリファレンス間のコサイン類似度を計算し、閾値以上なら出力フォルダへコピー
|
| 276 |
-
- マッチしたセグメントの総時間と、マッチしなかった時間を計算
|
| 277 |
-
- **戻り値:**
|
| 278 |
-
タプル (マッチ時間[ms], 非マッチ時間[ms], 出力フォルダパス)
|
| 279 |
-
|
| 280 |
-
---
|
| 281 |
-
|
| 282 |
-
### 8. `process_multi_audio`
|
| 283 |
-
- **目的:**
|
| 284 |
-
複数のリファレンス音声に対して、入力音声内の各セグメントの類似度を計算し、各リファレンスごとの一致時間を集計します。
|
| 285 |
-
- **引数:**
|
| 286 |
-
- `reference_pathes`:リファレンス音声のパスのリスト
|
| 287 |
-
- `input_path`:入力音声のパス
|
| 288 |
-
- `output_folder`:マッチしたセグメントの出力先ディレクトリ
|
| 289 |
-
- `seg_duration`:各セグメントの長さ(秒)
|
| 290 |
-
- `threshold`:類似度の閾値
|
| 291 |
-
- **処理:**
|
| 292 |
-
- 各リファレンス音声のエンベディングを先に計算
|
| 293 |
-
- 入力音声をセグメントに分割し、各セグメントのエンベディングを計算
|
| 294 |
-
- リファレンス毎に各セグメントとの類似度を算出し、最も高い類似度が閾値以上ならそのリファレンスとマッチと判断
|
| 295 |
-
- 各リファレンスごとにマッチしたセグメントの時間(秒)を集計
|
| 296 |
-
- **戻り値:**
|
| 297 |
-
タプル (各リファレンスごとの一致時間のリスト, セグメントが保存されたディレクトリパス)
|
| 298 |
-
|
| 299 |
-
---
|
| 300 |
-
|
| 301 |
-
### 9. `save_audio_from_base64`
|
| 302 |
-
- **目的:**
|
| 303 |
-
Base64 形式でエンコードされた音声データをデコードし、WAV ファイルとして保存します。
|
| 304 |
-
- **引数:**
|
| 305 |
-
- `base64_audio`:Base64 エンコードされた音声データ
|
| 306 |
-
- `output_dir`:出力先ディレクトリ
|
| 307 |
-
- `output_filename`:出力するファイル名
|
| 308 |
-
- `temp_format`:一時ファイルのフォーマット(デフォルトは 'webm')
|
| 309 |
-
- **処理:**
|
| 310 |
-
- Base64 文字列をデコードし、一時ファイルに保存
|
| 311 |
-
- `pydub` を用いてファイルを読み込み、WAV 形式に変換して保存
|
| 312 |
-
- 一時ファイルは処理後に削除
|
| 313 |
-
- **戻り値:**
|
| 314 |
-
保存された WAV ファイルのパス
|
| 315 |
-
|
| 316 |
-
---
|
| 317 |
-
|
| 318 |
-
### 10. `delete_files_in_directory`
|
| 319 |
-
- **目的:**
|
| 320 |
-
指定したディレクトリ内に存在するすべてのファイルを削除します。
|
| 321 |
-
- **引数:**
|
| 322 |
-
- `directory_path`:対象のディレクトリパス
|
| 323 |
-
- **処理:**
|
| 324 |
-
- ディレクトリ内の各ファイルを走査し、ファイルのみ削除
|
| 325 |
-
- 削除結果やエラーはコンソールに出力
|
| 326 |
-
|
| 327 |
-
---
|
| 328 |
-
|
| 329 |
-
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: JusTalk
|
| 3 |
+
emoji: ⚡
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,30 +1,36 @@
|
|
| 1 |
from flask import Flask, request, jsonify, render_template, send_from_directory
|
| 2 |
import base64
|
| 3 |
-
from pydub import AudioSegment
|
| 4 |
import os
|
| 5 |
import shutil
|
|
|
|
|
|
|
|
|
|
| 6 |
from process import AudioProcessor
|
| 7 |
from transcription import TranscriptionMaker
|
| 8 |
from analyze import TextAnalyzer
|
| 9 |
-
import json
|
| 10 |
|
| 11 |
-
process=AudioProcessor()
|
| 12 |
transcripter = TranscriptionMaker()
|
| 13 |
app = Flask(__name__)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
harassment_keywords = [
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
total_audio = ""
|
| 23 |
|
| 24 |
-
|
| 25 |
@app.route('/index', methods=['GET', 'POST'])
|
| 26 |
def index():
|
| 27 |
-
return render_template('index.html', users
|
| 28 |
|
| 29 |
# フィードバック画面(テンプレート: feedback.html)
|
| 30 |
@app.route('/feedback', methods=['GET', 'POST'])
|
|
@@ -35,128 +41,121 @@ def feedback():
|
|
| 35 |
@app.route('/talk_detail', methods=['GET', 'POST'])
|
| 36 |
def talk_detail():
|
| 37 |
return render_template('talkDetail.html')
|
| 38 |
-
|
| 39 |
-
# 音声登録画面(テンプレート: userRegister.html)
|
| 40 |
@app.route('/')
|
|
|
|
| 41 |
@app.route('/userregister', methods=['GET', 'POST'])
|
| 42 |
def userregister():
|
| 43 |
return render_template('userRegister.html')
|
| 44 |
|
| 45 |
-
|
| 46 |
-
@app.route('/confirm', methods=['GET'])
|
| 47 |
def confirm():
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
@app.route('/reset_html', methods=['GET', 'POST'])
|
| 52 |
def reset_html():
|
| 53 |
return render_template('reset.html')
|
| 54 |
|
| 55 |
-
|
| 56 |
-
def login():
|
| 57 |
-
global users#グローバル変数を編集できるようにする
|
| 58 |
-
try:
|
| 59 |
-
data = request.get_json()
|
| 60 |
-
name = data['name'] # 名前を取得
|
| 61 |
-
if name in users:
|
| 62 |
-
print("名前はリストに存在します。")
|
| 63 |
-
index()
|
| 64 |
-
else:
|
| 65 |
-
print("名前はリストに存在しません。")
|
| 66 |
-
|
| 67 |
-
except Exception as e:
|
| 68 |
-
print("Error in /upload_base_audio:", str(e))
|
| 69 |
-
return jsonify({"error": "サーバーエラー", "details": str(e)}), 500
|
| 70 |
-
|
| 71 |
-
#メンバー削除&累積音声削除
|
| 72 |
@app.route('/reset_member', methods=['GET', 'POST'])
|
| 73 |
def reset_member():
|
| 74 |
global users
|
| 75 |
global total_audio
|
| 76 |
global transcription_text
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
process.delete_files_in_directory('/tmp/data/transcription_audio')
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
os.remove(transcription_text)
|
| 82 |
print(f"{transcription_text} を削除しました。")
|
| 83 |
-
|
| 84 |
-
print(f"{
|
| 85 |
-
|
| 86 |
-
print(f"エラーが発生しました: {e}")
|
| 87 |
transcription_text = ""
|
|
|
|
| 88 |
try:
|
| 89 |
data = request.get_json()
|
| 90 |
if not data or "names" not in data:
|
| 91 |
-
return jsonify({"status": "error", "message": "Invalid request body"}), 400
|
| 92 |
|
| 93 |
names = data.get("names", [])
|
| 94 |
-
|
| 95 |
-
|
| 96 |
for name in names:
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
return jsonify({"status": "error", "message": f"Failed to delete {name}: {e}"}), 500
|
| 106 |
-
|
| 107 |
-
else:
|
| 108 |
-
print(f"ファイルが存在しません: {file_path}")
|
| 109 |
-
|
| 110 |
-
# usersリストを更新
|
| 111 |
users = [u for u in users if u not in names]
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
return jsonify({"status": "success", "message": "Members deleted successfully", "users": users}), 200 # 200 OK
|
| 115 |
|
| 116 |
except Exception as e:
|
| 117 |
print(f"An unexpected error occurred: {e}")
|
| 118 |
-
return jsonify({"status": "error", "message": f"Internal server error: {e}"}), 500
|
| 119 |
|
| 120 |
# 書き起こし作成エンドポイント
|
| 121 |
-
@app.route('/transcription',methods
|
| 122 |
def transcription():
|
| 123 |
global transcription_text
|
| 124 |
global total_audio
|
|
|
|
| 125 |
if not os.path.exists(transcription_text) or not transcription_text:
|
| 126 |
try:
|
| 127 |
if not total_audio or not os.path.exists(total_audio):
|
| 128 |
-
return jsonify({"error": "No audio segments provided"}),400
|
| 129 |
-
audio_directory = transcripter.merge_segments(total_audio,'/tmp/data/transcription_audio')
|
| 130 |
transcription_text = transcripter.create_transcription(audio_directory)
|
| 131 |
print("transcription")
|
| 132 |
print(transcription_text)
|
| 133 |
except Exception as e:
|
| 134 |
-
return jsonify({"error": str(e)}),500
|
|
|
|
| 135 |
try:
|
| 136 |
-
with open(transcription_text,'r',encoding='utf-8') as file:
|
| 137 |
file_content = file.read()
|
| 138 |
print(file_content)
|
| 139 |
-
return jsonify({'transcription': file_content}),200
|
| 140 |
except FileNotFoundError:
|
| 141 |
return jsonify({"error": "Transcription file not found"}), 404
|
| 142 |
except Exception as e:
|
| 143 |
return jsonify({"error": f"Unexpected error: {str(e)}"}), 500
|
| 144 |
|
| 145 |
-
|
| 146 |
# AI分析エンドポイント
|
| 147 |
-
@app.route('/analyze',methods
|
| 148 |
def analyze():
|
| 149 |
global transcription_text
|
| 150 |
global total_audio
|
|
|
|
| 151 |
if not os.path.exists(transcription_text) or not transcription_text:
|
| 152 |
try:
|
| 153 |
if not total_audio:
|
| 154 |
-
return jsonify({"error": "No audio segments provided"}),400
|
| 155 |
-
audio_directory = transcripter.merge_segments(total_audio,'/tmp/data/transcription_audio')
|
| 156 |
transcription_text = transcripter.create_transcription(audio_directory)
|
| 157 |
except Exception as e:
|
| 158 |
-
return jsonify({"error": str(e)}),500
|
| 159 |
-
|
| 160 |
analyzer = TextAnalyzer(transcription_text, harassment_keywords)
|
| 161 |
api_key = os.environ.get("DEEPSEEK")
|
| 162 |
if api_key is None:
|
|
@@ -182,18 +181,115 @@ def analyze():
|
|
| 182 |
print(f"繰り返しの程度: {repetition}")
|
| 183 |
print(f"会話の心地よさ: {pleasantConversation}")
|
| 184 |
print(f"非難またはハラスメントの程度: {blameOrHarassment}")
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
# 音声アップロード&解析エンドポイント
|
| 189 |
@app.route('/upload_audio', methods=['POST'])
|
| 190 |
def upload_audio():
|
| 191 |
global total_audio
|
|
|
|
|
|
|
| 192 |
try:
|
| 193 |
data = request.get_json()
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
# Base64デコードして音声バイナリを取得
|
| 199 |
audio_binary = base64.b64decode(data['audio_data'])
|
|
@@ -204,83 +300,174 @@ def upload_audio():
|
|
| 204 |
audio_path = os.path.join(audio_dir, f"{upload_name}.wav")
|
| 205 |
with open(audio_path, 'wb') as f:
|
| 206 |
f.write(audio_binary)
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
# 各ユーザーの参照音声ファイルのパスをリストに格納
|
| 209 |
reference_paths = []
|
| 210 |
-
base_audio_dir = "/tmp/data/base_audio"
|
| 211 |
for user in users:
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
# 複数人の場合は参照パスのリストを、1人の場合は単一のパスを渡す
|
| 218 |
if len(users) > 1:
|
| 219 |
print("複数人の場合の処理")
|
| 220 |
matched_times, segments_dir = process.process_multi_audio(reference_paths, audio_path, threshold=0.05)
|
| 221 |
-
total_audio =
|
| 222 |
# 各メンバーのrateを計算
|
| 223 |
total_time = sum(matched_times)
|
| 224 |
rates = [(time / total_time) * 100 if total_time > 0 else 0 for time in matched_times]
|
| 225 |
-
|
|
|
|
|
|
|
|
|
|
| 226 |
else:
|
| 227 |
matched_time, unmatched_time, segments_dir = process.process_audio(reference_paths[0], audio_path, threshold=0.05)
|
| 228 |
-
total_audio =
|
| 229 |
-
print("
|
| 230 |
print(total_audio)
|
| 231 |
total_time = matched_time + unmatched_time
|
| 232 |
rate = (matched_time / total_time) * 100 if total_time > 0 else 0
|
| 233 |
-
return jsonify({"rate": rate}), 200
|
|
|
|
| 234 |
except Exception as e:
|
| 235 |
print("Error in /upload_audio:", str(e))
|
| 236 |
return jsonify({"error": "サーバーエラー", "details": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
@app.route('/reset', methods=['GET'])
|
| 239 |
def reset():
|
| 240 |
global users
|
| 241 |
users = []
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
try:
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
except Exception as e:
|
| 253 |
-
print(
|
| 254 |
-
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
@app.route('/upload_base_audio', methods=['POST'])
|
| 259 |
def upload_base_audio():
|
| 260 |
-
global
|
|
|
|
| 261 |
try:
|
| 262 |
data = request.get_json()
|
| 263 |
if not data or 'audio_data' not in data or 'name' not in data:
|
| 264 |
return jsonify({"error": "音声データまたは名前がありません"}), 400
|
| 265 |
-
name = data['name']
|
| 266 |
-
print(name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
|
|
|
|
|
|
|
| 273 |
|
| 274 |
-
|
| 275 |
-
base64_audio=data['audio_data'], # 音声データ
|
| 276 |
-
output_dir= "/tmp/data/base_audio", #保存先
|
| 277 |
-
output_filename=f"{name}.wav" # 固定ファイル名(必要に応じて generate_filename() で一意のファイル名に変更可能)
|
| 278 |
-
)
|
| 279 |
-
return jsonify({"state": "Registration Success!", "path": audio_path}), 200
|
| 280 |
except Exception as e:
|
| 281 |
print("Error in /upload_base_audio:", str(e))
|
| 282 |
return jsonify({"error": "サーバーエラー", "details": str(e)}), 500
|
| 283 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
if __name__ == '__main__':
|
| 285 |
port = int(os.environ.get("PORT", 7860))
|
| 286 |
app.run(debug=True, host="0.0.0.0", port=port)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, render_template, send_from_directory
|
| 2 |
import base64
|
| 3 |
+
from pydub import AudioSegment
|
| 4 |
import os
|
| 5 |
import shutil
|
| 6 |
+
import requests
|
| 7 |
+
import tempfile
|
| 8 |
+
import json
|
| 9 |
from process import AudioProcessor
|
| 10 |
from transcription import TranscriptionMaker
|
| 11 |
from analyze import TextAnalyzer
|
|
|
|
| 12 |
|
| 13 |
+
process = AudioProcessor()
|
| 14 |
transcripter = TranscriptionMaker()
|
| 15 |
app = Flask(__name__)
|
| 16 |
|
| 17 |
+
# GASのエンドポイントURL
|
| 18 |
+
GAS_URL = "https://script.google.com/macros/s/AKfycbwR2cnMKVU1AxoT9NDaeZaNUaTiwRX64Ul0sH0AU4ccP49Byph-TpxtM_Lwm4G9zLnuYA/exec"
|
| 19 |
+
|
| 20 |
+
users = [] # 選択されたユーザーのリスト
|
| 21 |
+
all_users = [] # 利用可能なすべてのユーザーのリスト
|
| 22 |
+
transcription_text = ""
|
| 23 |
harassment_keywords = [
|
| 24 |
+
"バカ", "馬鹿", "アホ", "死ね", "クソ", "うざい",
|
| 25 |
+
"きもい", "キモい", "ブス", "デブ", "ハゲ",
|
| 26 |
+
"セクハラ", "パワハラ", "モラハラ"
|
| 27 |
+
]
|
| 28 |
total_audio = ""
|
| 29 |
|
| 30 |
+
|
| 31 |
@app.route('/index', methods=['GET', 'POST'])
|
| 32 |
def index():
|
| 33 |
+
return render_template('index.html', users=users)
|
| 34 |
|
| 35 |
# フィードバック画面(テンプレート: feedback.html)
|
| 36 |
@app.route('/feedback', methods=['GET', 'POST'])
|
|
|
|
| 41 |
@app.route('/talk_detail', methods=['GET', 'POST'])
|
| 42 |
def talk_detail():
|
| 43 |
return render_template('talkDetail.html')
|
| 44 |
+
# トップページ(テンプレート: index.html)
|
|
|
|
| 45 |
@app.route('/')
|
| 46 |
+
# 音声登録画面(テンプレート: userRegister.html)
|
| 47 |
@app.route('/userregister', methods=['GET', 'POST'])
|
| 48 |
def userregister():
|
| 49 |
return render_template('userRegister.html')
|
| 50 |
|
| 51 |
+
# 人数確認
|
| 52 |
+
@app.route('/confirm', methods=['GET'])
|
| 53 |
def confirm():
|
| 54 |
+
global all_users
|
| 55 |
+
# 最新のユーザーリストを取得
|
| 56 |
+
try:
|
| 57 |
+
update_all_users()
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"ユーザーリストの更新エラー: {str(e)}")
|
| 60 |
+
return jsonify({'members': users, 'all_members': all_users}), 200
|
| 61 |
|
| 62 |
+
# リセット画面(テンプレート: reset.html)
|
| 63 |
@app.route('/reset_html', methods=['GET', 'POST'])
|
| 64 |
def reset_html():
|
| 65 |
return render_template('reset.html')
|
| 66 |
|
| 67 |
+
# メンバー削除&累積音声削除
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
@app.route('/reset_member', methods=['GET', 'POST'])
|
| 69 |
def reset_member():
|
| 70 |
global users
|
| 71 |
global total_audio
|
| 72 |
global transcription_text
|
| 73 |
+
|
| 74 |
+
# 一時ディレクトリのクリーンアップ
|
| 75 |
+
if total_audio:
|
| 76 |
+
process.delete_files_in_directory(total_audio)
|
| 77 |
process.delete_files_in_directory('/tmp/data/transcription_audio')
|
| 78 |
+
|
| 79 |
+
# 書き起こしテキストの削除
|
| 80 |
+
if os.path.exists(transcription_text):
|
| 81 |
+
try:
|
| 82 |
os.remove(transcription_text)
|
| 83 |
print(f"{transcription_text} を削除しました。")
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"ファイル削除中にエラーが発生しました: {e}")
|
| 86 |
+
|
|
|
|
| 87 |
transcription_text = ""
|
| 88 |
+
|
| 89 |
try:
|
| 90 |
data = request.get_json()
|
| 91 |
if not data or "names" not in data:
|
| 92 |
+
return jsonify({"status": "error", "message": "Invalid request body"}), 400
|
| 93 |
|
| 94 |
names = data.get("names", [])
|
| 95 |
+
|
| 96 |
+
# GASからファイルを削除
|
| 97 |
for name in names:
|
| 98 |
+
try:
|
| 99 |
+
delete_from_cloud(f"{name}.wav")
|
| 100 |
+
print(f"クラウドから {name}.wav を削除しました。")
|
| 101 |
+
except Exception as e:
|
| 102 |
+
print(f"クラウド削除中にエラーが発生しました: {e}")
|
| 103 |
+
return jsonify({"status": "error", "message": f"Failed to delete {name} from cloud: {e}"}), 500
|
| 104 |
+
|
| 105 |
+
# usersリストから削除するユーザーを除外
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
users = [u for u in users if u not in names]
|
| 107 |
+
|
| 108 |
+
# 全ユーザーリストの更新
|
| 109 |
+
update_all_users()
|
| 110 |
|
| 111 |
+
return jsonify({"status": "success", "message": "Members deleted successfully", "users": users}), 200
|
|
|
|
| 112 |
|
| 113 |
except Exception as e:
|
| 114 |
print(f"An unexpected error occurred: {e}")
|
| 115 |
+
return jsonify({"status": "error", "message": f"Internal server error: {e}"}), 500
|
| 116 |
|
| 117 |
# 書き起こし作成エンドポイント
|
| 118 |
+
@app.route('/transcription', methods=['GET', 'POST'])
|
| 119 |
def transcription():
|
| 120 |
global transcription_text
|
| 121 |
global total_audio
|
| 122 |
+
|
| 123 |
if not os.path.exists(transcription_text) or not transcription_text:
|
| 124 |
try:
|
| 125 |
if not total_audio or not os.path.exists(total_audio):
|
| 126 |
+
return jsonify({"error": "No audio segments provided"}), 400
|
| 127 |
+
audio_directory = transcripter.merge_segments(total_audio, '/tmp/data/transcription_audio')
|
| 128 |
transcription_text = transcripter.create_transcription(audio_directory)
|
| 129 |
print("transcription")
|
| 130 |
print(transcription_text)
|
| 131 |
except Exception as e:
|
| 132 |
+
return jsonify({"error": str(e)}), 500
|
| 133 |
+
|
| 134 |
try:
|
| 135 |
+
with open(transcription_text, 'r', encoding='utf-8') as file:
|
| 136 |
file_content = file.read()
|
| 137 |
print(file_content)
|
| 138 |
+
return jsonify({'transcription': file_content}), 200
|
| 139 |
except FileNotFoundError:
|
| 140 |
return jsonify({"error": "Transcription file not found"}), 404
|
| 141 |
except Exception as e:
|
| 142 |
return jsonify({"error": f"Unexpected error: {str(e)}"}), 500
|
| 143 |
|
|
|
|
| 144 |
# AI分析エンドポイント
|
| 145 |
+
@app.route('/analyze', methods=['GET', 'POST'])
|
| 146 |
def analyze():
|
| 147 |
global transcription_text
|
| 148 |
global total_audio
|
| 149 |
+
|
| 150 |
if not os.path.exists(transcription_text) or not transcription_text:
|
| 151 |
try:
|
| 152 |
if not total_audio:
|
| 153 |
+
return jsonify({"error": "No audio segments provided"}), 400
|
| 154 |
+
audio_directory = transcripter.merge_segments(total_audio, '/tmp/data/transcription_audio')
|
| 155 |
transcription_text = transcripter.create_transcription(audio_directory)
|
| 156 |
except Exception as e:
|
| 157 |
+
return jsonify({"error": str(e)}), 500
|
| 158 |
+
|
| 159 |
analyzer = TextAnalyzer(transcription_text, harassment_keywords)
|
| 160 |
api_key = os.environ.get("DEEPSEEK")
|
| 161 |
if api_key is None:
|
|
|
|
| 181 |
print(f"繰り返しの程度: {repetition}")
|
| 182 |
print(f"会話の心地よさ: {pleasantConversation}")
|
| 183 |
print(f"非難またはハラスメントの程度: {blameOrHarassment}")
|
| 184 |
+
|
| 185 |
+
return jsonify({"results": results}), 200
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
# クラウドから音声を取得してローカ��に保存する関数
|
| 189 |
+
def download_from_cloud(filename, local_path):
|
| 190 |
+
try:
|
| 191 |
+
payload = {
|
| 192 |
+
"action": "download",
|
| 193 |
+
"fileName": filename
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
print(f"クラウドから {filename} をダウンロード中...")
|
| 197 |
+
response = requests.post(GAS_URL, json=payload)
|
| 198 |
+
if response.status_code != 200:
|
| 199 |
+
print(f"ダウンロードエラー: ステータスコード {response.status_code}")
|
| 200 |
+
print(f"レスポンス: {response.text}")
|
| 201 |
+
raise Exception(f"クラウドからのダウンロードに失敗しました: {response.text}")
|
| 202 |
+
|
| 203 |
+
try:
|
| 204 |
+
res_json = response.json()
|
| 205 |
+
except:
|
| 206 |
+
print("JSONデコードエラー、レスポンス内容:")
|
| 207 |
+
print(response.text[:500]) # 最初の500文字だけ表示
|
| 208 |
+
raise Exception("サーバーからの応答をJSONとして解析できませんでした")
|
| 209 |
+
|
| 210 |
+
if res_json.get("status") != "success":
|
| 211 |
+
print(f"ダウンロードステータスエラー: {res_json.get('message')}")
|
| 212 |
+
raise Exception(f"クラウドからのダウンロードに失敗しました: {res_json.get('message')}")
|
| 213 |
+
|
| 214 |
+
# Base64文字列をデコード
|
| 215 |
+
base64_data = res_json.get("base64Data")
|
| 216 |
+
if not base64_data:
|
| 217 |
+
print("Base64データが存在しません")
|
| 218 |
+
raise Exception("応答にBase64データが含まれていません")
|
| 219 |
+
|
| 220 |
+
try:
|
| 221 |
+
audio_binary = base64.b64decode(base64_data)
|
| 222 |
+
except Exception as e:
|
| 223 |
+
print(f"Base64デコードエラー: {str(e)}")
|
| 224 |
+
raise Exception(f"音声データのデコードに失敗しました: {str(e)}")
|
| 225 |
+
|
| 226 |
+
# 指定パスに保存
|
| 227 |
+
os.makedirs(os.path.dirname(local_path), exist_ok=True)
|
| 228 |
+
with open(local_path, 'wb') as f:
|
| 229 |
+
f.write(audio_binary)
|
| 230 |
+
|
| 231 |
+
print(f"{filename} をローカルに保存しました: {local_path}")
|
| 232 |
+
|
| 233 |
+
# データの整合性チェック(ファイルサイズが0より大きいかなど)
|
| 234 |
+
if os.path.getsize(local_path) <= 0:
|
| 235 |
+
raise Exception(f"保存されたファイル {local_path} のサイズが0バイトです")
|
| 236 |
+
|
| 237 |
+
return local_path
|
| 238 |
+
except Exception as e:
|
| 239 |
+
print(f"ダウンロード中にエラーが発生しました: {str(e)}")
|
| 240 |
+
# エラーを上位に伝播させる
|
| 241 |
+
raise
|
| 242 |
|
| 243 |
+
# クラウドからファイルを削除する関数
|
| 244 |
+
def delete_from_cloud(filename):
|
| 245 |
+
payload = {
|
| 246 |
+
"action": "delete",
|
| 247 |
+
"fileName": filename
|
| 248 |
+
}
|
| 249 |
+
response = requests.post(GAS_URL, json=payload)
|
| 250 |
+
if response.status_code != 200:
|
| 251 |
+
raise Exception(f"クラウドからの削除に失敗しました: {response.text}")
|
| 252 |
+
|
| 253 |
+
res_json = response.json()
|
| 254 |
+
if res_json.get("status") != "success":
|
| 255 |
+
raise Exception(f"クラウドからの削除に失敗しました: {res_json.get('message')}")
|
| 256 |
+
|
| 257 |
+
return True
|
| 258 |
+
# すべてのベース音声ユーザーリストを更新する関数
|
| 259 |
+
def update_all_users():
|
| 260 |
+
global all_users
|
| 261 |
+
|
| 262 |
+
payload = {"action": "list"}
|
| 263 |
+
response = requests.post(GAS_URL, json=payload)
|
| 264 |
+
if response.status_code != 200:
|
| 265 |
+
raise Exception(f"GAS一覧取得エラー: {response.text}")
|
| 266 |
+
|
| 267 |
+
res_json = response.json()
|
| 268 |
+
if res_json.get("status") != "success":
|
| 269 |
+
raise Exception(f"GAS一覧取得失敗: {res_json.get('message')}")
|
| 270 |
+
|
| 271 |
+
# ファイル名から拡張子を除去してユーザーリストを作成
|
| 272 |
+
all_users = [os.path.splitext(filename)[0] for filename in res_json.get("fileNames", [])]
|
| 273 |
+
return all_users
|
| 274 |
|
| 275 |
# 音声アップロード&解析エンドポイント
|
| 276 |
@app.route('/upload_audio', methods=['POST'])
|
| 277 |
def upload_audio():
|
| 278 |
global total_audio
|
| 279 |
+
global users
|
| 280 |
+
|
| 281 |
try:
|
| 282 |
data = request.get_json()
|
| 283 |
+
if not data or 'audio_data' not in data:
|
| 284 |
+
return jsonify({"error": "音声データがありません"}), 400
|
| 285 |
+
|
| 286 |
+
# リクエストからユーザーリストを取得(指定がなければ現在のusersを使用)
|
| 287 |
+
if 'selected_users' in data and data['selected_users']:
|
| 288 |
+
users = data['selected_users']
|
| 289 |
+
print(f"選択されたユーザー: {users}")
|
| 290 |
+
|
| 291 |
+
if not users:
|
| 292 |
+
return jsonify({"error": "選択されたユーザーがいません"}), 400
|
| 293 |
|
| 294 |
# Base64デコードして音声バイナリを取得
|
| 295 |
audio_binary = base64.b64decode(data['audio_data'])
|
|
|
|
| 300 |
audio_path = os.path.join(audio_dir, f"{upload_name}.wav")
|
| 301 |
with open(audio_path, 'wb') as f:
|
| 302 |
f.write(audio_binary)
|
| 303 |
+
|
| 304 |
+
print(f"処理を行うユーザー: {users}")
|
| 305 |
+
|
| 306 |
+
# ベース音声を一時ディレクトリにダウンロード
|
| 307 |
+
temp_dir = "/tmp/data/base_audio"
|
| 308 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 309 |
+
|
| 310 |
# 各ユーザーの参照音声ファイルのパスをリストに格納
|
| 311 |
reference_paths = []
|
|
|
|
| 312 |
for user in users:
|
| 313 |
+
try:
|
| 314 |
+
ref_path = os.path.join(temp_dir, f"{user}.wav")
|
| 315 |
+
if not os.path.exists(ref_path):
|
| 316 |
+
# クラウドから取得
|
| 317 |
+
download_from_cloud(f"{user}.wav", ref_path)
|
| 318 |
+
print(f"クラウドから {user}.wav をダウンロードしました")
|
| 319 |
+
|
| 320 |
+
if not os.path.exists(ref_path):
|
| 321 |
+
return jsonify({"error": "参照音声ファイルが見つかりません", "details": ref_path}), 500
|
| 322 |
+
|
| 323 |
+
reference_paths.append(ref_path)
|
| 324 |
+
except Exception as e:
|
| 325 |
+
return jsonify({"error": f"ユーザー {user} の音声取得に失敗しました", "details": str(e)}), 500
|
| 326 |
|
| 327 |
# 複数人の場合は参照パスのリストを、1人の場合は単一のパスを渡す
|
| 328 |
if len(users) > 1:
|
| 329 |
print("複数人の場合の処理")
|
| 330 |
matched_times, segments_dir = process.process_multi_audio(reference_paths, audio_path, threshold=0.05)
|
| 331 |
+
total_audio = segments_dir
|
| 332 |
# 各メンバーのrateを計算
|
| 333 |
total_time = sum(matched_times)
|
| 334 |
rates = [(time / total_time) * 100 if total_time > 0 else 0 for time in matched_times]
|
| 335 |
+
|
| 336 |
+
# ユーザー名と話した割合をマッピング
|
| 337 |
+
user_rates = {users[i]: rates[i] for i in range(len(users))}
|
| 338 |
+
return jsonify({"rates": rates, "user_rates": user_rates}), 200
|
| 339 |
else:
|
| 340 |
matched_time, unmatched_time, segments_dir = process.process_audio(reference_paths[0], audio_path, threshold=0.05)
|
| 341 |
+
total_audio = segments_dir
|
| 342 |
+
print("単一ユーザーの処理")
|
| 343 |
print(total_audio)
|
| 344 |
total_time = matched_time + unmatched_time
|
| 345 |
rate = (matched_time / total_time) * 100 if total_time > 0 else 0
|
| 346 |
+
return jsonify({"rate": rate, "user": users[0]}), 200
|
| 347 |
+
|
| 348 |
except Exception as e:
|
| 349 |
print("Error in /upload_audio:", str(e))
|
| 350 |
return jsonify({"error": "サーバーエラー", "details": str(e)}), 500
|
| 351 |
+
# ユーザー選択画面(テンプレート: userSelect.html)
|
| 352 |
+
@app.route('/userselect', methods=['GET'])
|
| 353 |
+
def userselect():
|
| 354 |
+
return render_template('userSelect.html')
|
| 355 |
+
# 選択したユーザーを設定するエンドポイント
|
| 356 |
+
@app.route('/select_users', methods=['POST'])
|
| 357 |
+
def select_users():
|
| 358 |
+
global users
|
| 359 |
|
| 360 |
+
try:
|
| 361 |
+
data = request.get_json()
|
| 362 |
+
if not data or 'users' not in data:
|
| 363 |
+
return jsonify({"error": "ユーザーリストがありません"}), 400
|
| 364 |
+
|
| 365 |
+
users = data['users']
|
| 366 |
+
print(f"選択されたユーザー: {users}")
|
| 367 |
+
|
| 368 |
+
return jsonify({"status": "success", "selected_users": users}), 200
|
| 369 |
+
except Exception as e:
|
| 370 |
+
print("Error in /select_users:", str(e))
|
| 371 |
+
return jsonify({"error": "サーバーエラー", "details": str(e)}), 500
|
| 372 |
+
|
| 373 |
@app.route('/reset', methods=['GET'])
|
| 374 |
def reset():
|
| 375 |
global users
|
| 376 |
users = []
|
| 377 |
+
return jsonify({"status": "success", "message": "Users reset"}), 200
|
| 378 |
+
|
| 379 |
+
@app.route('/copy_selected_files', methods=['POST'])
|
| 380 |
+
def copy_selected_files():
|
| 381 |
try:
|
| 382 |
+
data = request.get_json()
|
| 383 |
+
if not data or "names" not in data:
|
| 384 |
+
return jsonify({"error": "namesパラメータが存在しません"}), 400
|
| 385 |
+
|
| 386 |
+
names = data["names"]
|
| 387 |
+
dest_dir = "/tmp/data/selected_audio" # コピー先のフォルダ
|
| 388 |
+
os.makedirs(dest_dir, exist_ok=True)
|
| 389 |
+
|
| 390 |
+
copied_files = []
|
| 391 |
+
for name in names:
|
| 392 |
+
dest_path = os.path.join(dest_dir, f"{name}.wav")
|
| 393 |
+
try:
|
| 394 |
+
# クラウドから直接ダウンロード
|
| 395 |
+
download_from_cloud(f"{name}.wav", dest_path)
|
| 396 |
+
copied_files.append(name)
|
| 397 |
+
print(f"{name}.wav を {dest_path} にダウンロードしました。")
|
| 398 |
+
except Exception as e:
|
| 399 |
+
print(f"ダウンロード中にエラーが発生しました: {e}")
|
| 400 |
+
continue
|
| 401 |
+
|
| 402 |
+
return jsonify({"status": "success", "copied": copied_files}), 200
|
| 403 |
+
|
| 404 |
except Exception as e:
|
| 405 |
+
print("Error in /copy_selected_files:", str(e))
|
| 406 |
+
return jsonify({"error": "サーバー内部エラー", "details": str(e)}), 500
|
| 407 |
+
|
| 408 |
+
@app.route('/clear_tmp', methods=['GET'])
|
| 409 |
+
def clear_tmp():
|
| 410 |
+
try:
|
| 411 |
+
tmp_dir = "/tmp/data" # アプリケーションが使用しているtmpフォルダ
|
| 412 |
+
# ファイルのみの削除
|
| 413 |
+
process.delete_files_in_directory(tmp_dir)
|
| 414 |
+
# フォルダがあれば再帰的に削除
|
| 415 |
+
for item in os.listdir(tmp_dir):
|
| 416 |
+
item_path = os.path.join(tmp_dir, item)
|
| 417 |
+
if os.path.isdir(item_path):
|
| 418 |
+
shutil.rmtree(item_path)
|
| 419 |
+
print(f"ディレクトリを削除しました: {item_path}")
|
| 420 |
+
|
| 421 |
+
return jsonify({"status": "success", "message": "tmp配下がすべて削除されました"}), 200
|
| 422 |
|
| 423 |
+
except Exception as e:
|
| 424 |
+
print("Error in /clear_tmp:", str(e))
|
| 425 |
+
return jsonify({"error": "サーバー内部エラー", "details": str(e)}), 500
|
| 426 |
|
| 427 |
@app.route('/upload_base_audio', methods=['POST'])
|
| 428 |
def upload_base_audio():
|
| 429 |
+
global all_users
|
| 430 |
+
|
| 431 |
try:
|
| 432 |
data = request.get_json()
|
| 433 |
if not data or 'audio_data' not in data or 'name' not in data:
|
| 434 |
return jsonify({"error": "音声データまたは名前がありません"}), 400
|
| 435 |
+
name = data['name']
|
| 436 |
+
print(f"登録名: {name}")
|
| 437 |
+
|
| 438 |
+
# GASのアップロードエンドポイントにリクエスト
|
| 439 |
+
payload = {
|
| 440 |
+
"action": "upload",
|
| 441 |
+
"fileName": f"{name}.wav",
|
| 442 |
+
"base64Data": data['audio_data']
|
| 443 |
+
}
|
| 444 |
|
| 445 |
+
response = requests.post(GAS_URL, json=payload)
|
| 446 |
+
if response.status_code != 200:
|
| 447 |
+
return jsonify({"error": "GASアップロードエラー", "details": response.text}), 500
|
| 448 |
|
| 449 |
+
res_json = response.json()
|
| 450 |
+
if res_json.get("status") != "success":
|
| 451 |
+
return jsonify({"error": "GASアップロード失敗", "details": res_json.get("message")}), 500
|
| 452 |
|
| 453 |
+
# 全ユーザーリストを更新
|
| 454 |
+
update_all_users()
|
| 455 |
|
| 456 |
+
return jsonify({"state": "Registration Success!", "driveFileId": res_json.get("fileId")}), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
except Exception as e:
|
| 458 |
print("Error in /upload_base_audio:", str(e))
|
| 459 |
return jsonify({"error": "サーバーエラー", "details": str(e)}), 500
|
| 460 |
|
| 461 |
+
@app.route('/list_base_audio', methods=['GET'])
|
| 462 |
+
def list_base_audio():
|
| 463 |
+
try:
|
| 464 |
+
global all_users
|
| 465 |
+
all_users = update_all_users()
|
| 466 |
+
return jsonify({"status": "success", "fileNames": all_users}), 200
|
| 467 |
+
except Exception as e:
|
| 468 |
+
print("Error in /list_base_audio:", str(e))
|
| 469 |
+
return jsonify({"error": "サーバーエラー", "details": str(e)}), 500
|
| 470 |
+
|
| 471 |
if __name__ == '__main__':
|
| 472 |
port = int(os.environ.get("PORT", 7860))
|
| 473 |
app.run(debug=True, host="0.0.0.0", port=port)
|
transcription.py
CHANGED
|
@@ -1,90 +1,109 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from faster_whisper import WhisperModel
|
| 3 |
-
from pydub import AudioSegment
|
| 4 |
-
import string
|
| 5 |
-
import random
|
| 6 |
-
from datetime import datetime
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
os.environ["
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
self.
|
| 20 |
-
self.output_dir =
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
#音声ファイルのディレクトリを受け取り、書き起こしファイルを作成する
|
| 24 |
-
def create_transcription(self,audio_directory):
|
| 25 |
-
results = []
|
| 26 |
-
|
| 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 |
-
audio_files = sorted(os.listdir(audio_directory))
|
| 32 |
-
for audio_file in audio_files:
|
| 33 |
-
if os.path.splitext(audio_file)[-1].lower() != '.wav':
|
| 34 |
-
continue
|
| 35 |
-
audio_path = os.path.join(audio_directory, audio_file)
|
| 36 |
-
try:
|
| 37 |
-
segments,info = list(self.model.transcribe(audio_path))
|
| 38 |
-
except Exception as e:
|
| 39 |
-
print(f"Error transcripting file {audio_path}: {e}")
|
| 40 |
-
raise
|
| 41 |
-
sorted_segments = sorted(segments, key=lambda s: s.start)
|
| 42 |
-
for segment in sorted_segments:
|
| 43 |
-
results.append({
|
| 44 |
-
"start": segment.start,
|
| 45 |
-
"end": segment.end,
|
| 46 |
-
"text": segment.text
|
| 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 |
return filename
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from faster_whisper import WhisperModel
|
| 3 |
+
from pydub import AudioSegment
|
| 4 |
+
import string
|
| 5 |
+
import random
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
# Matplotlibのキャッシュディレクトリを変更
|
| 9 |
+
os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
|
| 10 |
+
|
| 11 |
+
# Hugging Faceのキャッシュディレクトリを変更
|
| 12 |
+
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 13 |
+
os.environ["HUGGINGFACE_HUB_CACHE"] = "/tmp/huggingface"
|
| 14 |
+
|
| 15 |
+
class TranscriptionMaker():
|
| 16 |
+
# 書き起こしファイルを吐き出すディレクトリを指定
|
| 17 |
+
def __init__(self, output_dir="/tmp/data/transcriptions"):
|
| 18 |
+
self.model = WhisperModel("base", device="cpu", download_root="/tmp/huggingface")
|
| 19 |
+
self.output_dir = output_dir
|
| 20 |
+
os.makedirs(self.output_dir, exist_ok=True)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
#音声ファイルのディレクトリを受け取り、書き起こしファイルを作成する
|
| 24 |
+
def create_transcription(self,audio_directory):
|
| 25 |
+
results = []
|
| 26 |
+
|
| 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 |
+
audio_files = sorted(os.listdir(audio_directory))
|
| 32 |
+
for audio_file in audio_files:
|
| 33 |
+
if os.path.splitext(audio_file)[-1].lower() != '.wav':
|
| 34 |
+
continue
|
| 35 |
+
audio_path = os.path.join(audio_directory, audio_file)
|
| 36 |
+
try:
|
| 37 |
+
segments,info = list(self.model.transcribe(audio_path))
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Error transcripting file {audio_path}: {e}")
|
| 40 |
+
raise
|
| 41 |
+
sorted_segments = sorted(segments, key=lambda s: s.start)
|
| 42 |
+
for segment in sorted_segments:
|
| 43 |
+
results.append({
|
| 44 |
+
"start": segment.start,
|
| 45 |
+
"end": segment.end,
|
| 46 |
+
"text": segment.text
|
| 47 |
+
})
|
| 48 |
+
#ファイルの書き込み。ファイル名は"transcription.txt"
|
| 49 |
+
output_file=os.path.join(self.output_dir,"transcription.txt")
|
| 50 |
+
try:
|
| 51 |
+
with open(output_file,"w",encoding="utf-8") as f:
|
| 52 |
+
for result in results:
|
| 53 |
+
f.write(f"{result['text']}\n")
|
| 54 |
+
except OSError as e:
|
| 55 |
+
print(f"Error writing transcription file: {e}")
|
| 56 |
+
raise
|
| 57 |
+
return output_file
|
| 58 |
+
|
| 59 |
+
#ファイル名が連続しているならくっつける
|
| 60 |
+
def merge_segments(self,segments_dir,output_dir = "/tmp/data/merged_segment"):
|
| 61 |
+
if not os.path.exists(output_dir):
|
| 62 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 63 |
+
|
| 64 |
+
files = sorted([f for f in os.listdir(segments_dir) if f.endswith('.wav')])
|
| 65 |
+
|
| 66 |
+
merged_files = []
|
| 67 |
+
current_group = []
|
| 68 |
+
previous_index = None
|
| 69 |
+
|
| 70 |
+
for file in files:
|
| 71 |
+
# ファイル名から番号を抽出(例: "0.wav" -> 0)
|
| 72 |
+
file_index = int(file.split('.')[0])
|
| 73 |
+
|
| 74 |
+
# 番号が連続していない場合、新しいグループを作成
|
| 75 |
+
if previous_index is not None and file_index != previous_index + 1:
|
| 76 |
+
# 現在のグループを結合して保存
|
| 77 |
+
if current_group:
|
| 78 |
+
merged_files.append(current_group)
|
| 79 |
+
current_group = []
|
| 80 |
+
|
| 81 |
+
# 現在のファイルをグループに追加
|
| 82 |
+
current_group.append(file)
|
| 83 |
+
previous_index = file_index
|
| 84 |
+
|
| 85 |
+
# 最後のグループを追加
|
| 86 |
+
if current_group:
|
| 87 |
+
merged_files.append(current_group)
|
| 88 |
+
|
| 89 |
+
# グループごとに結合して保存
|
| 90 |
+
for i, group in enumerate(merged_files):
|
| 91 |
+
combined_audio = AudioSegment.empty()
|
| 92 |
+
for file in group:
|
| 93 |
+
file_path = os.path.join(segments_dir, file)
|
| 94 |
+
segment = AudioSegment.from_file(file_path)
|
| 95 |
+
combined_audio += segment
|
| 96 |
+
# 出力ファイル名を設定して保存
|
| 97 |
+
output_file = os.path.join(output_dir, self.generate_filename(3))
|
| 98 |
+
combined_audio.export(output_file, format='wav')
|
| 99 |
+
|
| 100 |
+
return output_dir
|
| 101 |
+
|
| 102 |
+
def generate_random_string(self,length):
|
| 103 |
+
letters = string.ascii_letters + string.digits
|
| 104 |
+
return ''.join(random.choice(letters) for i in range(length))
|
| 105 |
+
|
| 106 |
+
def generate_filename(self,random_length):
|
| 107 |
+
current_time = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 108 |
+
filename = f"{current_time}.wav"
|
| 109 |
return filename
|