webnowa commited on
Commit
e993d4e
·
verified ·
1 Parent(s): b651c6e

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +320 -182
api.py CHANGED
@@ -1,7 +1,8 @@
1
  import os
 
2
  import torch
3
  from flask import Flask, request, jsonify, send_file, render_template_string
4
- from diffusers import StableDiffusionPipeline
5
  from PIL import Image, ImageFilter
6
  from deep_translator import GoogleTranslator
7
  from datetime import datetime
@@ -14,7 +15,7 @@ os.makedirs(OUTPUT_DIR, exist_ok=True)
14
  progress_value = 0
15
 
16
  # ======================
17
- # TRANSLATE
18
  # ======================
19
 
20
  def translate(text):
@@ -24,25 +25,45 @@ def translate(text):
24
  return text
25
 
26
 
27
- def enhance(text):
28
- extra = "ultra realistic, cinematic lighting, high detail, 8k, professional photography"
 
 
 
 
 
 
 
 
 
 
 
 
29
  return text + ", " + extra
30
 
31
 
32
  # ======================
33
- # MODEL
34
  # ======================
35
 
36
- print("Loading Stable Diffusion...")
 
 
37
 
38
  pipe = StableDiffusionPipeline.from_pretrained(
39
- "runwayml/stable-diffusion-v1-5",
40
  torch_dtype=torch.float32
41
  )
42
-
43
  pipe = pipe.to("cpu")
44
  pipe.enable_attention_slicing()
45
 
 
 
 
 
 
 
 
46
  print("MODEL READY")
47
 
48
  # ======================
@@ -52,153 +73,180 @@ print("MODEL READY")
52
  api = Flask(__name__)
53
 
54
  # ======================
55
- # HTML UI
56
  # ======================
57
 
58
  HTML = """
59
-
60
  <html>
61
-
62
  <head>
63
-
64
  <title>IMAGE PRO AI STUDIO</title>
65
-
66
  <style>
67
-
68
  body{
69
- background:black;
70
- color:white;
71
- text-align:center;
72
- font-family:Arial;
 
 
 
 
 
 
 
 
 
73
  }
74
-
75
  button{
76
- background:orange;
77
- border:none;
78
- padding:10px;
79
- margin:5px;
80
- cursor:pointer;
81
- font-weight:bold;
 
 
82
  }
83
-
84
- input{
85
- width:420px;
86
- padding:10px;
 
 
 
 
 
 
87
  }
88
-
89
  img{
90
- max-width:650px;
91
- margin-top:20px;
92
- border:2px solid orange;
 
93
  }
94
-
95
  #bar{
96
- width:0%;
97
- height:20px;
98
- background:orange;
 
99
  }
100
-
101
  #progress{
102
- width:400px;
103
- background:#333;
104
- margin:auto;
105
- display:none;
 
 
 
 
 
 
 
 
 
106
  }
107
-
108
  </style>
109
-
110
  </head>
111
-
112
  <body>
113
-
114
  <h1>IMAGE PRO AI STUDIO</h1>
115
 
116
- <input id="prompt" placeholder="Enter prompt">
117
-
118
- <br><br>
119
-
120
- <button onclick="generate()">IMAGE</button>
121
- <button onclick="product()">PRODUCT</button>
122
- <button onclick="logo()">LOGO</button>
123
- <button onclick="banner()">BANNER</button>
124
- <button onclick="social()">SOCIAL</button>
125
 
126
- <br><br>
 
 
 
 
 
 
 
127
 
128
- <input type="file" id="file">
 
 
 
 
 
 
 
129
 
130
- <br><br>
 
 
 
 
 
 
 
 
131
 
132
- <button onclick="restore()">RESTORE</button>
133
- <button onclick="upscale()">UPSCALE</button>
134
- <button onclick="color()">COLORIZE</button>
 
 
 
 
 
 
 
 
 
 
135
 
136
  <h3>GENERATING</h3>
137
-
138
  <div id="progress">
139
- <div id="bar"></div>
140
  </div>
141
-
142
  <p id="percent">0%</p>
143
-
144
  <h2>RESULT</h2>
145
-
146
  <img id="result">
 
147
 
148
  <script>
149
-
150
  let progressTimer
 
151
 
152
  function startProgress(){
153
-
154
- document.getElementById("progress").style.display="block"
155
-
156
- progressTimer=setInterval(updateProgress,500)
157
-
158
  }
159
-
160
  function updateProgress(){
161
-
162
- fetch("/progress")
163
- .then(r=>r.json())
164
- .then(data=>{
165
-
166
- let p=data.progress
167
-
168
- document.getElementById("bar").style.width=p+"%"
169
-
170
- document.getElementById("percent").innerText=p+"%"
171
-
172
- if(p>=100){
173
- clearInterval(progressTimer)
174
- }
175
-
176
- })
177
-
178
  }
179
-
180
  function show(blob){
181
-
182
- document.getElementById("result").src=URL.createObjectURL(blob)
183
-
184
  }
185
-
186
- function sendPrompt(endpoint){
187
-
188
- startProgress()
189
-
190
- fetch("/"+endpoint,{
191
- method:"POST",
192
- headers:{"Content-Type":"application/json"},
193
- body:JSON.stringify({
194
- prompt:document.getElementById("prompt").value
195
- })
196
- })
197
- .then(r=>r.blob())
198
- .then(show)
199
-
 
200
  }
201
-
202
  function generate(){sendPrompt("generate")}
203
  function product(){sendPrompt("product")}
204
  function logo(){sendPrompt("logo")}
@@ -206,33 +254,57 @@ function banner(){sendPrompt("banner")}
206
  function social(){sendPrompt("social")}
207
 
208
  function sendImage(endpoint){
209
-
210
- let file=document.getElementById("file").files[0]
211
-
212
- if(!file){
213
- alert("Select image first")
214
- return
 
 
 
 
 
215
  }
 
 
 
216
 
217
- let data=new FormData()
218
- data.append("image",file)
219
-
220
- fetch("/"+endpoint,{method:"POST",body:data})
221
- .then(r=>r.blob())
222
- .then(show)
223
 
 
 
 
224
  }
225
 
226
- function restore(){sendImage("restore")}
227
- function upscale(){sendImage("upscale")}
228
- function color(){sendImage("colorize")}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
 
 
 
230
  </script>
231
-
232
  </body>
233
-
234
  </html>
235
-
236
  """
