File size: 13,664 Bytes
f666cad
e993d4e
f7d2427
 
e993d4e
f7d2427
 
 
 
b651c6e
f7d2427
 
 
 
8d3fa62
af03b23
f7d2427
e993d4e
f7d2427
 
 
 
d14c733
f7d2427
 
 
 
e993d4e
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d2427
 
 
 
e993d4e
f7d2427
 
e993d4e
 
 
f7d2427
 
e993d4e
f7d2427
 
 
 
 
e993d4e
 
 
 
 
 
 
b651c6e
f7d2427
 
5129e9f
f7d2427
 
5129e9f
f7d2427
44ba1f2
e993d4e
44ba1f2
f7d2427
5129e9f
 
 
 
 
 
e993d4e
 
 
 
 
 
 
 
 
 
 
 
 
5129e9f
 
e993d4e
 
 
 
 
 
 
 
5129e9f
e993d4e
 
 
 
 
 
 
 
 
 
5129e9f
 
e993d4e
 
 
 
5129e9f
 
e993d4e
 
 
 
5129e9f
 
e993d4e
 
 
 
 
 
 
 
 
 
 
 
 
5129e9f
44ba1f2
 
 
e993d4e
f7d2427
b651c6e
e993d4e
 
 
b651c6e
e993d4e
 
 
 
 
 
 
 
b651c6e
e993d4e
 
 
 
 
 
 
 
b651c6e
e993d4e
 
 
 
 
 
 
 
 
b651c6e
e993d4e
 
 
 
 
 
 
 
 
 
 
 
 
b651c6e
af03b23
5129e9f
e993d4e
5129e9f
af03b23
f7d2427
 
e993d4e
b651c6e
f7d2427
af03b23
e993d4e
b651c6e
af03b23
e993d4e
 
 
 
af03b23
 
e993d4e
 
 
 
 
 
 
 
 
 
af03b23
f7d2427
e993d4e
f7d2427
e993d4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d2427
d14c733
 
 
 
 
b651c6e
d14c733
e993d4e
 
 
 
 
 
 
 
 
 
 
5129e9f
e993d4e
 
 
b651c6e
e993d4e
 
 
 
b651c6e
e993d4e
 
 
f7d2427
b651c6e
e993d4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b651c6e
e993d4e
 
f7d2427
 
 
af03b23
f7d2427
d14c733
f7d2427
 
 
af03b23
 
 
 
 
 
 
 
 
f7d2427
 
b651c6e
f7d2427
 
e993d4e
5129e9f
af03b23
e993d4e
5129e9f
e993d4e
 
5129e9f
b651c6e
 
 
af03b23
 
 
 
e993d4e
b651c6e
 
af03b23
 
 
f7d2427
 
 
 
5129e9f
f7d2427
 
 
 
5129e9f
f7d2427
 
d14c733
f7d2427
 
e993d4e
 
 
 
f7d2427
 
b651c6e
5129e9f
 
 
 
 
f7d2427
d14c733
f7d2427
e993d4e
 
 
5129e9f
e993d4e
f7d2427
 
 
5129e9f
 
 
 
d14c733
f7d2427
e993d4e
 
 
5129e9f
e993d4e
f7d2427
 
 
5129e9f
 
 
 
d14c733
f7d2427
e993d4e
 
 
5129e9f
e993d4e
f7d2427
 
 
5129e9f
 
 
 
d14c733
f7d2427
e993d4e
 
 
5129e9f
e993d4e
f7d2427
 
 
 
5129e9f
f7d2427
 
d14c733
f7d2427
 
 
5129e9f
 
b651c6e
f7d2427
5129e9f
e993d4e
f7d2427
5129e9f
f7d2427
 
 
5129e9f
 
 
 
d14c733
f7d2427
 
 
5129e9f
 
b651c6e
5129e9f
f7d2427
e993d4e
f7d2427
e993d4e
f7d2427
5129e9f
f7d2427
 
 
 
5129e9f
f7d2427
 
5129e9f
 
 
 
f7d2427
5129e9f
b651c6e
 
 
 
e993d4e
5129e9f
f7d2427
 
 
 
e993d4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d2427
 
 
e993d4e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import os
import io
import torch
from flask import Flask, request, jsonify, send_file, render_template_string
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
from PIL import Image, ImageFilter
from deep_translator import GoogleTranslator
from datetime import datetime

print("STARTING IMAGE PRO AI")

OUTPUT_DIR = "outputs"
os.makedirs(OUTPUT_DIR, exist_ok=True)

progress_value = 0

# ======================
# TRANSLATE & PROMPT ENHANCE
# ======================

