shukdevdatta123 commited on
Commit
5740d4f
·
verified ·
1 Parent(s): 642f415

Update abc2.txt

Browse files
Files changed (1) hide show
  1. abc2.txt +45 -38
abc2.txt 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")
@@ -39,14 +39,14 @@ st.sidebar.markdown("""
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.
@@ -79,8 +79,8 @@ speed = st.slider("Speed", min_value=0.5, max_value=2.0, value=1.0, step=0.1)
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
83
- openai_api_key = st.text_input("Enter your OpenAI API Key:", 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,14 +91,17 @@ 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=[
95
- {"role": "system", "content": "You are a helpful assistant that translates text."},
96
- {"role": "user", "content": prompt}
97
- ]
98
  )
99
-
100
- # Extract translated text from response
101
  translated_text = response['choices'][0]['message']['content'].strip()
 
 
 
 
 
102
  return translated_text
103
  except Exception as e:
104
  st.error(f"Error occurred during translation: {e}")
@@ -117,7 +120,9 @@ def generate_audio(text, lang_code, voice, speed):
117
  return buffer
118
 
119
  # Generate and display the audio file
120
- if st.button('Generate Audio') and openai_api_key:
 
 
121
  st.write("Generating speech for the original text...")
122
  audio_buffer = generate_audio(input_text, lang_code, voice, speed)
123
 
@@ -132,20 +137,22 @@ if st.button('Generate Audio') and openai_api_key:
132
  mime="audio/wav"
133
  )
134
 
135
- # Translate the input text to English using OpenAI
136
- translated_text = translate_to_english(openai_api_key, input_text, lang_code)
137
-
138
- # Generate audio for the translated English text
139
- translated_audio_buffer = generate_audio(translated_text, 'a', voice, speed)
140
-
141
- # Display Audio for the translated text
142
- st.write(f"Translated Text: {translated_text}")
143
- st.audio(translated_audio_buffer, format='audio/wav')
144
-
145
- # Optional: Save the generated audio file for download (Translated Text)
146
- st.download_button(
147
- label="Download Audio (Translated to English)",
148
- data=translated_audio_buffer,
149
- file_name="generated_speech_translated.wav",
150
- mime="audio/wav"
151
- )
 
 
 
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")
 
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.
 
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, removing any additional context or prefixes
99
  translated_text = response['choices'][0]['message']['content'].strip()
100
+
101
+ # Clean up any unwanted prefixes or context
102
+ if translated_text.lower().startswith("the translated text"):
103
+ translated_text = translated_text.split(":", 1)[1].strip()
104
+
105
  return translated_text
106
  except Exception as e:
107
  st.error(f"Error occurred during translation: {e}")
 
120
  return buffer
121
 
122
  # Generate and display the audio file
123
+ if st.button('Generate Audio'):
124
+
125
+ # Generate audio for the original text
126
  st.write("Generating speech for the original text...")
127
  audio_buffer = generate_audio(input_text, lang_code, voice, speed)
128
 
 
137
  mime="audio/wav"
138
  )
139
 
140
+ # Check if OpenAI API Key is provided for translation and English audio generation
141
+ if openai_api_key:
142
+ # Translate the input text to English using OpenAI
143
+ translated_text = translate_to_english(openai_api_key, input_text, lang_code)
144
+
145
+ # Generate audio for the translated English text
146
+ translated_audio_buffer = generate_audio(translated_text, 'a', voice, speed)
147
+
148
+ # Display Audio for the translated text
149
+ st.write(f"Translated Text: {translated_text}")
150
+ st.audio(translated_audio_buffer, format='audio/wav')
151
+
152
+ # Optional: Save the generated audio file for download (Translated Text)
153
+ st.download_button(
154
+ label="Download Audio (Translated to English)",
155
+ data=translated_audio_buffer,
156
+ file_name="generated_speech_translated.wav",
157
+ mime="audio/wav"
158
+ )