237
 
238
  @api.route("/")
@@ -253,13 +325,13 @@ def progress():
253
  # GENERATE IMAGE
254
  # ======================
255
 
256
- def generate_image(prompt):
257
 
258
  global progress_value
 
259
 
260
- steps = 20
261
-
262
- prompt = enhance(translate(prompt))
263
 
264
  def callback(step, timestep, latents):
265
  global progress_value
@@ -268,6 +340,7 @@ def generate_image(prompt):
268
  image = pipe(
269
  prompt,
270
  num_inference_steps=steps,
 
271
  callback=callback,
272
  callback_steps=1
273
  ).images[0]
@@ -275,9 +348,7 @@ def generate_image(prompt):
275
  progress_value = 100
276
 
277
  filename = datetime.now().strftime("%Y%m%d%H%M%S") + ".png"
278
-
279
  path = os.path.join(OUTPUT_DIR, filename)
280
-
281
  image.save(path)
282
 
283
  return path
@@ -289,17 +360,13 @@ def generate_image(prompt):
289
 
290
  @api.route("/generate", methods=["POST"])
291
  def generate():
292
-
293
  try:
294
-
295
- prompt = request.json.get("prompt", "")
296
-
297
- path = generate_image(prompt)
298
-
299
  return send_file(path, mimetype="image/png")
300
-
301
  except Exception as e:
302
-
303
  return jsonify({"error": str(e)})
304
 
305
 
@@ -309,13 +376,11 @@ def generate():
309
 
310
  @api.route("/product", methods=["POST"])
311
  def product():
312
-
313
- prompt = request.json["prompt"]
314
-
315
  prompt = translate(prompt) + ", product photography, studio lighting, white background"
316
-
317
- path = generate_image(prompt)
318
-
319
  return send_file(path, mimetype="image/png")
320
 
321
 
@@ -325,13 +390,11 @@ def product():
325
 