def translate(text):
    try:
        return GoogleTranslator(source="auto", target="en").translate(text)
    except:
        return text


STYLE_PRESETS = {
    "cinematic": "cinematic lighting, ultra realistic, 8k, film still, depth of field",
    "anime": "anime style, clean lines, vibrant colors, detailed illustration",
    "realistic": "photo realistic, natural lighting, high detail, 8k",
    "neon": "neon lights, cyberpunk, glowing colors, night city",
    "cartoon": "cartoon style, bold outlines, flat colors, playful",
}


def enhance(text, style: str | None = None):
    base = "high detail, professional, sharp focus"
    extra = base
    if style and style in STYLE_PRESETS:
        extra = STYLE_PRESETS[style] + ", " + base
    return text + ", " + extra


# ======================
# MODEL (SZYBSZY: SD-TURBO)
# ======================

print("Loading Stable Diffusion Turbo...")

MODEL_ID = os.getenv("MODEL_ID", "stabilityai/sd-turbo")

pipe = StableDiffusionPipeline.from_pretrained(
    MODEL_ID,
    torch_dtype=torch.float32
)
pipe = pipe.to("cpu")
pipe.enable_attention_slicing()

img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
    MODEL_ID,
    torch_dtype=torch.float32
)
img2img_pipe = img2img_pipe.to("cpu")
img2img_pipe.enable_attention_slicing()

print("MODEL READY")

# ======================
# API
# ======================

api = Flask(__name__)

# ======================
# HTML UI (ODŚWIEŻONY)
# ======================

HTML = """
<html>
<head>
<title>IMAGE PRO AI STUDIO</title>
<style>
body{
  background:#05060a;
  color:#f5f5f5;
  text-align:center;
  font-family:Arial, sans-serif;
}
h1{
  margin-top:20px;
  letter-spacing:2px;
}
.container{
  max-width:900px;
  margin:0 auto;
  padding:20px;
}
button{
  background:#ff8c00;
  border:none;
  padding:10px 16px;
  margin:5px;
  cursor:pointer;
  font-weight:bold;
  border-radius:6px;
  color:#111;
}
button:hover{
  background:#ffb347;
}
input, select{
  width:420px;
  padding:10px;
  border-radius:6px;
  border:1px solid #333;
  background:#111;
  color:#f5f5f5;
}
img{
  max-width:650px;
  margin-top:20px;
  border:2px solid #ff8c00;
  border-radius:8px;
}
#bar{
  width:0%;
  height:20px;
  background:#ff8c00;
  border-radius:10px;
}
#progress{
  width:400px;
  background:#222;
  margin:10px auto;
  display:none;
  border-radius:10px;
  padding:2px;
}
.section-title{
  margin-top:25px;
  font-size:18px;
  text-transform:uppercase;
  letter-spacing:1px;
  color:#ffb347;
}
</style>
</head>
<body>
<div class="container">
<h1>IMAGE PRO AI STUDIO</h1>

<div>
  <input id="prompt" placeholder="Enter prompt">
</div>

<div class="section-title">Styles</div>
<div>
  <button onclick="stylePreset('cinematic')">CINEMATIC</button>
  <button onclick="stylePreset('anime')">ANIME</button>
  <button onclick="stylePreset('realistic')">REALISTIC</button>
  <button onclick="stylePreset('neon')">NEON</button>
  <button onclick="stylePreset('cartoon')">CARTOON</button>
</div>

<div class="section-title">Generate</div>
<div>
  <button onclick="generate()">IMAGE</button>
  <button onclick="product()">PRODUCT</button>
  <button onclick="logo()">LOGO</button>
  <button onclick="banner()">BANNER</button>
  <button onclick="social()">SOCIAL</button>
</div>

<div class="section-title">Image Tools</div>
<div>
  <input type="file" id="file">
</div>
<div>
  <button onclick="restore()">RESTORE</button>
  <button onclick="upscale()">UPSCALE</button>
  <button onclick="color()">COLORIZE</button>
</div>

<div class="section-title">Blend PRO</div>
<div>
  <input type="file" id="blendA">
  <input type="file" id="blendB">
</div>
<div>
  <label>Mix: <span id="mixLabel">0.5</span></label><br>
  <input type="range" id="mixRange" min="0" max="1" step="0.05" value="0.5" style="width:300px" oninput="updateMix()">
</div>
<div>
  <button onclick="runBlend()">BLEND</button>
  <button onclick="runBlendPro()">BLEND PRO (AI)</button>
</div>

<h3>GENERATING</h3>
<div id="progress">
  <div id="bar"></div>
</div>
<p id="percent">0%</p>
<h2>RESULT</h2>
<img id="result">
</div>

<script>
let progressTimer
let currentStyle = null

function startProgress(){
  document.getElementById("progress").style.display="block"
  document.getElementById("bar").style.width="0%"
  document.getElementById("percent").innerText="0%"
  progressTimer=setInterval(updateProgress,500)
}
function updateProgress(){
  fetch("/progress")
  .then(r=>r.json())
  .then(data=>{
    let p=data.progress
    document.getElementById("bar").style.width=p+"%"
    document.getElementById("percent").innerText=p+"%"
    if(p>=100){
      clearInterval(progressTimer)
    }
  })
}
function show(blob){
  document.getElementById("result").src=URL.createObjectURL(blob)
}
function sendPrompt(endpoint, extraBody){
  startProgress()
  let body = {
    prompt: document.getElementById("prompt").value,
    style: currentStyle
  }
  if(extraBody){
    Object.assign(body, extraBody)
  }
  fetch("/"+endpoint,{
    method:"POST",
    headers:{"Content-Type":"application/json"},
    body:JSON.stringify(body)
  })
  .then(r=>r.blob())
  .then(show)
}
function generate(){sendPrompt("generate")}
function product(){sendPrompt("product")}
function logo(){sendPrompt("logo")}
function banner(){sendPrompt("banner")}
function social(){sendPrompt("social")}

function sendImage(endpoint){
  let file=document.getElementById("file").files[0]
  if(!file){
    alert("Select image first")
    return
  }
  let data=new FormData()
  data.append("image",file)
  startProgress()
  fetch("/"+endpoint,{method:"POST",body:data})
  .then(r=>r.blob())
  .then(show)
}
function restore(){sendImage("restore")}
function upscale(){sendImage("upscale")}
function color(){sendImage("colorize")}

function stylePreset(style){
  currentStyle = style
  alert("Style set: "+style.toUpperCase())
}

function updateMix(){
  const v = document.getElementById("mixRange").value
  document.getElementById("mixLabel").innerText = v
}

function runBlendCore(endpoint){
  let a=document.getElementById("blendA").files[0]
  let b=document.getElementById("blendB").files[0]
  if(!a || !b){
    alert("Select both images (A and B)")
    return
  }
  let data=new FormData()
  data.append("image_a",a)
  data.append("image_b",b)
  data.append("mix",document.getElementById("mixRange").value)
  if(currentStyle){
    data.append("style", currentStyle)
  }
  startProgress()
  fetch("/"+endpoint,{method:"POST",body:data})
  .then(r=>r.blob())
  .then(show)
}

function runBlend(){runBlendCore("blend")}
function runBlendPro(){runBlendCore("blend-pro")}
</script>
</body>
</html>
"""

