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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import glob
|
| 3 |
+
import torch
|
| 4 |
+
from flask import Flask, request, jsonify, send_file
|
| 5 |
+
from diffusers import StableDiffusionPipeline, AutoencoderKL
|
| 6 |
+
from PIL import Image, ImageFilter
|
| 7 |
+
from deep_translator import GoogleTranslator
|
| 8 |
+
|
| 9 |
+
print("IMAGE PRO AI STARTING...")
|
| 10 |
+
|
| 11 |
+
# ===============================
|
| 12 |
+
# PATHS
|
| 13 |
+
# ===============================
|
| 14 |
+
|
| 15 |
+
CACHE_DIR = "F:/AI_models"
|
| 16 |
+
OUTPUT_DIR = "outputs"
|
| 17 |
+
|
| 18 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 19 |
+
|
| 20 |
+
# ===============================
|
| 21 |
+
# TRANSLATOR
|
| 22 |
+
# ===============================
|
| 23 |
+
|
| 24 |
+
def translate_prompt(prompt):
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
|
| 28 |
+
prompt_en = GoogleTranslator(
|
| 29 |
+
source="pl",
|
| 30 |
+
target="en"
|
| 31 |
+
).translate(prompt)
|
| 32 |
+
|
| 33 |
+
print("PL:", prompt)
|
| 34 |
+
print("EN:", prompt_en)
|
| 35 |
+
|
| 36 |
+
return prompt_en
|
| 37 |
+
|
| 38 |
+
except:
|
| 39 |
+
|
| 40 |
+
return prompt
|
| 41 |
+
|
| 42 |
+
# ===============================
|
| 43 |
+
# FIND VAE
|
| 44 |
+
# ===============================
|
| 45 |
+
|
| 46 |
+
print("Searching VAE...")
|
| 47 |
+
|
| 48 |
+
vae = None
|
| 49 |
+
|
| 50 |
+
vae_files = glob.glob(
|
| 51 |
+
r"F:\AI_models\models--stabilityai--sd-vae-ft-mse\snapshots\*\vae-ft-mse-840000-ema-pruned.safetensors"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
if len(vae_files) > 0:
|
| 55 |
+
|
| 56 |
+
VAE_PATH = vae_files[0]
|
| 57 |
+
|
| 58 |
+
print("VAE FOUND:", VAE_PATH)
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
|
| 62 |
+
vae = AutoencoderKL.from_single_file(
|
| 63 |
+
VAE_PATH,
|
| 64 |
+
torch_dtype=torch.float32
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
except:
|
| 68 |
+
|
| 69 |
+
print("VAE LOAD FAILED")
|
| 70 |
+
|
| 71 |
+
# ===============================
|
| 72 |
+
# LOAD MODEL
|
| 73 |
+
# ===============================
|
| 74 |
+
|
| 75 |
+
print("Loading AI model...")
|
| 76 |
+
|
| 77 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 78 |
+
"SG161222/Realistic_Vision_V5.1_noVAE",
|
| 79 |
+
cache_dir=CACHE_DIR,
|
| 80 |
+
torch_dtype=torch.float32,
|
| 81 |
+
safety_checker=None,
|
| 82 |
+
vae=vae
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
pipe.enable_attention_slicing()
|
| 86 |
+
|
| 87 |
+
print("MODEL READY")
|
| 88 |
+
|
| 89 |
+
# ===============================
|
| 90 |
+
# FLASK
|
| 91 |
+
# ===============================
|
| 92 |
+
|
| 93 |
+
app = Flask(__name__)
|
| 94 |
+
|
| 95 |
+
# ======================================================
|
| 96 |
+
# 1 GENERATE IMAGE
|
| 97 |
+
# ======================================================
|
| 98 |
+
|
| 99 |
+
@app.route("/generate", methods=["POST"])
|
| 100 |
+
def generate():
|
| 101 |
+
|
| 102 |
+
data = request.json
|
| 103 |
+
|
| 104 |
+
prompt = translate_prompt(data["prompt"])
|
| 105 |
+
|
| 106 |
+
image = pipe(
|
| 107 |
+
prompt,
|
| 108 |
+
num_inference_steps=30,
|
| 109 |
+
guidance_scale=7.5
|
| 110 |
+
).images[0]
|
| 111 |
+
|
| 112 |
+
path = OUTPUT_DIR + "/gen.png"
|
| 113 |
+
image.save(path)
|
| 114 |
+
|
| 115 |
+
return send_file(path, mimetype="image/png")
|
| 116 |
+
|
| 117 |
+
# ======================================================
|
| 118 |
+
# 2 PRODUCT AD (2 STYLE DNA)
|
| 119 |
+
# ======================================================
|
| 120 |
+
|
| 121 |
+
@app.route("/product_ad", methods=["POST"])
|
| 122 |
+
def product_ad():
|
| 123 |
+
|
| 124 |
+
data = request.json
|
| 125 |
+
prompt = translate_prompt(data["prompt"])
|
| 126 |
+
|
| 127 |
+
style1 = prompt + ", luxury product photography, studio lighting"
|
| 128 |
+
style2 = prompt + ", commercial advertising, dramatic lighting"
|
| 129 |
+
|
| 130 |
+
img1 = pipe(style1).images[0]
|
| 131 |
+
img2 = pipe(style2).images[0]
|
| 132 |
+
|
| 133 |
+
p1 = OUTPUT_DIR + "/ad1.png"
|
| 134 |
+
p2 = OUTPUT_DIR + "/ad2.png"
|
| 135 |
+
|
| 136 |
+
img1.save(p1)
|
| 137 |
+
img2.save(p2)
|
| 138 |
+
|
| 139 |
+
return jsonify({
|
| 140 |
+
"image1": p1,
|
| 141 |
+
"image2": p2
|
| 142 |
+
})
|
| 143 |
+
|
| 144 |
+
# ======================================================
|
| 145 |
+
# 3 FIX OLD PHOTO
|
| 146 |
+
# ======================================================
|
| 147 |
+
|
| 148 |
+
@app.route("/restore", methods=["POST"])
|
| 149 |
+
def restore():
|
| 150 |
+
|
| 151 |
+
file = request.files["image"]
|
| 152 |
+
|
| 153 |
+
img = Image.open(file.stream)
|
| 154 |
+
|
| 155 |
+
img = img.filter(ImageFilter.SHARPEN)
|
| 156 |
+
|
| 157 |
+
path = OUTPUT_DIR + "/restored.png"
|
| 158 |
+
|
| 159 |
+
img.save(path)
|
| 160 |
+
|
| 161 |
+
return send_file(path, mimetype="image/png")
|
| 162 |
+
|
| 163 |
+
# ======================================================
|
| 164 |
+
# 4 HD UPSCALE
|
| 165 |
+
# ======================================================
|
| 166 |
+
|
| 167 |
+
@app.route("/upscale", methods=["POST"])
|
| 168 |
+
def upscale():
|
| 169 |
+
|
| 170 |
+
file = request.files["image"]
|
| 171 |
+
|
| 172 |
+
img = Image.open(file.stream)
|
| 173 |
+
|
| 174 |
+
width, height = img.size
|
| 175 |
+
|
| 176 |
+
img = img.resize(
|
| 177 |
+
(width*2, height*2),
|
| 178 |
+
Image.LANCZOS
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
path = OUTPUT_DIR + "/hd.png"
|
| 182 |
+
|
| 183 |
+
img.save(path)
|
| 184 |
+
|
| 185 |
+
return send_file(path, mimetype="image/png")
|
| 186 |
+
|
| 187 |
+
# ======================================================
|
| 188 |
+
# 5 COLORIZE
|
| 189 |
+
# ======================================================
|
| 190 |
+
|
| 191 |
+
@app.route("/colorize", methods=["POST"])
|
| 192 |
+
def colorize():
|
| 193 |
+
|
| 194 |
+
file = request.files["image"]
|
| 195 |
+
|
| 196 |
+
img = Image.open(file.stream)
|
| 197 |
+
|
| 198 |
+
img = img.convert("RGB")
|
| 199 |
+
|
| 200 |
+
path = OUTPUT_DIR + "/color.png"
|
| 201 |
+
|
| 202 |
+
img.save(path)
|
| 203 |
+
|
| 204 |
+
return send_file(path, mimetype="image/png")
|
| 205 |
+
|
| 206 |
+
# ======================================================
|
| 207 |
+
# MEGA UPGRADE — STUDIO PRO
|
| 208 |
+
# ======================================================
|
| 209 |
+
|
| 210 |
+
@app.route("/studio_pro", methods=["POST"])
|
| 211 |
+
def studio_pro():
|
| 212 |
+
|
| 213 |
+
data = request.json
|
| 214 |
+
prompt = translate_prompt(data["prompt"])
|
| 215 |
+
|
| 216 |
+
prompts = [
|
| 217 |
+
|
| 218 |
+
prompt + ", luxury studio lighting",
|
| 219 |
+
prompt + ", product on marble table",
|
| 220 |
+
prompt + ", premium advertising photo",
|
| 221 |
+
prompt + ", instagram product ad",
|
| 222 |
+
prompt + ", minimal product photography"
|
| 223 |
+
|
| 224 |
+
]
|
| 225 |
+
|
| 226 |
+
results = []
|
| 227 |
+
|
| 228 |
+
for p in prompts:
|
| 229 |
+
|
| 230 |
+
img = pipe(p).images[0]
|
| 231 |
+
|
| 232 |
+
path = OUTPUT_DIR + "/" + str(len(results)) + ".png"
|
| 233 |
+
|
| 234 |
+
img.save(path)
|
| 235 |
+
|
| 236 |
+
results.append(path)
|
| 237 |
+
|
| 238 |
+
return jsonify(results)
|
| 239 |
+
|
| 240 |
+
# ======================================================
|
| 241 |
+
# START SERVER
|
| 242 |
+
# ======================================================
|
| 243 |
+
|
| 244 |
+
if __name__ == "__main__":
|
| 245 |
+
|
| 246 |
+
print("IMAGE PRO API READY")
|
| 247 |
+
|
| 248 |
+
app.run(
|
| 249 |
+
host="0.0.0.0",
|
| 250 |
+
port=5000,
|
| 251 |
+
debug=False
|
| 252 |
+
)
|