kazuhina commited on
Commit
ca70004
·
1 Parent(s): 6a984a1

Add requirements.txt and API manual for Anime TTS

Browse files
Files changed (2) hide show
  1. API_MANUAL.md +616 -0
  2. requirements.txt +24 -0
API_MANUAL.md ADDED
@@ -0,0 +1,616 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Anime TTS API 使用マニュアル
2
+
3
+ ## 📋 目次
4
+ 1. [概要](#概要)
5
+ 2. [機能](#機能)
6
+ 3. [インストール](#インストール)
7
+ 4. [使用方法](#使用方法)
8
+ 5. [APIリファレンス](#apiリファレンス)
9
+ 6. [対応フォーマット](#対応フォーマット)
10
+ 7. [エラーハンドリング](#エラーハンドリング)
11
+ 8. [パフォーマンス最適化](#パフォーマンス最適化)
12
+ 9. [トラブルシューティング](#トラブルシューティング)
13
+ 10. [使用例](#使用例)
14
+
15
+ ---
16
+
17
+ ## 🎯 概要
18
+
19
+ **Anime TTS** は、日本語アニメ音声をテキストに変換する高性能な音声認識APIです。`litagin/anime-whisper`モデルを使用し、アニメ・ビジュアルノベル向けの特別な調整により、従来の音声認識システムよりも高い精度を実現しています。
20
+
21
+ ### 主な特徴
22
+ - 🗾 **日本語特化**: 日本語アニメ音声に最適化
23
+ - 🎭 **感情表現対応**: 感情的な表現や非音声Soundsを適切に処理
24
+ - 🎯 **高精度認識**: アニメダイアログ向けの特別な調整
25
+ - 📝 **自然な句読点**: 日本語の自然な句読点を自動付与
26
+ - 🚀 **GPU対応**: CUDA対応による高速処理
27
+
28
+ ---
29
+
30
+ ## ✨ 機能
31
+
32
+ ### コア機能
33
+ - **音声テキスト変換**: 日本語アニメ音声をテキストに変換
34
+ - **リアルタイム処理**: ストリーミング音声対応
35
+ - **バッチ処理**: 複数ファイルの同時処理
36
+ - **エラーハンドリング**: 堅牢なエラー処理とフォールバック
37
+
38
+ ### 技術仕様
39
+ - **モデル**: `litagin/anime-whisper`
40
+ - **フレームワーク**: Gradio 5.20.0
41
+ - **GPU対応**: CUDA/ROCm対応
42
+ - **メモリ最適化**: チャンク分割処理
43
+
44
+ ---
45
+
46
+ ## 📦 インストール
47
+
48
+ ### 必要な環境
49
+ - Python 3.8+
50
+ - CUDA対応GPU(推奨)
51
+ - 8GB以上のRAM(推奨)
52
+
53
+ ### 依存関係のインストール
54
+
55
+ ```bash
56
+ # 基本的な依存関係
57
+ pip install gradio torch transformers spaces
58
+
59
+ # 音声処理ライブラリ
60
+ pip install soundfile numpy
61
+
62
+ # GPU使用の場合(CUDA)
63
+ pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
64
+
65
+ # オプション: Hugging Face Hub高速化
66
+ pip install huggingface_hub[hf_xet]
67
+ ```
68
+
69
+ ### 環境変数設定
70
+ ```bash
71
+ # GPU使用の場合
72
+ export CUDA_VISIBLE_DEVICES=0
73
+
74
+ # メモリ制限設定(必要に応じて)
75
+ export PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:512
76
+ ```
77
+
78
+ ---
79
+
80
+ ## 🚀 使用方法
81
+
82
+ ### 基本的な使用方法
83
+
84
+ #### 1. ローカル実行
85
+ ```bash
86
+ python app.py
87
+ ```
88
+ - ブラウザで `http://localhost:7860` にアクセス
89
+ - 音声ファイルをアップロードしてテキスト変換
90
+
91
+ #### 2. プログラムからの呼び出し
92
+ ```python
93
+ from app import transcribe_audio
94
+
95
+ # 音声ファイルのパス
96
+ result = transcribe_audio("path/to/audio.wav")
97
+ print(result)
98
+ ```
99
+
100
+ #### 3. Gradioクライアントとして使用
101
+ ```python
102
+ import gradio as gr
103
+
104
+ # 既存のインターフェースを使用
105
+ demo = gr.load("huggingface.co/spaces/kazuhina/anime-tts")
106
+
107
+ # 音声ファイルで実行
108
+ result = demo("path/to/audio.wav")
109
+ ```
110
+
111
+ ---
112
+
113
+ ## 📚 APIリファレンス
114
+
115
+ ### `transcribe_audio(audio_file)`
116
+
117
+ #### パラメータ
118
+ | パラメータ | 型 | 必須 | 説明 |
119
+ |------------|-----|------|------|
120
+ | `audio_file` | str/Path/File | ✅ | 音声ファイルのパスまたはGradioファイルオブジェクト |
121
+
122
+ #### 戻り値
123
+ ```python
124
+ str # 変換された日本語テキスト
125
+ ```
126
+
127
+ #### 使用例
128
+ ```python
129
+ # ファイルパスでの使用
130
+ result = transcribe_audio("anime_dialogue.wav")
131
+
132
+ # Gradioファイルオブジェクトでの使用
133
+ import gradio as gr
134
+ file_obj = gr.upload_file("audio.mp3")
135
+ result = transcribe_audio(file_obj)
136
+ ```
137
+
138
+ ### `create_demo()`
139
+
140
+ #### 説明
141
+ デモ用のテスト音声ファイルを作成します。
142
+
143
+ #### 戻り値
144
+ ```python
145
+ str # 作成されたデモ音声ファイルのパス
146
+ ```
147
+
148
+ #### 使用例
149
+ ```python
150
+ from app import create_demo
151
+
152
+ demo_file = create_demo()
153
+ result = transcribe_audio(demo_file)
154
+ ```
155
+
156
+ ---
157
+
158
+ ## 📁 対応フォーマット
159
+
160
+ ### 入力フォーマット
161
+ | フォーマット | 拡張子 | 備考 |
162
+ |-------------|--------|------|
163
+ | **WAV** | `.wav` | 推奨、最高品質 |
164
+ | **MP3** | `.mp3` | 圧縮率高、処理時間増加 |
165
+ | **M4A** | `.m4a` | Appleフォーマット対応 |
166
+ | **FLAC** | `.flac` | 無損圧縮、品質保持 |
167
+
168
+ ### 推奨設定
169
+ - **サンプルレート**: 16kHz以上
170
+ - **ビット深度**: 16bit以上
171
+ - **チャンネル**: モノラルまたはステレオ
172
+ - **ファイルサイズ**: 100MB以下(推奨)
173
+
174
+ ### 音声品質要件
175
+ ```python
176
+ # 最適な設定例
177
+ sample_rate = 16000 # 16kHz
178
+ channels = 1 # モノラル
179
+ bit_depth = 16 # 16bit
180
+ format = "wav" # WAV形式
181
+ ```
182
+
183
+ ---
184
+
185
+ ## ⚠️ エラーハンドリング
186
+
187
+ ### 一般的なエラーと対応
188
+
189
+ #### 1. モデル読み込みエラー
190
+ ```python
191
+ # エラー例
192
+ "Error loading model: Connection timeout"
193
+
194
+ # 対応方法
195
+ # 1. インターネット接続確認
196
+ # 2. モデルキャッシュクリア
197
+ # 3. 再起動
198
+ ```
199
+
200
+ #### 2. 音声ファイルエラー
201
+ ```python
202
+ # エラー例
203
+ "Audio file not found."
204
+
205
+ # 対応方法
206
+ # 1. ファイルパス確認
207
+ # 2. ファイル存在確認
208
+ # 3. 権限確認
209
+ ```
210
+
211
+ #### 3. フォーマットエラー
212
+ ```python
213
+ # エラー例
214
+ "Invalid audio file format."
215
+
216
+ # 対応方法
217
+ # 1. サポートフォーマット確認
218
+ # 2. ファイル破損確認
219
+ # 3. フォーマット変換
220
+ ```
221
+
222
+ ### エラーハンドリングコード例
223
+ ```python
224
+ try:
225
+ result = transcribe_audio(audio_file)
226
+ print(f"変換成功: {result}")
227
+ except FileNotFoundError:
228
+ print("音声ファイルが見つかりません")
229
+ except ValueError as e:
230
+ print(f"無効なファイル形式: {e}")
231
+ except Exception as e:
232
+ print(f"予期しないエラー: {e}")
233
+ ```
234
+
235
+ ---
236
+
237
+ ## ⚡ パフォーマンス最適化
238
+
239
+ ### GPU使用の最適化
240
+ ```python
241
+ # CUDA使用確認
242
+ import torch
243
+ if torch.cuda.is_available():
244
+ print(f"GPU使用: {torch.cuda.get_device_name()}")
245
+ else:
246
+ print("CPU使用")
247
+ ```
248
+
249
+ ### メモリ管理
250
+ ```python
251
+ # チャンクサイズ調整
252
+ chunk_length_s = 30.0 # 30秒ずつ処理
253
+ batch_size = 64 if torch.cuda.is_available() else 8
254
+ ```
255
+
256
+ ### バッチ処理
257
+ ```python
258
+ import glob
259
+ import os
260
+
261
+ def batch_transcribe(audio_dir):
262
+ """複数ファイルのバッチ処理"""
263
+ audio_files = glob.glob(os.path.join(audio_dir, "*.wav"))
264
+ results = []
265
+
266
+ for audio_file in audio_files:
267
+ try:
268
+ result = transcribe_audio(audio_file)
269
+ results.append((audio_file, result))
270
+ except Exception as e:
271
+ print(f"エラー {audio_file}: {e}")
272
+
273
+ return results
274
+ ```
275
+
276
+ ### パフォーマンス設定
277
+ ```python
278
+ # 最適化設定
279
+ generate_kwargs = {
280
+ "language": "Japanese",
281
+ "no_repeat_ngram_size": 0,
282
+ "repetition_penalty": 1.0,
283
+ }
284
+
285
+ # パイプライン設定
286
+ pipe = pipeline(
287
+ "automatic-speech-recognition",
288
+ model="litagin/anime-whisper",
289
+ device="cuda" if torch.cuda.is_available() else "cpu",
290
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
291
+ chunk_length_s=30.0,
292
+ batch_size=64 if torch.cuda.is_available() else 8,
293
+ )
294
+ ```
295
+
296
+ ---
297
+
298
+ ## 🔧 トラブルシューティング
299
+
300
+ ### よくある問題と解決方法
301
+
302
+ #### 1. モデルダウンロードエラー
303
+ ```bash
304
+ # 解決方法
305
+ # 1. キャッシュクリア
306
+ rm -rf ~/.cache/huggingface/
307
+
308
+ # 2. 手動ダウンロード
309
+ from huggingface_hub import snapshot_download
310
+ snapshot_download(repo_id="litagin/anime-whisper")
311
+ ```
312
+
313
+ #### 2. メモリ不足エラー
314
+ ```python
315
+ # 解決方法
316
+ # 1. バッチサイズ縮小
317
+ batch_size = 1
318
+
319
+ # 2. チャンクサイズ調整
320
+ chunk_length_s = 10.0
321
+
322
+ # 3. CPU使用
323
+ device = "cpu"
324
+ ```
325
+
326
+ #### 3. 音声品質の問題
327
+ ```python
328
+ # 解決方法
329
+ # 1. 音声前処理
330
+ import librosa
331
+ import soundfile as sf
332
+
333
+ def preprocess_audio(input_file, output_file):
334
+ """音声前処理"""
335
+ audio, sr = librosa.load(input_file, sr=16000)
336
+ sf.write(output_file, audio, 16000)
337
+ return output_file
338
+ ```
339
+
340
+ #### 4. 処理速度の問題
341
+ ```python
342
+ # 解決方法
343
+ # 1. GPU使用
344
+ device = "cuda"
345
+
346
+ # 2. ファイルサイズ最適化
347
+ # 30秒以下のファイルに分割
348
+
349
+ # 3. 並列処理
350
+ from concurrent.futures import ThreadPoolExecutor
351
+
352
+ def parallel_transcribe(files):
353
+ with ThreadPoolExecutor(max_workers=4) as executor:
354
+ results = list(executor.map(transcribe_audio, files))
355
+ return results
356
+ ```
357
+
358
+ ### デバッグ方法
359
+ ```python
360
+ import logging
361
+
362
+ # ログレベル設定
363
+ logging.basicConfig(level=logging.DEBUG)
364
+
365
+ # 詳細ログ出力
366
+ def debug_transcribe(audio_file):
367
+ print(f"入力ファイル: {audio_file}")
368
+ print(f"ファイル存在: {os.path.exists(audio_file)}")
369
+ print(f"ファイルサイズ: {os.path.getsize(audio_file)} bytes")
370
+
371
+ result = transcribe_audio(audio_file)
372
+ print(f"変換結果: {result}")
373
+ return result
374
+ ```
375
+
376
+ ---
377
+
378
+ ## 💡 使用例
379
+
380
+ ### 基本的な使用例
381
+
382
+ #### 1. 単一ファイル処理
383
+ ```python
384
+ from app import transcribe_audio
385
+
386
+ # 基本的な使用
387
+ audio_file = "anime_scene.wav"
388
+ result = transcribe_audio(audio_file)
389
+ print(f"変換結果: {result}")
390
+ ```
391
+
392
+ #### 2. ファイルアップロード処理
393
+ ```python
394
+ import gradio as gr
395
+
396
+ def process_uploaded_file(file):
397
+ if file is None:
398
+ return "ファイルがアップロードされていません"
399
+
400
+ try:
401
+ result = transcribe_audio(file)
402
+ return f"変換結果:\n{result}"
403
+ except Exception as e:
404
+ return f"エラー: {e}"
405
+
406
+ # Gradioインターフェース
407
+ demo = gr.Interface(
408
+ fn=process_uploaded_file,
409
+ inputs=gr.Audio(label="音声ファイル", type="filepath"),
410
+ outputs=gr.Textbox(label="変換結果", lines=10),
411
+ title="Anime TTS Demo"
412
+ )
413
+ ```
414
+
415
+ #### 3. バッ��処理
416
+ ```python
417
+ import os
418
+ import glob
419
+ from app import transcribe_audio
420
+
421
+ def batch_process_directory(directory_path):
422
+ """ディレクトリ内の全音声ファイルを処理"""
423
+ audio_extensions = ['.wav', '.mp3', '.m4a', '.flac']
424
+ results = {}
425
+
426
+ for ext in audio_extensions:
427
+ files = glob.glob(os.path.join(directory_path, f"*{ext}"))
428
+ for file_path in files:
429
+ try:
430
+ print(f"処理中: {file_path}")
431
+ result = transcribe_audio(file_path)
432
+ results[file_path] = result
433
+ except Exception as e:
434
+ results[file_path] = f"エラー: {e}"
435
+
436
+ return results
437
+
438
+ # 使用例
439
+ results = batch_process_directory("./audio_files")
440
+ for file_path, result in results.items():
441
+ print(f"{file_path}: {result}")
442
+ ```
443
+
444
+ #### 4. Web API としての使用
445
+ ```python
446
+ from fastapi import FastAPI, UploadFile, File
447
+ from app import transcribe_audio
448
+ import tempfile
449
+
450
+ app = FastAPI()
451
+
452
+ @app.post("/transcribe")
453
+ async def transcribe_endpoint(file: UploadFile = File(...)):
454
+ """音声ファイルを受け取り、テキストを返すAPI"""
455
+ try:
456
+ # 一時ファイルに保存
457
+ with tempfile.NamedTemporaryFile(delete=False) as temp_file:
458
+ content = await file.read()
459
+ temp_file.write(content)
460
+ temp_file.flush()
461
+
462
+ # 音声認識実行
463
+ result = transcribe_audio(temp_file.name)
464
+
465
+ # 一時ファイル削除
466
+ os.unlink(temp_file.name)
467
+
468
+ return {"text": result, "status": "success"}
469
+
470
+ except Exception as e:
471
+ return {"error": str(e), "status": "error"}
472
+
473
+ # 起動
474
+ # uvicorn app:app --host 0.0.0.0 --port 8000
475
+ ```
476
+
477
+ #### 5. リアルタイム処理
478
+ ```python
479
+ import pyaudio
480
+ import wave
481
+ from app import transcribe_audio
482
+
483
+ def real_time_transcribe(duration=10):
484
+ """リアルタイム音声認識"""
485
+ # 音声入力設定
486
+ chunk = 1024
487
+ format = pyaudio.paInt16
488
+ channels = 1
489
+ rate = 16000
490
+
491
+ p = pyaudio.PyAudio()
492
+
493
+ # ストリーム開始
494
+ stream = p.open(format=format,
495
+ channels=channels,
496
+ rate=rate,
497
+ input=True,
498
+ frames_per_buffer=chunk)
499
+
500
+ print("音声認識開始...")
501
+ frames = []
502
+
503
+ # 音声データ収集
504
+ for i in range(0, int(rate / chunk * duration)):
505
+ data = stream.read(chunk)
506
+ frames.append(data)
507
+
508
+ # ストリーム終了
509
+ stream.stop_stream()
510
+ stream.close()
511
+ p.terminate()
512
+
513
+ # 一時ファイルに保存
514
+ with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
515
+ wf = wave.open(f.name, 'wb')
516
+ wf.setnchannels(channels)
517
+ wf.setsampwidth(p.get_sample_size(format))
518
+ wf.setframerate(rate)
519
+ wf.writeframes(b''.join(frames))
520
+ wf.close()
521
+
522
+ # 音声認識実行
523
+ result = transcribe_audio(f.name)
524
+
525
+ # 一時ファイル削除
526
+ os.unlink(f.name)
527
+
528
+ return result
529
+
530
+ # 使用例
531
+ # result = real_time_transcribe(duration=5)
532
+ # print(f"認識結果: {result}")
533
+ ```
534
+
535
+ ### 高度な使用例
536
+
537
+ #### 1. カスタム前処理
538
+ ```python
539
+ import librosa
540
+ import soundfile as sf
541
+ import numpy as np
542
+ from app import transcribe_audio
543
+
544
+ def advanced_preprocess(audio_file, output_file):
545
+ """高度な音声前処理"""
546
+ # 音声読み込み
547
+ audio, sr = librosa.load(audio_file, sr=16000)
548
+
549
+ # ノイズリダクション
550
+ audio = librosa.effects.preemphasis(audio)
551
+
552
+ # 音量正規化
553
+ audio = librosa.util.normalize(audio)
554
+
555
+ # silence removal
556
+ audio, _ = librosa.effects.trim(audio, top_db=20)
557
+
558
+ # 保存
559
+ sf.write(output_file, audio, 16000)
560
+ return output_file
561
+
562
+ # 使用例
563
+ processed_file = advanced_preprocess("noisy_audio.wav", "clean_audio.wav")
564
+ result = transcribe_audio(processed_file)
565
+ ```
566
+
567
+ #### 2. 結果後処理
568
+ ```python
569
+ import re
570
+ from app import transcribe_audio
571
+
572
+ def postprocess_result(text):
573
+ """変換結果の後処理"""
574
+ # 句読点調整
575
+ text = re.sub(r'([。!?])', r'\1 ', text)
576
+
577
+ # 改行調整
578
+ text = re.sub(r'\s+', ' ', text)
579
+
580
+ # 特殊文字除去
581
+ text = re.sub(r'[^\w\s。!?、ー]', '', text)
582
+
583
+ return text.strip()
584
+
585
+ # 使用例
586
+ raw_result = transcribe_audio("audio.wav")
587
+ clean_result = postprocess_result(raw_result)
588
+ print(f"後処理結果: {clean_result}")
589
+ ```
590
+
591
+ ---
592
+
593
+ ## 📞 サポート
594
+
595
+ ### 問題報告
596
+ 問題が発生した場合は、以下の情報を含めて報告してください:
597
+
598
+ 1. **エラー詳細**: 完全なエラーメッセージ
599
+ 2. **環境情報**: Pythonバージョン、OS、GPU情報
600
+ 3. **音声ファイル情報**: フォーマット、サイズ、期間
601
+ 4. **再現手順**: 問題を再現する手順
602
+
603
+ ### パフォーマンス最適化サポート
604
+ 最適なパフォーマンスを得るためのサポートも提供しております。
605
+
606
+ ---
607
+
608
+ ## 📄 ライセンス
609
+
610
+ このプロジェクトはHugging Face Spacesで公開されており、 соответствующихライセンス條件に従います。
611
+
612
+ ---
613
+
614
+ **最終更新**: 2025-10-31
615
+ **バージョン**: 1.0.0
616
+ **作成者**: Anime TTS Development Team
requirements.txt ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Anime TTS - Required Dependencies
2
+ # Core ML/AI libraries
3
+ torch>=2.0.0
4
+ transformers>=4.30.0
5
+ spaces>=0.19.0
6
+
7
+ # Gradio and UI
8
+ gradio>=5.0.0
9
+
10
+ # Audio processing
11
+ soundfile>=0.12.0
12
+ numpy>=1.21.0
13
+ librosa>=0.10.0
14
+
15
+ # Utilities
16
+ pathlib
17
+ tempfile
18
+ os-sys
19
+
20
+ # Optional: Hugging Face Hub enhancements
21
+ huggingface_hub>=0.15.0
22
+
23
+ # GPU support (optional)
24
+ # torch-audio # Uncomment if using CUDA