File size: 5,027 Bytes
a91dac8
 
 
5673c37
a91dac8
 
 
 
 
5673c37
 
a91dac8
 
5673c37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a91dac8
 
5673c37
a91dac8
 
 
 
 
 
 
 
 
 
 
 
 
 
5673c37
a91dac8
 
 
 
 
 
ba4d46e
a91dac8
 
 
 
 
 
 
 
 
 
 
5673c37
 
 
 
a91dac8
 
 
 
 
ebea340
a91dac8
 
5673c37
 
a91dac8
 
5df06b6
f7a4e72
5df06b6
5673c37
5df06b6
5673c37
 
 
5df06b6
a91dac8
5673c37
ebea340
a91dac8
5673c37
09eb8c9
a91dac8
5673c37
a91dac8
5673c37
a91dac8
 
 
 
 
5673c37
a91dac8
5673c37
a91dac8
5673c37
a91dac8
 
5673c37
a91dac8
19a0cd9
a91dac8
 
5673c37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import gradio as gr
from openai import OpenAI
import tempfile
from pydub import AudioSegment

# Hàm kiểm tra API key hợp lệ
def check_api_key(api_key):
    try:
        client = OpenAI(api_key=api_key)
        client.models.list()  # Kiểm tra kết nối API
        return True
    except Exception as e:
        print(f"Error: {e}")
        return False

# Hàm điều chỉnh pitch
def adjust_pitch(audio_path, pitch_factor=1.0):
    """
    Điều chỉnh pitch của file âm thanh.
    :param audio_path: Đường dẫn đến file âm thanh.
    :param pitch_factor: Hệ số điều chỉnh pitch:
        - pitch_factor > 1.0: Tăng pitch (giọng cao hơn).
        - pitch_factor < 1.0: Giảm pitch (giọng trầm hơn).
    :return: Đường dẫn đến file âm thanh đã điều chỉnh.
    """
    audio = AudioSegment.from_file(audio_path)
    new_frame_rate = int(audio.frame_rate * pitch_factor)
    pitched_audio = audio._spawn(audio.raw_data, overrides={
        "frame_rate": new_frame_rate
    }).set_frame_rate(audio.frame_rate)
    pitched_audio.export(audio_path, format="mp3")
    return audio_path

# Hàm TTS (Text to Speech)
def tts(text, model, voice, speed, api_key, audio_file=None, pitch_factor=1.0):
    # Kiểm tra tính hợp lệ của API key
    if not api_key or api_key.strip() == "":
        raise gr.Error('Please enter your OpenAI API Key')
    
    if not check_api_key(api_key):
        raise gr.Error('Invalid OpenAI API Key. Please enter a valid API key.')

    try:
        client = OpenAI(api_key=api_key)
        
        # Nếu người dùng tải lên tệp âm thanh, sử dụng Whisper để chuyển thành văn bản
        if audio_file:
            audio_file = open(audio_file, 'rb')
            transcript = client.audio.transcriptions.create(model='whisper-1', file=audio_file, response_format='text')
            text = transcript  # Lấy văn bản từ tệp âm thanh

        # Tạo yêu cầu TTS với tốc độ điều chỉnh
        response = client.audio.speech.create(
            model=model,
            voice=voice,
            input=text,
            speed=speed
        )

    except Exception as error:
        print(str(error))
        raise gr.Error("An error occurred while generating speech. Please check your API key and try again.")

    # Lưu âm thanh vào tệp tạm thời
    with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
        temp_file.write(response.content)
        temp_file_path = temp_file.name

    # Điều chỉnh pitch nếu pitch_factor khác 1.0
    if pitch_factor != 1.0:
        temp_file_path = adjust_pitch(temp_file_path, pitch_factor)

    return temp_file_path

# Hàm giao diện Gradio
def gradio_interface():
    with gr.Blocks() as demo:
        gr.Markdown("# <center> OpenAI Text-To-Speech V2.0 </center>")

        with gr.Row():
            # Cột bên trái: Nhập Text, chọn Model, Voice, Speed, Pitch và nút Generate
            with gr.Column(scale=2):
                api_key = gr.Textbox(type='password', label='Enter your OpenAI API Key', placeholder='Enter your OpenAI API key')

                with gr.Row():
                    model = gr.Dropdown(choices=['tts-1', 'tts-1-hd'], label='Model', value='tts-1', elem_id="model-dropdown", interactive=True)
                    voice = gr.Dropdown(
                        choices=['alloy', 'echo', 'onyx', 'nova'], 
                        label='Voice Options', 
                        value='echo',
                        elem_id="voice-dropdown", 
                        interactive=True
                    )

                speed = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, label="Speed", value=1.0)
                pitch = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, label="Pitch", value=1.0)  # Thêm thanh trượt pitch

                with gr.Row():
                    text = gr.Textbox(label="Input Text", placeholder="Enter your text here")
                btn = gr.Button("Generate Speech")

            # Cột bên phải: Upload audio và Speech Output
            with gr.Column(scale=2):
                input_type = gr.Radio(["Text", "Audio"], label="Input Type", value="Text")
                audio_file = gr.File(label="Upload Audio File")
                output_audio = gr.Audio(label="Speech Output")

        # Quy trình xử lý
        def process_input(input_type, text, audio_file, api_key, speed, pitch):
            if input_type == "Text":
                return tts(text, model.value, voice.value, speed, api_key, pitch_factor=pitch)
            elif input_type == "Audio":
                return tts(None, model.value, voice.value, speed, api_key, audio_file.name, pitch_factor=pitch)

        # Thiết lập sự kiện cho việc nhấn nút
        btn.click(fn=process_input, inputs=[input_type, text, audio_file, api_key, speed, pitch], outputs=output_audio)

    demo.launch()

if __name__ == "__main__":
    gradio_interface()