Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
from flask import Flask, request, jsonify, send_file, render_template_string, make_response
|
2 |
import requests
|
3 |
import io
|
4 |
-
import os
|
5 |
import random
|
6 |
from PIL import Image
|
7 |
from deep_translator import GoogleTranslator
|
@@ -9,8 +8,6 @@ from deep_translator import GoogleTranslator
|
|
9 |
app = Flask(__name__)
|
10 |
|
11 |
API_URL = "https://api-inference.huggingface.co/models/Ojimi/anime-kawai-diffusion"
|
12 |
-
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
13 |
-
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
14 |
timeout = 300 # タイムアウトを300秒に設定
|
15 |
|
16 |
# Function to query the API and return the generated image
|
@@ -43,7 +40,8 @@ def query(prompt, negative_prompt="", steps=35, cfg_scale=7, sampler="DPM++ 2M K
|
|
43 |
|
44 |
for attempt in range(3): # 最大3回の再試行
|
45 |
try:
|
46 |
-
|
|
|
47 |
if response.status_code != 200:
|
48 |
return None, f"Error: Failed to get image. Status code: {response.status_code}, Details: {response.text}"
|
49 |
|
@@ -74,7 +72,187 @@ def add_security_headers(response):
|
|
74 |
|
75 |
# HTML template for the index page
|
76 |
index_html = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
"""
|
79 |
|
80 |
@app.route('/')
|
@@ -104,4 +282,4 @@ def generate_image():
|
|
104 |
return send_file(img_bytes, mimetype='image/png')
|
105 |
|
106 |
if __name__ == "__main__":
|
107 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
1 |
from flask import Flask, request, jsonify, send_file, render_template_string, make_response
|
2 |
import requests
|
3 |
import io
|
|
|
4 |
import random
|
5 |
from PIL import Image
|
6 |
from deep_translator import GoogleTranslator
|
|
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
API_URL = "https://api-inference.huggingface.co/models/Ojimi/anime-kawai-diffusion"
|
|
|
|
|
11 |
timeout = 300 # タイムアウトを300秒に設定
|
12 |
|
13 |
# Function to query the API and return the generated image
|
|
|
40 |
|
41 |
for attempt in range(3): # 最大3回の再試行
|
42 |
try:
|
43 |
+
# Authorization header is removed
|
44 |
+
response = requests.post(API_URL, json=payload, timeout=timeout)
|
45 |
if response.status_code != 200:
|
46 |
return None, f"Error: Failed to get image. Status code: {response.status_code}, Details: {response.text}"
|
47 |
|
|
|
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 |
+
function startDrawing(event) {
|
100 |
+
isDrawing = true;
|
101 |
+
[lastX, lastY] = getMousePos(event);
|
102 |
+
}
|
103 |
+
function draw(event) {
|
104 |
+
if (!isDrawing) return;
|
105 |
+
const canvas = document.getElementById('canvas');
|
106 |
+
const ctx = canvas.getContext('2d');
|
107 |
+
const [newX, newY] = getMousePos(event);
|
108 |
+
ctx.beginPath();
|
109 |
+
ctx.moveTo(lastX, lastY);
|
110 |
+
ctx.lineTo(newX, newY);
|
111 |
+
ctx.strokeStyle = penColor;
|
112 |
+
ctx.lineWidth = penSize;
|
113 |
+
ctx.stroke();
|
114 |
+
ctx.closePath();
|
115 |
+
[lastX, lastY] = [newX, newY]; // 新しい位置を保存
|
116 |
+
}
|
117 |
+
function stopDrawing() {
|
118 |
+
isDrawing = false;
|
119 |
+
}
|
120 |
+
function getMousePos(event) {
|
121 |
+
const rect = document.getElementById('canvas').getBoundingClientRect();
|
122 |
+
return [event.clientX - rect.left, event.clientY - rect.top];
|
123 |
+
}
|
124 |
+
function setPenSize(value) {
|
125 |
+
penSize = value;
|
126 |
+
}
|
127 |
+
function setPenColor(value) {
|
128 |
+
penColor = value;
|
129 |
+
}
|
130 |
+
async function generateImage() {
|
131 |
+
const prompt = document.getElementById("prompt").value;
|
132 |
+
const negative_prompt = document.getElementById("negative_prompt").value;
|
133 |
+
const width = document.getElementById("width").value;
|
134 |
+
const height = document.getElementById("height").value;
|
135 |
+
const steps = document.getElementById("steps").value;
|
136 |
+
const cfgs = document.getElementById("cfgs").value;
|
137 |
+
const sampler = document.getElementById("sampler").value;
|
138 |
+
const strength = document.getElementById("strength").value;
|
139 |
+
const seed = document.getElementById("seed").value;
|
140 |
+
const button = document.getElementById("generate-button");
|
141 |
+
const errorMessage = document.getElementById("error-message");
|
142 |
+
button.disabled = true; // ボタンを無効化
|
143 |
+
errorMessage.textContent = ''; // エラーメッセージをクリア
|
144 |
+
|
145 |
+
const params = new URLSearchParams({
|
146 |
+
prompt,
|
147 |
+
negative_prompt,
|
148 |
+
width,
|
149 |
+
height,
|
150 |
+
steps,
|
151 |
+
cfgs,
|
152 |
+
sampler,
|
153 |
+
strength,
|
154 |
+
seed
|
155 |
+
});
|
156 |
+
const reqURL = `https://soiz-sdapi.hf.space/generate?${params.toString()}`;
|
157 |
+
document.getElementById("reqURL").value = reqURL; // URLを設定
|
158 |
+
|
159 |
+
try {
|
160 |
+
const response = await fetch(reqURL);
|
161 |
+
if (!response.ok) {
|
162 |
+
const errorText = await response.text(); // エラーメッセージを取得
|
163 |
+
throw new Error(`画像生成に失敗しました: ${errorText}`);
|
164 |
+
}
|
165 |
+
const imageBlob = await response.blob();
|
166 |
+
const reader = new FileReader();
|
167 |
+
reader.onloadend = function () {
|
168 |
+
const img = document.getElementById("generated-image");
|
169 |
+
img.src = reader.result; // dataURLを設��
|
170 |
+
img.style.display = 'block'; // 画像を表示
|
171 |
+
// 画像をキャンバスに描画
|
172 |
+
const canvas = document.getElementById('canvas');
|
173 |
+
const ctx = canvas.getContext('2d');
|
174 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height); // キャンバスをクリア
|
175 |
+
const imgElement = new Image();
|
176 |
+
imgElement.src = reader.result;
|
177 |
+
imgElement.onload = function () {
|
178 |
+
canvas.width = imgElement.width;
|
179 |
+
canvas.height = imgElement.height;
|
180 |
+
ctx.drawImage(imgElement, 0, 0);
|
181 |
+
};
|
182 |
+
};
|
183 |
+
reader.readAsDataURL(imageBlob);
|
184 |
+
} catch (error) {
|
185 |
+
errorMessage.textContent = error.message; // エラーメッセージを表示
|
186 |
+
console.error(error); // エラーをコンソールに出力
|
187 |
+
} finally {
|
188 |
+
button.disabled = false; // ボタンを再有効化
|
189 |
+
}
|
190 |
+
}
|
191 |
+
function syncWidth(value) {
|
192 |
+
document.getElementById("width-slider").value = value;
|
193 |
+
}
|
194 |
+
function syncHeight(value) {
|
195 |
+
document.getElementById("height-slider").value = value;
|
196 |
+
}
|
197 |
+
function updateWidthInput() {
|
198 |
+
const widthSlider = document.getElementById("width-slider");
|
199 |
+
const widthInput = document.getElementById("width");
|
200 |
+
widthInput.value = widthSlider.value;
|
201 |
+
}
|
202 |
+
function updateHeightInput() {
|
203 |
+
const heightSlider = document.getElementById("height-slider");
|
204 |
+
const heightInput = document.getElementById("height");
|
205 |
+
heightInput.value = heightSlider.value;
|
206 |
+
}
|
207 |
+
</script>
|
208 |
+
</head>
|
209 |
+
<body>
|
210 |
+
<h1>FLUX.1-Dev Image Generator</h1>
|
211 |
+
<form onsubmit="event.preventDefault(); generateImage();">
|
212 |
+
<label for="prompt">Prompt:</label><br>
|
213 |
+
<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>
|
214 |
+
|
215 |
+
<label for="negative_prompt">Negative Prompt:</label><br>
|
216 |
+
<textarea id="negative_prompt" name="negative_prompt" rows="4" cols="50" placeholder="Enter negative prompt"></textarea><br><br>
|
217 |
+
|
218 |
+
<label for="width">Width:</label>
|
219 |
+
<input id="width" name="width" type="number" value="1024" onchange="syncWidth(this.value)"><br>
|
220 |
+
<input id="width-slider" type="range" min="256" max="2048" value="1024" oninput="updateWidthInput()"><br><br>
|
221 |
+
|
222 |
+
<label for="height">Height:</label>
|
223 |
+
<input id="height" name="height" type="number" value="1024" onchange="syncHeight(this.value)"><br>
|
224 |
+
<input id="height-slider" type="range" min="256" max="2048" value="1024" oninput="updateHeightInput()"><br><br>
|
225 |
+
|
226 |
+
<label for="steps">Steps:</label>
|
227 |
+
<input id="steps" name="steps" type="number" value="35" min="1" max="100"><br><br>
|
228 |
+
|
229 |
+
<label for="cfgs">CFG Scale:</label>
|
230 |
+
<input id="cfgs" name="cfgs" type="number" value="7" step="0.1"><br><br>
|
231 |
+
|
232 |
+
<label for="sampler">Sampler:</label>
|
233 |
+
<select id="sampler" name="sampler">
|
234 |
+
<option value="DPM++ 2M Karras">DPM++ 2M Karras</option>
|
235 |
+
<option value="DPM++ 2S a">DPM++ 2S a</option>
|
236 |
+
<option value="DPM++ SDE Karras">DPM++ SDE Karras</option>
|
237 |
+
</select><br><br>
|
238 |
+
|
239 |
+
<label for="strength">Strength:</label>
|
240 |
+
<input id="strength" name="strength" type="number" value="0.7" step="0.1" min="0.0" max="1.0"><br><br>
|
241 |
+
|
242 |
+
<label for="seed">Seed:</label>
|
243 |
+
<input id="seed" name="seed" type="number" value="-1" min="-1"><br><br>
|
244 |
|
245 |
+
<button id="generate-button" type="submit">Generate Image</button>
|
246 |
+
<p id="error-message" style="color: red;"></p>
|
247 |
+
</form>
|
248 |
+
<br>
|
249 |
+
<div>
|
250 |
+
<canvas id="canvas" width="1024" height="1024"></canvas>
|
251 |
+
</div>
|
252 |
+
<img id="generated-image" src="" alt="Generated Image" style="display: none;"><br>
|
253 |
+
<textarea id="reqURL" readonly style="width: 100%; height: 50px;"></textarea>
|
254 |
+
</body>
|
255 |
+
</html>
|
256 |
"""
|
257 |
|
258 |
@app.route('/')
|
|
|
282 |
return send_file(img_bytes, mimetype='image/png')
|
283 |
|
284 |
if __name__ == "__main__":
|
285 |
+
app.run(host='0.0.0.0', port=7860)
|