Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -33,6 +33,7 @@ def query(prompt, negative_prompt="", steps=35, cfg_scale=7, sampler="DPM++ 2M K
|
|
33 |
"cfg_scale": cfg_scale,
|
34 |
"seed": seed if seed != -1 else random.randint(1, 1000000000),
|
35 |
"strength": strength,
|
|
|
36 |
"parameters": {
|
37 |
"width": width,
|
38 |
"height": height
|
@@ -72,210 +73,6 @@ def add_security_headers(response):
|
|
72 |
|
73 |
# HTML template for the index page
|
74 |
index_html = """
|
75 |
-
<!DOCTYPE html>
|
76 |
-
<html lang="ja">
|
77 |
-
<head>
|
78 |
-
<meta charset="UTF-8">
|
79 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
80 |
-
<title>FLUX.1-Dev Image Generator</title>
|
81 |
-
<style>
|
82 |
-
#canvas {
|
83 |
-
position: absolute;
|
84 |
-
top: 0;
|
85 |
-
left: 0;
|
86 |
-
cursor: crosshair;
|
87 |
-
}
|
88 |
-
#container {
|
89 |
-
position: relative;
|
90 |
-
display: inline-block;
|
91 |
-
}
|
92 |
-
</style>
|
93 |
-
<script>
|
94 |
-
let isDrawing = false;
|
95 |
-
let lastX = 0;
|
96 |
-
let lastY = 0;
|
97 |
-
let penSize = 5;
|
98 |
-
let penColor = '#000000';
|
99 |
-
|
100 |
-
function startDrawing(event) {
|
101 |
-
isDrawing = true;
|
102 |
-
[lastX, lastY] = getMousePos(event);
|
103 |
-
}
|
104 |
-
|
105 |
-
function draw(event) {
|
106 |
-
if (!isDrawing) return;
|
107 |
-
|
108 |
-
const canvas = document.getElementById('canvas');
|
109 |
-
const ctx = canvas.getContext('2d');
|
110 |
-
const [newX, newY] = getMousePos(event);
|
111 |
-
|
112 |
-
ctx.beginPath();
|
113 |
-
ctx.moveTo(lastX, lastY);
|
114 |
-
ctx.lineTo(newX, newY);
|
115 |
-
ctx.strokeStyle = penColor;
|
116 |
-
ctx.lineWidth = penSize;
|
117 |
-
ctx.stroke();
|
118 |
-
ctx.closePath();
|
119 |
-
|
120 |
-
[lastX, lastY] = [newX, newY]; // 新しい位置を保存
|
121 |
-
}
|
122 |
-
|
123 |
-
function stopDrawing() {
|
124 |
-
isDrawing = false;
|
125 |
-
}
|
126 |
-
|
127 |
-
function getMousePos(event) {
|
128 |
-
const rect = document.getElementById('canvas').getBoundingClientRect();
|
129 |
-
return [event.clientX - rect.left, event.clientY - rect.top];
|
130 |
-
}
|
131 |
-
|
132 |
-
function setPenSize(value) {
|
133 |
-
penSize = value;
|
134 |
-
}
|
135 |
-
|
136 |
-
function setPenColor(value) {
|
137 |
-
penColor = value;
|
138 |
-
}
|
139 |
-
|
140 |
-
async function generateImage() {
|
141 |
-
const prompt = document.getElementById("prompt").value;
|
142 |
-
const negative_prompt = document.getElementById("negative_prompt").value;
|
143 |
-
const width = document.getElementById("width").value;
|
144 |
-
const height = document.getElementById("height").value;
|
145 |
-
const steps = document.getElementById("steps").value;
|
146 |
-
const cfgs = document.getElementById("cfgs").value;
|
147 |
-
const sampler = document.getElementById("sampler").value;
|
148 |
-
const strength = document.getElementById("strength").value;
|
149 |
-
const seed = document.getElementById("seed").value;
|
150 |
-
const button = document.getElementById("generate-button");
|
151 |
-
const errorMessage = document.getElementById("error-message");
|
152 |
-
button.disabled = true; // ボタンを無効化
|
153 |
-
errorMessage.textContent = ''; // エラーメッセージをクリア
|
154 |
-
|
155 |
-
const params = new URLSearchParams({
|
156 |
-
prompt,
|
157 |
-
negative_prompt,
|
158 |
-
width,
|
159 |
-
height,
|
160 |
-
steps,
|
161 |
-
cfgs,
|
162 |
-
sampler,
|
163 |
-
strength,
|
164 |
-
seed
|
165 |
-
});
|
166 |
-
|
167 |
-
const reqURL = `https://soiz-dreamlike-anime-1-0api.hf.space/generate?${params.toString()}`;
|
168 |
-
document.getElementById("reqURL").value = reqURL; // URLを設定
|
169 |
-
|
170 |
-
try {
|
171 |
-
const response = await fetch(reqURL);
|
172 |
-
if (!response.ok) {
|
173 |
-
const errorText = await response.text(); // エラーメッセージを取得
|
174 |
-
throw new Error(`画像生成に失敗しました: ${errorText}`);
|
175 |
-
}
|
176 |
-
const imageBlob = await response.blob();
|
177 |
-
const reader = new FileReader();
|
178 |
-
reader.onloadend = function () {
|
179 |
-
const img = document.getElementById("generated-image");
|
180 |
-
img.src = reader.result; // dataURLを設定
|
181 |
-
img.style.display = 'block'; // 画像を表示
|
182 |
-
|
183 |
-
// 画像をキャンバスに描画
|
184 |
-
const canvas = document.getElementById('canvas');
|
185 |
-
const ctx = canvas.getContext('2d');
|
186 |
-
ctx.clearRect(0, 0, canvas.width, canvas.height); // キャンバスをクリア
|
187 |
-
const imgElement = new Image();
|
188 |
-
imgElement.src = reader.result;
|
189 |
-
imgElement.onload = function () {
|
190 |
-
canvas.width = imgElement.width;
|
191 |
-
canvas.height = imgElement.height;
|
192 |
-
ctx.drawImage(imgElement, 0, 0);
|
193 |
-
};
|
194 |
-
};
|
195 |
-
reader.readAsDataURL(imageBlob);
|
196 |
-
} catch (error) {
|
197 |
-
errorMessage.textContent = error.message; // エラーメッセージを表示
|
198 |
-
console.error(error); // エラーをコンソールに出力
|
199 |
-
} finally {
|
200 |
-
button.disabled = false; // ボタンを再有効化
|
201 |
-
}
|
202 |
-
}
|
203 |
-
|
204 |
-
function syncWidth(value) {
|
205 |
-
document.getElementById("width-slider").value = value;
|
206 |
-
}
|
207 |
-
|
208 |
-
function syncHeight(value) {
|
209 |
-
document.getElementById("height-slider").value = value;
|
210 |
-
}
|
211 |
-
|
212 |
-
function updateWidthInput() {
|
213 |
-
const widthSlider = document.getElementById("width-slider");
|
214 |
-
const widthInput = document.getElementById("width");
|
215 |
-
widthInput.value = widthSlider.value;
|
216 |
-
}
|
217 |
-
|
218 |
-
function updateHeightInput() {
|
219 |
-
const heightSlider = document.getElementById("height-slider");
|
220 |
-
const heightInput = document.getElementById("height");
|
221 |
-
heightInput.value = heightSlider.value;
|
222 |
-
}
|
223 |
-
</script>
|
224 |
-
</head>
|
225 |
-
<body>
|
226 |
-
<h1>FLUX.1-Dev Image Generator</h1>
|
227 |
-
<form onsubmit="event.preventDefault(); generateImage();">
|
228 |
-
<label for="prompt">Prompt:</label><br>
|
229 |
-
<textarea id="prompt" name="prompt" rows="4" cols="50" placeholder="Enter your prompt" required>real, sky, blue, clouds, light blue, light blue sky, sun, 32K, many islands floating in the sky, Ghibli, many islands, many islands in the sky, islands in the clouds, old islands, fog, high quality, cool</textarea><br><br>
|
230 |
-
|
231 |
-
<label for="negative_prompt">Negative Prompt:</label><br>
|
232 |
-
<textarea id="negative_prompt" name="negative_prompt" rows="4" cols="50" placeholder="Enter negative prompt (optional)"></textarea><br><br>
|
233 |
-
|
234 |
-
<label for="width">Width:</label>
|
235 |
-
<input type="number" id="width" name="width" value="1024" required>
|
236 |
-
<input type="range" id="width-slider" min="512" max="2048" value="1024" oninput="updateWidthInput()"><br><br>
|
237 |
-
|
238 |
-
<label for="height">Height:</label>
|
239 |
-
<input type="number" id="height" name="height" value="1024" required>
|
240 |
-
<input type="range" id="height-slider" min="512" max="2048" value="1024" oninput="updateHeightInput()"><br><br>
|
241 |
-
|
242 |
-
<label for="steps">Steps:</label>
|
243 |
-
<input type="number" id="steps" name="steps" value="35" required><br><br>
|
244 |
-
|
245 |
-
<label for="cfgs">CFG Scale:</label>
|
246 |
-
<input type="number" id="cfgs" name="cfgs" value="7" step="0.1" required><br><br>
|
247 |
-
|
248 |
-
<label for="sampler">Sampler:</label>
|
249 |
-
<input type="text" id="sampler" name="sampler" value="DPM++ 2M Karras" required><br><br>
|
250 |
-
|
251 |
-
<label for="strength">Strength:</label>
|
252 |
-
<input type="number" id="strength" name="strength" value="0.7" step="0.1" required><br><br>
|
253 |
-
|
254 |
-
<label for="seed">Seed (optional):</label>
|
255 |
-
<input type="number" id="seed" name="seed" value="-1"><br><br>
|
256 |
-
|
257 |
-
<button id="generate-button" type="submit">Generate Image</button><br><br>
|
258 |
-
|
259 |
-
<label for="reqURL">Request URL (for debugging):</label><br>
|
260 |
-
<input type="text" id="reqURL" name="reqURL" size="100"><br><br>
|
261 |
-
|
262 |
-
<span id="error-message" style="color: red;"></span>
|
263 |
-
</form>
|
264 |
-
|
265 |
-
<h2>Generated Image:</h2>
|
266 |
-
<img id="generated-image" src="" alt="Generated Image" style="display:none; width: 100%; height: auto;"><br><br>
|
267 |
-
|
268 |
-
<h2>Draw on Canvas:</h2>
|
269 |
-
<div id="container">
|
270 |
-
<canvas id="canvas" width="1024" height="1024" style="border:1px solid black;" onmousedown="startDrawing(event)" onmousemove="draw(event)" onmouseup="stopDrawing()" onmouseleave="stopDrawing()"></canvas><br><br>
|
271 |
-
<label for="pen-size">Pen Size:</label>
|
272 |
-
<input type="number" id="pen-size" name="pen-size" value="5" min="1" max="50" oninput="setPenSize(this.value)"><br><br>
|
273 |
-
|
274 |
-
<label for="pen-color">Pen Color:</label>
|
275 |
-
<input type="color" id="pen-color" name="pen-color" value="#000000" onchange="setPenColor(this.value)"><br><br>
|
276 |
-
</div>
|
277 |
-
</body>
|
278 |
-
</html>
|
279 |
"""
|
280 |
|
281 |
@app.route('/')
|
|
|
33 |
"cfg_scale": cfg_scale,
|
34 |
"seed": seed if seed != -1 else random.randint(1, 1000000000),
|
35 |
"strength": strength,
|
36 |
+
"negative_prompt": negative_prompt,
|
37 |
"parameters": {
|
38 |
"width": width,
|
39 |
"height": height
|
|
|
73 |
|
74 |
# HTML template for the index page
|
75 |
index_html = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
"""
|
77 |
|
78 |
@app.route('/')
|