File size: 13,021 Bytes
c034a74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2195c3
 
 
 
ff704b5
b2195c3
 
 
 
63d6fee
b2195c3
 
 
63d6fee
 
b2195c3
2bb03a8
 
 
 
ff704b5
b2195c3
 
 
 
 
 
 
 
 
 
 
 
 
 
63d6fee
4196bc7
b2195c3
 
4196bc7
 
 
b2195c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bb03a8
b2195c3
 
a318fb7
63d6fee
 
 
 
 
 
 
 
f1c0ddd
63d6fee
60e1507
63d6fee
b2195c3
63d6fee
 
 
 
 
 
b2195c3
 
 
 
63d6fee
b2195c3
 
63d6fee
b2195c3
63d6fee
 
b2195c3
63d6fee
 
b2195c3
63d6fee
 
 
 
 
 
 
 
f1c0ddd
63d6fee
 
 
b2195c3
 
 
 
63d6fee
b2195c3
63d6fee
b2195c3
63d6fee
 
 
 
b2195c3
63d6fee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2195c3
a318fb7
63d6fee
b2195c3
 
 
 
 
 
 
 
 
 
 
908288f
b2195c3
 
 
 
 
 
 
 
 
2bb03a8
b2195c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bb03a8
b2195c3
 
 
 
 
2bb03a8
 
 
b2195c3
 
 
 
 
 
 
2bb03a8
 
63d6fee
 
 
b2195c3
63d6fee
2bb03a8
 
63d6fee
 
 
 
b2195c3
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# from fastapi import FastAPI, Response
# from fastapi.responses import FileResponse
# from kokoro import KPipeline
# import soundfile as sf
# import os
# import numpy as np
# import torch 
# from huggingface_hub import InferenceClient

# def llm_chat_response(text):
#     HF_TOKEN = os.getenv("HF_TOKEN")
#     client = InferenceClient(api_key=HF_TOKEN)
#     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)

#     return response_from_llama.choices[0].message['content']

# app = FastAPI()

# # Initialize pipeline once at startup
# pipeline = KPipeline(lang_code='a')

# @app.post("/generate")
# async def generate_audio(text: str, voice: str = "af_heart", speed: float = 1.0):
    
#     text_reply = llm_chat_response(text)
    
#     # Generate audio
#     generator = pipeline(
#         text_reply, 
#         voice=voice,
#         speed=speed,
#         split_pattern=r'\n+'
#     )
    
#     # # Save first segment only for demo
#     # for i, (gs, ps, audio) in enumerate(generator):
#     #     sf.write(f"output_{i}.wav", audio, 24000)
#     #     return FileResponse(
#     #         f"output_{i}.wav",
#     #         media_type="audio/wav",
#     #         filename="output.wav"
#     #     )
    
#     # return Response("No audio generated", status_code=400)


#     # Process only the first segment for demo
#     for i, (gs, ps, audio) in enumerate(generator):

#         # Convert PyTorch tensor to NumPy array
#         audio_numpy = audio.cpu().numpy()
#         # Convert to 16-bit PCM
        
#         # Ensure the audio is in the range [-1, 1]
#         audio_numpy = np.clip(audio_numpy, -1, 1)
#         # Convert to 16-bit signed integers
#         pcm_data = (audio_numpy * 32767).astype(np.int16)
        
#         # Convert to bytes (automatically uses row-major order)
#         raw_audio = pcm_data.tobytes()
        
#         # Return PCM data with minimal necessary headers
#         return Response(
#             content=raw_audio,
#             media_type="application/octet-stream",
#             headers={
#                 "Content-Disposition": f'attachment; filename="output.pcm"',
#                 "X-Sample-Rate": "24000",
#                 "X-Bits-Per-Sample": "16",
#                 "X-Endianness": "little"
#             }
#         )
    
#     return Response("No audio generated", status_code=400)

