Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,42 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
import tempfile
|
|
|
|
| 4 |
|
| 5 |
# Hàm kiểm tra API key hợp lệ
|
| 6 |
def check_api_key(api_key):
|
| 7 |
try:
|
| 8 |
client = OpenAI(api_key=api_key)
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
return True # Nếu không có lỗi, API key hợp lệ
|
| 12 |
except Exception as e:
|
| 13 |
print(f"Error: {e}")
|
| 14 |
-
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Hàm TTS (Text to Speech)
|
| 17 |
-
def tts(text, model, voice, speed, api_key, audio_file=None):
|
| 18 |
# Kiểm tra tính hợp lệ của API key
|
| 19 |
if not api_key or api_key.strip() == "":
|
| 20 |
raise gr.Error('Please enter your OpenAI API Key')
|
| 21 |
|
| 22 |
-
# Kiểm tra tính hợp lệ của API key
|
| 23 |
if not check_api_key(api_key):
|
| 24 |
raise gr.Error('Invalid OpenAI API Key. Please enter a valid API key.')
|
| 25 |
|
|
@@ -30,7 +47,7 @@ def tts(text, model, voice, speed, api_key, audio_file=None):
|
|
| 30 |
if audio_file:
|
| 31 |
audio_file = open(audio_file, 'rb')
|
| 32 |
transcript = client.audio.transcriptions.create(model='whisper-1', file=audio_file, response_format='text')
|
| 33 |
-
text = transcript
|
| 34 |
|
| 35 |
# Tạo yêu cầu TTS với tốc độ điều chỉnh
|
| 36 |
response = client.audio.speech.create(
|
|
@@ -38,7 +55,6 @@ def tts(text, model, voice, speed, api_key, audio_file=None):
|
|
| 38 |
voice=voice,
|
| 39 |
input=text,
|
| 40 |
speed=speed
|
| 41 |
-
#pitch=0.8
|
| 42 |
)
|
| 43 |
|
| 44 |
except Exception as error:
|
|
@@ -50,55 +66,56 @@ def tts(text, model, voice, speed, api_key, audio_file=None):
|
|
| 50 |
temp_file.write(response.content)
|
| 51 |
temp_file_path = temp_file.name
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
return temp_file_path
|
| 54 |
|
| 55 |
# Hàm giao diện Gradio
|
| 56 |
def gradio_interface():
|
| 57 |
with gr.Blocks() as demo:
|
| 58 |
-
gr.Markdown("# <center> OpenAI Text-To-Speech with Speed Control </center>")
|
| 59 |
|
| 60 |
-
# Định dạng lại giao diện với hai cột
|
| 61 |
with gr.Row():
|
| 62 |
-
# Cột bên trái: Nhập Text, chọn Model, Voice, Speed và nút Generate
|
| 63 |
-
with gr.Column(scale=2):
|
| 64 |
api_key = gr.Textbox(type='password', label='Enter your OpenAI API Key', placeholder='Enter your OpenAI API key')
|
| 65 |
|
| 66 |
-
# Cột cho Model và Voice Options cùng nằm trên một hàng ngang
|
| 67 |
with gr.Row():
|
| 68 |
model = gr.Dropdown(choices=['tts-1', 'tts-1-hd'], label='Model', value='tts-1', elem_id="model-dropdown", interactive=True)
|
| 69 |
voice = gr.Dropdown(
|
| 70 |
-
choices=[
|
| 71 |
-
'alloy', 'ash', 'coral', 'echo', 'fable', 'onyx', 'nova', 'sage', 'shimmer'
|
| 72 |
-
],
|
| 73 |
label='Voice Options',
|
| 74 |
-
value='
|
| 75 |
-
elem_id="voice-dropdown",
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
-
speed = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, label="Speed", value=0
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
with gr.Row(): # Tạo một hàng riêng biệt cho phần nhập Text
|
| 82 |
text = gr.Textbox(label="Input Text", placeholder="Enter your text here")
|
| 83 |
btn = gr.Button("Generate Speech")
|
| 84 |
-
|
| 85 |
# Cột bên phải: Upload audio và Speech Output
|
| 86 |
-
with gr.Column(scale=2):
|
| 87 |
input_type = gr.Radio(["Text", "Audio"], label="Input Type", value="Text")
|
| 88 |
audio_file = gr.File(label="Upload Audio File")
|
| 89 |
output_audio = gr.Audio(label="Speech Output")
|
| 90 |
|
| 91 |
# Quy trình xử lý
|
| 92 |
-
def process_input(input_type, text, audio_file, api_key):
|
| 93 |
if input_type == "Text":
|
| 94 |
-
return tts(text, model.value, voice.value, speed
|
| 95 |
elif input_type == "Audio":
|
| 96 |
-
return tts(None, model.value, voice.value, speed
|
| 97 |
|
| 98 |
# Thiết lập sự kiện cho việc nhấn nút
|
| 99 |
-
btn.click(fn=process_input, inputs=[input_type, text, audio_file, api_key], outputs=output_audio)
|
| 100 |
|
| 101 |
demo.launch()
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
-
gradio_interface()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
import tempfile
|
| 4 |
+
from pydub import AudioSegment
|
| 5 |
|
| 6 |
# Hàm kiểm tra API key hợp lệ
|
| 7 |
def check_api_key(api_key):
|
| 8 |
try:
|
| 9 |
client = OpenAI(api_key=api_key)
|
| 10 |
+
client.models.list() # Kiểm tra kết nối API
|
| 11 |
+
return True
|
|
|
|
| 12 |
except Exception as e:
|
| 13 |
print(f"Error: {e}")
|
| 14 |
+
return False
|
| 15 |
+
|
| 16 |
+
# Hàm điều chỉnh pitch
|
| 17 |
+
def adjust_pitch(audio_path, pitch_factor=1.0):
|
| 18 |
+
"""
|
| 19 |
+
Điều chỉnh pitch của file âm thanh.
|
| 20 |
+
:param audio_path: Đường dẫn đến file âm thanh.
|
| 21 |
+
:param pitch_factor: Hệ số điều chỉnh pitch:
|
| 22 |
+
- pitch_factor > 1.0: Tăng pitch (giọng cao hơn).
|
| 23 |
+
- pitch_factor < 1.0: Giảm pitch (giọng trầm hơn).
|
| 24 |
+
:return: Đường dẫn đến file âm thanh đã điều chỉnh.
|
| 25 |
+
"""
|
| 26 |
+
audio = AudioSegment.from_file(audio_path)
|
| 27 |
+
new_frame_rate = int(audio.frame_rate * pitch_factor)
|
| 28 |
+
pitched_audio = audio._spawn(audio.raw_data, overrides={
|
| 29 |
+
"frame_rate": new_frame_rate
|
| 30 |
+
}).set_frame_rate(audio.frame_rate)
|
| 31 |
+
pitched_audio.export(audio_path, format="mp3")
|
| 32 |
+
return audio_path
|
| 33 |
|
| 34 |
# Hàm TTS (Text to Speech)
|
| 35 |
+
def tts(text, model, voice, speed, api_key, audio_file=None, pitch_factor=1.0):
|
| 36 |
# Kiểm tra tính hợp lệ của API key
|
| 37 |
if not api_key or api_key.strip() == "":
|
| 38 |
raise gr.Error('Please enter your OpenAI API Key')
|
| 39 |
|
|
|
|
| 40 |
if not check_api_key(api_key):
|
| 41 |
raise gr.Error('Invalid OpenAI API Key. Please enter a valid API key.')
|
| 42 |
|
|
|
|
| 47 |
if audio_file:
|
| 48 |
audio_file = open(audio_file, 'rb')
|
| 49 |
transcript = client.audio.transcriptions.create(model='whisper-1', file=audio_file, response_format='text')
|
| 50 |
+
text = transcript # Lấy văn bản từ tệp âm thanh
|
| 51 |
|
| 52 |
# Tạo yêu cầu TTS với tốc độ điều chỉnh
|
| 53 |
response = client.audio.speech.create(
|
|
|
|
| 55 |
voice=voice,
|
| 56 |
input=text,
|
| 57 |
speed=speed
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
except Exception as error:
|
|
|
|
| 66 |
temp_file.write(response.content)
|
| 67 |
temp_file_path = temp_file.name
|
| 68 |
|
| 69 |
+
# Điều chỉnh pitch nếu pitch_factor khác 1.0
|
| 70 |
+
if pitch_factor != 1.0:
|
| 71 |
+
temp_file_path = adjust_pitch(temp_file_path, pitch_factor)
|
| 72 |
+
|
| 73 |
return temp_file_path
|
| 74 |
|
| 75 |
# Hàm giao diện Gradio
|
| 76 |
def gradio_interface():
|
| 77 |
with gr.Blocks() as demo:
|
| 78 |
+
gr.Markdown("# <center> OpenAI Text-To-Speech with Speed and Pitch Control </center>")
|
| 79 |
|
|
|
|
| 80 |
with gr.Row():
|
| 81 |
+
# Cột bên trái: Nhập Text, chọn Model, Voice, Speed, Pitch và nút Generate
|
| 82 |
+
with gr.Column(scale=2):
|
| 83 |
api_key = gr.Textbox(type='password', label='Enter your OpenAI API Key', placeholder='Enter your OpenAI API key')
|
| 84 |
|
|
|
|
| 85 |
with gr.Row():
|
| 86 |
model = gr.Dropdown(choices=['tts-1', 'tts-1-hd'], label='Model', value='tts-1', elem_id="model-dropdown", interactive=True)
|
| 87 |
voice = gr.Dropdown(
|
| 88 |
+
choices=['alloy', 'echo', 'onyx', 'nova'],
|
|
|
|
|
|
|
| 89 |
label='Voice Options',
|
| 90 |
+
value='echo',
|
| 91 |
+
elem_id="voice-dropdown",
|
| 92 |
+
interactive=True
|
| 93 |
)
|
| 94 |
|
| 95 |
+
speed = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, label="Speed", value=1.0)
|
| 96 |
+
pitch = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, label="Pitch", value=0.8) # Thêm thanh trượt pitch
|
| 97 |
|
| 98 |
+
with gr.Row():
|
|
|
|
| 99 |
text = gr.Textbox(label="Input Text", placeholder="Enter your text here")
|
| 100 |
btn = gr.Button("Generate Speech")
|
| 101 |
+
|
| 102 |
# Cột bên phải: Upload audio và Speech Output
|
| 103 |
+
with gr.Column(scale=2):
|
| 104 |
input_type = gr.Radio(["Text", "Audio"], label="Input Type", value="Text")
|
| 105 |
audio_file = gr.File(label="Upload Audio File")
|
| 106 |
output_audio = gr.Audio(label="Speech Output")
|
| 107 |
|
| 108 |
# Quy trình xử lý
|
| 109 |
+
def process_input(input_type, text, audio_file, api_key, speed, pitch):
|
| 110 |
if input_type == "Text":
|
| 111 |
+
return tts(text, model.value, voice.value, speed, api_key, pitch_factor=pitch)
|
| 112 |
elif input_type == "Audio":
|
| 113 |
+
return tts(None, model.value, voice.value, speed, api_key, audio_file.name, pitch_factor=pitch)
|
| 114 |
|
| 115 |
# Thiết lập sự kiện cho việc nhấn nút
|
| 116 |
+
btn.click(fn=process_input, inputs=[input_type, text, audio_file, api_key, speed, pitch], outputs=output_audio)
|
| 117 |
|
| 118 |
demo.launch()
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
| 121 |
+
gradio_interface()
|