# pip install transformers torch scipy uroman
import streamlit as st
from transformers import VitsModel, AutoTokenizer
import torch
import scipy.io
import os
import base64
# 페이지 설정
st.set_page_config(
page_title="한국어 TTS",
page_icon="🎵",
layout="centered"
)
# CSS 스타일 수정
st.markdown("""
""", unsafe_allow_html=True)
# 헤더
st.markdown('
🎵 한국어 음성 생성기
', unsafe_allow_html=True)
# 소개 섹션
with st.container():
st.markdown('
✨ AI가 당신의 텍스트를 자연스러운 음성으로 변환해드립니다
', unsafe_allow_html=True)
# 모델 로드
@st.cache_resource
def load_model():
model = VitsModel.from_pretrained("facebook/mms-tts-kor")
tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-kor")
return model, tokenizer
# 모델 로드
model, tokenizer = load_model()
# 메인 입력 섹션
st.markdown("""
✏️ 아래 입력창에 변환할 텍스트를 입력하세요
""", unsafe_allow_html=True)
text = st.text_area(
"", # 레이블 제거
placeholder="예시) 안녕하세요. 텍스트를 음성으로 변환해드립니다.",
value="",
height=100
)
st.markdown('
', unsafe_allow_html=True)
def generate_audio(text):
inputs = tokenizer(text, return_tensors="pt")
with torch.no_grad():
output = model(**inputs).waveform
return output[0].numpy(), model.config.sampling_rate
def get_binary_file_downloader_html(bin_file, file_label='File'):
with open(bin_file, 'rb') as f:
data = f.read()
bin_str = base64.b64encode(data).decode()
href = f''
return href
# 사이드바에 사용방법 추가
with st.sidebar:
st.markdown("""
💡 사용 방법
간단 3단계로 음성을 생성하세요:
1️⃣ 텍스트 입력창에 원하는 한글 텍스트를 입력합니다.
2️⃣ '음성 생성하기' 버튼을 클릭합니다.
3️⃣ 생성된 음성을 재생하거나 다운로드 받으세요.
""", unsafe_allow_html=True)
# 버튼 정렬 개선
st.markdown('
', unsafe_allow_html=True)
if st.button("🔊 음성 생성하기", key="generate"):
if text:
with st.spinner("🎵 음성을 생성하는 중..."):
# 음성 생성
audio_array, sample_rate = generate_audio(text)
# WAV 파일로 저장
filepath = "output.wav"
scipy.io.wavfile.write(filepath, rate=sample_rate, data=audio_array)
# 결과 컨테이너
result_container = st.container()
with result_container:
st.markdown('
', unsafe_allow_html=True)
st.success("✅ 음성 생성 완료! 플레이 버튼을 눌러 재생해보세요.")
# 오디오 재생 위젯 표시
st.audio(filepath)
# 다운로드 버튼
with open(filepath, "rb") as file:
st.download_button(
label="💾 음성 파일 다운로드",
data=file,
file_name="output.wav",
mime="audio/wav",
use_container_width=True
)
st.markdown('