326
  @api.route("/logo", methods=["POST"])
327
  def logo():
328
-
329
- prompt = request.json["prompt"]
330
-
331
  prompt = translate(prompt) + ", minimalist vector logo"
332
-
333
- path = generate_image(prompt)
334
-
335
  return send_file(path, mimetype="image/png")
336
 
337
 
@@ -341,13 +404,11 @@ def logo():
341
 
342
  @api.route("/banner", methods=["POST"])
343
  def banner():
344
-
345
- prompt = request.json["prompt"]
346
-
347
  prompt = translate(prompt) + ", modern website banner design"
348
-
349
- path = generate_image(prompt)
350
-
351
  return send_file(path, mimetype="image/png")
352
 
353
 
@@ -357,13 +418,11 @@ def banner():
357
 
358
  @api.route("/social", methods=["POST"])
359
  def social():
360
-
361
- prompt = request.json["prompt"]
362
-
363
  prompt = translate(prompt) + ", instagram social media post"
364
-
365
- path = generate_image(prompt)
366
-
367
  return send_file(path, mimetype="image/png")
368
 
369
 
@@ -373,18 +432,14 @@ def social():
373
 
374
  @api.route("/restore", methods=["POST"])
375
  def restore():
376
-
377
  if "image" not in request.files:
378
  return "no image", 400
379
 
380
  file = request.files["image"]
381
-
382
  img = Image.open(file.stream)
383
-
384
  img = img.filter(ImageFilter.SHARPEN)
385
 
386
- path = OUTPUT_DIR + "/restore.png"
387
-
388
  img.save(path)
389
 
390
  return send_file(path, mimetype="image/png")
@@ -396,20 +451,16 @@ def restore():
396
 
397
  @api.route("/upscale", methods=["POST"])
398
  def upscale():
399
-
400
  if "image" not in request.files:
401
  return "no image", 400
402
 
403
  file = request.files["image"]
404
-
405
  img = Image.open(file.stream)
406
 
407
  w, h = img.size
 
408
 
409
- img = img.resize((w*2, h*2), Image.LANCZOS)
410
-
411
- path = OUTPUT_DIR + "/upscale.png"
412
-
413
  img.save(path)
414
 
415
  return send_file(path, mimetype="image/png")
@@ -421,24 +472,111 @@ def upscale():
421
 
422
  @api.route("/colorize", methods=["POST"])
423
  def colorize():
424
-
425
  if "image" not in request.files:
426
  return "no image", 400
427
 
428
  file = request.files["image"]
429
-
430
  img = Image.open(file.stream)
431
 
432
  img = img.convert("RGB")
433
 
434
- path = OUTPUT_DIR + "/color.png"
435
-
436
  img.save(path)
437
 
438
  return send_file(path, mimetype="image/png")
439
 
440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
441
  # ======================
442
 
443
  if __name__ == "__main__":
444
- api.run(host="0.0.0.0", port=7860)
 
1
  import os
2
+ import io
3
  import torch
4
  from flask import Flask, request, jsonify, send_file, render_template_string
5
+ from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
6
  from PIL import Image, ImageFilter
7
  from deep_translator import GoogleTranslator
8
  from datetime import datetime
 
15
  progress_value = 0
16
 
17
  # ======================
18
+ # TRANSLATE & PROMPT ENHANCE
19
  # ======================
20
 
21
  def translate(text):
 
25
  return text
26
 
27
 
28
+ STYLE_PRESETS = {
29
+ "cinematic": "cinematic lighting, ultra realistic, 8k, film still, depth of field",
30
+ "anime": "anime style, clean lines, vibrant colors, detailed illustration",
31
+ "realistic": "photo realistic, natural lighting, high detail, 8k",
32
+ "neon": "neon lights, cyberpunk, glowing colors, night city",
33
+ "cartoon": "cartoon style, bold outlines, flat colors, playful",
34
+ }
35
+
36
+
37
+ def enhance(text, style: str | None = None):
38
+ base = "high detail, professional, sharp focus"
39
+ extra = base
40
+ if style and style in STYLE_PRESETS:
41
+ extra = STYLE_PRESETS[style] + ", " + base
42
  return text + ", " + extra
43
 
44
 
