soiz commited on
Commit
26da075
·
verified ·
1 Parent(s): b5fa1af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -23
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from flask import Flask, request, jsonify, render_template_string
2
  import requests
3
  import io
4
  import os
@@ -69,7 +69,7 @@ index_html = """
69
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
70
  <title>FLUX.1-Dev Image Generator</title>
71
  <script>
72
- function generateImage() {
73
  const prompt = document.getElementById("prompt").value;
74
  const negative_prompt = document.getElementById("negative_prompt").value;
75
  const width = document.getElementById("width").value;
@@ -80,21 +80,36 @@ index_html = """
80
  const strength = document.getElementById("strength").value;
81
  const seed = document.getElementById("seed").value;
82
 
83
- const img = document.getElementById("generated-image");
84
  const button = document.getElementById("generate-button");
85
  button.disabled = true; // ボタンを無効化
86
 
87
- const imgSrc = `/generate?prompt=${encodeURIComponent(prompt)}&negative_prompt=${encodeURIComponent(negative_prompt)}&width=${width}&height=${height}&steps=${steps}&cfgs=${cfgs}&sampler=${sampler}&strength=${strength}&seed=${seed}`;
88
-
89
- img.src = imgSrc; // 画像のsrcを設定
90
-
91
- img.onload = function() {
92
- button.disabled = false; // 画像が読み込まれたらボタンを再有効化
93
- };
94
- img.onerror = function() {
95
- alert("画像の生成に失敗しました。");
96
- button.disabled = false; // エラー時もボタンを再有効化
97
- };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  }
99
  </script>
100
  </head>
@@ -138,7 +153,7 @@ index_html = """
138
  <button type="submit" id="generate-button">Generate Image</button>
139
  </form>
140
  <h2>Generated Image:</h2>
141
- <img id="generated-image" src="" alt="Generated Image" style="max-width: 100%; height: auto; display: none;" onload="this.style.display='block';" onerror="this.style.display='none';">
142
  </body>
143
  </html>
144
  """
@@ -171,14 +186,8 @@ def generate_image():
171
  image.save(img_io, 'PNG')
172
  img_io.seek(0)
173
 
174
- # Create a unique filename for the image
175
- image_filename = f"generated_image_{random.randint(1, 10000)}.png"
176
-
177
- # Save the image to a file
178
- image.save(os.path.join('static', image_filename))
179
-
180
- # Return the image URL
181
- return jsonify({"url": f"/static/{image_filename}"})
182
 
183
  if __name__ == '__main__':
184
  # Ensure the 'static' folder exists for saving images
 
1
+ from flask import Flask, request, jsonify, send_file, render_template_string
2
  import requests
3
  import io
4
  import os
 
69
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
70
  <title>FLUX.1-Dev Image Generator</title>
71
  <script>
72
+ async function generateImage() {
73
  const prompt = document.getElementById("prompt").value;
74
  const negative_prompt = document.getElementById("negative_prompt").value;
75
  const width = document.getElementById("width").value;
 
80
  const strength = document.getElementById("strength").value;
81
  const seed = document.getElementById("seed").value;
82
 
 
83
  const button = document.getElementById("generate-button");
84
  button.disabled = true; // ボタンを無効化
85
 
86
+ const params = new URLSearchParams({
87
+ prompt,
88
+ negative_prompt,
89
+ width,
90
+ height,
91
+ steps,
92
+ cfgs,
93
+ sampler,
94
+ strength,
95
+ seed
96
+ });
97
+
98
+ try {
99
+ const response = await fetch(`/generate?${params.toString()}`);
100
+ if (!response.ok) {
101
+ throw new Error('画像生成に失敗しました');
102
+ }
103
+ const imageBlob = await response.blob();
104
+ const imageUrl = URL.createObjectURL(imageBlob);
105
+ const img = document.getElementById("generated-image");
106
+ img.src = imageUrl; // 生成された画像を設定
107
+ img.style.display = 'block'; // 画像を表示
108
+ } catch (error) {
109
+ alert(error.message);
110
+ } finally {
111
+ button.disabled = false; // ボタンを再有効化
112
+ }
113
  }
114
  </script>
115
  </head>
 
153
  <button type="submit" id="generate-button">Generate Image</button>
154
  </form>
155
  <h2>Generated Image:</h2>
156
+ <img id="generated-image" src="" alt="Generated Image" style="max-width: 100%; height: auto; display: none;">
157
  </body>
158
  </html>
159
  """
 
186
  image.save(img_io, 'PNG')
187
  img_io.seek(0)
188
 
189
+ # Return the image directly
190
+ return send_file(img_io, mimetype='image/png')
 
 
 
 
 
 
191
 
192
  if __name__ == '__main__':
193
  # Ensure the 'static' folder exists for saving images