@api.route("/")
def home():
    return render_template_string(HTML)

# ======================
# PROGRESS
# ======================

@api.route("/progress")
def progress():
    global progress_value
    return jsonify({"progress": progress_value})


# ======================
# GENERATE IMAGE
# ======================

def generate_image(prompt, style: str | None = None):

    global progress_value
    steps = 8  # szybciej

    prompt = translate(prompt)
    prompt = enhance(prompt, style)

    def callback(step, timestep, latents):
        global progress_value
        progress_value = int((step/steps)*100)

    image = pipe(
        prompt,
        num_inference_steps=steps,
        guidance_scale=1.5,
        callback=callback,
        callback_steps=1
    ).images[0]

    progress_value = 100

    filename = datetime.now().strftime("%Y%m%d%H%M%S") + ".png"
    path = os.path.join(OUTPUT_DIR, filename)
    image.save(path)

    return path


# ======================
# GENERATE
# ======================

@api.route("/generate", methods=["POST"])
def generate():
    try:
        data = request.get_json(force=True)
        prompt = data.get("prompt", "")
        style = data.get("style")
        path = generate_image(prompt, style)
        return send_file(path, mimetype="image/png")
    except Exception as e:
        return jsonify({"error": str(e)})


# ======================
# PRODUCT
# ======================

@api.route("/product", methods=["POST"])
def product():
    data = request.get_json(force=True)
    prompt = data.get("prompt", "")
    style = data.get("style") or "realistic"
    prompt = translate(prompt) + ", product photography, studio lighting, white background"
    path = generate_image(prompt, style)
    return send_file(path, mimetype="image/png")


# ======================
# LOGO
# ======================

