Amelia-James commited on
Commit
561e8cb
·
verified ·
1 Parent(s): 3c33e15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -11
app.py CHANGED
@@ -1,15 +1,22 @@
1
  import streamlit as st
2
- import numpy as np
3
  import os
4
- import soundfile as sf
5
  from TTS.api import TTS
6
- import torch
7
  from io import BytesIO
 
8
 
9
  # Set up the model for text-to-speech (TTS)
10
  MODEL_NAME = "tts_models/en/ljspeech/tacotron2-DDC" # Example TTS model; adjust as needed
11
  tts = TTS(model_name=MODEL_NAME, progress_bar=True, gpu=False)
12
 
 
 
 
 
 
 
 
 
13
  # Function to load audio file
14
  def load_audio(file):
15
  audio_data, sample_rate = sf.read(file)
@@ -24,23 +31,40 @@ def save_audio(output_audio, sample_rate):
24
  # Streamlit app
25
  def main():
26
  st.title("Voice Cloning Tool")
27
- st.markdown("Upload a voice input, and get the cloned voice output.")
28
 
29
  # File upload
30
- audio_file = st.file_uploader("Upload your audio file", type=["wav", "mp3","mp4"])
31
-
32
  if audio_file is not None:
33
- st.audio(audio_file, format="audio/wav")
34
-
35
- # Load audio file
36
- audio_data, sample_rate = load_audio(audio_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  # Perform voice cloning (This assumes your TTS model supports some form of input)
39
  try:
40
  st.text("Processing your input...")
41
  output_audio = tts.tts(audio_data) # Pass the audio to your TTS model for cloning
42
  output_path = save_audio(output_audio, sample_rate)
43
-
44
  # Provide download link
45
  st.audio(output_path, format="audio/wav")
46
  st.markdown(f"[Download Cloned Voice](/{output_path})")
 
1
  import streamlit as st
 
2
  import os
 
3
  from TTS.api import TTS
4
+ import moviepy.editor as mp
5
  from io import BytesIO
6
+ import soundfile as sf
7
 
8
  # Set up the model for text-to-speech (TTS)
9
  MODEL_NAME = "tts_models/en/ljspeech/tacotron2-DDC" # Example TTS model; adjust as needed
10
  tts = TTS(model_name=MODEL_NAME, progress_bar=True, gpu=False)
11
 
12
+ # Function to extract audio from MP4 file
13
+ def extract_audio_from_mp4(mp4_file):
14
+ video = mp.VideoFileClip(mp4_file)
15
+ audio = video.audio
16
+ audio_file = "temp_audio.wav"
17
+ audio.write_audiofile(audio_file)
18
+ return audio_file
19
+
20
  # Function to load audio file
21
  def load_audio(file):
22
  audio_data, sample_rate = sf.read(file)
 
31
  # Streamlit app
32
  def main():
33
  st.title("Voice Cloning Tool")
34
+ st.markdown("Upload an MP4, WAV, or MP3 file, and get the cloned voice output.")
35
 
36
  # File upload
37
+ audio_file = st.file_uploader("Upload your audio file", type=["wav", "mp3", "mp4"])
38
+
39
  if audio_file is not None:
40
+ # Handle MP4 file by extracting audio
41
+ if audio_file.type == "video/mp4":
42
+ # Save the uploaded file to a temporary location
43
+ with open("uploaded_video.mp4", "wb") as f:
44
+ f.write(audio_file.getbuffer())
45
+
46
+ # Extract audio from MP4
47
+ audio_path = extract_audio_from_mp4("uploaded_video.mp4")
48
+ st.audio(audio_path, format="audio/wav")
49
+
50
+ # Load audio for TTS processing
51
+ audio_data, sample_rate = load_audio(audio_path)
52
+ else:
53
+ # For audio files directly
54
+ st.audio(audio_file, format=f"audio/{audio_file.type.split('/')[1]}")
55
+
56
+ # Load audio file
57
+ with open("temp_audio.wav", "wb") as f:
58
+ f.write(audio_file.getbuffer())
59
+
60
+ audio_data, sample_rate = load_audio("temp_audio.wav")
61
 
62
  # Perform voice cloning (This assumes your TTS model supports some form of input)
63
  try:
64
  st.text("Processing your input...")
65
  output_audio = tts.tts(audio_data) # Pass the audio to your TTS model for cloning
66
  output_path = save_audio(output_audio, sample_rate)
67
+
68
  # Provide download link
69
  st.audio(output_path, format="audio/wav")
70
  st.markdown(f"[Download Cloned Voice](/{output_path})")