SatyamSinghal commited on
Commit
50192f2
·
verified ·
1 Parent(s): 480ea91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -28
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  from gtts import gTTS
3
- import os
4
  from io import BytesIO
5
 
6
  def text_to_speech(text, language="en"):
@@ -20,7 +19,7 @@ def text_to_speech(text, language="en"):
20
  except Exception as e:
21
  return f"Error: {str(e)}", None
22
 
23
- # Define the HTML content with embedded JavaScript and CSS
24
  html_content = """
25
  <!DOCTYPE html>
26
  <html lang="en">
@@ -169,11 +168,15 @@ html_content = """
169
  // Show loading status
170
  statusDiv.innerHTML = "Converting... Please wait.";
171
 
172
- // Simulate API call with setTimeout (replace this with your real API call)
173
- setTimeout(() => {
174
- // Here, you should call your backend and get the audio file
175
- let audioBlob = new Blob([new ArrayBuffer(1024)], { type: 'audio/mp3' }); // Mock blob for demo
176
- audioPlayer.src = URL.createObjectURL(audioBlob);
 
 
 
 
177
  audioPlayer.style.display = "block";
178
 
179
  // Show success popup using SweetAlert2
@@ -182,30 +185,19 @@ html_content = """
182
  title: 'Conversion Successful!',
183
  text: 'Your text has been converted to speech.',
184
  });
185
- }, 2000); // Simulating delay of 2 seconds
 
 
 
186
  }
187
  </script>
188
  </body>
189
  </html>
190
  """
191
 
192
- # Gradio interface setup
193
- interface = gr.Interface(
194
- fn=text_to_speech,
195
- inputs=[
196
- gr.Textbox(label="Enter Text", placeholder="Type your text here..."),
197
- gr.Dropdown(choices=["en", "es", "fr", "de"], label="Select Language", value="en"),
198
- ],
199
- outputs=[
200
- gr.Textbox(label="Status"),
201
- gr.Audio(label="Generated Speech"),
202
- ],
203
- title="AI Text-to-Speech Web App",
204
- description="Convert text into natural-sounding speech using AI. Select a language, type text, and download the audio!",
205
- allow_flagging="never",
206
- live=True,
207
- )
208
-
209
- # Launch the interface
210
- if __name__ == "__main__":
211
- interface.launch()
 
1
  import gradio as gr
2
  from gtts import gTTS
 
3
  from io import BytesIO
4
 
5
  def text_to_speech(text, language="en"):
 
19
  except Exception as e:
20
  return f"Error: {str(e)}", None
21
 
22
+ # HTML content with animations, styling, and JavaScript
23
  html_content = """
24
  <!DOCTYPE html>
25
  <html lang="en">
 
168
  // Show loading status
169
  statusDiv.innerHTML = "Converting... Please wait.";
170
 
171
+ // Call the backend to get the audio file (we simulate it here)
172
+ fetch('/convert', {
173
+ method: 'POST',
174
+ body: JSON.stringify({ text: text, language: language }),
175
+ headers: { 'Content-Type': 'application/json' }
176
+ })
177
+ .then(response => response.blob())
178
+ .then(blob => {
179
+ audioPlayer.src = URL.createObjectURL(blob);
180
  audioPlayer.style.display = "block";
181
 
182
  // Show success popup using SweetAlert2
 
185
  title: 'Conversion Successful!',
186
  text: 'Your text has been converted to speech.',
187
  });
188
+ })
189
+ .catch(error => {
190
+ statusDiv.innerHTML = "Error: Could not convert text to speech.";
191
+ });
192
  }
193
  </script>
194
  </body>
195
  </html>
196
  """
197
 
198
+ # Gradio interface setup for integrating frontend
199
+ def gradio_interface():
200
+ return gr.HTML(html_content)
201
+
202
+ interface = gr.Interface(fn=text_to_speech, inputs=[gr.Textbox(), gr.Dropdown()], outputs=[gr.Textbox(), gr.Audio()])
203
+ interface.launch()