szili2011 commited on
Commit
cca9d56
·
verified ·
1 Parent(s): 3e967d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -7
app.py CHANGED
@@ -1,11 +1,13 @@
1
  import numpy as np
2
  import torch
3
  import gradio as gr
 
4
 
5
  # Placeholder for model loading and voice cloning logic
6
  class VoiceCloner:
7
  def __init__(self):
8
  self.model = None
 
9
 
10
  def load_model(self, npz_file):
11
  data = np.load(npz_file)
@@ -13,38 +15,57 @@ class VoiceCloner:
13
  # Initialize your model here with the loaded parameters
14
  self.model = data # Example; replace with your actual model loading code
15
 
16
- def clone_voice(self, audio_file, text=None):
17
  # Implement the logic to clone voice from the uploaded audio file
18
- # and possibly from the text if provided
19
  return audio_file # Placeholder; return processed audio
20
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  # Create the Gradio interface
22
  def create_interface():
23
  cloner = VoiceCloner()
24
 
25
  with gr.Blocks() as demo:
26
- gr.Markdown("## Voice Cloning Application")
27
 
28
  # User uploads their .npz file
29
  npz_file = gr.File(label="Upload Your .npz Voice Model")
30
  audio_input = gr.Audio(source="upload", type="filepath", label="Upload Original Audio")
31
- text_input = gr.Textbox(label="Text Input for TTS (Optional)")
32
- output_audio = gr.Audio(label="Cloned Voice Output")
 
33
 
34
  upload_button = gr.Button("Load Model")
35
 
36
  # Button to clone voice
37
  clone_button = gr.Button("Clone Voice")
38
-
 
 
 
39
  # Load the model when the user uploads the .npz file
40
  def load_and_initialize(npz):
41
  cloner.load_model(npz.name) # Use the file path to load the model
 
42
  return "Model Loaded!"
43
 
44
  upload_button.click(fn=load_and_initialize, inputs=npz_file, outputs="text")
45
 
46
  # Clone the voice when the button is pressed
47
- clone_button.click(fn=cloner.clone_voice, inputs=[audio_input, text_input], outputs=output_audio)
 
 
 
48
 
49
  return demo
50
 
 
1
  import numpy as np
2
  import torch
3
  import gradio as gr
4
+ from TTS.api import TTS # Import TTS library
5
 
6
  # Placeholder for model loading and voice cloning logic
7
  class VoiceCloner:
8
  def __init__(self):
9
  self.model = None
10
+ self.tts = None
11
 
12
  def load_model(self, npz_file):
13
  data = np.load(npz_file)
 
15
  # Initialize your model here with the loaded parameters
16
  self.model = data # Example; replace with your actual model loading code
17
 
18
+ def clone_voice(self, audio_file):
19
  # Implement the logic to clone voice from the uploaded audio file
 
20
  return audio_file # Placeholder; return processed audio
21
 
22
+ def load_tts_model(self):
23
+ # Load a pretrained TTS model
24
+ self.tts = TTS(model_name="tts_models/en/ljspeech/glow-tts") # You can choose a different model if needed
25
+
26
+ def text_to_speech(self, text):
27
+ # Use the loaded TTS model to convert text to speech
28
+ if self.tts is not None:
29
+ output_audio = self.tts.tts(text)
30
+ return output_audio # Return the generated audio
31
+ else:
32
+ return "TTS model not loaded!"
33
+
34
  # Create the Gradio interface
35
  def create_interface():
36
  cloner = VoiceCloner()
37
 
38
  with gr.Blocks() as demo:
39
+ gr.Markdown("## Voice Cloning and TTS Application")
40
 
41
  # User uploads their .npz file
42
  npz_file = gr.File(label="Upload Your .npz Voice Model")
43
  audio_input = gr.Audio(source="upload", type="filepath", label="Upload Original Audio")
44
+ text_input = gr.Textbox(label="Text Input for TTS")
45
+
46
+ output_audio = gr.Audio(label="Cloned Voice Output or TTS Output")
47
 
48
  upload_button = gr.Button("Load Model")
49
 
50
  # Button to clone voice
51
  clone_button = gr.Button("Clone Voice")
52
+
53
+ # Button to convert text to speech
54
+ tts_button = gr.Button("Convert Text to Speech")
55
+
56
  # Load the model when the user uploads the .npz file
57
  def load_and_initialize(npz):
58
  cloner.load_model(npz.name) # Use the file path to load the model
59
+ cloner.load_tts_model() # Load the TTS model
60
  return "Model Loaded!"
61
 
62
  upload_button.click(fn=load_and_initialize, inputs=npz_file, outputs="text")
63
 
64
  # Clone the voice when the button is pressed
65
+ clone_button.click(fn=cloner.clone_voice, inputs=audio_input, outputs=output_audio)
66
+
67
+ # Convert text to speech when the button is pressed
68
+ tts_button.click(fn=cloner.text_to_speech, inputs=text_input, outputs=output_audio)
69
 
70
  return demo
71