from fastapi import FastAPI, Response, HTTPException
from fastapi.responses import FileResponse, JSONResponse
from kokoro import KPipeline
import soundfile as sf
import os
import numpy as np
import torch
from huggingface_hub import InferenceClient
from pydantic import BaseModel
import base64
from io import BytesIO
from PIL import Image
import logging
from typing import Optional
import uuid
import pathlib

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Create a directory for temporary image storage
TEMP_DIR = pathlib.Path("./temp_images")
TEMP_DIR.mkdir(exist_ok=True)

class TextImageRequest(BaseModel):
    text: Optional[str] = None
    image_base64: Optional[str] = None
    voice: str = "af_heart"
    speed: float = 1.0

class AudioResponse(BaseModel):
    status: str
    message: str

# Initialize FastAPI app
app = FastAPI(
    title="Text-to-Speech API with Vision Support",
    description="API for generating speech from text with optional image analysis",
    version="1.0.0"
)

def save_base64_image(image_base64):
    """Save base64 image to a temporary file and return the file path"""
    try:
        # Generate a unique filename
        filename = f"{uuid.uuid4()}.jpg"
        filepath = TEMP_DIR / filename
        
        # Decode and save the image
        image_data = base64.b64decode(image_base64)
        with open(filepath, "wb") as f:
            f.write(image_data)
            
        # Return the file URL (using file:// protocol)
        return f"file://{filepath.absolute()}"
    except Exception as e:
        logger.error(f"Error saving base64 image: {str(e)}")
        raise HTTPException(status_code=400, detail=f"Invalid base64 image data: {str(e)}")

def llm_chat_response(text, image_base64=None):
    """Function to get responses from LLM with text and optionally image input."""
    try:
        HF_TOKEN = os.getenv("HF_TOKEN")
        logger.info("Checking HF_TOKEN...")
        if not HF_TOKEN:
            logger.error("HF_TOKEN not found in environment variables")
            raise HTTPException(status_code=500, detail="HF_TOKEN not configured")
        
        logger.info("Initializing InferenceClient...")
        client = InferenceClient(
            provider="cerebras",  # Using cerebras as in this example
            api_key=HF_TOKEN
        )
        
        # Build the messages payload using the format from your working example
        message_content = [{
            "type": "text",
            "text": text + ("" if image_base64 else " describe in one line only")
        }]
        
        if image_base64:
            logger.info("Processing base64 image...")
            # Save the base64 image to a file and get the file URL
            image_url = save_base64_image(image_base64)
            logger.info(f"Image saved at: {image_url}")
            
            # Create data URI
            data_uri = f"data:image/jpeg;base64,{image_base64}"
            
            # Add image to message content
            message_content.append({
                "type": "image_url",
                "image_url": {"url": data_uri}
            })
        
        # Construct the messages array exactly as in your working example
        messages = [{
            "role": "user",
            "content": message_content
        }]
        
        logger.info("Sending request to model...")
        try:
            completion = client.chat.completions.create(
                model="meta-llama/Llama-4-Scout-17B-16E-Instruct",
                messages=messages,
                max_tokens=500
            )
        except Exception as http_err:
            # Log HTTP errors from the request
            logger.error(f"HTTP error occurred: {str(http_err)}")
            raise HTTPException(status_code=500, detail=str(http_err))
        
        logger.info(f"Raw model response received")
        
        # Extract the response using the same method as your working code
        if not completion.choices or len(completion.choices) == 0:
            logger.error("No choices returned from model.")
            raise HTTPException(status_code=500, detail="Model returned no choices.")
        
        # Extract the response message from the first choice
        choice = completion.choices[0]
        response_message = None
        if hasattr(choice, "message"):
            response_message = choice.message
        elif isinstance(choice, dict):
            response_message = choice.get("message")
        
        if not response_message:
            logger.error(f"Response message is empty: {choice}")
            raise HTTPException(status_code=500, detail="Model response did not include a message.")
        
        content = None
        if isinstance(response_message, dict):
            content = response_message.get("content")
        if content is None and hasattr(response_message, "content"):
            content = response_message.content
        
        if not content:
            logger.error(f"Message content is missing: {response_message}")
            raise HTTPException(status_code=500, detail="Model message did not include content.")
        
        return content
        
    except Exception as e:
        logger.error(f"Error in llm_chat_response: {str(e)}")
        # Fallback response in case of error
        return "I couldn't process that input. Please try again with a different image or text query."

