Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,15 +9,15 @@ st.title("Text-to-Speech Translator with Kokoro")
|
|
9 |
|
10 |
# Expander section to display information in multiple languages
|
11 |
with st.expander("Sample Prompt!"):
|
12 |
-
st.markdown("""
|
13 |
-
- My name is Shukdev. (In English)
|
14 |
-
- Mi nombre es Shukdev. (In Spanish)
|
15 |
-
- Je m'appelle Choukdev. (In French)
|
16 |
-
- मेरा नाम शुकदेव है. (In Hindi)
|
17 |
-
- Il mio nome è Shukdev. (In Italy)
|
18 |
-
- Meu nome é Sukhdev. (In Portuguese, Brazil)
|
19 |
-
- 我叫苏赫德夫。(In Chinese)
|
20 |
-
- 私の名前はスクデフです。(In Japanese)
|
21 |
""")
|
22 |
|
23 |
st.sidebar.header("Configuration & Instructions")
|
@@ -38,23 +38,18 @@ st.sidebar.markdown("""
|
|
38 |
- 🇧🇷 **Portuguese** (Brazilian)
|
39 |
- 🇨🇳 **Chinese** (Mandarin)
|
40 |
- 🇯🇵 **Japanese**
|
41 |
-
|
42 |
3. **Select Voice**:
|
43 |
- Choose the voice you want for the speech. There are multiple voice styles based on tone and gender (e.g., `af_heart`, `af_joy`, etc.).
|
44 |
|
45 |
4. **Adjust Speech Speed**:
|
46 |
- Use the slider to adjust how fast the speech will be generated. The speed can be set from `0.5x` to `2.0x`, with `1.0x` being the default normal speed.
|
47 |
-
|
48 |
5. **Generate Speech**:
|
49 |
- Once you've selected the text, language, voice, and speed, click the **"Generate Audio"** button. The app will process the text and generate the speech.
|
50 |
-
|
51 |
6. **Download Audio**:
|
52 |
- After the audio is generated, you can play it directly within the app or download it as a `.wav` file by clicking the **"Download Audio"** button.
|
53 |
-
|
54 |
### Additional Features:
|
55 |
- **Text Translation**:
|
56 |
- The app can automatically translate the text to English before generating audio. After the translation, you will hear the audio in English with your chosen voice.
|
57 |
-
|
58 |
Enjoy exploring different languages, voices, and speeds with the text-to-speech conversion!
|
59 |
""")
|
60 |
|
@@ -84,8 +79,8 @@ speed = st.slider("Speed", min_value=0.5, max_value=2.0, value=1.0, step=0.1)
|
|
84 |
# Initialize the TTS pipeline with user-selected language
|
85 |
pipeline = KPipeline(lang_code=lang_code)
|
86 |
|
87 |
-
# Function to get the OpenAI API key from the user
|
88 |
-
openai_api_key = st.text_input("Enter your OpenAI API Key
|
89 |
|
90 |
# Function to translate text to English using OpenAI's Chat API
|
91 |
def translate_to_english(api_key, text, lang_code):
|
@@ -96,12 +91,10 @@ def translate_to_english(api_key, text, lang_code):
|
|
96 |
|
97 |
response = openai.ChatCompletion.create(
|
98 |
model="gpt-4", # Using ChatGPT model for translation
|
99 |
-
messages=[
|
100 |
-
|
101 |
-
{"role": "user", "content": prompt}
|
102 |
-
]
|
103 |
)
|
104 |
-
|
105 |
# Extract translated text from response
|
106 |
translated_text = response['choices'][0]['message']['content'].strip()
|
107 |
return translated_text
|
@@ -122,7 +115,9 @@ def generate_audio(text, lang_code, voice, speed):
|
|
122 |
return buffer
|
123 |
|
124 |
# Generate and display the audio file
|
125 |
-
if st.button('Generate Audio')
|
|
|
|
|
126 |
st.write("Generating speech for the original text...")
|
127 |
audio_buffer = generate_audio(input_text, lang_code, voice, speed)
|
128 |
|
@@ -137,20 +132,22 @@ if st.button('Generate Audio') and openai_api_key:
|
|
137 |
mime="audio/wav"
|
138 |
)
|
139 |
|
140 |
-
#
|
141 |
-
|
|
|
|
|
142 |
|
143 |
-
|
144 |
-
|
145 |
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
|
|
9 |
|
10 |
# Expander section to display information in multiple languages
|
11 |
with st.expander("Sample Prompt!"):
|
12 |
+
st.markdown("""
|
13 |
+
- My name is Shukdev. (In English)
|
14 |
+
- Mi nombre es Shukdev. (In Spanish)
|
15 |
+
- Je m'appelle Choukdev. (In French)
|
16 |
+
- मेरा नाम शुकदेव है. (In Hindi)
|
17 |
+
- Il mio nome è Shukdev. (In Italy)
|
18 |
+
- Meu nome é Sukhdev. (In Portuguese, Brazil)
|
19 |
+
- 我叫苏赫德夫。(In Chinese)
|
20 |
+
- 私の名前はスクデフです。(In Japanese)
|
21 |
""")
|
22 |
|
23 |
st.sidebar.header("Configuration & Instructions")
|
|
|
38 |
- 🇧🇷 **Portuguese** (Brazilian)
|
39 |
- 🇨🇳 **Chinese** (Mandarin)
|
40 |
- 🇯🇵 **Japanese**
|
|
|
41 |
3. **Select Voice**:
|
42 |
- Choose the voice you want for the speech. There are multiple voice styles based on tone and gender (e.g., `af_heart`, `af_joy`, etc.).
|
43 |
|
44 |
4. **Adjust Speech Speed**:
|
45 |
- Use the slider to adjust how fast the speech will be generated. The speed can be set from `0.5x` to `2.0x`, with `1.0x` being the default normal speed.
|
|
|
46 |
5. **Generate Speech**:
|
47 |
- Once you've selected the text, language, voice, and speed, click the **"Generate Audio"** button. The app will process the text and generate the speech.
|
|
|
48 |
6. **Download Audio**:
|
49 |
- After the audio is generated, you can play it directly within the app or download it as a `.wav` file by clicking the **"Download Audio"** button.
|
|
|
50 |
### Additional Features:
|
51 |
- **Text Translation**:
|
52 |
- The app can automatically translate the text to English before generating audio. After the translation, you will hear the audio in English with your chosen voice.
|
|
|
53 |
Enjoy exploring different languages, voices, and speeds with the text-to-speech conversion!
|
54 |
""")
|
55 |
|
|
|
79 |
# Initialize the TTS pipeline with user-selected language
|
80 |
pipeline = KPipeline(lang_code=lang_code)
|
81 |
|
82 |
+
# Function to get the OpenAI API key from the user (optional for translation)
|
83 |
+
openai_api_key = st.text_input("Enter your OpenAI API Key (Optional for Translation)", type="password")
|
84 |
|
85 |
# Function to translate text to English using OpenAI's Chat API
|
86 |
def translate_to_english(api_key, text, lang_code):
|
|
|
91 |
|
92 |
response = openai.ChatCompletion.create(
|
93 |
model="gpt-4", # Using ChatGPT model for translation
|
94 |
+
messages=[{"role": "system", "content": "You are a helpful assistant that translates text."},
|
95 |
+
{"role": "user", "content": prompt}]
|
|
|
|
|
96 |
)
|
97 |
+
|
98 |
# Extract translated text from response
|
99 |
translated_text = response['choices'][0]['message']['content'].strip()
|
100 |
return translated_text
|
|
|
115 |
return buffer
|
116 |
|
117 |
# Generate and display the audio file
|
118 |
+
if st.button('Generate Audio'):
|
119 |
+
|
120 |
+
# Generate audio for the original text
|
121 |
st.write("Generating speech for the original text...")
|
122 |
audio_buffer = generate_audio(input_text, lang_code, voice, speed)
|
123 |
|
|
|
132 |
mime="audio/wav"
|
133 |
)
|
134 |
|
135 |
+
# Check if OpenAI API Key is provided for translation and English audio generation
|
136 |
+
if openai_api_key:
|
137 |
+
# Translate the input text to English using OpenAI
|
138 |
+
translated_text = translate_to_english(openai_api_key, input_text, lang_code)
|
139 |
|
140 |
+
# Generate audio for the translated English text
|
141 |
+
translated_audio_buffer = generate_audio(translated_text, 'a', voice, speed)
|
142 |
|
143 |
+
# Display Audio for the translated text
|
144 |
+
st.write(f"Translated Text: {translated_text}")
|
145 |
+
st.audio(translated_audio_buffer, format='audio/wav')
|
146 |
|
147 |
+
# Optional: Save the generated audio file for download (Translated Text)
|
148 |
+
st.download_button(
|
149 |
+
label="Download Audio (Translated to English)",
|
150 |
+
data=translated_audio_buffer,
|
151 |
+
file_name="generated_speech_translated.wav",
|
152 |
+
mime="audio/wav"
|
153 |
+
)
|