45
  # ======================
46
+ # MODEL (SZYBSZY: SD-TURBO)
47
  # ======================
48
 
49
+ print("Loading Stable Diffusion Turbo...")
50
+
51
+ MODEL_ID = os.getenv("MODEL_ID", "stabilityai/sd-turbo")
52
 
53
  pipe = StableDiffusionPipeline.from_pretrained(
54
+ MODEL_ID,
55
  torch_dtype=torch.float32
56
  )
 
57
  pipe = pipe.to("cpu")
58
  pipe.enable_attention_slicing()
59
 
60
+ img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
61
+ MODEL_ID,
62
+ torch_dtype=torch.float32
63
+ )
64
+ img2img_pipe = img2img_pipe.to("cpu")
65
+ img2img_pipe.enable_attention_slicing()
66
+
67
  print("MODEL READY")
68
 
69
  # ======================
 
73
  api = Flask(__name__)
74
 
75
  # ======================
76
+ # HTML UI (ODŚWIEŻONY)
77
  # ======================
78
 
79
  HTML = """
 
80
  <html>
 
81
  <head>
 
82
  <title>IMAGE PRO AI STUDIO</title>
 
83
  <style>
 
84
  body{
85
+ background:#05060a;
86
+ color:#f5f5f5;
87
+ text-align:center;
88
+ font-family:Arial, sans-serif;
89
+ }
90
+ h1{
91
+ margin-top:20px;
92
+ letter-spacing:2px;
93
+ }
94
+ .container{
95
+ max-width:900px;
96
+ margin:0 auto;
97
+ padding:20px;
98
  }
 
99
  button{
100
+ background:#ff8c00;
101
+ border:none;
102
+ padding:10px 16px;
103
+ margin:5px;
104
+ cursor:pointer;
105
+ font-weight:bold;
106
+ border-radius:6px;
107
+ color:#111;
108
  }
109
+ button:hover{
110
+ background:#ffb347;
111
+ }
112
+ input, select{
113
+ width:420px;
114
+ padding:10px;
115
+ border-radius:6px;
116
+ border:1px solid #333;
117
+ background:#111;
118
+ color:#f5f5f5;
119
  }
 
120
  img{
121
+ max-width:650px;
122
+ margin-top:20px;
123
+ border:2px solid #ff8c00;
124
+ border-radius:8px;
125
  }
 
126
  #bar{
127
+ width:0%;
128
+ height:20px;
129
+ background:#ff8c00;
130
+ border-radius:10px;
131
  }
 
132
  #progress{
133
+ width:400px;
134
+ background:#222;
135
+ margin:10px auto;
136
+ display:none;
137
+ border-radius:10px;
138
+ padding:2px;
139
+ }
140
+ .section-title{
141
+ margin-top:25px;
142
+ font-size:18px;
143
+ text-transform:uppercase;
144
+ letter-spacing:1px;
145
+ color:#ffb347;
146
  }
 
147
  </style>
 
148
  </head>
 
149
  <body>
150
+ <div class="container">
151
  <h1>IMAGE PRO AI STUDIO</h1>
152
 
153
+ <div>
154
+ <input id="prompt" placeholder="Enter prompt">
155
+ </div>
 
 
 
 
 
 
156
 
157
+ <div class="section-title">Styles</div>
158
+ <div>
159
+ <button onclick="stylePreset('cinematic')">CINEMATIC</button>
160
+ <button onclick="stylePreset('anime')">ANIME</button>
161
+ <button onclick="stylePreset('realistic')">REALISTIC</button>
162
+ <button onclick="stylePreset('neon')">NEON</button>
163
+ <button onclick="stylePreset('cartoon')">CARTOON</button>
164
+ </div>
165
 
166
+ <div class="section-title">Generate</div>
167
+ <div>
168
+ <button onclick="generate()">IMAGE</button>
169
+ <button onclick="product()">PRODUCT</button>
170
+ <button onclick="logo()">LOGO</button>
171
+ <button onclick="banner()">BANNER</button>
172
+ <button onclick="social()">SOCIAL</button>
173
+ </div>
174
 
175
+ <div class="section-title">Image Tools</div>
176
+ <div>
177
+ <input type="file" id="file">
178
+ </div>
179
+ <div>
180
+ <button onclick="restore()">RESTORE</button>
181
+ <button onclick="upscale()">UPSCALE</button>
182
+ <button onclick="color()">COLORIZE</button>
183
+ </div>
184
 
185
+ <div class="section-title">Blend PRO</div>
186
+ <div>
187
+ <input type="file" id="blendA">
188
+ <input type="file" id="blendB">
189
+ </div>
190
+ <div>
191
+ <label>Mix: <span id="mixLabel">0.5</span></label><br>
192
+ <input type="range" id="mixRange" min="0" max="1" step="0.05" value="0.5" style="width:300px" oninput="updateMix()">
193
+ </div>
194
+ <div>
195
+ <button onclick="runBlend()">BLEND</button>
196
+ <button onclick="runBlendPro()">BLEND PRO (AI)</button>
197
+ </div>
198
 
199
  <h3>GENERATING</h3>
 
200
  <div id="progress">
201
+ <div id="bar"></div>
202
  </div>
 
203
  <p id="percent">0%</p>
 
204
  <h2>RESULT</h2>
 
205
  <img id="result">
206
+ </div>
207
 
208
  <script>
 
209
  let progressTimer
210
+ let currentStyle = null
211
 
212
  function startProgress(){
213
+ document.getElementById("progress").style.display="block"
214
+ document.getElementById("bar").style.width="0%"
215
+ document.getElementById("percent").innerText="0%"
216
+ progressTimer=setInterval(updateProgress,500)
 
217
  }
 
218
  function updateProgress(){
219
+ fetch("/progress")
220
+ .then(r=>r.json())
221
+ .then(data=>{
222
+ let p=data.progress
223
+ document.getElementById("bar").style.width=p+"%"
224
+ document.getElementById("percent").innerText=p+"%"
225
+ if(p>=100){
226
+ clearInterval(progressTimer)
227
+ }
228
+ })
 
 
 
 
 
 
 
229
  }
 
230
  function show(blob){
231
+ document.getElementById("result").src=URL.createObjectURL(blob)
 
 
232
  }
233
+ function sendPrompt(endpoint, extraBody){
234
+ startProgress()
235
+ let body = {
236
+ prompt: document.getElementById("prompt").value,
237
+ style: currentStyle
238
+ }
239
+ if(extraBody){
240
+ Object.assign(body, extraBody)
241
+ }
242
+ fetch("/"+endpoint,{
243
+ method:"POST",
244
+ headers:{"Content-Type":"application/json"},
245
+ body:JSON.stringify(body)
246
+ })
247
+ .then(r=>r.blob())
248
+ .then(show)
249
  }
 
250
  function generate(){sendPrompt("generate")}
251
  function product(){sendPrompt("product")}
252
  function logo(){sendPrompt("logo")}
 
254
  function social(){sendPrompt("social")}
255
 
256
  function sendImage(endpoint){
257
+ let file=document.getElementById("file").files[0]
258
+ if(!file){
259
+ alert("Select image first")
260
+ return
261
+ }
262
+ let data=new FormData()
263
+ data.append("image",file)
264
+ startProgress()
265
+ fetch("/"+endpoint,{method:"POST",body:data})
266
+ .then(r=>r.blob())
267
+ .then(show)
268
  }
269
+ function restore(){sendImage("restore")}
270
+ function upscale(){sendImage("upscale")}
271
+ function color(){sendImage("colorize")}
272
 
273
+ function stylePreset(style){
274
+ currentStyle = style
275
+ alert("Style set: "+style.toUpperCase())
276
+ }
 
 
277
 
278
+ function updateMix(){
279
+ const v = document.getElementById("mixRange").value
280
+ document.getElementById("mixLabel").innerText = v
281
  }
282
 
283
+ function runBlendCore(endpoint){
284
+ let a=document.getElementById("blendA").files[0]
285
+ let b=document.getElementById("blendB").files[0]
286
+ if(!a || !b){
287
+ alert("Select both images (A and B)")
288
+ return
289
+ }
290
+ let data=new FormData()
291
+ data.append("image_a",a)
292
+ data.append("image_b",b)
293
+ data.append("mix",document.getElementById("mixRange").value)
294
+ if(currentStyle){
295
+ data.append("style", currentStyle)
296
+ }
297
+ startProgress()
298
+ fetch("/"+endpoint,{method:"POST",body:data})
299
+ .then(r=>r.blob())
300
+ .then(show)
301
+ }
302
 
303
+ function runBlend(){runBlendCore("blend")}
304
+ function runBlendPro(){runBlendCore("blend-pro")}
305
  </script>
 
306
  </body>
 
307
  </html>
 
308
  """
