Spaces:
Build error
Build error
| #!/usr/bin/env python3 | |
| """ | |
| ββββββββββββββββββββββββββββββββββββββββ | |
| β SAMPLE GENERATOR β | |
| β Retro-styled AI music sampler β | |
| β Powered by Meta's MusicGen β | |
| ββββββββββββββββββββββββββββββββββββββββ | |
| Local usage: | |
| pip install -r requirements.txt | |
| python app.py β http://localhost:7860 | |
| HF Spaces: | |
| Push this folder to a Docker Space β the Dockerfile handles everything. | |
| """ | |
| from flask import Flask, send_file, send_from_directory, jsonify, request | |
| import os, uuid, traceback, threading | |
| app = Flask(__name__) | |
| _model = None | |
| _model_name = None # model id from first load; reused for all later requests | |
| _model_lock = threading.Lock() | |
| # βββ Serve the frontend ββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def index(): | |
| return send_from_directory(os.path.dirname(__file__), 'index.html') | |
| # βββ Generate endpoint βββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def generate(): | |
| global _model, _model_name | |
| data = request.get_json() or {} | |
| description = data.get('description', 'ambient electronic music').strip() | |
| duration = max(1, min(int(data.get('duration', 8)), 60)) | |
| model_id = data.get('model', 'facebook/musicgen-small') | |
| # Whitelist allowed model IDs | |
| allowed = {'facebook/musicgen-small', 'facebook/musicgen-medium'} | |
| if model_id not in allowed: | |
| return jsonify({'error': f'Unknown model: {model_id}'}), 400 | |
| try: | |
| # ββ Load model once per process (first request wins; concurrent first calls share one load) | |
| with _model_lock: | |
| if _model is None: | |
| from audiocraft.models import MusicGen | |
| _model = MusicGen.get_pretrained(model_id) | |
| _model_name = model_id | |
| # Already loaded: never call get_pretrained again (ignore later model_id changes) | |
| _model.set_generation_params(duration=duration) | |
| wav = _model.generate([description]) | |
| # ββ Save to /tmp βββββββββββββββββββββββββββββββββββββββββββββββ | |
| tmp_path = f'/tmp/sample_{uuid.uuid4().hex[:10]}' | |
| from audiocraft.data.audio import audio_write | |
| audio_write(tmp_path, wav[0].cpu(), _model.sample_rate, | |
| strategy='loudness') | |
| return send_file(f'{tmp_path}.wav', mimetype='audio/wav') | |
| except Exception as e: | |
| return jsonify({ | |
| 'error': str(e), | |
| 'traceback': traceback.format_exc() | |
| }), 500 | |
| # βββ Entry point βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if __name__ == '__main__': | |
| # HF Spaces requires port 7860; falls back to that locally too | |
| port = int(os.environ.get('PORT', 7860)) | |
| print('\n' + '=' * 50) | |
| print(' SAMPLE GENERATOR') | |
| print('=' * 50) | |
| print(f' β http://localhost:{port}') | |
| print('=' * 50 + '\n') | |
| app.run(host='0.0.0.0', port=port, debug=False) | |