soiz commited on
Commit
a2384b5
·
verified ·
1 Parent(s): 618d35c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -31
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from flask import Flask, request, jsonify, send_file, render_template_string
2
  import requests
3
  import io
4
  import random
@@ -16,7 +16,7 @@ def query(prompt, negative_prompt="", steps=35, cfg_scale=7, sampler="DPM++ 2M K
16
  return None, "Prompt is required"
17
 
18
  key = random.randint(0, 999)
19
-
20
  # Translate the prompt from Russian to English if necessary
21
  prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
22
  print(f'Generation {key} translation: {prompt}')
@@ -40,6 +40,7 @@ def query(prompt, negative_prompt="", steps=35, cfg_scale=7, sampler="DPM++ 2M K
40
 
41
  for attempt in range(3): # 最大3回の再試行
42
  try:
 
43
  response = requests.post(API_URL, json=payload, timeout=timeout)
44
  if response.status_code != 200:
45
  return None, f"Error: Failed to get image. Status code: {response.status_code}, Details: {response.text}"
@@ -212,47 +213,44 @@ index_html = """
212
  <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>
213
 
214
  <label for="negative_prompt">Negative Prompt:</label><br>
215
- <textarea id="negative_prompt" name="negative_prompt" rows="2" cols="50" placeholder="Enter your negative prompt"></textarea><br><br>
216
-
217
- <label for="width">Width: <span id="width-value">1024</span></label><br>
218
- <input type="range" id="width-slider" name="width-slider" min="256" max="4096" value="1024" oninput="syncWidth(this.value)" onchange="updateWidthInput()"><br>
219
- <input type="number" id="width" name="width" value="1024" min="256" max="4096" onchange="updateWidthInput()"><br><br>
220
 
221
- <label for="height">Height: <span id="height-value">1024</span></label><br>
222
- <input type="range" id="height-slider" name="height-slider" min="256" max="4096" value="1024" oninput="syncHeight(this.value)" onchange="updateHeightInput()"><br>
223
- <input type="number" id="height" name="height" value="1024" min="256" max="4096" onchange="updateHeightInput()"><br><br>
 
 
 
 
224
 
225
  <label for="steps">Steps:</label>
226
- <input type="number" id="steps" name="steps" value="35" min="1"><br><br>
227
-
228
  <label for="cfgs">CFG Scale:</label>
229
- <input type="number" id="cfgs" name="cfgs" value="7" min="1" step="0.1"><br><br>
230
-
231
  <label for="sampler">Sampler:</label>
232
  <select id="sampler" name="sampler">
233
  <option value="DPM++ 2M Karras">DPM++ 2M Karras</option>
234
- <option value="DDIM">DDIM</option>
235
  <option value="DPM++ SDE Karras">DPM++ SDE Karras</option>
236
- <option value="Euler">Euler</option>
237
- <option value="LMS">LMS</option>
238
  </select><br><br>
239
-
240
  <label for="strength">Strength:</label>
241
- <input type="number" id="strength" name="strength" value="0.7" min="0" step="0.1"><br><br>
242
-
243
  <label for="seed">Seed:</label>
244
- <input type="number" id="seed" name="seed" value="-1" step="1"><br><br>
245
 
246
- <input type="submit" id="generate-button" value="Generate Image"><br>
 
247
  </form>
248
-
249
- <p id="error-message" style="color:red;"></p>
250
- <input type="text" id="reqURL" style="width: 100%;" readonly><br><br>
251
-
252
- <div id="container">
253
- <canvas id="canvas" width="1024" height="1024" style="border:1px solid #000000;"></canvas>
254
- <img id="generated-image" src="#" alt="Generated Image" style="display:none; max-width:100%;"><br>
255
  </div>
 
 
256
  </body>
257
  </html>
258
  """
@@ -261,5 +259,27 @@ index_html = """
261
  def index():
262
  return render_template_string(index_html)
263
 
264
- if __name__ == '__main__':
265
- app.run(debug=True, 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
 
16
  return None, "Prompt is required"
17
 
18
  key = random.randint(0, 999)
19
+
20
  # Translate the prompt from Russian to English if necessary
21
  prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
22
  print(f'Generation {key} translation: {prompt}')
 
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}"
 
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
  """
 
259
  def index():
260
  return render_template_string(index_html)
261
 
262
+ @app.route('/generate', methods=['GET'])
263
+ def generate_image():
264
+ prompt = request.args.get("prompt", "")
265
+ negative_prompt = request.args.get("negative_prompt", "")
266
+ steps = int(request.args.get("steps", 35))
267
+ cfg_scale = float(request.args.get("cfgs", 7))
268
+ sampler = request.args.get("sampler", "DPM++ 2M Karras")
269
+ strength = float(request.args.get("strength", 0.7))
270
+ seed = int(request.args.get("seed", -1))
271
+ width = int(request.args.get("width", 1024))
272
+ height = int(request.args.get("height", 1024))
273
+
274
+ image, error = query(prompt, negative_prompt, steps, cfg_scale, sampler, seed, strength, width, height)
275
+
276
+ if error:
277
+ return jsonify({"error": error}), 400
278
+
279
+ img_bytes = io.BytesIO()
280
+ image.save(img_bytes, format='PNG')
281
+ img_bytes.seek(0)
282
+ return send_file(img_bytes, mimetype='image/png')
283
+
284
+ if __name__ == "__main__":
285
+ app.run(host='0.0.0.0', port=7860)