This is a refactored version of the Dhive AI Music Generator with a clean modular structure. The original codebase has been organized into separate modules for better maintainability and code organization.
coding-interview/
├── main.py # Main application entry point
├── conf/ # Configuration module
│ ├── __init__.py
│ └── config.py # App configuration, database setup, and environment variables
├── models/ # Database models
│ ├── __init__.py
│ └── song.py # Song model and SongStatus enum
├── utils/ # Utility functions and core logic
│ ├── __init__.py
│ ├── helpers.py # Helper functions for text processing, Cloudinary uploads, etc.
│ ├── music_generator.py # SunoMusicGenerator class and music generation logic
│ └── routes.py # Flask route definitions
├── views/ # Frontend templates and static files
│ ├── README.md # Documentation for views structure
│ ├── static/
│ │ ├── css/
│ │ │ └── style.css # Main stylesheet for documentation page
│ │ └── js/
│ │ └── app.js # JavaScript for interactive API testing
│ └── templates/
│ └── index.html # HTML template for API documentation
├── migrations/ # Database migration files (generated by Flask-Migrate)
├── requirements.txt # Python dependencies
├── .env # Environment variables (create from .env.example)
├── .gitignore # Git ignore rules
└── README.md # This file
config.py: Contains all configuration settings including:- Environment variable loading
- Logging configuration
- Flask app initialization
- Database configuration
- Cloudinary configuration
- API keys and URLs
song.py: Contains database models:SongStatusenum for tracking song generation statusSongSQLAlchemy model for storing song metadata
-
helpers.py: Helper utility functions:sanitize_for_logging(): Text sanitization for loggingnormalize_content(): Content normalization for LLM responsesextract_task_id(): Task ID extraction from API responsesupload_to_cloudinary(): Cloudinary upload functionalitytry_notify(): Notification helper function
-
music_generator.py: Core music generation logic:SunoMusicGeneratorclass with methods for:- Lyrics generation using Google Gemini LLM
- Music generation using Suno API
- Status checking
- Song generation workflow
- Retry logic and error handling
- Audio polling functionality
-
routes.py: Flask route definitions:/create_song- Create a new song/check_status/<task_id>- Check song generation status/receive_song- Callback endpoint for Suno API/download- Download generated audio files/- Root endpoint/health- Health check endpoint
- Simplified entry point that:
- Imports all necessary modules
- Registers routes by importing the routes module
- Initializes the database
- Starts the Flask application
- Separation of Concerns: Each module has a specific responsibility
- Maintainability: Code is easier to find, understand, and modify
- Reusability: Utility functions can be easily reused across the application
- Testability: Individual modules can be tested in isolation
- Scalability: New features can be added without cluttering the main file
- Configuration Management: All settings are centralized in the config module
This refactoring preserves all original functionality while improving code organization. No business logic has been modified - only the structure and organization of the code.
-
Clone the repository:
git clone <repository-url> cd coding-interview
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
cp .env.example .env # Edit .env file with your actual API keys and database credentials -
Set up your PostgreSQL database:
- Create a PostgreSQL database for the application
- Update the database credentials in your
.envfile - Run the database migrations (see Database Setup section below)
First, create the database using your PostgreSQL client:
# Using psql command line
createdb -U admin dhive_ai_music
# Or using psql interactive
psql -U admin -c "CREATE DATABASE dhive_ai_music;"Initialize and run the database migrations:
# Activate virtual environment
source venv/bin/activate # On Windows: venv\Scripts\activate
# Set Flask app environment variable
export FLASK_APP=main.py
# Initialize migrations (if not already done)
flask db init
# Create migration for current models
flask db migrate -m "Initial migration for Song model"
# Apply migrations to database
flask db upgradeCheck that tables were created successfully:
# Connect to database and list tables
psql -U admin -d dhive_ai_music -c "\dt"You should see a song table in the output.
The application includes a comprehensive interactive API documentation and testing interface accessible at the root URL.
- 📖 Complete API Documentation - All endpoints documented with examples
- 🧪 Interactive Testing - Test forms for all API endpoints directly in browser
- 📱 Responsive Design - Works on desktop and mobile devices
- 🎨 Modern UI - Professional design with real-time status updates
- ⚡ Auto-refresh - Automatic status checking for song generation
- Start the Flask application:
python main.py - Visit:
http://127.0.0.1:5000 - Navigate through the tabs: Overview, API Endpoints, Interactive Testing
GET /- API documentation and testing interfaceGET /health- Health check endpointPOST /create_song- Create a new AI-generated songGET /check_status/<task_id>- Check song generation statusPOST /download- Download generated audio files
The application works exactly the same as before. Simply run:
python main.pyThen visit http://127.0.0.1:5000 to access the interactive documentation and test the API endpoints.
Make sure to set the following environment variables in your .env file:
POSTGRES_USER- Your PostgreSQL usernamePOSTGRES_PASSWORD- Your PostgreSQL passwordPOSTGRES_HOST- PostgreSQL host (usuallylocalhost)POSTGRES_DB- Database name (e.g.,dhive_ai_music)
CLOUDINARY_CLOUD_NAME- Your Cloudinary cloud nameCLOUDINARY_API_KEY- Your Cloudinary API keyCLOUDINARY_API_SECRET- Your Cloudinary API secret
SUNO_API_KEY- Your Suno API keyGEMINI_API_KEY- Your Google Gemini API key
APP_BASE_URL- Application base URL (optional, defaults tohttp://localhost:5000)
# Database Configuration
POSTGRES_USER=admin
POSTGRES_PASSWORD=your_password
POSTGRES_HOST=localhost
POSTGRES_DB=dhive_ai_music
# Cloudinary Configuration
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret
# API Keys
SUNO_API_KEY=your_suno_key
GEMINI_API_KEY=your_gemini_key
# Application Configuration
APP_BASE_URL=http://localhost:5000