hamza2923 commited on
Commit
6bf1c66
·
verified ·
1 Parent(s): 30d7bcd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -28
app.py CHANGED
@@ -1,39 +1,57 @@
1
  import gradio as gr
2
  import pyttsx3
3
  import os
 
4
 
5
- # Initialize the TTS engine
6
- engine = pyttsx3.init()
 
 
7
 
8
- # Get available voices
9
- voices = engine.getProperty('voices')
 
 
 
 
 
 
 
 
 
10
 
11
- # Create a dictionary to map voice names to IDs
12
- voice_map = {f"{voice.name} ({voice.languages[0] if voice.languages else 'Unknown'})": voice.id for voice in voices}
13
 
14
  def text_to_speech(text, voice_name, rate=200):
15
  """
16
  Convert text to speech with selected voice and rate.
17
  Saves output as an audio file and returns the file path.
18
  """
19
- # Initialize engine for each call to avoid threading issues with Gradio
20
- engine = pyttsx3.init()
21
-
22
- # Set voice
23
- voice_id = voice_map.get(voice_name)
24
- if not voice_id:
25
- return "Error: Selected voice not found."
26
- engine.setProperty('voice', voice_id)
27
-
28
- # Set speech rate
29
- engine.setProperty('rate', rate)
30
-
31
- # Save audio to a file
32
- output_file = "output.wav"
33
- engine.save_to_file(text, output_file)
34
- engine.runAndWait()
35
-
36
- return output_file
 
 
 
 
 
 
37
 
38
  # Gradio interface
39
  with gr.Blocks(title="Text-to-Speech with Different Voices") as demo:
@@ -41,7 +59,7 @@ with gr.Blocks(title="Text-to-Speech with Different Voices") as demo:
41
  gr.Markdown("Enter text and select a voice to convert it to speech with different voices and accents.")
42
 
43
  text_input = gr.Textbox(label="Enter Text", placeholder="Type your text here...")
44
- voice_dropdown = gr.Dropdown(choices=list(voice_map.keys()), label="Select Voice/Accent")
45
  rate_slider = gr.Slider(minimum=100, maximum=300, value=200, step=10, label="Speech Rate")
46
 
47
  convert_button = gr.Button("Convert to Speech")
@@ -53,8 +71,5 @@ with gr.Blocks(title="Text-to-Speech with Different Voices") as demo:
53
  outputs=audio_output
54
  )
55
 
56
- # Launch the app (commented out for Hugging Face Spaces deployment)
57
- # demo.launch()
58
-
59
  if __name__ == "__main__":
60
  demo.launch()
 
1
  import gradio as gr
2
  import pyttsx3
3
  import os
4
+ import warnings
5
 
6
+ # Suppress pygame welcome message
7
+ with warnings.catch_warnings():
8
+ warnings.simplefilter("ignore")
9
+ import pygame
10
 
11
+ def get_voices():
12
+ """Initialize engine and get available voices with error handling"""
13
+ try:
14
+ engine = pyttsx3.init()
15
+ voices = engine.getProperty('voices')
16
+ return {f"{voice.name} ({voice.languages[0] if voice.languages else 'Unknown'})": voice.id
17
+ for voice in voices}
18
+ except Exception as e:
19
+ print(f"Error initializing TTS engine: {e}")
20
+ # Return a default voice if engine fails
21
+ return {"Default Voice": None}
22
 
23
+ # Get available voices
24
+ voice_map = get_voices()
25
 
26
  def text_to_speech(text, voice_name, rate=200):
27
  """
28
  Convert text to speech with selected voice and rate.
29
  Saves output as an audio file and returns the file path.
30
  """
31
+ try:
32
+ # Initialize engine for each call
33
+ engine = pyttsx3.init()
34
+
35
+ # Set voice if available
36
+ voice_id = voice_map.get(voice_name)
37
+ if voice_id:
38
+ engine.setProperty('voice', voice_id)
39
+
40
+ # Set speech rate
41
+ engine.setProperty('rate', rate)
42
+
43
+ # Save audio to a file
44
+ output_file = "output.wav"
45
+ engine.save_to_file(text, output_file)
46
+ engine.runAndWait()
47
+
48
+ # Ensure file exists before returning
49
+ if os.path.exists(output_file):
50
+ return output_file
51
+ return None
52
+ except Exception as e:
53
+ print(f"Error in text_to_speech: {e}")
54
+ return None
55
 
56
  # Gradio interface
57
  with gr.Blocks(title="Text-to-Speech with Different Voices") as demo:
 
59
  gr.Markdown("Enter text and select a voice to convert it to speech with different voices and accents.")
60
 
61
  text_input = gr.Textbox(label="Enter Text", placeholder="Type your text here...")
62
+ voice_dropdown = gr.Dropdown(choices=list(voice_map.keys()), label="Select Voice/Accent", value=list(voice_map.keys())[0] if voice_map else None)
63
  rate_slider = gr.Slider(minimum=100, maximum=300, value=200, step=10, label="Speech Rate")
64
 
65
  convert_button = gr.Button("Convert to Speech")
 
71
  outputs=audio_output
72
  )
73
 
 
 
 
74
  if __name__ == "__main__":
75
  demo.launch()