shukdevdatta123 commited on
Commit
9934dbc
·
verified ·
1 Parent(s): 474d5c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -19
app.py CHANGED
@@ -1,14 +1,15 @@
1
  import streamlit as st
 
2
  from kokoro import KPipeline
3
  import soundfile as sf
4
  import io
5
- import requests
6
 
7
  # Streamlit App UI Setup
8
  st.title("Text-to-Speech with Kokoro")
 
9
  # Expander section to display information in multiple languages
10
  with st.expander("Sample Prompt!"):
11
- st.markdown("""
12
  - My name is Shukdev. (In English)
13
  - Mi nombre es Shukdev. (In Spanish)
14
  - Je m'appelle Choukdev. (In French)
@@ -77,22 +78,30 @@ speed = st.slider("Speed", min_value=0.5, max_value=2.0, value=1.0, step=0.1)
77
  # Initialize the TTS pipeline with user-selected language
78
  pipeline = KPipeline(lang_code=lang_code)
79
 
80
- # LibreTranslate API for translation
81
- LIBRETRANSLATE_URL = "https://libretranslate.com/translate"
82
 
83
- # Translate text to English using LibreTranslate API
84
- def translate_to_english(text, lang_code):
85
- response = requests.post(LIBRETRANSLATE_URL, data={
86
- 'q': text,
87
- 'source': lang_code,
88
- 'target': 'en'
89
- })
90
 
91
- if response.status_code == 200:
92
- return response.json()['translatedText']
93
- else:
94
- st.error(f"Translation failed. Status Code: {response.status_code}")
95
- return text # Return original text in case of failure
 
 
 
 
 
 
 
 
 
96
 
97
  # Generate Audio function
98
  def generate_audio(text, lang_code, voice, speed):
@@ -107,7 +116,7 @@ def generate_audio(text, lang_code, voice, speed):
107
  return buffer
108
 
109
  # Generate and display the audio file
110
- if st.button('Generate Audio'):
111
  st.write("Generating speech for the original text...")
112
  audio_buffer = generate_audio(input_text, lang_code, voice, speed)
113
 
@@ -122,8 +131,8 @@ if st.button('Generate Audio'):
122
  mime="audio/wav"
123
  )
124
 
125
- # Translate the input text to English using LibreTranslate
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)
 
1
  import streamlit as st
2
+ import openai
3
  from kokoro import KPipeline
4
  import soundfile as sf
5
  import io
 
6
 
7
  # Streamlit App UI Setup
8
  st.title("Text-to-Speech 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)
 
78
  # Initialize the TTS pipeline with user-selected language
79
  pipeline = KPipeline(lang_code=lang_code)
80
 
81
+ # Function to get the OpenAI API key from the user
82
+ openai_api_key = st.text_input("Enter your OpenAI API Key:", type="password")
83
 
84
+ # Function to translate text to English using OpenAI's Chat API
85
+ def translate_to_english(api_key, text, lang_code):
86
+ openai.api_key = api_key
87
+ try:
88
+ # Construct the prompt for translation
89
+ prompt = f"Translate the following text from {lang_code} to English: \n\n{text}"
 
90
 
91
+ response = openai.ChatCompletion.create(
92
+ model="gpt-4o", # Using ChatGPT model for translation
93
+ messages=[
94
+ {"role": "system", "content": "You are a helpful assistant that translates text."},
95
+ {"role": "user", "content": prompt}
96
+ ]
97
+ )
98
+
99
+ # Extract translated text from response
100
+ translated_text = response['choices'][0]['message']['content'].strip()
101
+ return translated_text
102
+ except Exception as e:
103
+ st.error(f"Error occurred during translation: {e}")
104
+ return text # Fallback to original text in case of an error
105
 
106
  # Generate Audio function
107
  def generate_audio(text, lang_code, voice, speed):
 
116
  return buffer
117
 
118
  # Generate and display the audio file
119
+ if st.button('Generate Audio') and openai_api_key:
120
  st.write("Generating speech for the original text...")
121
  audio_buffer = generate_audio(input_text, lang_code, voice, speed)
122
 
 
131
  mime="audio/wav"
132
  )
133
 
134
+ # Translate the input text to English using OpenAI
135
+ translated_text = translate_to_english(openai_api_key, input_text, lang_code)
136
 
137
  # Generate audio for the translated English text
138
  translated_audio_buffer = generate_audio(translated_text, 'a', voice, speed)