Spaces:
Running
Running
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() | |