Shifterr commited on
Commit
479b63b
·
verified ·
1 Parent(s): d247428

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -19
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
  from TTS.api import TTS
3
  import os
 
 
4
 
5
  # 1. Load Model
6
  device = "cuda" if os.environ.get("CUDA_VISIBLE_DEVICES") else "cpu"
@@ -12,27 +14,30 @@ except Exception as e:
12
  print(f"Error loading model: {e}")
13
  tts = None
14
 
15
- def clone_voice(text, speaker_file_bytes):
 
 
 
 
16
  print(f"--- NEW REQUEST ---")
17
 
18
- if speaker_file_bytes is None:
19
- print("Error: Received NONE for audio.")
20
- return None, "Error: Upload failed. No data received."
21
 
22
- # DEBUG: Print the type to see what we actually got
23
- print(f"Received type: {type(speaker_file_bytes)}")
 
24
 
25
  if not tts:
26
  return None, "Error: Model failed to load."
27
 
28
- # 2. MANUALLY SAVE THE FILE
29
- # Since we are asking for 'binary', we get bytes. We must save them to disk for XTTS to read.
30
  temp_wav_path = "temp_reference.wav"
31
  output_path = "output.wav"
32
 
33
  try:
34
- with open(temp_wav_path, "wb") as f:
35
- f.write(speaker_file_bytes)
36
  print(f"Saved reference audio to {temp_wav_path}")
37
  except Exception as e:
38
  return None, f"Error saving file: {e}"
@@ -53,21 +58,21 @@ def clone_voice(text, speaker_file_bytes):
53
 
54
  # 3. UI Setup
55
  with gr.Blocks() as demo:
56
- gr.Markdown("# PLOT Voice Engine (Binary Upload Mode)")
57
 
58
  with gr.Row():
59
  with gr.Column():
60
- text_input = gr.Textbox(label="Text", value="Testing binary upload.")
61
 
62
- # THE FIX: type="binary"
63
- # This bypasses the temp-file logic that is breaking.
64
- audio_input = gr.File(
65
- label="Upload Voice Sample",
66
- type="binary",
67
- file_types=[".wav", ".mp3"]
68
  )
69
 
70
- submit_btn = gr.Button("Generate", variant="primary")
71
 
72
  with gr.Column():
73
  audio_output = gr.Audio(label="Result")
 
1
  import gradio as gr
2
  from TTS.api import TTS
3
  import os
4
+ import numpy as np
5
+ import scipy.io.wavfile
6
 
7
  # 1. Load Model
8
  device = "cuda" if os.environ.get("CUDA_VISIBLE_DEVICES") else "cpu"
 
14
  print(f"Error loading model: {e}")
15
  tts = None
16
 
17
+ def clone_voice(text, audio_data):
18
+ """
19
+ audio_data comes in as a tuple: (sample_rate, numpy_array)
20
+ We must manually save it to a WAV file for XTTS to read.
21
+ """
22
  print(f"--- NEW REQUEST ---")
23
 
24
+ if audio_data is None:
25
+ return None, "Error: No audio provided. Please record or upload."
 
26
 
27
+ # Unpack the raw audio data from Gradio
28
+ sample_rate, audio_array = audio_data
29
+ print(f"Received Audio: Rate={sample_rate}, Shape={audio_array.shape}")
30
 
31
  if not tts:
32
  return None, "Error: Model failed to load."
33
 
34
+ # 2. MANUALLY SAVE THE WAV FILE
35
+ # This bypasses the 'filepath=None' bug by doing it ourselves.
36
  temp_wav_path = "temp_reference.wav"
37
  output_path = "output.wav"
38
 
39
  try:
40
+ scipy.io.wavfile.write(temp_wav_path, sample_rate, audio_array)
 
41
  print(f"Saved reference audio to {temp_wav_path}")
42
  except Exception as e:
43
  return None, f"Error saving file: {e}"
 
58
 
59
  # 3. UI Setup
60
  with gr.Blocks() as demo:
61
+ gr.Markdown("# PLOT Voice Engine (Numpy Mode)")
62
 
63
  with gr.Row():
64
  with gr.Column():
65
+ text_input = gr.Textbox(label="Text to Speak", value="This is a test of the cloning system.")
66
 
67
+ # THE FIX: Restore gr.Audio but use type="numpy"
68
+ # This brings back the Microphone + Upload UI
69
+ audio_input = gr.Audio(
70
+ label="Reference Voice (Record or Upload)",
71
+ type="numpy", # <--- Receives raw data, not a path
72
+ sources=["microphone", "upload"]
73
  )
74
 
75
+ submit_btn = gr.Button("Generate Voice", variant="primary")
76
 
77
  with gr.Column():
78
  audio_output = gr.Audio(label="Result")