File size: 2,079 Bytes
aa95dd5
 
 
530748c
aa95dd5
1040619
7d0efdb
1040619
7d0efdb
 
e92c6df
7d0efdb
 
1040619
 
 
a7f8c36
7d0efdb
 
1040619
 
 
 
 
7d0efdb
 
1040619
aa95dd5
 
1040619
aa95dd5
1040619
 
 
 
 
 
 
 
 
 
 
ae677e4
aa95dd5
1040619
 
 
aa95dd5
7d0efdb
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
import gradio as gr
import os
import tempfile
from openai import OpenAI

def tts(text, model, voice, language, api_key):
    if api_key == '':
        raise gr.Error('Please enter your API Key')
    else:
        try:
            client = OpenAI(api_key=api_key)

            response = client.audio.speech.create(
                model=model,
                voice=voice,
                language=language,  # Parametrul adăugat pentru limbă
                input=text,
            )

            with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
                temp_file.write(response.content)
                temp_file_path = temp_file.name

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

with gr.Blocks() as demo:
    gr.Markdown("# <center> Text-To-Speech API Key </center>")

    with gr.Row(variant='panel'):
        api_key = gr.Textbox(type='password', label='OpenAI API Key', placeholder='Enter your API key to access the TTS')
        model = gr.Dropdown(choices=['tts-1', 'tts-1-hd'], label='Model', value='tts-1-hd')
        voice = gr.Dropdown(choices=['alloy', 'ash', 'coral', 'echo', 'fable', 'onyx', 'nova', 'sage', 'shimmer'], label='Voice Options', value='echo')
        language = gr.Dropdown(
            choices=['en', 'fr', 'es', 'de', 'it', 'ro', 'tr', 'br', 'ru'],
            label='Choose a language',
            value='en'
        )

    text = gr.Textbox(label="Input text", placeholder="Enter your text and then click the 'Text-To-Speech' button or press Enter.")
    btn = gr.Button("Text-To-Speech")
    output_audio = gr.Audio(label="Speech Output")

    text.submit(fn=tts, inputs=[text, model, voice, language, api_key], outputs=output_audio, api_name="tts_enter_key", concurrency_limit=None)
    btn.click(fn=tts, inputs=[text, model, voice, language, api_key], outputs=output_audio, api_name="tts_button", concurrency_limit=None)

demo.launch()