fengmiguoji commited on
Commit
e6170b9
·
verified ·
1 Parent(s): 438f46f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -0
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from gradio_client import Client, handle_file
4
+ import shutil
5
+ from groq import Groq
6
+
7
+ groq_client = Groq()
8
+
9
+ def run_tts(ref_audio_file, ref_text, gen_text):
10
+ """
11
+ 调用 TTS 模型并保存结果
12
+ """
13
+ try:
14
+ if ref_audio_file is None:
15
+ return "请上传参考音频文件。", None # 返回错误消息和None
16
+
17
+ ref_audio_path = ref_audio_file.name # 获取上传文件的路径
18
+
19
+ # 初始化客户端,不使用 token
20
+ client = Client("abidlabs/E2-F5-TTS")
21
+
22
+ # 调用 /infer 端点
23
+ print("调用 /infer 端点...")
24
+ result = client.predict(
25
+ ref_audio_orig=handle_file(ref_audio_path), # 使用上传的音频文件
26
+ ref_text=ref_text,
27
+ gen_text=gen_text,
28
+ exp_name="F5-TTS",
29
+ remove_silence=False,
30
+ cross_fade_duration=0.15,
31
+ api_name="/infer"
32
+ )
33
+ print("infer端点返回结果:", result)
34
+
35
+ # 处理返回结果,将文件保存到当前目录
36
+ output_file = None
37
+ if isinstance(result, tuple):
38
+ for item in result:
39
+ if isinstance(item, str) and item.lower().endswith(".wav"):
40
+ if os.path.exists(item): # 确保item是一个文件路径且存在
41
+ # 获取文件名
42
+ filename = os.path.basename(item)
43
+ # 构造新的保存路径,保持在当前目录
44
+ new_path = os.path.join(".", filename)
45
+ # 复制文件到新路径
46
+ shutil.copy2(item, new_path)
47
+ print(f"音频文件 '{filename}' 已保存到: {new_path}")
48
+ output_file = new_path
49
+ break #只保留一个音频文件路径
50
+ else:
51
+ print(f"文件路径不存在,跳过: {item}")
52
+ elif isinstance(item,str):
53
+ print(f"跳过非音频文件: {item}")
54
+ elif isinstance(result, str) and result.lower().endswith(".wav"):
55
+ if os.path.exists(result):
56
+ # 获取文件名
57
+ filename = os.path.basename(result)
58
+ # 构造新的保存路径,保持在当前目录
59
+ new_path = os.path.join(".", filename)
60
+ # 复制文件到新路径
61
+ shutil.copy2(result, new_path)
62
+ print(f"音频文件 '{filename}' 已保存到: {new_path}")
63
+ output_file = new_path
64
+ else:
65
+ print(f"文件路径不存在,跳过: {result}")
66
+ elif isinstance(result,str):
67
+ print(f"跳过非音频文件: {result}")
68
+ else:
69
+ print(f"跳过非字符串/元组类型的返回值: {result}")
70
+
71
+ if output_file:
72
+ return output_file , ref_text # 返回音频文件路径字符串和修改后的参考文本
73
+ else:
74
+ return "未生成音频文件。", ref_text # 返回错误提示和修改后的参考文本
75
+ except FileNotFoundError as e:
76
+ return f"发生错误:{e}", ref_text
77
+ except Exception as e:
78
+ return f"发生未知错误:{e}", ref_text
79
+
80
+
81
+ def transcribe_audio(audio_file):
82
+ """
83
+ 使用 Groq 进行语音识别
84
+ """
85
+ try:
86
+ if audio_file is None:
87
+ return "请上传参考音频文件。", None
88
+
89
+ audio_path = audio_file.name
90
+ with open(audio_path, "rb") as file:
91
+ transcription = groq_client.audio.transcriptions.create(
92
+ file=(audio_path, file.read()),
93
+ model="whisper-large-v3-turbo",
94
+ language="zh",
95
+ )
96
+ return transcription.text, audio_file # 返回识别文本和音频文件
97
+ except Exception as e:
98
+ return f"语音识别失败: {e}", None
99
+
100
+
101
+ def update_ref_text(audio_file, ref_text_box):
102
+ """
103
+ 语音识别并更新参考文本
104
+ """
105
+ transcribed_text, audio_file = transcribe_audio(audio_file)
106
+ return transcribed_text, audio_file
107
+
108
+
109
+ with gr.Blocks(title="快速语音合成") as iface:
110
+ gr.Markdown("上传参考语音和输入参考及生成文本,生成相应的语音。")
111
+ ref_audio_input = gr.File(file_types=["audio"], label="参考语音 (上传音频自动识别)")
112
+ ref_text_input = gr.Textbox(label="参考语言 (文本)")
113
+ gen_text_input = gr.Textbox(label="生成语言 (文本)")
114
+
115
+ audio_output = gr.Audio(label="生成的语音 (下载)")
116
+
117
+ ref_audio_input.upload(
118
+ update_ref_text,
119
+ inputs=[ref_audio_input, ref_text_input],
120
+ outputs=[ref_text_input, ref_audio_input]
121
+ )
122
+
123
+
124
+ btn = gr.Button("合成")
125
+ btn.click(
126
+ run_tts,
127
+ inputs=[ref_audio_input, ref_text_input, gen_text_input],
128
+ outputs=[audio_output,ref_text_input],
129
+ )
130
+
131
+ if __name__ == "__main__":
132
+ iface.launch(share=True)