webnowa commited on
Commit
b4d6d6a
·
verified ·
1 Parent(s): be2d262

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +80 -31
api.py CHANGED
@@ -1,7 +1,7 @@
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
@@ -22,21 +22,12 @@ os.makedirs(OUTPUT_DIR, exist_ok=True)
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
  # ===============================
@@ -58,14 +49,11 @@ if len(vae_files) > 0:
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
  # ===============================
@@ -93,14 +81,80 @@ print("MODEL READY")
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(
@@ -115,7 +169,7 @@ def generate():
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"])
@@ -142,7 +196,7 @@ def product_ad():
142
  })
143
 
144
  # ======================================================
145
- # 3 FIX OLD PHOTO
146
  # ======================================================
147
 
148
  @app.route("/restore", methods=["POST"])
@@ -151,7 +205,6 @@ def restore():
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"
@@ -161,7 +214,7 @@ def restore():
161
  return send_file(path, mimetype="image/png")
162
 
163
  # ======================================================
164
- # 4 HD UPSCALE
165
  # ======================================================
166
 
167
  @app.route("/upscale", methods=["POST"])
@@ -185,7 +238,7 @@ def upscale():
185
  return send_file(path, mimetype="image/png")
186
 
187
  # ======================================================
188
- # 5 COLORIZE
189
  # ======================================================
190
 
191
  @app.route("/colorize", methods=["POST"])
@@ -194,7 +247,6 @@ def colorize():
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"
@@ -204,7 +256,7 @@ def colorize():
204
  return send_file(path, mimetype="image/png")
205
 
206
  # ======================================================
207
- # MEGA UPGRADE — STUDIO PRO
208
  # ======================================================
209
 
210
  @app.route("/studio_pro", methods=["POST"])
@@ -214,13 +266,11 @@ def studio_pro():
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 = []
@@ -230,7 +280,6 @@ def studio_pro():
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)
 
1
  import os
2
  import glob
3
  import torch
4
+ from flask import Flask, request, jsonify, send_file, render_template_string
5
  from diffusers import StableDiffusionPipeline, AutoencoderKL
6
  from PIL import Image, ImageFilter
7
  from deep_translator import GoogleTranslator
 
22
  # ===============================
23
 
24
  def translate_prompt(prompt):
 
25
  try:
26
+ prompt_en = GoogleTranslator(source="pl", target="en").translate(prompt)
 
 
 
 
 
27
  print("PL:", prompt)
28
  print("EN:", prompt_en)
 
29
  return prompt_en
 
30
  except:
 
31
  return prompt
32
 
33
  # ===============================
 
49
  print("VAE FOUND:", VAE_PATH)
50
 
51
  try:
 
52
  vae = AutoencoderKL.from_single_file(
53
  VAE_PATH,
54
  torch_dtype=torch.float32
55
  )
 
56
  except:
 
57
  print("VAE LOAD FAILED")
58
 
59
  # ===============================
 
81
  app = Flask(__name__)
82
 
83
  # ======================================================
84
+ # WEB PANEL (TEST GENERATORA)
85
+ # ======================================================
86
+
87
+ HTML_PAGE = """
88
+ <!DOCTYPE html>
89
+ <html>
90
+ <head>
91
+ <title>IMAGE PRO AI</title>
92
+ <style>
93
+ body{font-family:Arial;background:#111;color:white;text-align:center;padding:40px}
94
+ input{width:400px;padding:10px}
95
+ button{padding:10px 20px;background:#ff6a00;border:none;color:white;font-size:16px}
96
+ img{margin-top:20px;max-width:512px}
97
+ </style>
98
+ </head>
99
+ <body>
100
+
101
+ <h1>IMAGE PRO AI</h1>
102
+
103
+ <form action="/generate_web" method="post">
104
+ <input name="prompt" placeholder="Wpisz prompt (np. kot w butach)">
105
+ <br><br>
106
+ <button type="submit">GENERUJ</button>
107
+ </form>
108
+
109
+ {% if image %}
110
+ <h2>Wynik</h2>
111
+ <img src="{{image}}">
112
+ {% endif %}
113
+
114
+ </body>
115
+ </html>
116
+ """
117
+
118
+ @app.route("/", methods=["GET"])
119
+ def home():
120
+ return render_template_string(HTML_PAGE)
121
+
122
+ # ======================================================
123
+ # GENERATE FROM WEB PANEL
124
+ # ======================================================
125
+
126
+ @app.route("/generate_web", methods=["POST"])
127
+ def generate_web():
128
+
129
+ prompt = request.form["prompt"]
130
+ prompt = translate_prompt(prompt)
131
+
132
+ image = pipe(
133
+ prompt,
134
+ num_inference_steps=30,
135
+ guidance_scale=7.5
136
+ ).images[0]
137
+
138
+ path = OUTPUT_DIR + "/web_gen.png"
139
+ image.save(path)
140
+
141
+ return render_template_string(
142
+ HTML_PAGE,
143
+ image="/image/web_gen.png"
144
+ )
145
+
146
+ @app.route("/image/<filename>")
147
+ def serve_image(filename):
148
+ return send_file(OUTPUT_DIR + "/" + filename, mimetype="image/png")
149
+
150
+ # ======================================================
151
+ # API GENERATE
152
  # ======================================================
153
 
154
  @app.route("/generate", methods=["POST"])
155
  def generate():
156
 
157
  data = request.json
 
158
  prompt = translate_prompt(data["prompt"])
159
 
160
  image = pipe(
 
169
  return send_file(path, mimetype="image/png")
170
 
171
  # ======================================================
172
+ # PRODUCT AD
173
  # ======================================================
174
 
175
  @app.route("/product_ad", methods=["POST"])
 
196
  })
197
 
198
  # ======================================================
199
+ # RESTORE PHOTO
200
  # ======================================================
201
 
202
  @app.route("/restore", methods=["POST"])
 
205
  file = request.files["image"]
206
 
207
  img = Image.open(file.stream)
 
208
  img = img.filter(ImageFilter.SHARPEN)
209
 
210
  path = OUTPUT_DIR + "/restored.png"
 
214
  return send_file(path, mimetype="image/png")
215
 
216
  # ======================================================
217
+ # UPSCALE HD
218
  # ======================================================
219
 
220
  @app.route("/upscale", methods=["POST"])
 
238
  return send_file(path, mimetype="image/png")
239
 
240
  # ======================================================
241
+ # COLORIZE
242
  # ======================================================
243
 
244
  @app.route("/colorize", methods=["POST"])
 
247
  file = request.files["image"]
248
 
249
  img = Image.open(file.stream)
 
250
  img = img.convert("RGB")
251
 
252
  path = OUTPUT_DIR + "/color.png"
 
256
  return send_file(path, mimetype="image/png")
257
 
258
  # ======================================================
259
+ # STUDIO PRO
260
  # ======================================================
261
 
262
  @app.route("/studio_pro", methods=["POST"])
 
266
  prompt = translate_prompt(data["prompt"])
267
 
268
  prompts = [
269
+ prompt + ", luxury studio lighting",
270
+ prompt + ", product on marble table",
271
+ prompt + ", premium advertising photo",
272
+ prompt + ", instagram product ad",
273
+ prompt + ", minimal product photography"
 
 
274
  ]
275
 
276
  results = []
 
280
  img = pipe(p).images[0]
281
 
282
  path = OUTPUT_DIR + "/" + str(len(results)) + ".png"
 
283
  img.save(path)
284
 
285
  results.append(path)