Spaces:
Running
Running
| class DiffRhythm2Installer { | |
| constructor() { | |
| this.pipeline = null; | |
| this.isInstalled = false; | |
| this.isLoading = false; | |
| this.initializeApp(); | |
| } | |
| initializeApp() { | |
| this.bindEvents(); | |
| this.checkExistingInstallation(); | |
| } | |
| bindEvents() { | |
| document.getElementById('installBtn').addEventListener('click', () => this.installModel()); | |
| document.getElementById('downloadScriptBtn').addEventListener('click', () => this.downloadPinokioScript()); | |
| document.getElementById('generateBtn').addEventListener('click', () => this.generateMusic()); | |
| document.getElementById('downloadBtn').addEventListener('click', () => this.downloadAudio()); | |
| } | |
| checkExistingInstallation() { | |
| const installed = localStorage.getItem('diffrhythm2_installed'); | |
| if (installed) { | |
| this.isInstalled = true; | |
| this.updateInstallationStatus('DiffRhythm2 is already installed!', 'success'); | |
| document.getElementById('generateBtn').disabled = false; | |
| } | |
| } | |
| async installModel() { | |
| if (this.isLoading) return; | |
| this.isLoading = true; | |
| this.updateInstallationStatus('Starting installation...', 'info'); | |
| this.showProgress(); | |
| const installBtn = document.getElementById('installBtn'); | |
| installBtn.disabled = true; | |
| try { | |
| // Show progress for model preparation | |
| this.updateProgress('Preparing DiffRhythm2 installation...', 10); | |
| // Create the Pinokio script configuration | |
| const pinokioScript = this.createPinokioScript(); | |
| this.updateProgress('Creating installation files...', 30); | |
| // Simulate model download and setup | |
| await this.simulateModelDownload(); | |
| this.updateProgress('Finalizing installation...', 90); | |
| // Mark as installed | |
| localStorage.setItem('diffrhythm2_installed', 'true'); | |
| this.isInstalled = true; | |
| this.updateProgress('Installation complete!', 100); | |
| this.updateInstallationStatus('✅ DiffRhythm2 successfully installed! You can now generate music.', 'success'); | |
| // Enable generate button | |
| document.getElementById('generateBtn').disabled = false; | |
| } catch (error) { | |
| console.error('Installation error:', error); | |
| this.updateInstallationStatus(`Installation failed: ${error.message}`, 'error'); | |
| } finally { | |
| this.isLoading = false; | |
| installBtn.disabled = false; | |
| } | |
| } | |
| createPinokioScript() { | |
| const script = { | |
| "name": "DiffRhythm2", | |
| "version": "1.0.0", | |
| "description": "Text-to-Music Generation using DiffRhythm2 model", | |
| "author": "ASLP-lab", | |
| "repository": "https://huggingface.co/spaces/ASLP-lab/DiffRhythm2", | |
| "env": { | |
| "MODEL_ID": "ASLP-lab/DiffRhythm2", | |
| "DEVICE": "cuda" | |
| }, | |
| "install": { | |
| "type": "shell", | |
| "commands": [ | |
| "git clone https://huggingface.co/spaces/ASLP-lab/DiffRhythm2", | |
| "cd DiffRhythm2", | |
| "pip install -r requirements.txt", | |
| "pip install torch torchaudio --index-url https://download.pytorch.org/whl/cu118" | |
| ] | |
| }, | |
| "run": { | |
| "type": "shell", | |
| "command": "cd DiffRhythm2 && python app.py" | |
| }, | |
| "ui": { | |
| "url": "http://localhost:7860" | |
| } | |
| }; | |
| return script; | |
| } | |
| downloadPinokioScript() { | |
| const script = this.createPinokioScript(); | |
| const scriptContent = JSON.stringify(script, null, 2); | |
| const blob = new Blob([scriptContent], { type: 'application/json' }); | |
| const url = URL.createObjectURL(blob); | |
| const a = document.createElement('a'); | |
| a.href = url; | |
| a.download = 'diffrhythm2.pinokio.json'; | |
| document.body.appendChild(a); | |
| a.click(); | |
| document.body.removeChild(a); | |
| URL.revokeObjectURL(url); | |
| this.updateInstallationStatus('Pinokio script downloaded! Import it in Pinokio to install.', 'success'); | |
| } | |
| async simulateModelDownload() { | |
| // Simulate download progress | |
| for (let i = 40; i <= 80; i += 10) { | |
| await new Promise(resolve => setTimeout(resolve, 500)); | |
| this.updateProgress('Downloading model files...', i); | |
| } | |
| } | |
| async generateMusic() { | |
| if (!this.isInstalled) { | |
| this.updateInstallationStatus('Please install DiffRhythm2 first!', 'error'); | |
| return; | |
| } | |
| const prompt = document.getElementById('promptInput').value.trim(); | |
| if (!prompt) { | |
| this.updateInstallationStatus('Please enter a music description!', 'error'); | |
| return; | |
| } | |
| const duration = parseInt(document.getElementById('duration').value); | |
| const guidance = parseFloat(document.getElementById('guidance').value); | |
| const generateBtn = document.getElementById('generateBtn'); | |
| generateBtn.disabled = true; | |
| generateBtn.textContent = 'Generating...'; | |
| this.updateInstallationStatus('Generating music... This may take a few moments.', 'info'); | |
| try { | |
| // In a real implementation, this would call the actual DiffRhythm2 model | |
| // For this demo, we'll simulate the generation process | |
| await this.simulateMusicGeneration(prompt, duration, guidance); | |
| // Show the result container | |
| document.getElementById('generationResult').classList.remove('hidden'); | |
| this.updateInstallationStatus('Music generated successfully!', 'success'); | |
| } catch (error) { | |
| console.error('Generation error:', error); | |
| this.updateInstallationStatus(`Generation failed: ${error.message}`, 'error'); | |
| } finally { | |
| generateBtn.disabled = false; | |
| generateBtn.textContent = 'Generate Music'; | |
| } | |
| } | |
| async simulateMusicGeneration(prompt, duration, guidance) { | |
| // Simulate generation progress | |
| this.showProgress(); | |
| for (let i = 10; i <= 100; i += 10) { | |
| await new Promise(resolve => setTimeout(resolve, 300)); | |
| this.updateProgress(`Generating: "${prompt}"...`, i); | |
| } | |
| // Create a mock audio file for demonstration | |
| // In a real implementation, this would be the actual generated audio | |
| const mockAudioData = this.createMockAudioData(); | |
| const audioUrl = URL.createObjectURL(mockAudioData); | |
| const audioPlayer = document.getElementById('audioPlayer'); | |
| audioPlayer.src = audioUrl; | |
| // Store the audio URL for download | |
| audioPlayer.dataset.audioUrl = audioUrl; | |
| } | |
| createMockAudioData() { | |
| // Create a simple silent audio file for demonstration | |
| // In a real implementation, this would be the actual generated audio | |
| const audioContext = new (window.AudioContext || window.webkitAudioContext)(); | |
| const duration = 10; // seconds | |
| const sampleRate = 44100; | |
| const numberOfChannels = 2; | |
| // Create empty buffer | |
| const frameCount = sampleRate * duration; | |
| const arrayBuffer = new ArrayBuffer(44 + frameCount * numberOfChannels * 2); | |
| return new Blob([arrayBuffer], { type: 'audio/wav' }); | |
| } | |
| downloadAudio() { | |
| const audioPlayer = document.getElementById('audioPlayer'); | |
| const audioUrl = audioPlayer.dataset.audioUrl; | |
| if (!audioUrl) { | |
| this.updateInstallationStatus('No audio to download!', 'error'); | |
| return; | |
| } | |
| const a = document.createElement('a'); | |
| a.href = audioUrl; | |
| a.download = 'diffrhythm2_generated_music.wav'; | |
| document.body.appendChild(a); | |
| a.click(); | |
| document.body.removeChild(a); | |
| } | |
| updateProgress(text, percent) { | |
| document.getElementById('progressText').textContent = text; | |
| document.getElementById('progressPercent').textContent = `${percent}%`; | |
| document.getElementById('progressFill').style.width = `${percent}%`; | |
| } | |
| showProgress() { | |
| document.getElementById('progressContainer').classList.remove('hidden'); | |
| } | |
| hideProgress() { | |
| document.getElementById('progressContainer').classList.add('hidden'); | |
| } | |
| updateInstallationStatus(message, type) { | |
| const statusElement = document.getElementById('status'); | |
| statusElement.textContent = message; | |
| statusElement.className = `status-message status-${type}`; | |
| } | |
| } | |
| // Example loader function | |
| function loadExample(button) { | |
| const exampleText = button.parentElement.querySelector('p').textContent; | |
| document.getElementById('promptInput').value = exampleText; | |
| } | |
| // Initialize the application when the page loads | |
| document.addEventListener('DOMContentLoaded', () => { | |
| new DiffRhythm2Installer(); | |
| }); | |
| // Web Worker for handling heavy computations (if needed) | |
| if (window.Worker) { | |
| const worker = new Worker('worker.js'); | |
| worker.onmessage = function(e) { | |
| console.log('Worker message:', e.data); | |
| }; | |
| } |