-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_worker.js
163 lines (145 loc) · 4.1 KB
/
_worker.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
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
import { Ai } from './vendor/@cloudflare/ai.js';
export default {
async fetch(request, env) {
if(request.method == "GET")
{
return new Response(`<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
<title>SDXL Image Generator</title>
<style>
.grow-wrap {
/* easy way to plop the elements on top of each other and have them both sized based on the tallest one's height */
display: grid;
}
.grow-wrap::after {
/* Note the weird space! Needed to preventy jumpy behavior */
content: attr(data-replicated-value) \" \";
/* This is how textarea text behaves */
white-space: pre-wrap;
/* Hidden from view, clicks, and screen readers */
visibility: hidden;
}
.grow-wrap > textarea {
/* You could leave this, but after a user resizes, then it ruins the auto sizing */
resize: none;
/* Firefox shows scrollbar on growth, you can hide like this. */
overflow: hidden;
}
.grow-wrap > textarea,
.grow-wrap::after {
/* Identical styling required!! */
border: 1px solid black;
padding: 0.5rem;
font: inherit;
/* Place on top of each other */
grid-area: 1 / 1 / 2 / 2;
}
body {
margin: 2rem;
font: 1rem/1.4 system-ui, sans-serif;
}
label {
display: block;
}
input[type=submit] {
background-color: #335ccb;
border: none;
color: white;
padding: 16px 32px;
text-decoration: none;
margin: 4px 2px;
cursor: pointer;
}
</style>
</head>
<body>
<form action=\"\" method=\"post\" enctype=\"multipart/form-data\">
<label for=\"password\">Password:</label>
<input type=\"password\" id=\"password\" name=\"password\"><br>
<label for=\"prompt\">Prompt:</label>
<div class=\"grow-wrap\">
<textarea name=\"prompt\" id=\"prompt\" onInput=\"this.parentNode.dataset.replicatedValue = this.value\"></textarea>
</div>
<br>
<label for=\"image\">Image:</label>
<input type=\"file\" id=\"image\" name=\"image\"><br><br>
<input type=\"submit\" value=\"Generate\">
</form>
</body>
</html>`, {
headers: {
'content-type': 'text/html; charset=utf-8'
}
});
}
const ai = new Ai(env.AI);
const url = new URL(request.url);
const params = url.searchParams;
let ACCESS_PASSWORD = `${env.ACCESS_PASSWORD}`;
if(ACCESS_PASSWORD == null)
{
ACCESS_PASSWORD = "";
}
const formData = await request.formData();
const password = formData.get('password');
const prompt = formData.get('prompt');
const file = formData.get('image');
if(prompt == null || prompt == "")
{
return new Response("What are you doing here :-| ???");
}
if(password != ACCESS_PASSWORD)
{
return new Response("Wrong password :-/ ???");
}
let chunks = null;
if (file != null && file != "")
{
const reader = await file.stream().getReader();
chunks = [];
let result = await reader.read();
while (!result.done)
{
chunks.push(result.value);
result = await reader.read();
}
}
let response = new Uint8Array();
let model = ""
try
{
if(chunks == null)
{
const inputs = { prompt: prompt };
model = '@cf/stabilityai/stable-diffusion-xl-base-1.0';
response = await ai.run(model, inputs);
}
else
{
const inputImage = new Uint8Array(chunks.reduce((acc, chunk) => acc.concat(Array.from(chunk)), []));
const inputs = {
prompt: prompt,
image: [...inputImage],
};
model = '@cf/runwayml/stable-diffusion-v1-5-img2img';
response = await ai.run(model, inputs);
}
}
catch (e)
{
if (e instanceof Error)
{
return new Response(e.name + '\n' + e.message + '\n' + e.stack, { status: 500 });
}
}
return new Response(response, {
headers: {
'content-type': 'image/png',
'model': model
}
});
}
};