bgtts / app.py
englissi's picture
Update app.py
2252ea3 verified
raw
history blame
1.9 kB
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
from TTS.api import TTS # Coqui TTS 라이브러리
# λͺ¨λΈ 이름
model_name = "mradermacher/SambaLingo-Bulgarian-Base-i1-GGUF"
# λͺ¨λΈ 및 ν† ν¬λ‚˜μ΄μ € λ‘œλ“œ
try:
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
except Exception as e:
raise RuntimeError(f"λͺ¨λΈ λ‘œλ“œ 쀑 λ¬Έμ œκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}")
# TTS λͺ¨λΈ λ‘œλ“œ (Coqui TTS)
try:
tts = TTS(model_name="tts_models/bg/cv/vits", progress_bar=False)
except Exception as e:
raise RuntimeError(f"TTS λͺ¨λΈ λ‘œλ“œ 쀑 λ¬Έμ œκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}")
# ν…μŠ€νŠΈ 생성 및 μŒμ„± λ³€ν™˜ ν•¨μˆ˜
def generate_audio(input_text):
# ν…μŠ€νŠΈ 생성
try:
inputs = tokenizer.encode(input_text, return_tensors="pt")
outputs = model.generate(inputs, max_length=100, num_return_sequences=1)
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
except Exception as e:
return f"ν…μŠ€νŠΈ 생성 쀑 λ¬Έμ œκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}", None
# TTS λ³€ν™˜
try:
audio_path = "output.wav"
tts.tts_to_file(text=generated_text, file_path=audio_path)
return generated_text, audio_path
except Exception as e:
return f"TTS λ³€ν™˜ 쀑 λ¬Έμ œκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}", None
# Gradio μΈν„°νŽ˜μ΄μŠ€ 생성
interface = gr.Interface(
fn=generate_audio,
inputs=gr.Textbox(lines=5, label="λΆˆκ°€λ¦¬μ•„μ–΄ ν…μŠ€νŠΈ μž…λ ₯"),
outputs=[gr.Textbox(label="μƒμ„±λœ ν…μŠ€νŠΈ"), gr.Audio(label="μƒμ„±λœ μŒμ„±")],
title="λΆˆκ°€λ¦¬μ•„μ–΄ ν…μŠ€νŠΈ 생성 및 μŒμ„± λ³€ν™˜",
description="λΆˆκ°€λ¦¬μ•„μ–΄ ν…μŠ€νŠΈλ₯Ό μž…λ ₯ν•˜λ©΄ ν…μŠ€νŠΈλ₯Ό μƒμ„±ν•˜κ³  μŒμ„±μœΌλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€."
)
# μ•± μ‹€ν–‰
if __name__ == "__main__":
interface.launch()