# Initialize pipeline once at startup
try:
    logger.info("Initializing KPipeline...")
    pipeline = KPipeline(lang_code='a')
    logger.info("KPipeline initialized successfully")
except Exception as e:
    logger.error(f"Failed to initialize KPipeline: {str(e)}")
    # We'll let the app start anyway, but log the error

@app.post("/generate")
async def generate_audio(request: TextImageRequest):
    """
    Generate audio from text and optionally analyze an image.
    
    - If text is provided, uses that as input
    - If image is provided, analyzes the image
    - Converts the LLM response to speech using the specified voice and speed
    """
    try:
        logger.info(f"Received audio generation request")
        
        # If no text is provided but image is provided, use default prompt
        user_text = request.text if request.text is not None else ""
        if not user_text and request.image_base64:
            user_text = "Describe what you see in the image"
        elif not user_text and not request.image_base64:
            logger.error("Neither text nor image provided in request")
            return JSONResponse(
                status_code=400, 
                content={"error": "Request must include either text or image_base64"}
            )
        
        # Generate response using text and image if provided
        logger.info("Getting LLM response...")
        text_reply = llm_chat_response(user_text, request.image_base64)
        logger.info(f"LLM response: {text_reply}")
        
        # Generate audio
        logger.info(f"Generating audio using voice={request.voice}, speed={request.speed}")
        try:
            generator = pipeline(
                text_reply,
                voice=request.voice,
                speed=request.speed,
                split_pattern=r'\n+'
            )
            
            # Process only the first segment for demo
            for i, (gs, ps, audio) in enumerate(generator):
                logger.info(f"Audio generated successfully: segment {i}")
                
                # Convert PyTorch tensor to NumPy array
                audio_numpy = audio.cpu().numpy()
                
                # Convert to 16-bit PCM
                # Ensure the audio is in the range [-1, 1]
                audio_numpy = np.clip(audio_numpy, -1, 1)
                # Convert to 16-bit signed integers
                pcm_data = (audio_numpy * 32767).astype(np.int16)
                
                # Convert to bytes (automatically uses row-major order)
                raw_audio = pcm_data.tobytes()
                
                # Return PCM data with minimal necessary headers
                return Response(
                    content=raw_audio,
                    media_type="application/octet-stream",
                    headers={
                        "Content-Disposition": f'attachment; filename="output.pcm"',
                        "X-Sample-Rate": "24000",
                        "X-Bits-Per-Sample": "16",
                        "X-Endianness": "little"
                    }
                )
                
            logger.error("No audio segments generated")
            return JSONResponse(
                status_code=400,
                content={"error": "No audio generated", "detail": "The pipeline did not produce any audio"}
            )
            
        except Exception as e:
            logger.error(f"Error generating audio: {str(e)}")
            return JSONResponse(
                status_code=500,
                content={"error": "Audio generation failed", "detail": str(e)}
            )
    
    except Exception as e:
        logger.error(f"Unexpected error in generate_audio endpoint: {str(e)}")
        return JSONResponse(
            status_code=500,
            content={"error": "Internal server error", "detail": str(e)}
        )

@app.get("/")
async def root():
    return {"message": "Welcome to the Text-to-Speech API with Vision Support. Use POST /generate endpoint with 'text' and optionally 'image_base64' for queries."}

# Cleanup function to periodically remove old temporary images
@app.on_event("startup")
async def startup_event():
    # You could add scheduled tasks here to clean up old images
    pass

@app.exception_handler(404)
async def not_found_handler(request, exc):
    return JSONResponse(
        status_code=404,
        content={"error": "Endpoint not found. Please use POST /generate for queries."}
    )

@app.exception_handler(405)
async def method_not_allowed_handler(request, exc):
    return JSONResponse(
        status_code=405,
        content={"error": "Method not allowed. Please check the API documentation."}
    )