309
 
310
  @api.route("/")
 
325
  # GENERATE IMAGE
326
  # ======================
327
 
328
+ def generate_image(prompt, style: str | None = None):
329
 
330
  global progress_value
331
+ steps = 8 # szybciej
332
 
333
+ prompt = translate(prompt)
334
+ prompt = enhance(prompt, style)
 
335
 
336
  def callback(step, timestep, latents):
337
  global progress_value
 
340
  image = pipe(
341
  prompt,
342
  num_inference_steps=steps,
343
+ guidance_scale=1.5,
344
  callback=callback,
345
  callback_steps=1
346
  ).images[0]
 
348
  progress_value = 100
349
 
350
  filename = datetime.now().strftime("%Y%m%d%H%M%S") + ".png"
 
351
  path = os.path.join(OUTPUT_DIR, filename)
 
352
  image.save(path)
353
 
354
  return path
 
360
 
361
  @api.route("/generate", methods=["POST"])
362
  def generate():
 
363
  try:
364
+ data = request.get_json(force=True)
365
+ prompt = data.get("prompt", "")
366
+ style = data.get("style")
367
+ path = generate_image(prompt, style)
 
368
  return send_file(path, mimetype="image/png")
 
369
  except Exception as e:
 
370
  return jsonify({"error": str(e)})
