Update app.py
Browse files
app.py
CHANGED
|
@@ -41,11 +41,9 @@ MAX_CHUNKS = int(os.getenv("MAX_CHUNKS", "12"))
|
|
| 41 |
PAUSE_SECONDS = float(os.getenv("PAUSE_SECONDS", "0.15"))
|
| 42 |
DOWNLOAD_TIMEOUT = int(os.getenv("DOWNLOAD_TIMEOUT", "90"))
|
| 43 |
|
| 44 |
-
#
|
| 45 |
ROOT_DIR = Path(__file__).parent
|
| 46 |
-
|
| 47 |
-
ASSETS_DIR.mkdir(exist_ok=True)
|
| 48 |
-
DEFAULT_SPEAKER_PATH = ASSETS_DIR / "default_speaker.wav"
|
| 49 |
|
| 50 |
# Global Cache untuk mengunci embedding karakteristik suara ke dalam RAM
|
| 51 |
CACHED_EMBEDDINGS = {}
|
|
@@ -98,7 +96,7 @@ def _resolve_audio_input(audio_file, audio_url: str):
|
|
| 98 |
except Exception:
|
| 99 |
pass
|
| 100 |
|
| 101 |
-
# Jika parameter audio kosong, otomatis gunakan file suara default
|
| 102 |
if DEFAULT_SPEAKER_PATH.exists():
|
| 103 |
return str(DEFAULT_SPEAKER_PATH)
|
| 104 |
|
|
@@ -147,7 +145,7 @@ def _split_text_safely(text: str, max_chars: int = MAX_CHARS_PER_CHUNK):
|
|
| 147 |
return chunks
|
| 148 |
|
| 149 |
# =====================================================================
|
| 150 |
-
# ENGINE UTAMA
|
| 151 |
# =====================================================================
|
| 152 |
def clone_voice(text: str, audio_file, audio_url: str, progress=gr.Progress(track_tqdm=False)):
|
| 153 |
global CACHED_EMBEDDINGS
|
|
@@ -156,10 +154,10 @@ def clone_voice(text: str, audio_file, audio_url: str, progress=gr.Progress(trac
|
|
| 156 |
if not raw_text:
|
| 157 |
raise gr.Error("Text prompt tidak boleh kosong.")
|
| 158 |
|
| 159 |
-
# Ambil jalur audio (Custom atau Default)
|
| 160 |
prompt_path = _resolve_audio_input(audio_file, audio_url)
|
| 161 |
if not prompt_path:
|
| 162 |
-
raise gr.Error("Suara acuan tidak ditemukan
|
| 163 |
|
| 164 |
chunks = _split_text_safely(raw_text, max_chars=MAX_CHARS_PER_CHUNK)
|
| 165 |
chunks = [ch for ch in chunks if re.search(r'[a-zA-Z0-9]', ch)]
|
|
@@ -230,7 +228,6 @@ def clone_voice(text: str, audio_file, audio_url: str, progress=gr.Progress(trac
|
|
| 230 |
kwargs["text"] = ch
|
| 231 |
wav = model.generate(**kwargs)
|
| 232 |
else:
|
| 233 |
-
# Jalur teraman jika custom kwargs ditolak total oleh internal model
|
| 234 |
wav = model.generate(ch)
|
| 235 |
|
| 236 |
if wav.dim() == 1:
|
|
@@ -264,12 +261,12 @@ def clone_voice(text: str, audio_file, audio_url: str, progress=gr.Progress(trac
|
|
| 264 |
# =====================================================================
|
| 265 |
# INTERFACE DESIGN
|
| 266 |
# =====================================================================
|
| 267 |
-
with gr.Blocks(title="Chatterbox
|
| 268 |
-
gr.Markdown("## EduScanner AI Voice Backend -
|
| 269 |
-
gr.Markdown("
|
| 270 |
|
| 271 |
text_in = gr.Textbox(label="Teks Rangkuman Materi Kuliah", lines=8, placeholder="Ketik teks di sini...")
|
| 272 |
-
wav_in = gr.Audio(label="Opsi Custom Voice (Kosongkan jika ingin pakai suara default
|
| 273 |
url_in = gr.Textbox(label="Opsi URL File Audio Custom", placeholder="https://domain-kamu.com/audio.wav")
|
| 274 |
|
| 275 |
btn = gr.Button("Sintesis Audio Kloning Suara", variant="primary")
|
|
|
|
| 41 |
PAUSE_SECONDS = float(os.getenv("PAUSE_SECONDS", "0.15"))
|
| 42 |
DOWNLOAD_TIMEOUT = int(os.getenv("DOWNLOAD_TIMEOUT", "90"))
|
| 43 |
|
| 44 |
+
# Sesuai request: Suara default ditaruh langsung di root folder (/)
|
| 45 |
ROOT_DIR = Path(__file__).parent
|
| 46 |
+
DEFAULT_SPEAKER_PATH = ROOT_DIR / "default_speaker.wav"
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Global Cache untuk mengunci embedding karakteristik suara ke dalam RAM
|
| 49 |
CACHED_EMBEDDINGS = {}
|
|
|
|
| 96 |
except Exception:
|
| 97 |
pass
|
| 98 |
|
| 99 |
+
# Jika parameter audio kosong, otomatis gunakan file suara default langsung dari root (/)
|
| 100 |
if DEFAULT_SPEAKER_PATH.exists():
|
| 101 |
return str(DEFAULT_SPEAKER_PATH)
|
| 102 |
|
|
|
|
| 145 |
return chunks
|
| 146 |
|
| 147 |
# =====================================================================
|
| 148 |
+
# ENGINE UTAMA
|
| 149 |
# =====================================================================
|
| 150 |
def clone_voice(text: str, audio_file, audio_url: str, progress=gr.Progress(track_tqdm=False)):
|
| 151 |
global CACHED_EMBEDDINGS
|
|
|
|
| 154 |
if not raw_text:
|
| 155 |
raise gr.Error("Text prompt tidak boleh kosong.")
|
| 156 |
|
| 157 |
+
# Ambil jalur audio (Custom atau Default Root)
|
| 158 |
prompt_path = _resolve_audio_input(audio_file, audio_url)
|
| 159 |
if not prompt_path:
|
| 160 |
+
raise gr.Error("Suara acuan default_speaker.wav tidak ditemukan di root server.")
|
| 161 |
|
| 162 |
chunks = _split_text_safely(raw_text, max_chars=MAX_CHARS_PER_CHUNK)
|
| 163 |
chunks = [ch for ch in chunks if re.search(r'[a-zA-Z0-9]', ch)]
|
|
|
|
| 228 |
kwargs["text"] = ch
|
| 229 |
wav = model.generate(**kwargs)
|
| 230 |
else:
|
|
|
|
| 231 |
wav = model.generate(ch)
|
| 232 |
|
| 233 |
if wav.dim() == 1:
|
|
|
|
| 261 |
# =====================================================================
|
| 262 |
# INTERFACE DESIGN
|
| 263 |
# =====================================================================
|
| 264 |
+
with gr.Blocks(title="Chatterbox Flat Root Safe Engine") as demo:
|
| 265 |
+
gr.Markdown("## EduScanner AI Voice Backend - Flat Root Configuration")
|
| 266 |
+
gr.Markdown("Sistem membaca `default_speaker.wav` langsung dari tingkat root (/) folder Space.")
|
| 267 |
|
| 268 |
text_in = gr.Textbox(label="Teks Rangkuman Materi Kuliah", lines=8, placeholder="Ketik teks di sini...")
|
| 269 |
+
wav_in = gr.Audio(label="Opsi Custom Voice (Kosongkan jika ingin pakai suara default Mythia Batford 1:15 di root)", type="filepath")
|
| 270 |
url_in = gr.Textbox(label="Opsi URL File Audio Custom", placeholder="https://domain-kamu.com/audio.wav")
|
| 271 |
|
| 272 |
btn = gr.Button("Sintesis Audio Kloning Suara", variant="primary")
|