Add music-gen-api console script entrypoint for FastAPI server #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The FastAPI server existed but lacked a proper entrypoint. Users had to invoke uvicorn directly with the full module path.
Changes
main()function that readsPORTfrom environment (defaults to 8080) and runs uvicornmusic-gen-apiconsole scriptmusic-gen-apientrypointUsage
The server runs in simulate mode by default (no GCP credentials required). All existing functionality unchanged.
Original prompt
Context
You are working on branch
copilot/add-song-track-generation-apiinkngms/github-dev-sandbox.Already implemented:
src/music_generatorwith:music-gen(generate/list-presets/show-preset/save-preset/delete-preset/setup commands)./presetswith built-insGOOGLE_CLOUD_PROJECTsrc/music_generator/{__init__.py, cli.py, generator.py, models.py, presets.py},requirements.txt,setup.py,README.md,examples/,presets/Goal:
Make this easily runnable locally and in Cloud Run by adding a FastAPI HTTP server, making GCP truly optional (simulate mode), and adding deployment support.
Requirements
A) Make GCP optional (must be done first)
Introduce a mode flag:
MUSIC_GEN_MODE=simulate|gcp(default:simulate)In
simulatemode:MusicGeneratormust NOT requireGOOGLE_CLOUD_PROJECTaiplatform.init()at startupIn
gcpmode:GOOGLE_CLOUD_PROJECTenv var)GOOGLE_APPLICATION_CREDENTIALSfile pathUpdate existing code:
MusicGenerator.__init__to check mode and conditionally initialize GCPGOOGLE_CLOUD_PROJECTis missing in simulate modeB) Add FastAPI HTTP server (new)
Add dependencies to
requirements.txt:fastapi>=0.104.0uvicorn[standard]>=0.24.0pytest>=7.4.0,httpx>=0.25.0Create FastAPI app at
src/music_generator/api.py:Endpoints:
POST
/tracks/generate{ "text_input": "string (required)", "genre": "string (optional if preset_name provided)", "duration_seconds": 180, "preset_name": "string (optional)", "structure": {...}, "style_references": [...], "temperature": 0.7 }preset_nameis provided: load preset, then merge/override with any provided fields (text_input, duration_seconds, structure, style_references, temperature)MusicGenerator.generate_track()and return JSON resultGET
/presets[{"name": "...", "genre": "...", "description": "..."}]GET
/presets/{name}POST
/presetsDELETE
/presets/{name}GET
/prompt-tipspreset_name(string)preset_nameprovided: return tips for that preset only[{"preset": "name", "tips": "..."}]or{"tips": "..."}GET
/health{"status": "healthy", "mode": "simulate|gcp"}Add optional API authentication:
MUSIC_GEN_API_KEYis set, require it on all endpoints except/healthX-API-Key: <key>Authorization: Bearer <key>MUSIC_GEN_API_KEYis not set, run without auth (useful for local dev)Server entrypoint:
main()function inapi.pythat runsuvicorn.run(app, host="0.0.0.0", port=port)$PORTenv var (for Cloud Run), default to 8080setup.py:music-gen-api=music_generator.api:mainC) Cloud Run readiness
Dockerfile:pip install -e .ENV MUSIC_GEN_MODE=simulateas default$PORT(Cloud Run requirement)This pull request was created from Copilot chat.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.