Fix Lyria API response parsing — audio in parts[1], not parts[0]
The Lyria API returns a candidates[].content.parts[] array where: - parts[0] = text metadata (e.g. '<instrumental>') - parts[1] = inlineData with the actual audio payload Previous code only checked parts[0] for inlineData, which always failed with 'No audio data in response'. Fixed by iterating all parts to find the inlineData entry. Successfully generated first real audio clip (lo-fi hip hop, 30s, 728KB MP3). Also includes: .env.example, requirements updates, docker-compose fixes, frontend vite config, logger setup.
This commit is contained in:
1 parent
a2bc785b14
commit
23672387e5
6 files changed
+156
-10
No files matched your search
+43
-4
@@ -1,11 +1,16 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import FileResponse, StreamingResponse
|
||||
from fastapi.responses import FileResponse
|
||||
from pathlib import Path
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables BEFORE importing lyria_client (it reads env at module level)
|
||||
load_dotenv(Path(__file__).parent.parent / ".env")
|
||||
|
||||
from models import GenerateRequest, GenerateResponse, ClipList
|
||||
from database import init_db, create_clip, get_clips, get_clip, delete_clip, update_clip_status
|
||||
from lyria_client import generate_clip, save_clip_file
|
||||
|
||||
app = FastAPI(title="Lyria Studio", version="0.1.0")
|
||||
|
||||
@@ -28,10 +33,44 @@ async def startup():
|
||||
async def health():
|
||||
return {"status": "ok", "version": "0.1.0"}
|
||||
|
||||
async def _generate_and_save(clip_id: str, prompt: str, model: str, duration: int):
|
||||
"""Background task: call Lyria API and save the result."""
|
||||
try:
|
||||
result = await generate_clip(prompt, model, duration)
|
||||
|
||||
if result["success"]:
|
||||
file_info = await save_clip_file(clip_id, result["audio_bytes"])
|
||||
await update_clip_status(
|
||||
clip_id,
|
||||
status="ready",
|
||||
file_size=file_info["file_size"]
|
||||
)
|
||||
else:
|
||||
await update_clip_status(
|
||||
clip_id,
|
||||
status="failed",
|
||||
error=result.get("error", "Unknown error")
|
||||
)
|
||||
except Exception as e:
|
||||
await update_clip_status(
|
||||
clip_id,
|
||||
status="failed",
|
||||
error=str(e)
|
||||
)
|
||||
|
||||
@app.post("/api/generate", response_model=GenerateResponse)
|
||||
async def generate(request: GenerateRequest):
|
||||
async def generate(request: GenerateRequest, background_tasks: BackgroundTasks):
|
||||
clip_id = await create_clip(request.prompt, request.model, request.duration)
|
||||
# TODO: T-003 — integrate Lyria API client
|
||||
|
||||
# Kick off generation in background
|
||||
background_tasks.add_task(
|
||||
_generate_and_save,
|
||||
clip_id,
|
||||
request.prompt,
|
||||
request.model,
|
||||
request.duration
|
||||
)
|
||||
|
||||
return GenerateResponse(
|
||||
id=clip_id,
|
||||
status="generating",
|
||||
|
||||
Reference in new issue
Block a user