MrHalk commited on
Commit
9bb31a0
Β·
verified Β·
1 Parent(s): 0e4e7cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -80
app.py CHANGED
@@ -1,80 +1,74 @@
1
- import streamlit as st
2
- import pyttsx3
3
- from gtts import gTTS
4
- import os
5
- import librosa
6
- import soundfile as sf
7
- import time
8
- import numpy as np
9
- from io import BytesIO
10
-
11
- # Function to Convert Text to Speech
12
- def text_to_speech(text, engine='offline', gender='male'):
13
- filename = f"audio_{int(time.time())}.wav"
14
- if engine == 'offline':
15
- tts_engine = pyttsx3.init()
16
- tts_engine.setProperty('rate', 160) # Adjust speed
17
- tts_engine.setProperty('volume', 2.0) # Max volume
18
- voices = tts_engine.getProperty('voices')
19
-
20
- if gender == 'male':
21
- tts_engine.setProperty('voice', voices[0].id) # Male voice
22
- else:
23
- tts_engine.setProperty('voice', voices[1].id) # Female voice
24
-
25
- tts_engine.save_to_file(text, filename)
26
- tts_engine.runAndWait()
27
- elif engine == 'online':
28
- tts = gTTS(text=text, lang='en', slow=False)
29
- filename = f"audio_{int(time.time())}.mp3"
30
- tts.save(filename)
31
- return filename
32
-
33
- # Function to Modify Voice Pitch
34
- def change_voice(input_file, gender="male"):
35
- y, sr = librosa.load(input_file, sr=44100)
36
- pitch_factor = -3 if gender == "male" else 5 # Adjust pitch naturally
37
- y_shifted = librosa.effects.pitch_shift(y, n_steps=pitch_factor, sr=sr)
38
- output_file = f"modified_{int(time.time())}.wav"
39
- sf.write(output_file, y_shifted, sr)
40
- return output_file
41
-
42
- # Streamlit UI
43
- st.set_page_config(page_title="🎀 Voice Generator App", layout="centered")
44
- st.title("🎀 AI-Powered Voice Generator")
45
- st.markdown("**Convert your text into speech with natural, clear voices!** 🎢")
46
- st.divider()
47
-
48
- st.subheader("Enter Your Text")
49
- text = st.text_area("πŸ“ Type or paste your text below:")
50
-
51
- st.subheader("Choose Voice Engine")
52
- engine = st.radio("βš™οΈ Select a Text-to-Speech Engine:",
53
- ["Offline (Fast, Customizable)", "Online (Natural, Requires Internet)"])
54
- engine_mode = "offline" if "Offline" in engine else "online"
55
-
56
- st.subheader("Select Voice Type")
57
- gender = st.radio("🎭 Choose Voice Gender:", ["Male", "Female"])
58
-
59
- if st.button("πŸŽ™οΈ Generate Voice"):
60
- if text:
61
- st.info("⏳ Processing your request... Please wait.")
62
- audio_file = text_to_speech(text, engine_mode, gender.lower())
63
- modified_audio = change_voice(audio_file, gender.lower())
64
-
65
- st.success("βœ… Voice generated successfully!")
66
- st.audio(modified_audio, format='audio/wav')
67
-
68
- with open(modified_audio, "rb") as file:
69
- audio_bytes = file.read()
70
-
71
- st.download_button(label="⬇️ Download Audio", data=audio_bytes,
72
- file_name=modified_audio, mime="audio/wav")
73
- else:
74
- st.warning("⚠️ Please enter text to generate voice.")
75
-
76
- st.markdown("---")
77
- st.caption("πŸ”Ή **Offline Mode** uses your system's built-in voices and works without the internet.")
78
- st.caption("πŸ”Ή **Online Mode** uses Google TTS for a more natural voice but requires an internet connection.")
79
- st.divider()
80
- st.caption("πŸ‘» So called Arman.")
 
1
+ import streamlit as st
2
+ import edge_tts
3
+ import asyncio
4
+ import librosa
5
+ import soundfile as sf
6
+ import time
7
+ import numpy as np
8
+ from io import BytesIO
9
+
10
+ # Function to Convert Text to Speech with More Voice Options
11
+ async def text_to_speech(text, voice="en-US-GuyNeural"):
12
+ filename = f"audio_{int(time.time())}.mp3"
13
+
14
+ # Use Edge TTS to generate speech
15
+ communicate = edge_tts.Communicate(text, voice)
16
+ await communicate.save(filename)
17
+
18
+ return filename
19
+
20
+ # Function to Modify Voice Pitch
21
+ def change_voice(input_file, gender="male"):
22
+ y, sr = librosa.load(input_file, sr=44100)
23
+ pitch_factor = -3 if gender == "male" else 5 # Adjust pitch naturally
24
+ y_shifted = librosa.effects.pitch_shift(y, n_steps=pitch_factor, sr=sr)
25
+
26
+ output_file = f"modified_{int(time.time())}.wav"
27
+ sf.write(output_file, y_shifted, sr)
28
+
29
+ return output_file
30
+
31
+ # Streamlit UI
32
+ st.set_page_config(page_title="🎀 Voice Generator App", layout="centered")
33
+ st.title("🎀 AI-Powered Voice Generator")
34
+ st.markdown("**Convert your text into speech with natural, clear voices!** 🎢")
35
+ st.divider()
36
+
37
+ # User Input for Text
38
+ st.subheader("Enter Your Text")
39
+ text = st.text_area("πŸ“ Type or paste your text below:")
40
+
41
+ # Select Voice Type
42
+ st.subheader("Select Voice Type")
43
+ voice_options = {
44
+ "Male (US)": "en-US-GuyNeural",
45
+ "Male (UK)": "en-GB-RyanNeural",
46
+ "Female (US)": "en-US-JennyNeural",
47
+ "Female (UK)": "en-GB-SoniaNeural"
48
+ }
49
+ selected_voice = st.selectbox("🎭 Choose a Voice:", list(voice_options.keys()))
50
+
51
+ if st.button("πŸŽ™οΈ Generate Voice"):
52
+ if text:
53
+ st.info("⏳ Processing your request... Please wait.")
54
+
55
+ # Generate Speech
56
+ audio_file = asyncio.run(text_to_speech(text, voice_options[selected_voice]))
57
+
58
+ st.success("βœ… Voice generated successfully!")
59
+ st.audio(audio_file, format='audio/mp3')
60
+
61
+ # Allow users to download the generated audio
62
+ with open(audio_file, "rb") as file:
63
+ audio_bytes = file.read()
64
+
65
+ st.download_button(label="⬇️ Download Audio", data=audio_bytes,
66
+ file_name=audio_file, mime="audio/mp3")
67
+ else:
68
+ st.warning("⚠️ Please enter text to generate voice.")
69
+
70
+ st.markdown("---")
71
+ st.caption("πŸ”Ή **Supports high-quality male & female voices with different accents.**")
72
+ st.caption("πŸ”Ή **Runs offline using Edge TTS for fast voice generation.**")
73
+ st.divider()
74
+ st.caption("πŸ‘» So called Arman.")