shukdevdatta123 commited on
Commit
8792f10
·
verified ·
1 Parent(s): cfec890

Create abc3.txt

Browse files
Files changed (1) hide show
  1. abc3.txt +172 -0
abc3.txt ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import openai
3
+ from kokoro import KPipeline
4
+ import soundfile as sf
5
+ import io
6
+ import time
7
+
8
+ # Streamlit App UI Setup
9
+ st.title("Text-to-Speech Translator with Kokoro")
10
+
11
+ # Expander section to display information in multiple languages
12
+ with st.expander("Sample Prompt!"):
13
+ st.markdown("""
14
+ - My name is Shukdev. (In English)
15
+ - Mi nombre es Shukdev. (In Spanish)
16
+ - Je m'appelle Choukdev. (In French)
17
+ - मेरा नाम शुकदेव है. (In Hindi)
18
+ - Il mio nome è Shukdev. (In Italy)
19
+ - Meu nome é Sukhdev. (In Portuguese, Brazil)
20
+ - 我叫苏赫德夫。(In Chinese)
21
+ - 私の名前はスクデフです。(In Japanese)
22
+ """)
23
+
24
+ st.sidebar.markdown("""
25
+ ### Courtesy: [Kokoro](https://huggingface.co/hexgrad/Kokoro-82M?fbclid=IwY2xjawIKqzxleHRuA2FlbQIxMAABHaf9GldgYOzXktNuoRtNKqd-aL7r-S7zPGyC8ttYOiG2zYfQqLyV4Qm75A_aem_0wKLC2C87ZZ2F04WjPJbtA)
26
+ """)
27
+
28
+ st.sidebar.header("Configuration & Instructions")
29
+
30
+ st.sidebar.markdown("""
31
+ ### How to Use the Text-to-Speech App:
32
+ 1. **Enter Text**:
33
+ - Type or paste the text you want to convert to speech in the main text area.
34
+
35
+ 2. **Select Language**:
36
+ - Choose the language of the input text. The available language options include:
37
+ - 🇺🇸 American English (`a`)
38
+ - 🇬🇧 British English (`b`)
39
+ - 🇪🇸 Spanish (`e`)
40
+ - 🇫🇷 French (`f`)
41
+ - 🇮🇳 Hindi (`h`)
42
+ - 🇮🇹 Italian (`i`)
43
+ - 🇧🇷 Brazilian Portuguese (`p`)
44
+ - 🇨🇳 Mandarin Chinese (`z`)
45
+ - 🇯🇵 Japanese (`j`)
46
+ 3. **Select Voice**:
47
+ - 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.).
48
+
49
+ 4. **Adjust Speech Speed**:
50
+ - 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.
51
+ 5. **Generate Speech**:
52
+ - 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.
53
+ 6. **Download Audio**:
54
+ - 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.
55
+ ### Additional Features:
56
+ - **Text Translation**:
57
+ - If you enter text in another language and want to hear it in English, provide your OpenAI API key (optional).
58
+ - The app will automatically translate the text to English and generate the speech in English with the voice you selected.
59
+ - Enjoy exploring different languages, voices, and speeds with the text-to-speech conversion!
60
+ """)
61
+
62
+ # User input for text, language, and voice settings
63
+ input_text = st.text_area("Enter your text here", placeholder="The sky above the port was the color of television...")
64
+ lang_code = st.selectbox("Select Language", ['a', 'b', 'e', 'f', 'h', 'i', 'p', 'z', 'j'])
65
+ voice = st.selectbox("Select Voice", ['af_alloy', 'af_aoede', 'af_bella', 'af_heart', 'af_jessica', 'af_kore', 'af_nicole', 'af_nova', 'af_river', 'af_sarah', 'af_sky',
66
+ 'am_adam', 'am_echo', 'am_eric', 'am_fenrir', 'am_liam', 'am_michael', 'am_onyx', 'am_puck', 'am_santa',
67
+ 'bf_alice', 'bf_emma', 'bf_isabella', 'bf_lily',
68
+ 'bm_daniel', 'bm_fable', 'bm_george', 'bm_lewis',
69
+ 'ef_dora',
70
+ 'em_alex', 'em_santa',
71
+ 'ff_siwis',
72
+ 'hf_alpha', 'hf_beta',
73
+ 'hm_omega', 'hm_psi',
74
+ 'if_sara',
75
+ 'im_nicola',
76
+ 'jf_alpha', 'jf_gongitsune', 'jf_nezumi', 'jf_tebukuro',
77
+ 'jm_kumo',
78
+ 'pf_dora',
79
+ 'pm_alex', 'pm_santa',
80
+ 'zf_xiaobei', 'zf_xiaoni', 'zf_xiaoxiao', 'zf_xiaoyi',
81
+ 'zm_yunjian', 'zm_yunxi', 'zm_yunxia', 'zm_yunyang']
82
+ ) # Change voice options as per model
83
+ speed = st.slider("Speed", min_value=0.5, max_value=2.0, value=1.0, step=0.1)
84
+
85
+ # Initialize the TTS pipeline with user-selected language
86
+ pipeline = KPipeline(lang_code=lang_code)
87
+
88
+ # Function to get the OpenAI API key from the user (optional for translation)
89
+ openai_api_key = st.text_input("Enter your OpenAI API Key (Optional for Translation)", type="password")
90
+
91
+ # Function to translate text to English using OpenAI's Chat API
92
+ def translate_to_english(api_key, text, lang_code):
93
+ openai.api_key = api_key
94
+ try:
95
+ # Construct the prompt for translation
96
+ prompt = f"Translate the following text from {lang_code} to English: \n\n{text}"
97
+
98
+ response = openai.ChatCompletion.create(
99
+ model="gpt-4", # Using ChatGPT model for translation
100
+ messages=[{"role": "system", "content": "You are a helpful assistant that translates text."},
101
+ {"role": "user", "content": prompt}]
102
+ )
103
+
104
+ # Extract translated text from response, removing any additional context or prefixes
105
+ translated_text = response['choices'][0]['message']['content'].strip()
106
+
107
+ # Clean up any unwanted prefixes or context
108
+ if translated_text.lower().startswith("the translated text"):
109
+ translated_text = translated_text.split(":", 1)[1].strip()
110
+
111
+ return translated_text
112
+ except Exception as e:
113
+ st.error(f"Error occurred during translation: {e}")
114
+ return text # Fallback to original text in case of an error
115
+
116
+ # Generate Audio function
117
+ def generate_audio(text, lang_code, voice, speed):
118
+ generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
119
+ audio_data = None
120
+ for i, (gs, ps, audio) in enumerate(generator):
121
+ audio_data = audio
122
+ time.sleep(1) # Simulate processing delay for the spinner (this can be removed or adjusted)
123
+ # Save audio to in-memory buffer
124
+ buffer = io.BytesIO()
125
+ # Explicitly specify format as WAV
126
+ sf.write(buffer, audio_data, 24000, format='WAV') # Add 'format="WAV"'
127
+ buffer.seek(0)
128
+ return buffer
129
+
130
+ # Generate and display the audio file
131
+ if st.button('Generate Audio'):
132
+
133
+ # Show the spinner with percentage count
134
+ with st.spinner("Generating audio... 0%"):
135
+ for i in range(1, 101, 10):
136
+ time.sleep(0.5) # Simulate work being done, you can adjust timing here
137
+ st.spinner(f"Generating audio... {i}%")
138
+
139
+ # Generate audio for the original text after the spinner
140
+ st.write("Generating speech for the original text...")
141
+ audio_buffer = generate_audio(input_text, lang_code, voice, speed)
142
+
143
+ # Display Audio player for the original language
144
+ st.audio(audio_buffer, format='audio/wav')
145
+
146
+ # Optional: Save the generated audio file for download (Original Text)
147
+ st.download_button(
148
+ label="Download Audio (Original Text)",
149
+ data=audio_buffer,
150
+ file_name="generated_speech_original.wav",
151
+ mime="audio/wav"
152
+ )
153
+
154
+ # Check if OpenAI API Key is provided for translation and English audio generation
155
+ if openai_api_key:
156
+ # Translate the input text to English using OpenAI
157
+ translated_text = translate_to_english(openai_api_key, input_text, lang_code)
158
+
159
+ # Generate audio for the translated English text
160
+ translated_audio_buffer = generate_audio(translated_text, 'a', voice, speed)
161
+
162
+ # Display Audio for the translated text
163
+ st.write(f"Translated Text: {translated_text}")
164
+ st.audio(translated_audio_buffer, format='audio/wav')
165
+
166
+ # Optional: Save the generated audio file for download (Translated Text)
167
+ st.download_button(
168
+ label="Download Audio (Translated to English)",
169
+ data=translated_audio_buffer,
170
+ file_name="generated_speech_translated.wav",
171
+ mime="audio/wav"
172
+ )