shukdevdatta123 commited on
Commit
2f0bafd
·
verified ·
1 Parent(s): 7c40c70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -8
app.py CHANGED
@@ -3,6 +3,7 @@ from kokoro import KPipeline
3
  import soundfile as sf
4
  import io
5
  import os
 
6
 
7
  # Install espeak-ng if not installed
8
  # if not os.system("which espeak-ng"):
@@ -15,7 +16,7 @@ import os
15
  st.title("Text-to-Speech with Kokoro")
16
  # Expander section to display information in multiple languages
17
  with st.expander("Sample Prompt!"):
18
- st.markdown("""
19
  - My name is Shukdev. (In English)
20
  - Mi nombre es Shukdev. (In Spanish)
21
  - Je m'appelle Choukdev. (In French)
@@ -57,10 +58,6 @@ st.sidebar.markdown("""
57
  Enjoy experimenting with the text-to-speech conversion, and feel free to try different voices, speeds, and languages!
58
  """)
59
 
60
- st.sidebar.markdown("""
61
- ### Courtesy: [Kokoro](https://huggingface.co/hexgrad/Kokoro-82M?fbclid=IwY2xjawIKqzxleHRuA2FlbQIxMAABHaf9GldgYOzXktNuoRtNKqd-aL7r-S7zPGyC8ttYOiG2zYfQqLyV4Qm75A_aem_0wKLC2C87ZZ2F04WjPJbtA)
62
- """)
63
-
64
  # User input for text, language, and voice settings
65
  input_text = st.text_area("Enter your text here", placeholder="The sky above the port was the color of television...")
66
  lang_code = st.selectbox("Select Language", ['a', 'b', 'e', 'f', 'h', 'i', 'p', 'z', 'j'])
@@ -87,6 +84,9 @@ speed = st.slider("Speed", min_value=0.5, max_value=2.0, value=1.0, step=0.1)
87
  # Initialize the TTS pipeline with user-selected language
88
  pipeline = KPipeline(lang_code=lang_code)
89
 
 
 
 
90
  # Generate Audio function
91
  def generate_audio(text, lang_code, voice, speed):
92
  generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
@@ -99,6 +99,13 @@ def generate_audio(text, lang_code, voice, speed):
99
  buffer.seek(0)
100
  return buffer
101
 
 
 
 
 
 
 
 
102
  # Generate and display the audio file
103
  if st.button('Generate Audio'):
104
  st.write("Generating speech...")
@@ -109,8 +116,26 @@ if st.button('Generate Audio'):
109
 
110
  # Optional: Save the generated audio file for download
111
  st.download_button(
112
- label="Download Audio",
113
  data=audio_buffer,
114
- file_name="generated_speech.wav",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  mime="audio/wav"
116
- )
 
3
  import soundfile as sf
4
  import io
5
  import os
6
+ from googletrans import Translator # We will use googletrans for translation
7
 
8
  # Install espeak-ng if not installed
9
  # if not os.system("which espeak-ng"):
 
16
  st.title("Text-to-Speech with Kokoro")
17
  # Expander section to display information in multiple languages
18
  with st.expander("Sample Prompt!"):
19
+ st.markdown("""
20
  - My name is Shukdev. (In English)
21
  - Mi nombre es Shukdev. (In Spanish)
22
  - Je m'appelle Choukdev. (In French)
 
58
  Enjoy experimenting with the text-to-speech conversion, and feel free to try different voices, speeds, and languages!
59
  """)
60
 
 
 
 
 
61
  # User input for text, language, and voice settings
62
  input_text = st.text_area("Enter your text here", placeholder="The sky above the port was the color of television...")
63
  lang_code = st.selectbox("Select Language", ['a', 'b', 'e', 'f', 'h', 'i', 'p', 'z', 'j'])
 
84
  # Initialize the TTS pipeline with user-selected language
85
  pipeline = KPipeline(lang_code=lang_code)
86
 
87
+ # Initialize translator
88
+ translator = Translator()
89
+
90
  # Generate Audio function
91
  def generate_audio(text, lang_code, voice, speed):
92
  generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
 
99
  buffer.seek(0)
100
  return buffer
101
 
102
+ # Translate text to English
103
+ def translate_to_english(text, lang_code):
104
+ if lang_code != 'a': # Only translate if it's not already English
105
+ translation = translator.translate(text, src=lang_code, dest='en')
106
+ return translation.text
107
+ return text
108
+
109
  # Generate and display the audio file
110
  if st.button('Generate Audio'):
111
  st.write("Generating speech...")
 
116
 
117
  # Optional: Save the generated audio file for download
118
  st.download_button(
119
+ label="Download Audio (Original Text)",
120
  data=audio_buffer,
121
+ file_name="generated_speech_original.wav",
122
+ mime="audio/wav"
123
+ )
124
+
125
+ # Translate the input text to English
126
+ translated_text = translate_to_english(input_text, lang_code)
127
+
128
+ # Generate audio for the translated English text
129
+ translated_audio_buffer = generate_audio(translated_text, 'a', voice, speed)
130
+
131
+ # Display Audio for the translated text
132
+ st.write(f"Translated Text: {translated_text}")
133
+ st.audio(translated_audio_buffer, format='audio/wav')
134
+
135
+ # Download option for the translated audio
136
+ st.download_button(
137
+ label="Download Audio (Translated to English)",
138
+ data=translated_audio_buffer,
139
+ file_name="generated_speech_translated.wav",
140
  mime="audio/wav"
141
+ )