371
 
372
 
 
376
 
377
  @api.route("/product", methods=["POST"])
378
  def product():
379
+ data = request.get_json(force=True)
380
+ prompt = data.get("prompt", "")
381
+ style = data.get("style") or "realistic"
382
  prompt = translate(prompt) + ", product photography, studio lighting, white background"
383
+ path = generate_image(prompt, style)
 
 
384
  return send_file(path, mimetype="image/png")
385
 
386
 
 
390
 
391
  @api.route("/logo", methods=["POST"])
392
  def logo():
393
+ data = request.get_json(force=True)
394
+ prompt = data.get("prompt", "")
395
+ style = data.get("style") or "cartoon"
396
  prompt = translate(prompt) + ", minimalist vector logo"
397
+ path = generate_image(prompt, style)
 
 
398
  return send_file(path, mimetype="image/png")
399
 
400
 
 
404
 
405
  @api.route("/banner", methods=["POST"])
406
  def banner():
407
+ data = request.get_json(force=True)
408
+ prompt = data.get("prompt", "")
409
+ style = data.get("style") or "cinematic"
410
  prompt = translate(prompt) + ", modern website banner design"
411
+ path = generate_image(prompt, style)
 
 
412
  return send_file(path, mimetype="image/png")
413
 
414
 
 
418
 
419
  @api.route("/social", methods=["POST"])
420
  def social():
421
+ data = request.get_json(force=True)
422
+ prompt = data.get("prompt", "")
423
+ style = data.get("style") or "neon"
424
  prompt = translate(prompt) + ", instagram social media post"
425
+ path = generate_image(prompt, style)
 
 
426
  return send_file(path, mimetype="image/png")
427
 
428
 
 
432
 
433
  @api.route("/restore", methods=["POST"])
434
  def restore():
 
435
  if "image" not in request.files:
436
  return "no image", 400
437
 
438
  file = request.files["image"]
 
439
  img = Image.open(file.stream)
 
440
  img = img.filter(ImageFilter.SHARPEN)
441
 
442
+ path = os.path.join(OUTPUT_DIR, "restore.png")
 
443
  img.save(path)
444
 
445
  return send_file(path, mimetype="image/png")
 
451
 
452
  @api.route("/upscale", methods=["POST"])
453
  def upscale():
 
454
  if "image" not in request.files:
455
  return "no image", 400
