VoiceCloning / src /streamlit_app.py
Osmanerendgn's picture
Update src/streamlit_app.py
9bbcad0 verified
import os
import tempfile
import streamlit as st
from TTS.api import TTS
st.set_page_config(page_title="XTTS v2 (TR) - Streamlit", layout="centered")
st.title("🎙️ XTTS v2 Türkçe TTS (Voice Cloning)")
@st.cache_resource
def load_tts():
return TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False, progress_bar=True)
tts_model = load_tts()
text = st.text_area(
"Metin",
height=180,
value="Merhaba! Bu bir deneme metnidir."
)
speaker_file = st.file_uploader(
"Speaker WAV yükle (voice cloning için)",
type=["wav"]
)
generate = st.button("🔊 Ses üret", type="primary")
if generate:
if not text.strip():
st.warning("Lütfen bir metin gir.")
st.stop()
if speaker_file is None:
st.warning("Lütfen bir speaker WAV dosyası yükle.")
st.stop()
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as spk_tmp:
spk_tmp.write(speaker_file.getbuffer())
speaker_wav_path = spk_tmp.name
out_tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
out_path = out_tmp.name
out_tmp.close()
try:
with st.spinner("Üretiliyor..."):
tts_model.tts_to_file(
text=text,
speaker_wav=speaker_wav_path,
language="tr",
file_path=out_path,
split_sentences=False
)
with open(out_path, "rb") as f:
audio_bytes = f.read()
st.success("Hazır ✅")
st.audio(audio_bytes, format="audio/wav")
st.download_button(
"⬇️ WAV indir",
data=audio_bytes,
file_name="tts_output.wav",
mime="audio/wav"
)
finally:
for p in [speaker_wav_path, out_path]:
try:
if os.path.exists(p):
os.remove(p)
except Exception:
pass