Update index.html
Browse files- index.html +38 -37
index.html
CHANGED
@@ -6,50 +6,51 @@
|
|
6 |
<title>Image Generator</title>
|
7 |
<script>
|
8 |
async function generateImage(e) {
|
9 |
-
|
10 |
|
11 |
-
|
12 |
-
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
generateButton.disabled = true;
|
22 |
-
generateButton.innerText = 'Generating...';
|
23 |
|
24 |
-
|
25 |
-
// Make a POST request to the API endpoint
|
26 |
-
const response = await fetch('https://742b-35-198-254-120.ngrok-free.app/generate-image/', {
|
27 |
-
method: 'POST',
|
28 |
-
headers: {
|
29 |
-
'Content-Type': 'application/x-www-form-urlencoded'
|
30 |
-
},
|
31 |
-
// Send the prompt as form data
|
32 |
-
body: `prompt=${encodeURIComponent(promptInput.value)}`
|
33 |
-
});
|
34 |
|
35 |
-
|
36 |
-
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
}
|
45 |
-
} catch (error) {
|
46 |
-
console.error('Network or other error:', error);
|
47 |
-
alert('Failed to generate image.');
|
48 |
-
} finally {
|
49 |
-
// Re-enable the button and reset its text regardless of the outcome
|
50 |
-
generateButton.disabled = false;
|
51 |
-
generateButton.innerText = 'Generate';
|
52 |
-
}
|
53 |
}
|
54 |
</script>
|
55 |
</head>
|
|
|
6 |
<title>Image Generator</title>
|
7 |
<script>
|
8 |
async function generateImage(e) {
|
9 |
+
e.preventDefault();
|
10 |
|
11 |
+
const generateButton = document.getElementById('generate-btn');
|
12 |
+
const promptInput = document.getElementById('prompt-input');
|
13 |
|
14 |
+
if (!promptInput.value.trim()) {
|
15 |
+
alert('Please enter a prompt.');
|
16 |
+
return;
|
17 |
+
}
|
18 |
+
|
19 |
+
// Disable the button and change its text to indicate loading
|
20 |
+
generateButton.disabled = true;
|
21 |
+
generateButton.innerText = 'Generating...';
|
22 |
+
|
23 |
+
try {
|
24 |
+
const response = await fetch('https://2c70-35-198-254-120.ngrok-free.app/generate-image/', {
|
25 |
+
method: 'POST',
|
26 |
+
headers: {
|
27 |
+
'Content-Type': 'application/x-www-form-urlencoded'
|
28 |
+
},
|
29 |
+
body: `prompt=${encodeURIComponent(promptInput.value)}`
|
30 |
+
});
|
31 |
|
32 |
+
const data = await response.json();
|
|
|
|
|
33 |
|
34 |
+
console.log(data); // Debugging: Log the API response to ensure data structure
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
if (response.ok) {
|
37 |
+
// Assuming 'data.imageBase64' is the correct path to access your base64 image data
|
38 |
+
const imageBase64 = data.imageBase64; // Check if this key matches your API's response
|
39 |
|
40 |
+
// Ensure you're using the correct MIME type: replace 'image/png' with the appropriate type if different
|
41 |
+
document.getElementById('generated-image').src = `data:image/png;base64,${imageBase64}`;
|
42 |
+
} else {
|
43 |
+
console.error('Error from API:', data);
|
44 |
+
alert('Failed to generate image.');
|
45 |
+
}
|
46 |
+
} catch (error) {
|
47 |
+
console.error('Network or other error:', error);
|
48 |
+
alert('Failed to generate image.');
|
49 |
+
} finally {
|
50 |
+
// Re-enable the button and reset its text regardless of the outcome
|
51 |
+
generateButton.disabled = false;
|
52 |
+
generateButton.innerText = 'Generate';
|
53 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
}
|
55 |
</script>
|
56 |
</head>
|