Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 23, 2025

The FastAPI server existed but lacked a proper entrypoint. Users had to invoke uvicorn directly with the full module path.

Changes

  • api.py: Added main() function that reads PORT from environment (defaults to 8080) and runs uvicorn
  • setup.py: Added music-gen-api console script
  • Dockerfile: Updated CMD to use music-gen-api entrypoint
  • Documentation: Updated README and run-api.sh to use new entrypoint

Usage

# Start server (binds to 0.0.0.0:8080)
music-gen-api

# Custom port
PORT=3000 music-gen-api

# Cloud Run compatible (reads $PORT)
docker run -e PORT=8080 -e MUSIC_GEN_MODE=simulate music-track-generator

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-api in kngms/github-dev-sandbox.

Already implemented:

  • Python package under src/music_generator with:
    • Click CLI music-gen (generate/list-presets/show-preset/save-preset/delete-preset/setup commands)
    • Pydantic models (TrackConfig, PresetConfig, SongStructure, StyleReference) with duration validation (60–240s)
    • PresetManager storing YAML presets in ./presets with built-ins
    • MusicGenerator that builds prompts and returns simulated generation results, but currently initializes Vertex AI and requires GOOGLE_CLOUD_PROJECT
  • Good README, but it claims "API" exists even though there is no HTTP server yet
  • Files: src/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)

  1. Introduce a mode flag:

    • Environment variable: MUSIC_GEN_MODE=simulate|gcp (default: simulate)
  2. In simulate mode:

    • MusicGenerator must NOT require GOOGLE_CLOUD_PROJECT
    • Must NOT call aiplatform.init() at startup
    • Must still build prompts and return the simulated result as it does now
    • All CLI commands must work without any GCP credentials
  3. In gcp mode:

    • Require project id (from constructor parameter or GOOGLE_CLOUD_PROJECT env var)
    • Initialize Vertex AI as needed
    • Support ADC (Application Default Credentials) for Cloud Run - do not require GOOGLE_APPLICATION_CREDENTIALS file path
  4. Update existing code:

    • Refactor MusicGenerator.__init__ to check mode and conditionally initialize GCP
    • Update CLI to work without errors when GOOGLE_CLOUD_PROJECT is missing in simulate mode
    • Keep all existing functionality working

B) Add FastAPI HTTP server (new)

  1. Add dependencies to requirements.txt:

    • fastapi>=0.104.0
    • uvicorn[standard]>=0.24.0
    • For tests: pytest>=7.4.0, httpx>=0.25.0
  2. Create FastAPI app at src/music_generator/api.py:

    Endpoints:

    • POST /tracks/generate

      • Request body supports both direct config and preset-based generation:
        {
          "text_input": "string (required)",
          "genre": "string (optional if preset_name provided)",
          "duration_seconds": 180,
          "preset_name": "string (optional)",
          "structure": {...},
          "style_references": [...],
          "temperature": 0.7
        }
      • If preset_name is provided: load preset, then merge/override with any provided fields (text_input, duration_seconds, structure, style_references, temperature)
      • Validate duration 60–240 (use Pydantic models)
      • Call MusicGenerator.generate_track() and return JSON result
      • Return 404 if preset not found
      • Return 422 for validation errors
    • GET /presets

      • List all presets with metadata (name, genre, description)
      • Response: [{"name": "...", "genre": "...", "description": "..."}]
    • GET /presets/{name}

      • Return full preset configuration
      • Return 404 if not found
    • POST /presets

      • Body: PresetConfig
      • Create or update preset
      • Return created preset
    • DELETE /presets/{name}

      • Delete preset
      • Return 404 if not found
      • Return success message
    • GET /prompt-tips

      • Optional query param: preset_name (string)
      • If preset_name provided: return tips for that preset only
      • Otherwise: return aggregated tips from all presets
      • Response: [{"preset": "name", "tips": "..."}] or {"tips": "..."}
    • GET /health

      • Simple health check endpoint
      • Return: {"status": "healthy", "mode": "simulate|gcp"}
  3. Add optional API authentication:

    • If env var MUSIC_GEN_API_KEY is set, require it on all endpoints except /health
    • Accept either:
      • Header: X-API-Key: <key>
      • Header: Authorization: Bearer <key>
    • Return 401 if key is required but missing or incorrect
    • If MUSIC_GEN_API_KEY is not set, run without auth (useful for local dev)
  4. Server entrypoint:

    • Add main() function in api.py that runs uvicorn.run(app, host="0.0.0.0", port=port)
    • Port should read from $PORT env var (for Cloud Run), default to 8080
    • Add console_scripts entry to setup.py: music-gen-api=music_generator.api:main

C) Cloud Run readiness

  1. Add Dockerfile:
    • Use Python 3.11 slim base image
    • Copy and install requirements
    • Copy src/
    • Install package with pip install -e .
    • Set ENV MUSIC_GEN_MODE=simulate as default
    • Expose $PORT (Cloud Run requirement)
    • CMD to run `music-gen-ap...

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.

Copilot AI and others added 3 commits December 23, 2025 10:30
- Add main() function to api.py that runs uvicorn server
- Add music-gen-api console script to setup.py
- Update Dockerfile CMD to use music-gen-api entrypoint
- All tests pass (13/13)

Co-authored-by: kngms <167722266+kngms@users.noreply.github.com>
- Update README to show music-gen-api as primary method
- Update run-api.sh to use music-gen-api
- Keep uvicorn alternative documented for flexibility

Co-authored-by: kngms <167722266+kngms@users.noreply.github.com>
Address code review feedback by moving uvicorn import statement to the top of the file with other imports rather than inside the main() function.

Co-authored-by: kngms <167722266+kngms@users.noreply.github.com>
Copilot AI changed the title [WIP] Add FastAPI HTTP server for music generation Add music-gen-api console script entrypoint for FastAPI server Dec 23, 2025
Copilot AI requested a review from kngms December 23, 2025 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants