-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
128 lines (116 loc) · 3.33 KB
/
Copy pathindex.html
File metadata and controls
128 lines (116 loc) · 3.33 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Prompt to Photo Generator (Replicate)</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f9f9f9;
margin: 0;
padding: 20px;
}
.container {
max-width: 700px;
margin: auto;
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
h2 {
text-align: center;
margin-bottom: 20px;
}
label {
display: block;
margin: 10px 0 5px;
}
input[type="text"], input[type="file"] {
width: 100%;
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
background-color: #007bff;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
#output {
margin-top: 20px;
text-align: center;
}
#output img {
max-width: 100%;
border-radius: 8px;
margin-top: 15px;
}
</style>
</head>
<body>
<div class="container">
<h2>Prompt to Photo Generator (Replicate)</h2>
<label for="prompt">Enter a prompt:</label>
<input type="text" id="prompt" placeholder="e.g. A majestic mountain landscape at sunrise" />
<label for="reference">Upload a reference photo (optional):</label>
<input type="file" id="reference" accept="image/*" />
<button onclick="generatePhoto()">Generate Photo</button>
<div id="output"></div>
</div>
<script>
async function generatePhoto() {
const prompt = document.getElementById("prompt").value;
const reference = document.getElementById("reference").files[0];
const output = document.getElementById("output");
output.innerHTML = "<p>Generating image...</p>";
let imageBase64 = null;
if (reference) {
imageBase64 = await toBase64(reference);
}
const response = await fetch("/api/generate", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ prompt, imageBase64 })
});
const prediction = await response.json();
if (prediction.error) {
output.innerHTML = `<p style='color:red;'>Error: ${prediction.error}</p>`;
return;
}
const pollUrl = prediction.urls.get;
let image = null;
while (!image) {
await new Promise(res => setTimeout(res, 3000));
const pollRes = await fetch(pollUrl);
const pollData = await pollRes.json();
if (pollData.status === "succeeded") {
image = pollData.output[0];
} else if (pollData.status === "failed") {
output.innerHTML = `<p style='color:red;'>Generation failed</p>`;
return;
}
}
output.innerHTML = `<p><strong>Prompt:</strong> ${prompt}</p><img src="${image}" alt="Generated Image" />`;
}
function toBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});
}
</script>
</body>
</html>