Spaces:
Paused
Paused
| import os | |
| import torch | |
| import tempfile | |
| import time | |
| from huggingface_hub import hf_hub_download | |
| import torchaudio | |
| class HFHubTTS: | |
| """Hugging Face Hub์์ ์ง์ TTS ๋ชจ๋ธ์ ๋ก๋ํ๋ ํด๋์ค""" | |
| def __init__(self, model_id="facebook/mms-tts-eng"): | |
| """ | |
| Hugging Face Hub์์ TTS ๋ชจ๋ธ ์ด๊ธฐํ | |
| Args: | |
| model_id: ์ฌ์ฉํ ๋ชจ๋ธ ID (๊ธฐ๋ณธ๊ฐ: "facebook/mms-tts-eng") | |
| """ | |
| self.available = False | |
| self.model_id = model_id | |
| try: | |
| from transformers import pipeline | |
| print(f"[HF Hub TTS] ๋ชจ๋ธ ๋ก๋ ์ค: {model_id}") | |
| start_time = time.time() | |
| # ๋ชจ๋ธ ๋ก๋ | |
| self.tts = pipeline("text-to-speech", model=model_id) | |
| elapsed = time.time() - start_time | |
| self.available = True | |
| print(f"[HF Hub TTS] ๋ชจ๋ธ '{model_id}' ๋ก๋ ์ฑ๊ณต! ({elapsed:.1f}์ด)") | |
| except Exception as e: | |
| print(f"[HF Hub TTS] ๋ชจ๋ธ '{model_id}' ๋ก๋ ์คํจ: {e}") | |
| self.available = False | |
| def generate_speech(self, text, output_path): | |
| """ | |
| ํ ์คํธ๋ฅผ ์์ฑ์ผ๋ก ๋ณํ | |
| Args: | |
| text: ๋ณํํ ํ ์คํธ | |
| output_path: ์ ์ฅํ ํ์ผ ๊ฒฝ๋ก | |
| Returns: | |
| bool: ์ฑ๊ณต ์ฌ๋ถ | |
| """ | |
| if not self.available: | |
| return False | |
| try: | |
| # ํ ์คํธ ๊ฒ์ฆ | |
| if not text or len(text.strip()) == 0: | |
| print("[HF Hub TTS] ๋น ํ ์คํธ๊ฐ ์ ๋ ฅ๋์์ต๋๋ค.") | |
| return False | |
| print(f"[HF Hub TTS] ์์ฑ ์์ฑ ์์: '{text[:50]}{'...' if len(text) > 50 else ''}'") | |
| start_time = time.time() | |
| # ์์ฑ ์์ฑ | |
| speech = self.tts(text) | |
| # ํ์ผ ์ ์ฅ | |
| with open(output_path, "wb") as f: | |
| f.write(speech["audio"]) | |
| elapsed = time.time() - start_time | |
| # ๊ฒฐ๊ณผ ํ์ธ | |
| if os.path.exists(output_path) and os.path.getsize(output_path) > 0: | |
| print(f"[HF Hub TTS] ์์ฑ ์์ฑ ์ฑ๊ณต: {output_path} ({elapsed:.1f}์ด)") | |
| return True | |
| else: | |
| print(f"[HF Hub TTS] ์์ฑ ํ์ผ์ด ์์ฑ๋์ง ์์์ต๋๋ค.") | |
| return False | |
| except Exception as e: | |
| print(f"[HF Hub TTS] ์์ฑ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {e}") | |
| return False |