-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
97 lines (85 loc) · 3.63 KB
/
script.js
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
// Function to update the image preview
function updateImagePreview(file) {
const reader = new FileReader();
reader.onload = function(e) {
const imagePreview = document.getElementById('imagePreview');
if (imagePreview) {
imagePreview.src = e.target.result;
imagePreview.style.display = 'block';
}
};
reader.readAsDataURL(file);
}
// Set up event listeners for the file input and drag-and-drop area
document.getElementById('imageInput').addEventListener('change', function() {
if (this.files && this.files[0]) {
updateImagePreview(this.files[0]);
}
});
document.getElementById('drop-area').addEventListener('dragover', (event) => {
event.stopPropagation();
event.preventDefault();
event.dataTransfer.dropEffect = 'copy';
});
document.getElementById('drop-area').addEventListener('drop', (event) => {
event.stopPropagation();
event.preventDefault();
const files = event.dataTransfer.files;
if (files.length > 0) {
document.getElementById('imageInput').files = files;
updateImagePreview(files[0]);
}
});
// Handle the generate button click
document.getElementById('generateButton').addEventListener('click', function() {
const input = document.getElementById('imageInput');
if (input.files && input.files[0]) {
const formData = new FormData();
formData.append('file', input.files[0]);
// Display a loading message while processing the image
const loadingMessage = document.getElementById('loadingMessage');
loadingMessage.style.display = 'block';
fetch('/process-image', {
method: 'POST',
body: formData
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json(); // This will parse the JSON if the response is OK
})
.then(data => {
loadingMessage.style.display = 'none'; // Hide loading message
document.getElementById('textOutput').textContent = data.textOutput; // Update text output
console.log(`/audio/${data.speechAudioFile}`); // output
const speechAudioPlayer = document.getElementById('speechAudioPlayer');
speechAudioPlayer.src = `/audio/${data.speechAudioFile}`;
speechAudioPlayer.style.display = 'block';
const downloadMusicLink = document.getElementById('downloadMusicLink');
downloadMusicLink.href = `/audio/${data.speechAudioFile}`;
downloadMusicLink.style.display = 'block';
downloadMusicLink.textContent = 'Download Your BrandVision';
})
.catch(error => {
loadingMessage.style.display = 'none'; // Hide loading message
console.error('Error:', error);
alert('There was a problem with your request: ' + error.message);
});
} else {
alert('Please upload an image first.');
}
});
// Optionally
document.getElementById('generateMusicButton').disabled = true;
function scrollToAnalysis() {
// Check if the textOutput has content
var textOutput = document.getElementById('textOutput');
if (textOutput.textContent.trim() !== '') {
// If content is not empty, scroll to the 'response-section'
document.getElementById('response-section').scrollIntoView({ behavior: 'smooth' });
} else {
// If content is empty, wait and then try to scroll again
setTimeout(scrollToAnalysis, 500);
}
}