|
import gradio as gr |
|
import numpy as np |
|
import tempfile |
|
import os |
|
from gtts import gTTS |
|
import librosa |
|
def text_to_speech(text, language="ko"): |
|
"""Convert text to speech using Google TTS""" |
|
if not text.strip(): |
|
return None, None |
|
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as fp: |
|
temp_filename = fp.name |
|
tts = gTTS(text=text, lang=language, slow=False) |
|
tts.save(temp_filename) |
|
|
|
y, sr = librosa.load(temp_filename, sr=None) |
|
return temp_filename, (sr, y) |
|
def clear_outputs(): |
|
return None, None |
|
css = """ |
|
@keyframes pulse { |
|
0% { |
|
box-shadow: 0 0 0 0 rgba(255, 215, 0, 0.7); |
|
} |
|
70% { |
|
box-shadow: 0 0 0 10px rgba(255, 215, 0, 0); |
|
} |
|
100% { |
|
box-shadow: 0 0 0 0 rgba(255, 215, 0, 0); |
|
} |
|
} |
|
.playing-audio { |
|
animation: pulse 1.5s infinite; |
|
border: 2px solid #FFD700 !important; |
|
background-color: rgba(255, 215, 0, 0.1) !important; |
|
} |
|
/* μ μΈλ μ€νμΌ μμ± λ°μ€ */ |
|
.gradio-audio { |
|
background: linear-gradient(135deg, #50C878, #4CBB17) !important; |
|
border-radius: 15px !important; |
|
padding: 15px !important; |
|
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2) !important; |
|
transition: all 0.3s ease !important; |
|
} |
|
.gradio-audio:hover { |
|
transform: translateY(-5px) !important; |
|
box-shadow: 0 12px 25px rgba(0, 0, 0, 0.25) !important; |
|
} |
|
/* μ€λμ€ νλ μ΄μ΄ μ€νμΌλ§ */ |
|
audio { |
|
border-radius: 30px !important; |
|
background-color: rgba(255, 255, 255, 0.2) !important; |
|
width: 100% !important; |
|
} |
|
/* λ μ΄λΈ μ€νμΌ */ |
|
.gradio-audio label { |
|
color: white !important; |
|
font-weight: bold !important; |
|
font-size: 1.1em !important; |
|
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3) !important; |
|
margin-bottom: 10px !important; |
|
} |
|
/* μμ± νν 컨ν
μ΄λ */ |
|
.gradio-audio .waveform-container { |
|
background-color: rgba(255, 255, 255, 0.1) !important; |
|
border-radius: 10px !important; |
|
padding: 5px !important; |
|
} |
|
/* λΉ¨κ°μ λ²νΌ μ€νμΌ */ |
|
.red-button { |
|
background-color: #FF0000 !important; |
|
color: white !important; |
|
border: none !important; |
|
font-weight: bold !important; |
|
} |
|
.red-button:hover { |
|
background-color: #CC0000 !important; |
|
} |
|
""" |
|
with gr.Blocks(theme=gr.themes.Soft(primary_hue="green"), css=css) as demo: |
|
gr.Markdown("# νκ΅μ΄ ν
μ€νΈ μμ± λ³ν (TTS)") |
|
with gr.Row(): |
|
with gr.Column(scale=3): |
|
text_input = gr.Textbox( |
|
placeholder="λ³νν ν
μ€νΈλ₯Ό μ
λ ₯νμΈμ...", |
|
label="ν
μ€νΈ μ
λ ₯", |
|
lines=5 |
|
) |
|
with gr.Row(): |
|
lang_selector = gr.Dropdown( |
|
choices=["ko", "en", "ja", "zh-CN", "fr", "es", "de"], |
|
value="ko", |
|
label="μΈμ΄ μ ν", |
|
info="ko: νκ΅μ΄, en: μμ΄, ja: μΌλ³Έμ΄, zh-CN: μ€κ΅μ΄, fr: νλμ€μ΄, es: μ€νμΈμ΄, de: λ
μΌμ΄" |
|
) |
|
btn = gr.Button("μμ± μ°½μ‘°", elem_classes=["red-button"]) |
|
clear_btn = gr.Button("μ΄κΈ°ν") |
|
with gr.Column(scale=3): |
|
audio_output = gr.Audio(label="μμ±λ μμ±", type="filepath") |
|
waveform = gr.Audio(label="νν", type="numpy", visible=True) |
|
|
|
audio_js = """ |
|
function setupAudioAnimation() { |
|
const audioElements = document.querySelectorAll('audio'); |
|
audioElements.forEach(audio => { |
|
audio.addEventListener('play', () => { |
|
const audioContainer = audio.closest('.gradio-audio'); |
|
if (audioContainer) { |
|
audioContainer.classList.add('playing-audio'); |
|
} |
|
}); |
|
audio.addEventListener('pause', () => { |
|
const audioContainer = audio.closest('.gradio-audio'); |
|
if (audioContainer) { |
|
audioContainer.classList.remove('playing-audio'); |
|
} |
|
}); |
|
audio.addEventListener('ended', () => { |
|
const audioContainer = audio.closest('.gradio-audio'); |
|
if (audioContainer) { |
|
audioContainer.classList.remove('playing-audio'); |
|
} |
|
}); |
|
}); |
|
} |
|
// Setup initial elements |
|
setupAudioAnimation(); |
|
// Setup observer for dynamically added elements |
|
const observer = new MutationObserver((mutations) => { |
|
for (const mutation of mutations) { |
|
if (mutation.addedNodes.length) { |
|
setupAudioAnimation(); |
|
} |
|
} |
|
}); |
|
observer.observe(document.body, { childList: true, subtree: true }); |
|
""" |
|
demo.load(None, js=audio_js) |
|
btn.click( |
|
fn=text_to_speech, |
|
inputs=[text_input, lang_selector], |
|
outputs=[audio_output, waveform] |
|
) |
|
clear_btn.click( |
|
fn=clear_outputs, |
|
inputs=[], |
|
outputs=[audio_output, waveform] |
|
) |
|
gr.Markdown(""" |
|
### μ¬μ© λ°©λ² |
|
1. λ³ννκ³ μΆμ ν
μ€νΈλ₯Ό μ
λ ₯νμΈμ |
|
2. μΈμ΄λ₯Ό μ ννμΈμ (κΈ°λ³Έ: νκ΅μ΄) |
|
3. 'μμ± μ°½μ‘°' λ²νΌμ ν΄λ¦νμΈμ |
|
4. μμ±λ μμ±μ λ€μ΄λ³΄μΈμ |
|
""") |
|
|
|
if __name__ == '__main__': |
|
demo.launch() |