Shifterr commited on
Commit
8bf81de
·
verified ·
1 Parent(s): 195bbda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -24
app.py CHANGED
@@ -12,59 +12,67 @@ except Exception as e:
12
  print(f"Error loading model: {e}")
13
  tts = None
14
 
15
- def clone_voice(text, speaker_wav_path):
16
  print(f"--- NEW REQUEST ---")
17
- print(f"Text Input: {text}")
18
- print(f"Audio Path Received: {speaker_wav_path}")
 
 
 
 
 
19
 
20
  if not tts:
21
  return None, "Error: Model failed to load."
22
 
23
- if not text:
24
- return None, "Error: Please enter text."
25
-
26
- if not speaker_wav_path:
27
- return None, "Error: No audio file found. Please upload a WAV/MP3 file."
28
-
29
  output_path = "output.wav"
30
 
31
  try:
32
- # Generate audio
 
 
 
 
 
 
 
33
  tts.tts_to_file(
34
  text=text,
35
  file_path=output_path,
36
- speaker_wav=speaker_wav_path,
37
  language="en"
38
  )
39
- print(f"Success! Generated: {output_path}")
40
  return output_path, "Success"
41
 
42
  except Exception as e:
43
  print(f"CRASH: {e}")
44
  return None, f"Error: {str(e)}"
45
 
46
- # 2. Robust UI Layout (Blocks)
47
  with gr.Blocks() as demo:
48
- gr.Markdown("# PLOT Voice Engine (XTTS v2)")
49
 
50
  with gr.Row():
51
  with gr.Column():
52
- text_input = gr.Textbox(label="Text to Speak", value="Hello, this is my cloned voice.")
53
 
54
- # EXPLICIT SOURCES: Allows both upload and microphone
55
- audio_input = gr.Audio(
56
- label="Reference Audio (6-10s)",
57
- type="filepath",
58
- sources=["upload", "microphone"]
 
59
  )
60
 
61
- submit_btn = gr.Button("Generate Voice", variant="primary")
62
 
63
  with gr.Column():
64
  audio_output = gr.Audio(label="Result")
65
- status_output = gr.Textbox(label="Status", interactive=False)
66
 
67
- # 3. Explicit Event Wiring
68
  submit_btn.click(
69
  fn=clone_voice,
70
  inputs=[text_input, audio_input],
@@ -72,5 +80,4 @@ with gr.Blocks() as demo:
72
  )
73
 
74
  if __name__ == "__main__":
75
- # Disable SSR to fix upload bugs
76
  demo.launch(ssr_mode=False)
 
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}"
39
+
40
+ try:
41
+ # Generate audio using our manually saved file
42
  tts.tts_to_file(
43
  text=text,
44
  file_path=output_path,
45
+ speaker_wav=temp_wav_path,
46
  language="en"
47
  )
 
48
  return output_path, "Success"
49
 
50
  except Exception as e:
51
  print(f"CRASH: {e}")
52
  return None, f"Error: {str(e)}"
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")
74
+ status_output = gr.Textbox(label="Status")
75
 
 
76
  submit_btn.click(
77
  fn=clone_voice,
78
  inputs=[text_input, audio_input],
 
80
  )
81
 
82
  if __name__ == "__main__":
 
83
  demo.launch(ssr_mode=False)