Simonlob commited on
Commit
49d918b
·
1 Parent(s): 5523412
Files changed (2) hide show
  1. app.py +27 -3
  2. index.html +4 -0
app.py CHANGED
@@ -36,6 +36,7 @@ except ImportError: # local run — no-op stand-in
36
 
37
  import os # noqa: E402
38
  from pathlib import Path # noqa: E402
 
39
 
40
  import numpy as np # noqa: E402
41
  import scipy.io.wavfile as wavfile # noqa: E402
@@ -74,6 +75,22 @@ def _save_wav(sample_rate: int, wave: np.ndarray) -> str:
74
  return str(path)
75
 
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  app = Server()
78
 
79
 
@@ -82,7 +99,7 @@ app = Server()
82
  def synthesize(
83
  mode: str,
84
  speaker: str,
85
- ref_audio: str,
86
  text: str,
87
  temperature: float,
88
  top_k: float,
@@ -107,9 +124,16 @@ def synthesize(
107
  raise ValueError("Please choose a preset speaker.")
108
  ref_codes = engine.speakers.codes(speaker)
109
  else:
110
- if not ref_audio:
 
 
 
 
 
 
 
111
  raise ValueError("Please record or upload a reference clip.")
112
- ref_codes = engine.encode_reference(ref_audio)
113
 
114
  params = GenerationParams(
115
  temperature=temperature,
 
36
 
37
  import os # noqa: E402
38
  from pathlib import Path # noqa: E402
39
+ from typing import Optional # noqa: E402
40
 
41
  import numpy as np # noqa: E402
42
  import scipy.io.wavfile as wavfile # noqa: E402
 
75
  return str(path)
76
 
77
 
78
+ def _ref_audio_path(ref_audio) -> Optional[str]:
79
+ """Extract the local file path from an ``@app.api()`` file argument.
80
+
81
+ Gradio may deliver the uploaded reference as a ``FileData`` object
82
+ (``.path``), a serialized dict (``{"path": ...}``), or a bare string —
83
+ normalize all three to the temp path librosa/soundfile can open.
84
+ """
85
+ if not ref_audio:
86
+ return None
87
+ if isinstance(ref_audio, str):
88
+ return ref_audio
89
+ if isinstance(ref_audio, dict):
90
+ return ref_audio.get("path")
91
+ return getattr(ref_audio, "path", None)
92
+
93
+
94
  app = Server()
95
 
96
 
 
99
  def synthesize(
100
  mode: str,
101
  speaker: str,
102
+ ref_audio: Optional[FileData],
103
  text: str,
104
  temperature: float,
105
  top_k: float,
 
124
  raise ValueError("Please choose a preset speaker.")
125
  ref_codes = engine.speakers.codes(speaker)
126
  else:
127
+ # ``@app.api()`` uploads the reference to a local temp file and hands
128
+ # us its FileData. Depending on the gradio version this arrives as a
129
+ # ``FileData`` object OR a plain dict — the old ``ref_audio: str``
130
+ # signature passed the whole dict straight into librosa, which is why
131
+ # every clone request crashed with "Invalid file: {...}". Pull the
132
+ # local ``path`` out defensively so both shapes work.
133
+ ref_path = _ref_audio_path(ref_audio)
134
+ if not ref_path:
135
  raise ValueError("Please record or upload a reference clip.")
136
+ ref_codes = engine.encode_reference(ref_path)
137
 
138
  params = GenerationParams(
139
  temperature=temperature,
index.html CHANGED
@@ -1226,6 +1226,10 @@ els.renderBtn.addEventListener("click", async () => {
1226
  // Auto-play once loaded
1227
  audioEl.addEventListener("loadedmetadata", () => audioEl.play(), { once: true });
1228
  }
 
 
 
 
1229
  }
1230
  }
1231
  } catch (err) {
 
1226
  // Auto-play once loaded
1227
  audioEl.addEventListener("loadedmetadata", () => audioEl.play(), { once: true });
1228
  }
1229
+ } else if (event.type === "status" && event.stage === "error") {
1230
+ // Server-side failure (e.g. bad reference clip): surface it instead
1231
+ // of leaving the UI stuck on "Rendering" with the controls dimmed.
1232
+ throw new Error(event.message || "Rendering failed on the server.");
1233
  }
1234
  }
1235
  } catch (err) {