@api.route("/logo", methods=["POST"])
def logo():
    data = request.get_json(force=True)
    prompt = data.get("prompt", "")
    style = data.get("style") or "cartoon"
    prompt = translate(prompt) + ", minimalist vector logo"
    path = generate_image(prompt, style)
    return send_file(path, mimetype="image/png")


# ======================
# BANNER
# ======================

@api.route("/banner", methods=["POST"])
def banner():
    data = request.get_json(force=True)
    prompt = data.get("prompt", "")
    style = data.get("style") or "cinematic"
    prompt = translate(prompt) + ", modern website banner design"
    path = generate_image(prompt, style)
    return send_file(path, mimetype="image/png")


# ======================
# SOCIAL
# ======================

@api.route("/social", methods=["POST"])
def social():
    data = request.get_json(force=True)
    prompt = data.get("prompt", "")
    style = data.get("style") or "neon"
    prompt = translate(prompt) + ", instagram social media post"
    path = generate_image(prompt, style)
    return send_file(path, mimetype="image/png")


# ======================
# RESTORE
# ======================

@api.route("/restore", methods=["POST"])
def restore():
    if "image" not in request.files:
        return "no image", 400

    file = request.files["image"]
    img = Image.open(file.stream)
    img = img.filter(ImageFilter.SHARPEN)

    path = os.path.join(OUTPUT_DIR, "restore.png")
    img.save(path)

    return send_file(path, mimetype="image/png")


# ======================
# UPSCALE
# ======================

@api.route("/upscale", methods=["POST"])
def upscale():
    if "image" not in request.files:
        return "no image", 400

    file = request.files["image"]
    img = Image.open(file.stream)

    w, h = img.size
    img = img.resize((w * 2, h * 2), Image.LANCZOS)

    path = os.path.join(OUTPUT_DIR, "upscale.png")
    img.save(path)

    return send_file(path, mimetype="image/png")


# ======================
# COLORIZE
# ======================

@api.route("/colorize", methods=["POST"])
def colorize():
    if "image" not in request.files:
        return "no image", 400

    file = request.files["image"]
    img = Image.open(file.stream)

    img = img.convert("RGB")

    path = os.path.join(OUTPUT_DIR, "color.png")
    img.save(path)

    return send_file(path, mimetype="image/png")


# ======================
# BLEND (KLASYCZNY)
# ======================

def _blend_simple(file_a, file_b, mix: float, style: str | None = None):
    img_a = Image.open(file_a.stream).convert("RGBA")
    img_b = Image.open(file_b.stream).convert("RGBA")

    img_b = img_b.resize(img_a.size)
    mix = max(0.0, min(1.0, mix))

    blended = Image.blend(img_a, img_b, mix)

    path = os.path.join(OUTPUT_DIR, "blend.png")
    blended.save(path)

    return path


# ======================
# BLEND PRO (AI IMG2IMG)
# ======================

def _blend_ai(file_a, file_b, mix: float, style: str | None = None):
    img_a = Image.open(file_a.stream).convert("RGBA")
    img_b = Image.open(file_b.stream).convert("RGBA")

    img_b = img_b.resize(img_a.size)
    mix = max(0.0, min(1.0, mix))

    base = Image.blend(img_a, img_b, mix).convert("RGB")

    prompt = "high quality artistic blend of two images"
    if style and style in STYLE_PRESETS:
        prompt += ", " + STYLE_PRESETS[style]

    images = img2img_pipe(
        prompt=prompt,
        image=base,
        strength=0.6,
        num_inference_steps=8,
        guidance_scale=1.5,
    ).images

    result = images[0]

    path = os.path.join(OUTPUT_DIR, "blend_pro.png")
    result.save(path)

    return path


@api.route("/blend", methods=["POST"])
def blend():
    if "image_a" not in request.files or "image_b" not in request.files:
        return "need image_a and image_b", 400

    file_a = request.files["image_a"]
    file_b = request.files["image_b"]

    try:
        mix = float(request.form.get("mix", 0.5))
    except:
        mix = 0.5

    style = request.form.get("style")

    path = _blend_simple(file_a, file_b, mix, style)
    return send_file(path, mimetype="image/png")


@api.route("/blend-pro", methods=["POST"])
def blend_pro():
    if "image_a" not in request.files or "image_b" not in request.files:
        return "need image_a and image_b", 400

    file_a = request.files["image_a"]
    file_b = request.files["image_b"]

    try:
        mix = float(request.form.get("mix", 0.5))
    except:
        mix = 0.5

    style = request.form.get("style")

    path = _blend_ai(file_a, file_b, mix, style)
    return send_file(path, mimetype="image/png")


# ======================

if __name__ == "__main__":
    api.run(host="0.0.0.0", port=7860)