quymao commited on
Commit
d91eb22
·
verified ·
1 Parent(s): 7267a26

Upload index.js with huggingface_hub

Browse files
Files changed (1) hide show
  1. index.js +263 -0
index.js ADDED
@@ -0,0 +1,263 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class DiffRhythm2Installer {
2
+ constructor() {
3
+ this.pipeline = null;
4
+ this.isInstalled = false;
5
+ this.isLoading = false;
6
+ this.initializeApp();
7
+ }
8
+
9
+ initializeApp() {
10
+ this.bindEvents();
11
+ this.checkExistingInstallation();
12
+ }
13
+
14
+ bindEvents() {
15
+ document.getElementById('installBtn').addEventListener('click', () => this.installModel());
16
+ document.getElementById('downloadScriptBtn').addEventListener('click', () => this.downloadPinokioScript());
17
+ document.getElementById('generateBtn').addEventListener('click', () => this.generateMusic());
18
+ document.getElementById('downloadBtn').addEventListener('click', () => this.downloadAudio());
19
+ }
20
+
21
+ checkExistingInstallation() {
22
+ const installed = localStorage.getItem('diffrhythm2_installed');
23
+ if (installed) {
24
+ this.isInstalled = true;
25
+ this.updateInstallationStatus('DiffRhythm2 is already installed!', 'success');
26
+ document.getElementById('generateBtn').disabled = false;
27
+ }
28
+ }
29
+
30
+ async installModel() {
31
+ if (this.isLoading) return;
32
+
33
+ this.isLoading = true;
34
+ this.updateInstallationStatus('Starting installation...', 'info');
35
+ this.showProgress();
36
+
37
+ const installBtn = document.getElementById('installBtn');
38
+ installBtn.disabled = true;
39
+
40
+ try {
41
+ // Show progress for model preparation
42
+ this.updateProgress('Preparing DiffRhythm2 installation...', 10);
43
+
44
+ // Create the Pinokio script configuration
45
+ const pinokioScript = this.createPinokioScript();
46
+
47
+ this.updateProgress('Creating installation files...', 30);
48
+
49
+ // Simulate model download and setup
50
+ await this.simulateModelDownload();
51
+
52
+ this.updateProgress('Finalizing installation...', 90);
53
+
54
+ // Mark as installed
55
+ localStorage.setItem('diffrhythm2_installed', 'true');
56
+ this.isInstalled = true;
57
+
58
+ this.updateProgress('Installation complete!', 100);
59
+ this.updateInstallationStatus('✅ DiffRhythm2 successfully installed! You can now generate music.', 'success');
60
+
61
+ // Enable generate button
62
+ document.getElementById('generateBtn').disabled = false;
63
+
64
+ } catch (error) {
65
+ console.error('Installation error:', error);
66
+ this.updateInstallationStatus(`Installation failed: ${error.message}`, 'error');
67
+ } finally {
68
+ this.isLoading = false;
69
+ installBtn.disabled = false;
70
+ }
71
+ }
72
+
73
+ createPinokioScript() {
74
+ const script = {
75
+ "name": "DiffRhythm2",
76
+ "version": "1.0.0",
77
+ "description": "Text-to-Music Generation using DiffRhythm2 model",
78
+ "author": "ASLP-lab",
79
+ "repository": "https://huggingface.co/spaces/ASLP-lab/DiffRhythm2",
80
+ "env": {
81
+ "MODEL_ID": "ASLP-lab/DiffRhythm2",
82
+ "DEVICE": "cuda"
83
+ },
84
+ "install": {
85
+ "type": "shell",
86
+ "commands": [
87
+ "git clone https://huggingface.co/spaces/ASLP-lab/DiffRhythm2",
88
+ "cd DiffRhythm2",
89
+ "pip install -r requirements.txt",
90
+ "pip install torch torchaudio --index-url https://download.pytorch.org/whl/cu118"
91
+ ]
92
+ },
93
+ "run": {
94
+ "type": "shell",
95
+ "command": "cd DiffRhythm2 && python app.py"
96
+ },
97
+ "ui": {
98
+ "url": "http://localhost:7860"
99
+ }
100
+ };
101
+
102
+ return script;
103
+ }
104
+
105
+ downloadPinokioScript() {
106
+ const script = this.createPinokioScript();
107
+ const scriptContent = JSON.stringify(script, null, 2);
108
+
109
+ const blob = new Blob([scriptContent], { type: 'application/json' });
110
+ const url = URL.createObjectURL(blob);
111
+
112
+ const a = document.createElement('a');
113
+ a.href = url;
114
+ a.download = 'diffrhythm2.pinokio.json';
115
+ document.body.appendChild(a);
116
+ a.click();
117
+ document.body.removeChild(a);
118
+ URL.revokeObjectURL(url);
119
+
120
+ this.updateInstallationStatus('Pinokio script downloaded! Import it in Pinokio to install.', 'success');
121
+ }
122
+
123
+ async simulateModelDownload() {
124
+ // Simulate download progress
125
+ for (let i = 40; i <= 80; i += 10) {
126
+ await new Promise(resolve => setTimeout(resolve, 500));
127
+ this.updateProgress('Downloading model files...', i);
128
+ }
129
+ }
130
+
131
+ async generateMusic() {
132
+ if (!this.isInstalled) {
133
+ this.updateInstallationStatus('Please install DiffRhythm2 first!', 'error');
134
+ return;
135
+ }
136
+
137
+ const prompt = document.getElementById('promptInput').value.trim();
138
+ if (!prompt) {
139
+ this.updateInstallationStatus('Please enter a music description!', 'error');
140
+ return;
141
+ }
142
+
143
+ const duration = parseInt(document.getElementById('duration').value);
144
+ const guidance = parseFloat(document.getElementById('guidance').value);
145
+
146
+ const generateBtn = document.getElementById('generateBtn');
147
+ generateBtn.disabled = true;
148
+ generateBtn.textContent = 'Generating...';
149
+
150
+ this.updateInstallationStatus('Generating music... This may take a few moments.', 'info');
151
+
152
+ try {
153
+ // In a real implementation, this would call the actual DiffRhythm2 model
154
+ // For this demo, we'll simulate the generation process
155
+ await this.simulateMusicGeneration(prompt, duration, guidance);
156
+
157
+ // Show the result container
158
+ document.getElementById('generationResult').classList.remove('hidden');
159
+
160
+ this.updateInstallationStatus('Music generated successfully!', 'success');
161
+
162
+ } catch (error) {
163
+ console.error('Generation error:', error);
164
+ this.updateInstallationStatus(`Generation failed: ${error.message}`, 'error');
165
+ } finally {
166
+ generateBtn.disabled = false;
167
+ generateBtn.textContent = 'Generate Music';
168
+ }
169
+ }
170
+
171
+ async simulateMusicGeneration(prompt, duration, guidance) {
172
+ // Simulate generation progress
173
+ this.showProgress();
174
+
175
+ for (let i = 10; i <= 100; i += 10) {
176
+ await new Promise(resolve => setTimeout(resolve, 300));
177
+ this.updateProgress(`Generating: "${prompt}"...`, i);
178
+ }
179
+
180
+ // Create a mock audio file for demonstration
181
+ // In a real implementation, this would be the actual generated audio
182
+ const mockAudioData = this.createMockAudioData();
183
+ const audioUrl = URL.createObjectURL(mockAudioData);
184
+
185
+ const audioPlayer = document.getElementById('audioPlayer');
186
+ audioPlayer.src = audioUrl;
187
+
188
+ // Store the audio URL for download
189
+ audioPlayer.dataset.audioUrl = audioUrl;
190
+ }
191
+
192
+ createMockAudioData() {
193
+ // Create a simple silent audio file for demonstration
194
+ // In a real implementation, this would be the actual generated audio
195
+ const audioContext = new (window.AudioContext || window.webkitAudioContext)();
196
+ const duration = 10; // seconds
197
+ const sampleRate = 44100;
198
+ const numberOfChannels = 2;
199
+
200
+ // Create empty buffer
201
+ const frameCount = sampleRate * duration;
202
+ const arrayBuffer = new ArrayBuffer(44 + frameCount * numberOfChannels * 2);
203
+
204
+ return new Blob([arrayBuffer], { type: 'audio/wav' });
205
+ }
206
+
207
+ downloadAudio() {
208
+ const audioPlayer = document.getElementById('audioPlayer');
209
+ const audioUrl = audioPlayer.dataset.audioUrl;
210
+
211
+ if (!audioUrl) {
212
+ this.updateInstallationStatus('No audio to download!', 'error');
213
+ return;
214
+ }
215
+
216
+ const a = document.createElement('a');
217
+ a.href = audioUrl;
218
+ a.download = 'diffrhythm2_generated_music.wav';
219
+ document.body.appendChild(a);
220
+ a.click();
221
+ document.body.removeChild(a);
222
+ }
223
+
224
+ updateProgress(text, percent) {
225
+ document.getElementById('progressText').textContent = text;
226
+ document.getElementById('progressPercent').textContent = `${percent}%`;
227
+ document.getElementById('progressFill').style.width = `${percent}%`;
228
+ }
229
+
230
+ showProgress() {
231
+ document.getElementById('progressContainer').classList.remove('hidden');
232
+ }
233
+
234
+ hideProgress() {
235
+ document.getElementById('progressContainer').classList.add('hidden');
236
+ }
237
+
238
+ updateInstallationStatus(message, type) {
239
+ const statusElement = document.getElementById('status');
240
+ statusElement.textContent = message;
241
+ statusElement.className = `status-message status-${type}`;
242
+ }
243
+ }
244
+
245
+ // Example loader function
246
+ function loadExample(button) {
247
+ const exampleText = button.parentElement.querySelector('p').textContent;
248
+ document.getElementById('promptInput').value = exampleText;
249
+ }
250
+
251
+ // Initialize the application when the page loads
252
+ document.addEventListener('DOMContentLoaded', () => {
253
+ new DiffRhythm2Installer();
254
+ });
255
+
256
+ // Web Worker for handling heavy computations (if needed)
257
+ if (window.Worker) {
258
+ const worker = new Worker('worker.js');
259
+
260
+ worker.onmessage = function(e) {
261
+ console.log('Worker message:', e.data);
262
+ };
263
+ }