File size: 6,911 Bytes
a3b1df2
2eaa44a
 
8c4b3e2
2eaa44a
c9b6f29
 
55ba65d
 
 
 
c9b6f29
 
2eaa44a
 
 
 
 
 
 
 
 
 
 
 
8c4b3e2
2eaa44a
 
 
 
 
fde2e87
8c4b3e2
 
2eaa44a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c4b3e2
 
 
 
 
 
 
 
 
93b51e2
8c4b3e2
 
 
 
 
 
15d99f5
8c4b3e2
 
 
 
908cc3c
8c4b3e2
 
 
 
2eaa44a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1e6aff
2eaa44a
 
 
 
 
 
 
c9b6f29
 
 
55ba65d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9b6f29
55ba65d
 
 
 
 
 
2eaa44a
55ba65d
 
c9b6f29
 
 
 
55ba65d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9b6f29
55ba65d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9b6f29
55ba65d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9b6f29
55ba65d
 
 
 
c9b6f29
 
55ba65d
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import gradio as gr
import os
import torch
from huggingface_hub import InferenceClient


# Khurram
# from fastapi import FastAPI, Query
# from pydantic import BaseModel
# import uvicorn
# from fastapi.responses import JSONResponse
#################

# Import eSpeak TTS pipeline
from tts_cli import (
    build_model as build_model_espeak,
    generate_long_form_tts as generate_long_form_tts_espeak,
)

# Import OpenPhonemizer TTS pipeline
from tts_cli_op import (
    build_model as build_model_open,
    generate_long_form_tts as generate_long_form_tts_open,
)
from pretrained_models import Kokoro
#
# ---------------------------------------------------------------------
# Path to models and voicepacks
# ---------------------------------------------------------------------
MODELS_DIR = "pretrained_models/Kokoro"
VOICES_DIR = "pretrained_models/Kokoro/voices"
HF_TOKEN = os.getenv("HF_TOKEN")
client = InferenceClient(api_key=HF_TOKEN)


# ---------------------------------------------------------------------
# List the models (.pth) and voices (.pt)
# ---------------------------------------------------------------------
def get_models():
    return sorted([f for f in os.listdir(MODELS_DIR) if f.endswith(".pth")])


def get_voices():
    return sorted([f for f in os.listdir(VOICES_DIR) if f.endswith(".pt")])


# ---------------------------------------------------------------------
# We'll map engine selection -> (build_model_func, generate_func)
# ---------------------------------------------------------------------
ENGINES = {
    "espeak": (build_model_espeak, generate_long_form_tts_espeak),
    "openphonemizer": (build_model_open, generate_long_form_tts_open),
}


# ---------------------------------------------------------------------
# The main inference function called by Gradio
# ---------------------------------------------------------------------
def tts_inference(text, engine, model_file, voice_file, speed=1.0):
    """
    text:        Input string
    engine:      "espeak" or "openphonemizer"
    model_file:  Selected .pth from the models folder
    voice_file:  Selected .pt from the voices folder
    speed:       Speech speed
    """

    # 0) Get the response of user query from LLAMA 

    messages = [
	{
		"role": "user",
		"content": [
			{
				"type": "text",
				"text": text + str('describe in one line only')
			} #,
			# {
			# 	"type": "image_url",
			# 	"image_url": {
			# 		"url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
			# 	}
			# }
            ]
	}
    ]

    response_from_llama = client.chat.completions.create(
    model="meta-llama/Llama-3.2-11B-Vision-Instruct", 
	messages=messages, 
	max_tokens=500)
    
    # 1) Map engine to the correct build_model + generate_long_form_tts
    build_fn, gen_fn = ENGINES[engine]

    # 2) Prepare paths
    model_path = os.path.join(MODELS_DIR, model_file)
    voice_path = os.path.join(VOICES_DIR, voice_file)

    # 3) Decide device
    device = "cuda" if torch.cuda.is_available() else "cpu"

    # 4) Load model
    model = build_fn(model_path, device=device)
    # Set submodules eval
    for k, subm in model.items():
        if hasattr(subm, "eval"):
            subm.eval()

    # 5) Load voicepack
    voicepack = torch.load(voice_path, map_location=device)
    if hasattr(voicepack, "eval"):
        voicepack.eval()

    # 6) Generate TTS
    audio, phonemes = gen_fn(model, response_from_llama.choices[0].message['content'], voicepack, speed=speed)
    sr = 22050  # or your actual sample rate

    return (sr, audio)  # Gradio expects (sample_rate, np_array)




#------------------------------------------
# FAST API 
#---------------
# app = FastAPI()

# class TTSRequest(BaseModel):
#     text: str
#     engine: str
#     model_file: str
#     voice_file: str
#     speed: float = 1.0

# @app.post("/tts")
# def generate_tts(request: TTSRequest):
#     try:
#         sr, audio = tts_inference(
#             text="What is Deep SeEK? define in 2 lines",
#             engine="openphonemizer",
#             model_file="kokoro-v0_19.pth",
#             voice_file="af_bella.pt",
#             speed=1.0
#         )
        
#         return JSONResponse(content={
#             "sample_rate": sr,
#             "audio_tensor": audio.tolist()
#         })
#     except Exception as e:
#         return JSONResponse(content={"error": str(e)}, status_code=500)

# if __name__ == "__main__":
#     uvicorn.run(app, host="0.0.0.0", port=8000)


###############################

# ---------------------------------------------------------------------
# Build Gradio App
# ---------------------------------------------------------------------
def create_gradio_app():
    model_list = get_models()
    voice_list = get_voices()

    css = """
    h4 {
        text-align: center;
        display:block;
    }
    h2 {
        text-align: center;
        display:block;
    }
    """
    with gr.Blocks(theme=gr.themes.Ocean(), css=css) as demo:
        gr.Markdown("## LLAMA TTS DEMO - API - GRADIO VISUAL")

        # Row 1: Text input
        text_input = gr.Textbox(
            label="Enter your question",
            value="What is AI?",
            lines=2,
        )

        # Row 2: Engine selection
        # engine_dropdown = gr.Dropdown(
        #     choices=["espeak", "openphonemizer"],
        #     value="openphonemizer",
        #     label="Phonemizer",
        # )

        # Row 3: Model dropdown
        # model_dropdown = gr.Dropdown(
        #     choices=model_list,
        #     value=model_list[0] if model_list else None,
        #     label="Model (.pth)",
        # )

        # Row 4: Voice dropdown
        # voice_dropdown = gr.Dropdown(
        #     choices=voice_list,
        #     value=voice_list[0] if voice_list else None,
        #     label="Voice (.pt)",
        # )

        # Row 5: Speed slider
        speed_slider = gr.Slider(
            minimum=0.5, maximum=2.0, value=1.0, step=0.1, label="Speech Speed"
        )

        # Generate button + audio output
        generate_btn = gr.Button("Generate")
        tts_output = gr.Audio(label="TTS Output")

        # Connect the button to our inference function
        generate_btn.click(
            fn=tts_inference,
            inputs=[
                text_input,
                gr.State("openphonemizer"), #engine_dropdown, 
                gr.State("kokoro-v0_19.pth"), #model_dropdown,
                gr.State("af_bella.pt"), #voice_dropdown,
                speed_slider,
            ],
            outputs=tts_output,
        )

        gr.Markdown(
            "#### LLAMA - TTS"
        )
    return demo


# ---------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------
if __name__ == "__main__":
    app = create_gradio_app()
    app.launch()