File size: 3,531 Bytes
b04ae4b
 
389d4d2
 
 
 
 
 
 
 
 
 
b04ae4b
c3846dd
 
 
 
b04ae4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389d4d2
b04ae4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3846dd
 
 
 
b04ae4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import sys

# Mock spaces if running locally to support @spaces.GPU decorator
try:
    import spaces
except ImportError:
    class MockSpaces:
        def GPU(self, func):
            return func
    sys.modules["spaces"] = MockSpaces()
    import spaces
import wave
try:
    import pyaudio
except ImportError:
    pyaudio = None

# Ensure custom f5_tts from sooktam2 is resolvable
repo_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "sooktam2")
sys.path.append(repo_dir)
sys.path.append(os.path.join(repo_dir, "src"))

from transformers import AutoModel

# Load the model globally so it doesn't reload on every request
MODEL_ID = "bharatgenai/sooktam2"
print(f"Loading TTS Model: {MODEL_ID}...")
try:
    import torch
    device = "cuda" if torch.cuda.is_available() else ("mps" if torch.backends.mps.is_available() else "cpu")
    model_tts = AutoModel.from_pretrained(
        MODEL_ID,
        trust_remote_code=True,
    ).to(device)
    print(f"TTS Model loaded successfully and moved to {device}.")
except Exception as e:
    print(f"Error loading TTS model: {e}")
    model_tts = None

# We need a reference audio and text for Sooktam-2
# Using the same ones from gen_voice.py
REF_AUDIO = "ishita_tts_audio.mp3 (1) (1).wav" # Ensure this exists locally or update path
REF_TEXT = "வணக்கம், ஜென் லேப் ல இருந்து பேசுறேன். டெஸ்ட் புக் பண்ணணுமா இல்ல அப்பாயின்ட்மென்ட் ஷெட்யூல் பண்ணணுமா?"

OUT_DIR = "outputs"
os.makedirs(OUT_DIR, exist_ok=True)

@spaces.GPU
def synthesize_speech(text: str, output_filename: str = "response.wav"):
    if not model_tts:
        print("TTS model is not loaded. Cannot synthesize speech.")
        return None

    out_wav_path = os.path.join(OUT_DIR, output_filename)
    
    # Check if reference audio exists
    if not os.path.exists(REF_AUDIO):
        print(f"Warning: Reference audio {REF_AUDIO} not found.")
        # Try to find a fallback or just pass it anyway, inference might fail
        
    print(f"Synthesizing speech: '{text}'")
    try:
        wav, sr, _ = model_tts.infer(
            ref_file=REF_AUDIO,
            ref_text=REF_TEXT,
            gen_text=text,
            tokenizer="cls",
            cls_language="tamil", # Can be made dynamic if needed
            file_wave=out_wav_path,
        )
        print(f"Speech saved to: {out_wav_path}")
        return out_wav_path
    except Exception as e:
        print(f"Error during TTS synthesis: {e}")
        return None

def play_audio(file_path: str):
    if not pyaudio:
        print("pyaudio is not installed. Cannot play audio locally.")
        return

    if not file_path or not os.path.exists(file_path):
        print(f"Cannot play audio: file {file_path} not found.")
        return
        
    try:
        wf = wave.open(file_path, 'rb')
        p = pyaudio.PyAudio()
        
        stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                        channels=wf.getnchannels(),
                        rate=wf.getframerate(),
                        output=True)
                        
        data = wf.readframes(1024)
        while len(data) > 0:
            stream.write(data)
            data = wf.readframes(1024)
            
        stream.stop_stream()
        stream.close()
        p.terminate()
    except Exception as e:
        print(f"Error playing audio: {e}")