Spaces:
Running
Running
File size: 9,539 Bytes
d91eb22 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
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);
};
} |