Initial scaffold: FastAPI backend + React frontend + Docker Compose
- Backend: FastAPI with SQLite, clip CRUD, health check - Frontend: React + Vite + TypeScript + Tailwind, dark theme - Components: Generator, Gallery, Player - Docker: Compose setup for backend + frontend - Architecture docs in Palace KB
This commit is contained in:
commit
a2bc785b14
23 files changed
+819
No files matched your search
@@ -0,0 +1,80 @@
|
||||
import aiosqlite
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
DB_PATH = Path(__file__).parent.parent / "data" / "lyria.db"
|
||||
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
INIT_SQL = """
|
||||
CREATE TABLE IF NOT EXISTS clips (
|
||||
id TEXT PRIMARY KEY,
|
||||
prompt TEXT NOT NULL,
|
||||
model TEXT NOT NULL,
|
||||
duration_seconds INTEGER,
|
||||
filename TEXT NOT NULL,
|
||||
file_size_bytes INTEGER,
|
||||
status TEXT DEFAULT 'generating',
|
||||
error_message TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_clips_created ON clips(created_at DESC);
|
||||
"""
|
||||
|
||||
async def init_db():
|
||||
async with aiosqlite.connect(DB_PATH) as db:
|
||||
await db.executescript(INIT_SQL)
|
||||
await db.commit()
|
||||
|
||||
async def create_clip(prompt: str, model: str, duration: int) -> str:
|
||||
clip_id = str(uuid.uuid4())
|
||||
filename = f"{clip_id}.mp3"
|
||||
async with aiosqlite.connect(DB_PATH) as db:
|
||||
await db.execute(
|
||||
"INSERT INTO clips (id, prompt, model, duration_seconds, filename, status) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
(clip_id, prompt, model, duration, filename, "generating")
|
||||
)
|
||||
await db.commit()
|
||||
return clip_id
|
||||
|
||||
async def update_clip_status(clip_id: str, status: str, file_size: int = None, error: str = None):
|
||||
async with aiosqlite.connect(DB_PATH) as db:
|
||||
if file_size is not None:
|
||||
await db.execute(
|
||||
"UPDATE clips SET status = ?, file_size_bytes = ? WHERE id = ?",
|
||||
(status, file_size, clip_id)
|
||||
)
|
||||
elif error is not None:
|
||||
await db.execute(
|
||||
"UPDATE clips SET status = ?, error_message = ? WHERE id = ?",
|
||||
(status, error, clip_id)
|
||||
)
|
||||
else:
|
||||
await db.execute(
|
||||
"UPDATE clips SET status = ? WHERE id = ?",
|
||||
(status, clip_id)
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
async def get_clips(limit: int = 50, offset: int = 0):
|
||||
async with aiosqlite.connect(DB_PATH) as db:
|
||||
db.row_factory = aiosqlite.Row
|
||||
async with db.execute(
|
||||
"SELECT * FROM clips ORDER BY created_at DESC LIMIT ? OFFSET ?",
|
||||
(limit, offset)
|
||||
) as cursor:
|
||||
rows = await cursor.fetchall()
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
async def get_clip(clip_id: str):
|
||||
async with aiosqlite.connect(DB_PATH) as db:
|
||||
db.row_factory = aiosqlite.Row
|
||||
async with db.execute("SELECT * FROM clips WHERE id = ?", (clip_id,)) as cursor:
|
||||
row = await cursor.fetchone()
|
||||
return dict(row) if row else None
|
||||
|
||||
async def delete_clip(clip_id: str):
|
||||
async with aiosqlite.connect(DB_PATH) as db:
|
||||
await db.execute("DELETE FROM clips WHERE id = ?", (clip_id,))
|
||||
await db.commit()
|
||||
@@ -0,0 +1,76 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import FileResponse, StreamingResponse
|
||||
from pathlib import Path
|
||||
import os
|
||||
|
||||
from models import GenerateRequest, GenerateResponse, ClipList
|
||||
from database import init_db, create_clip, get_clips, get_clip, delete_clip, update_clip_status
|
||||
|
||||
app = FastAPI(title="Lyria Studio", version="0.1.0")
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
CLIPS_DIR = Path(__file__).parent.parent / "data" / "clips"
|
||||
CLIPS_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
await init_db()
|
||||
|
||||
@app.get("/api/health")
|
||||
async def health():
|
||||
return {"status": "ok", "version": "0.1.0"}
|
||||
|
||||
@app.post("/api/generate", response_model=GenerateResponse)
|
||||
async def generate(request: GenerateRequest):
|
||||
clip_id = await create_clip(request.prompt, request.model, request.duration)
|
||||
# TODO: T-003 — integrate Lyria API client
|
||||
return GenerateResponse(
|
||||
id=clip_id,
|
||||
status="generating",
|
||||
message="Clip generation started. Check back for status."
|
||||
)
|
||||
|
||||
@app.get("/api/clips", response_model=ClipList)
|
||||
async def list_clips(limit: int = 50, offset: int = 0):
|
||||
clips = await get_clips(limit, offset)
|
||||
return ClipList(clips=clips, total=len(clips))
|
||||
|
||||
@app.get("/api/clips/{clip_id}")
|
||||
async def get_clip_by_id(clip_id: str):
|
||||
clip = await get_clip(clip_id)
|
||||
if not clip:
|
||||
raise HTTPException(status_code=404, detail="Clip not found")
|
||||
return clip
|
||||
|
||||
@app.get("/api/clips/{clip_id}/audio")
|
||||
async def stream_audio(clip_id: str):
|
||||
clip = await get_clip(clip_id)
|
||||
if not clip:
|
||||
raise HTTPException(status_code=404, detail="Clip not found")
|
||||
file_path = CLIPS_DIR / clip["filename"]
|
||||
if not file_path.exists():
|
||||
raise HTTPException(status_code=404, detail="Audio file not found")
|
||||
return FileResponse(file_path, media_type="audio/mpeg", filename=clip["filename"])
|
||||
|
||||
@app.delete("/api/clips/{clip_id}")
|
||||
async def remove_clip(clip_id: str):
|
||||
clip = await get_clip(clip_id)
|
||||
if not clip:
|
||||
raise HTTPException(status_code=404, detail="Clip not found")
|
||||
file_path = CLIPS_DIR / clip["filename"]
|
||||
if file_path.exists():
|
||||
file_path.unlink()
|
||||
await delete_clip(clip_id)
|
||||
return {"deleted": True}
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
@@ -0,0 +1,28 @@
|
||||
from pydantic import BaseModel
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
class GenerateRequest(BaseModel):
|
||||
prompt: str
|
||||
model: str = "lyria-3-clip-preview"
|
||||
duration: int = 30
|
||||
|
||||
class GenerateResponse(BaseModel):
|
||||
id: str
|
||||
status: str
|
||||
message: str
|
||||
|
||||
class Clip(BaseModel):
|
||||
id: str
|
||||
prompt: str
|
||||
model: str
|
||||
duration_seconds: int
|
||||
filename: str
|
||||
file_size_bytes: Optional[int] = None
|
||||
status: str
|
||||
error_message: Optional[str] = None
|
||||
created_at: datetime
|
||||
|
||||
class ClipList(BaseModel):
|
||||
clips: list[Clip]
|
||||
total: int
|
||||
Reference in new issue
Block a user