soiz1 commited on
Commit
61d7bec
·
verified ·
1 Parent(s): 47b843f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tempfile
3
+ import os
4
+
5
+ # 保存したnpzファイルのディレクトリ
6
+ npz_save_dir = tempfile.gettempdir()
7
+
8
+ # 必要な関数(`make_npz_prompt` と `infer_from_prompt`)は既に提供済みのものを利用
9
+ def save_npz_file(name, uploaded_audio, recorded_audio, transcript_content):
10
+ result, file_path = make_npz_prompt(name, uploaded_audio, recorded_audio, transcript_content)
11
+ if file_path:
12
+ return result, gr.File(file_path)
13
+ return result, None
14
+
15
+ def generate_from_npz(npz_file, text, language, accent, preset_prompt):
16
+ if npz_file:
17
+ prompt_path = npz_file.name
18
+ else:
19
+ prompt_path = None
20
+
21
+ message, output = infer_from_prompt(text, language, accent, preset_prompt, prompt_path)
22
+ if output:
23
+ return message, output
24
+ return message, None
25
+
26
+ def get_available_npz_files():
27
+ # 一時ディレクトリ内のすべての .npz ファイルをリストアップ
28
+ return [f for f in os.listdir(npz_save_dir) if f.endswith(".npz")]
29
+
30
+ # Gradio アプリのインターフェース作成
31
+ with gr.Blocks() as app:
32
+ with gr.Tabs():
33
+ with gr.Tab("NPZファイルを作成"):
34
+ gr.Markdown("### 音声とテキストから .npz ファイルを作成")
35
+ name = gr.Textbox(label="ファイル名", placeholder="保存する .npz ファイル名を入力")
36
+ uploaded_audio = gr.Audio(label="アップロード音声", type="numpy")
37
+ recorded_audio = gr.Audio(label="録音音声", source="microphone", type="numpy")
38
+ transcript_content = gr.Textbox(label="テキスト内容", placeholder="音声に対応する文字起こしを入力")
39
+ result_message = gr.Textbox(label="結果", interactive=False)
40
+ npz_output = gr.File(label=".npz ファイル")
41
+ save_button = gr.Button("変換して保存")
42
+
43
+ save_button.click(
44
+ save_npz_file,
45
+ inputs=[name, uploaded_audio, recorded_audio, transcript_content],
46
+ outputs=[result_message, npz_output],
47
+ )
48
+
49
+ with gr.Tab("NPZファイルで生成"):
50
+ gr.Markdown("### 保存した .npz ファイルから音声を生成")
51
+ npz_files_dropdown = gr.Dropdown(
52
+ label="利用可能な .npz ファイル", choices=get_available_npz_files(), interactive=True
53
+ )
54
+ text_input = gr.Textbox(label="生成するテキスト", placeholder="150文字以内のテキストを入力")
55
+ language = gr.Radio(
56
+ label="言語選択",
57
+ choices=["auto-detect", "en", "ja", "zh"],
58
+ value="auto-detect"
59
+ )
60
+ accent = gr.Radio(
61
+ label="アクセント選択",
62
+ choices=["no-accent", "en-accent", "ja-accent", "zh-accent"],
63
+ value="no-accent"
64
+ )
65
+ preset_prompt = gr.Textbox(label="プロンプト名", placeholder="既存のプロンプトを選択")
66
+ synthesis_message = gr.Textbox(label="結果", interactive=False)
67
+ audio_output = gr.Audio(label="生成音声", type="numpy")
68
+ generate_button = gr.Button("生成開始")
69
+
70
+ generate_button.click(
71
+ generate_from_npz,
72
+ inputs=[npz_files_dropdown, text_input, language, accent, preset_prompt],
73
+ outputs=[synthesis_message, audio_output],
74
+ )
75
+
76
+ app.launch()