456
 
457
  file = request.files["image"]
 
458
  img = Image.open(file.stream)
459
 
460
  w, h = img.size
461
+ img = img.resize((w * 2, h * 2), Image.LANCZOS)
462
 
463
+ path = os.path.join(OUTPUT_DIR, "upscale.png")
 
 
 
464
  img.save(path)
465
 
466
  return send_file(path, mimetype="image/png")
 
472
 
473
  @api.route("/colorize", methods=["POST"])
474
  def colorize():
 
475
  if "image" not in request.files:
476
  return "no image", 400
477
 
478
  file = request.files["image"]
 
479
  img = Image.open(file.stream)
480
 
481
  img = img.convert("RGB")
482
 
483
+ path = os.path.join(OUTPUT_DIR, "color.png")
 
484
  img.save(path)
485
 
486
  return send_file(path, mimetype="image/png")
487
 
488
 
489
+ # ======================
490
+ # BLEND (KLASYCZNY)
491
+ # ======================
492
+
493
+ def _blend_simple(file_a, file_b, mix: float, style: str | None = None):
494
+ img_a = Image.open(file_a.stream).convert("RGBA")
495
+ img_b = Image.open(file_b.stream).convert("RGBA")
496
+
497
+ img_b = img_b.resize(img_a.size)
498
+ mix = max(0.0, min(1.0, mix))
499
+
500
+ blended = Image.blend(img_a, img_b, mix)
501
+
502
+ path = os.path.join(OUTPUT_DIR, "blend.png")
503
+ blended.save(path)
504
+
505
+ return path
506
+
507
+
508
+ # ======================
509
+ # BLEND PRO (AI IMG2IMG)
510
+ # ======================
511
+
512
+ def _blend_ai(file_a, file_b, mix: float, style: str | None = None):
513
+ img_a = Image.open(file_a.stream).convert("RGBA")
514
+ img_b = Image.open(file_b.stream).convert("RGBA")
515
+
516
+ img_b = img_b.resize(img_a.size)
517
+ mix = max(0.0, min(1.0, mix))
518
+
519
+ base = Image.blend(img_a, img_b, mix).convert("RGB")
520
+
521
+ prompt = "high quality artistic blend of two images"
522
+ if style and style in STYLE_PRESETS:
523
+ prompt += ", " + STYLE_PRESETS[style]
524
+
525
+ images = img2img_pipe(
526
+ prompt=prompt,
527
+ image=base,
528
+ strength=0.6,
529
+ num_inference_steps=8,
530
+ guidance_scale=1.5,
531
+ ).images
532
+
533
+ result = images[0]
534
+
535
+ path = os.path.join(OUTPUT_DIR, "blend_pro.png")
536
+ result.save(path)
537
+
538
+ return path
539
+
540
+
541
+ @api.route("/blend", methods=["POST"])
542
+ def blend():
543
+ if "image_a" not in request.files or "image_b" not in request.files:
544
+ return "need image_a and image_b", 400
545
+
546
+ file_a = request.files["image_a"]
547
+ file_b = request.files["image_b"]
548
+
549
+ try:
550
+ mix = float(request.form.get("mix", 0.5))
551
+ except:
552
+ mix = 0.5
553
+
554
+ style = request.form.get("style")
555
+
556
+ path = _blend_simple(file_a, file_b, mix, style)
557
+ return send_file(path, mimetype="image/png")
558
+
559
+
560
+ @api.route("/blend-pro", methods=["POST"])
561
+ def blend_pro():
562
+ if "image_a" not in request.files or "image_b" not in request.files:
563
+ return "need image_a and image_b", 400
564
+
565
+ file_a = request.files["image_a"]
566
+ file_b = request.files["image_b"]
567
+
568
+ try:
569
+ mix = float(request.form.get("mix", 0.5))
570
+ except:
571
+ mix = 0.5
572
+
573
+ style = request.form.get("style")
574
+
575
+ path = _blend_ai(file_a, file_b, mix, style)
576
+ return send_file(path, mimetype="image/png")
577
+
578
+
579
  # ======================
580
 
581
  if __name__ == "__main__":
582
+ api.run(host="0.0.0.0", port=7860)