webnowa commited on
Commit
cc183c7
·
verified ·
1 Parent(s): 56204f6

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +228 -163
api.py CHANGED
@@ -7,18 +7,16 @@ from deep_translator import GoogleTranslator
7
 
8
  print("IMAGE PRO AI STUDIO MAX STARTING...")
9
 
10
- # ===============================
11
- # PATHS
12
- # ===============================
13
 
14
- CACHE_DIR="models"
15
  OUTPUT_DIR="outputs"
16
-
17
  os.makedirs(OUTPUT_DIR,exist_ok=True)
18
 
19
- # ===============================
20
  # TRANSLATOR
21
- # ===============================
22
 
23
  def translate(prompt):
24
 
@@ -27,9 +25,19 @@ def translate(prompt):
27
  except:
28
  return prompt
29
 
30
- # ===============================
 
 
 
 
 
 
 
 
 
 
31
  # LOAD MODEL
32
- # ===============================
33
 
34
  print("Loading AI model...")
35
 
@@ -42,24 +50,27 @@ pipe.enable_attention_slicing()
42
 
43
  print("MODEL READY")
44
 
45
- # ===============================
46
  # FLASK
47
- # ===============================
48
 
49
  app=Flask(__name__)
50
 
51
- # ===============================
52
- # WEB INTERFACE
53
- # ===============================
54
 
55
  HTML="""
 
56
  <html>
57
  <head>
58
- <title>IMAGE PRO AI</title>
 
 
59
  <style>
60
 
61
  body{
62
- background:#000;
63
  color:white;
64
  text-align:center;
65
  font-family:Arial;
@@ -73,7 +84,7 @@ padding:10px;
73
  button{
74
  background:orange;
75
  border:none;
76
- padding:12px 20px;
77
  margin:5px;
78
  cursor:pointer;
79
  }
@@ -84,13 +95,14 @@ margin-top:20px;
84
  }
85
 
86
  </style>
 
87
  </head>
88
 
89
  <body>
90
 
91
  <h1>IMAGE PRO AI STUDIO MAX</h1>
92
 
93
- <input id="prompt" placeholder="Opis obrazu">
94
 
95
  <br><br>
96
 
@@ -102,6 +114,11 @@ margin-top:20px;
102
 
103
  <br><br>
104
 
 
 
 
 
 
105
  <input type="file" id="file">
106
 
107
  <br><br>
@@ -109,6 +126,11 @@ margin-top:20px;
109
  <button onclick="restore()">RESTORE</button>
110
  <button onclick="upscale()">UPSCALE</button>
111
  <button onclick="color()">COLORIZE</button>
 
 
 
 
 
112
 
113
  <h2>RESULT</h2>
114
 
@@ -117,221 +139,249 @@ margin-top:20px;
117
  <script>
118
 
119
  function gen(){
120
-
121
- fetch("/generate",{
122
- method:"POST",
123
- headers:{"Content-Type":"application/json"},
124
- body:JSON.stringify({prompt:document.getElementById("prompt").value})
125
- })
126
  .then(r=>r.blob())
127
- .then(b=>{
128
- document.getElementById("result").src=URL.createObjectURL(b)
129
- })
130
-
131
  }
132
 
133
  function product(){
134
-
135
- fetch("/product",{
136
- method:"POST",
137
- headers:{"Content-Type":"application/json"},
138
- body:JSON.stringify({prompt:document.getElementById("prompt").value})
139
- })
140
  .then(r=>r.blob())
141
- .then(b=>{
142
- document.getElementById("result").src=URL.createObjectURL(b)
143
- })
144
-
145
  }
146
 
147
  function logo(){
148
-
149
- fetch("/logo",{
150
- method:"POST",
151
- headers:{"Content-Type":"application/json"},
152
- body:JSON.stringify({prompt:document.getElementById("prompt").value})
153
- })
154
  .then(r=>r.blob())
155
- .then(b=>{
156
- document.getElementById("result").src=URL.createObjectURL(b)
157
- })
158
-
159
  }
160
 
161
  function banner(){
162
-
163
- fetch("/banner",{
164
- method:"POST",
165
- headers:{"Content-Type":"application/json"},
166
- body:JSON.stringify({prompt:document.getElementById("prompt").value})
167
- })
168
  .then(r=>r.blob())
169
- .then(b=>{
170
- document.getElementById("result").src=URL.createObjectURL(b)
171
- })
172
-
173
  }
174
 
175
  function social(){
176
-
177
- fetch("/social",{
178
- method:"POST",
179
- headers:{"Content-Type":"application/json"},
180
- body:JSON.stringify({prompt:document.getElementById("prompt").value})
181
- })
182
  .then(r=>r.blob())
183
- .then(b=>{
184
- document.getElementById("result").src=URL.createObjectURL(b)
185
- })
186
-
187
  }
188
 
189
- function restore(){
 
 
 
 
 
190
 
191
- var file=document.getElementById("file").files[0]
 
 
 
 
 
192
 
193
- var data=new FormData()
194
- data.append("image",file)
 
 
195
 
196
- fetch("/restore",{method:"POST",body:data})
197
  .then(r=>r.blob())
198
- .then(b=>{
199
- document.getElementById("result").src=URL.createObjectURL(b)
200
- })
201
-
202
  }
203
 
204
  function upscale(){
 
 
 
205
 
206
- var file=document.getElementById("file").files[0]
207
-
208
- var data=new FormData()
209
- data.append("image",file)
210
-
211
- fetch("/upscale",{method:"POST",body:data})
212
  .then(r=>r.blob())
213
- .then(b=>{
214
- document.getElementById("result").src=URL.createObjectURL(b)
215
- })
216
-
217
  }
218
 
219
  function color(){
 
 
 
220
 
221
- var file=document.getElementById("file").files[0]
 
 
 
222
 
223
- var data=new FormData()
224
- data.append("image",file)
 
 
225
 
226
- fetch("/colorize",{method:"POST",body:data})
227
  .then(r=>r.blob())
228
- .then(b=>{
229
- document.getElementById("result").src=URL.createObjectURL(b)
230
- })
231
 
 
 
232
  }
233
 
234
  </script>
235
 
236
  </body>
237
  </html>
 
238
  """
239
 
240
  @app.route("/")
241
  def home():
242
  return render_template_string(HTML)
243
 
244
- # ===============================
245
- # GENERATE IMAGE
246
- # ===============================
247
 
248
  @app.route("/generate",methods=["POST"])
249
  def generate():
250
 
251
- prompt=translate(request.json["prompt"])
 
252
 
253
- image=pipe(prompt,num_inference_steps=20).images[0]
254
 
255
  path=OUTPUT_DIR+"/image.png"
256
- image.save(path)
257
 
258
  return send_file(path,mimetype="image/png")
259
 
260
- # ===============================
261
- # PRODUCT PHOTO
262
- # ===============================
263
 
264
  @app.route("/product",methods=["POST"])
265
  def product():
266
 
267
- prompt=translate(request.json["prompt"])
268
-
269
- prompt=prompt+", luxury product photography studio lighting"
270
 
271
- image=pipe(prompt).images[0]
272
 
273
  path=OUTPUT_DIR+"/product.png"
274
- image.save(path)
275
 
276
  return send_file(path,mimetype="image/png")
277
 
278
- # ===============================
279
  # LOGO
280
- # ===============================
281
 
282
  @app.route("/logo",methods=["POST"])
283
  def logo():
284
 
285
- prompt=translate(request.json["prompt"])
286
-
287
- prompt=prompt+", minimalist vector logo white background"
288
 
289
- image=pipe(prompt).images[0]
290
 
291
  path=OUTPUT_DIR+"/logo.png"
292
- image.save(path)
293
 
294
  return send_file(path,mimetype="image/png")
295
 
296
- # ===============================
297
  # BANNER
298
- # ===============================
299
 
300
  @app.route("/banner",methods=["POST"])
301
  def banner():
302
 
303
- prompt=translate(request.json["prompt"])
304
-
305
- prompt=prompt+", website banner design modern"
306
 
307
- image=pipe(prompt).images[0]
308
 
309
  path=OUTPUT_DIR+"/banner.png"
310
- image.save(path)
311
 
312
  return send_file(path,mimetype="image/png")
313
 
314
- # ===============================
315
  # SOCIAL
316
- # ===============================
317
 
318
  @app.route("/social",methods=["POST"])
319
  def social():
320
 
321
- prompt=translate(request.json["prompt"])
322
-
323
- prompt=prompt+", instagram advertising design"
324
 
325
- image=pipe(prompt).images[0]
326
 
327
  path=OUTPUT_DIR+"/social.png"
328
- image.save(path)
329
 
330
  return send_file(path,mimetype="image/png")
331
 
332
- # ===============================
333
- # RESTORE PHOTO
334
- # ===============================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335
 
336
  @app.route("/restore",methods=["POST"])
337
  def restore():
@@ -348,9 +398,9 @@ def restore():
348
 
349
  return send_file(path,mimetype="image/png")
350
 
351
- # ===============================
352
  # UPSCALE
353
- # ===============================
354
 
355
  @app.route("/upscale",methods=["POST"])
356
  def upscale():
@@ -369,9 +419,9 @@ def upscale():
369
 
370
  return send_file(path,mimetype="image/png")
371
 
372
- # ===============================
373
  # COLORIZE
374
- # ===============================
375
 
376
  @app.route("/colorize",methods=["POST"])
377
  def colorize():
@@ -388,46 +438,61 @@ def colorize():
388
 
389
  return send_file(path,mimetype="image/png")
390
 
391
- # ===============================
392
- # STUDIO PRO (5 IMAGES)
393
- # ===============================
394
 
395
- @app.route("/studio_pro",methods=["POST"])
396
- def studio_pro():
397
 
398
- prompt=translate(request.json["prompt"])
399
 
400
- styles=[
401
- prompt+", luxury studio photo",
402
- prompt+", advertising photo",
403
- prompt+", minimal product photo",
404
- prompt+", ecommerce product image",
405
- prompt+", dramatic lighting"
406
- ]
407
 
408
- results=[]
409
 
410
- for i,p in enumerate(styles):
411
 
412
- img=pipe(p).images[0]
413
 
414
- path=OUTPUT_DIR+"/studio_"+str(i)+".png"
415
 
416
- img.save(path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417
 
418
- results.append(path)
419
 
420
- return jsonify(results)
421
 
422
- # ===============================
423
  # START SERVER
424
- # ===============================
425
 
426
  if __name__=="__main__":
427
 
428
- print("IMAGE PRO AI READY")
429
 
430
- app.run(
431
- host="0.0.0.0",
432
- port=7860
433
- )
 
7
 
8
  print("IMAGE PRO AI STUDIO MAX STARTING...")
9
 
10
+ # =============================
11
+ # FOLDERS
12
+ # =============================
13
 
 
14
  OUTPUT_DIR="outputs"
 
15
  os.makedirs(OUTPUT_DIR,exist_ok=True)
16
 
17
+ # =============================
18
  # TRANSLATOR
19
+ # =============================
20
 
21
  def translate(prompt):
22
 
 
25
  except:
26
  return prompt
27
 
28
+ # =============================
29
+ # PROMPT ENHANCER
30
+ # =============================
31
+
32
+ def enhance(prompt):
33
+
34
+ prompt=prompt+", ultra realistic, 8k, cinematic lighting, high quality"
35
+
36
+ return prompt
37
+
38
+ # =============================
39
  # LOAD MODEL
40
+ # =============================
41
 
42
  print("Loading AI model...")
43
 
 
50
 
51
  print("MODEL READY")
52
 
53
+ # =============================
54
  # FLASK
55
+ # =============================
56
 
57
  app=Flask(__name__)
58
 
59
+ # =============================
60
+ # WEB UI
61
+ # =============================
62
 
63
  HTML="""
64
+
65
  <html>
66
  <head>
67
+
68
+ <title>IMAGE PRO AI STUDIO MAX</title>
69
+
70
  <style>
71
 
72
  body{
73
+ background:black;
74
  color:white;
75
  text-align:center;
76
  font-family:Arial;
 
84
  button{
85
  background:orange;
86
  border:none;
87
+ padding:10px 15px;
88
  margin:5px;
89
  cursor:pointer;
90
  }
 
95
  }
96
 
97
  </style>
98
+
99
  </head>
100
 
101
  <body>
102
 
103
  <h1>IMAGE PRO AI STUDIO MAX</h1>
104
 
105
+ <input id="prompt" placeholder="opis obrazu">
106
 
107
  <br><br>
108
 
 
114
 
115
  <br><br>
116
 
117
+ <button onclick="batch()">BATCH</button>
118
+ <button onclick="variations()">VARIATIONS</button>
119
+
120
+ <br><br>
121
+
122
  <input type="file" id="file">
123
 
124
  <br><br>
 
126
  <button onclick="restore()">RESTORE</button>
127
  <button onclick="upscale()">UPSCALE</button>
128
  <button onclick="color()">COLORIZE</button>
129
+ <button onclick="removebg()">REMOVE BG</button>
130
+
131
+ <br><br>
132
+
133
+ <button onclick="gallery()">GALLERY</button>
134
 
135
  <h2>RESULT</h2>
136
 
 
139
  <script>
140
 
141
  function gen(){
142
+ fetch("/generate",{method:"POST",headers:{"Content-Type":"application/json"},
143
+ body:JSON.stringify({prompt:prompt.value})})
 
 
 
 
144
  .then(r=>r.blob())
145
+ .then(b=>result.src=URL.createObjectURL(b))
 
 
 
146
  }
147
 
148
  function product(){
149
+ fetch("/product",{method:"POST",headers:{"Content-Type":"application/json"},
150
+ body:JSON.stringify({prompt:prompt.value})})
 
 
 
 
151
  .then(r=>r.blob())
152
+ .then(b=>result.src=URL.createObjectURL(b))
 
 
 
153
  }
154
 
155
  function logo(){
156
+ fetch("/logo",{method:"POST",headers:{"Content-Type":"application/json"},
157
+ body:JSON.stringify({prompt:prompt.value})})
 
 
 
 
158
  .then(r=>r.blob())
159
+ .then(b=>result.src=URL.createObjectURL(b))
 
 
 
160
  }
161
 
162
  function banner(){
163
+ fetch("/banner",{method:"POST",headers:{"Content-Type":"application/json"},
164
+ body:JSON.stringify({prompt:prompt.value})})
 
 
 
 
165
  .then(r=>r.blob())
166
+ .then(b=>result.src=URL.createObjectURL(b))
 
 
 
167
  }
168
 
169
  function social(){
170
+ fetch("/social",{method:"POST",headers:{"Content-Type":"application/json"},
171
+ body:JSON.stringify({prompt:prompt.value})})
 
 
 
 
172
  .then(r=>r.blob())
173
+ .then(b=>result.src=URL.createObjectURL(b))
 
 
 
174
  }
175
 
176
+ function batch(){
177
+ fetch("/batch",{method:"POST",headers:{"Content-Type":"application/json"},
178
+ body:JSON.stringify({prompt:prompt.value})})
179
+ .then(r=>r.json())
180
+ .then(data=>alert("Generated "+data.length+" images"))
181
+ }
182
 
183
+ function variations(){
184
+ fetch("/variations",{method:"POST",headers:{"Content-Type":"application/json"},
185
+ body:JSON.stringify({prompt:prompt.value})})
186
+ .then(r=>r.json())
187
+ .then(data=>alert("Created "+data.length+" variations"))
188
+ }
189
 
190
+ function restore(){
191
+ var f=file.files[0]
192
+ var d=new FormData()
193
+ d.append("image",f)
194
 
195
+ fetch("/restore",{method:"POST",body:d})
196
  .then(r=>r.blob())
197
+ .then(b=>result.src=URL.createObjectURL(b))
 
 
 
198
  }
199
 
200
  function upscale(){
201
+ var f=file.files[0]
202
+ var d=new FormData()
203
+ d.append("image",f)
204
 
205
+ fetch("/upscale",{method:"POST",body:d})
 
 
 
 
 
206
  .then(r=>r.blob())
207
+ .then(b=>result.src=URL.createObjectURL(b))
 
 
 
208
  }
209
 
210
  function color(){
211
+ var f=file.files[0]
212
+ var d=new FormData()
213
+ d.append("image",f)
214
 
215
+ fetch("/colorize",{method:"POST",body:d})
216
+ .then(r=>r.blob())
217
+ .then(b=>result.src=URL.createObjectURL(b))
218
+ }
219
 
220
+ function removebg(){
221
+ var f=file.files[0]
222
+ var d=new FormData()
223
+ d.append("image",f)
224
 
225
+ fetch("/removebg",{method:"POST",body:d})
226
  .then(r=>r.blob())
227
+ .then(b=>result.src=URL.createObjectURL(b))
228
+ }
 
229
 
230
+ function gallery(){
231
+ window.location="/gallery"
232
  }
233
 
234
  </script>
235
 
236
  </body>
237
  </html>
238
+
239
  """
240
 
241
  @app.route("/")
242
  def home():
243
  return render_template_string(HTML)
244
 
245
+ # =============================
246
+ # IMAGE GENERATOR
247
+ # =============================
248
 
249
  @app.route("/generate",methods=["POST"])
250
  def generate():
251
 
252
+ prompt=request.json["prompt"]
253
+ prompt=enhance(translate(prompt))
254
 
255
+ img=pipe(prompt,num_inference_steps=20).images[0]
256
 
257
  path=OUTPUT_DIR+"/image.png"
258
+ img.save(path)
259
 
260
  return send_file(path,mimetype="image/png")
261
 
262
+ # =============================
263
+ # PRODUCT
264
+ # =============================
265
 
266
  @app.route("/product",methods=["POST"])
267
  def product():
268
 
269
+ prompt=request.json["prompt"]
270
+ prompt=translate(prompt)+", luxury product photography"
 
271
 
272
+ img=pipe(prompt).images[0]
273
 
274
  path=OUTPUT_DIR+"/product.png"
275
+ img.save(path)
276
 
277
  return send_file(path,mimetype="image/png")
278
 
279
+ # =============================
280
  # LOGO
281
+ # =============================
282
 
283
  @app.route("/logo",methods=["POST"])
284
  def logo():
285
 
286
+ prompt=request.json["prompt"]
287
+ prompt=translate(prompt)+", minimalist vector logo"
 
288
 
289
+ img=pipe(prompt).images[0]
290
 
291
  path=OUTPUT_DIR+"/logo.png"
292
+ img.save(path)
293
 
294
  return send_file(path,mimetype="image/png")
295
 
296
+ # =============================
297
  # BANNER
298
+ # =============================
299
 
300
  @app.route("/banner",methods=["POST"])
301
  def banner():
302
 
303
+ prompt=request.json["prompt"]
304
+ prompt=translate(prompt)+", website banner design"
 
305
 
306
+ img=pipe(prompt).images[0]
307
 
308
  path=OUTPUT_DIR+"/banner.png"
309
+ img.save(path)
310
 
311
  return send_file(path,mimetype="image/png")
312
 
313
+ # =============================
314
  # SOCIAL
315
+ # =============================
316
 
317
  @app.route("/social",methods=["POST"])
318
  def social():
319
 
320
+ prompt=request.json["prompt"]
321
+ prompt=translate(prompt)+", instagram advertising post"
 
322
 
323
+ img=pipe(prompt).images[0]
324
 
325
  path=OUTPUT_DIR+"/social.png"
326
+ img.save(path)
327
 
328
  return send_file(path,mimetype="image/png")
329
 
330
+ # =============================
331
+ # BATCH GENERATOR
332
+ # =============================
333
+
334
+ @app.route("/batch",methods=["POST"])
335
+ def batch():
336
+
337
+ prompt=enhance(translate(request.json["prompt"]))
338
+
339
+ paths=[]
340
+
341
+ for i in range(4):
342
+
343
+ img=pipe(prompt).images[0]
344
+
345
+ path=OUTPUT_DIR+"/batch_"+str(i)+".png"
346
+
347
+ img.save(path)
348
+
349
+ paths.append(path)
350
+
351
+ return jsonify(paths)
352
+
353
+ # =============================
354
+ # VARIATIONS
355
+ # =============================
356
+
357
+ @app.route("/variations",methods=["POST"])
358
+ def variations():
359
+
360
+ prompt=translate(request.json["prompt"])
361
+
362
+ styles=[
363
+ prompt+", cinematic lighting",
364
+ prompt+", studio photography",
365
+ prompt+", dramatic lighting"
366
+ ]
367
+
368
+ paths=[]
369
+
370
+ for i,p in enumerate(styles):
371
+
372
+ img=pipe(p).images[0]
373
+
374
+ path=OUTPUT_DIR+"/var_"+str(i)+".png"
375
+
376
+ img.save(path)
377
+
378
+ paths.append(path)
379
+
380
+ return jsonify(paths)
381
+
382
+ # =============================
383
+ # RESTORE
384
+ # =============================
385
 
386
  @app.route("/restore",methods=["POST"])
387
  def restore():
 
398
 
399
  return send_file(path,mimetype="image/png")
400
 
401
+ # =============================
402
  # UPSCALE
403
+ # =============================
404
 
405
  @app.route("/upscale",methods=["POST"])
406
  def upscale():
 
419
 
420
  return send_file(path,mimetype="image/png")
421
 
422
+ # =============================
423
  # COLORIZE
424
+ # =============================
425
 
426
  @app.route("/colorize",methods=["POST"])
427
  def colorize():
 
438
 
439
  return send_file(path,mimetype="image/png")
440
 
441
+ # =============================
442
+ # REMOVE BACKGROUND
443
+ # =============================
444
 
445
+ @app.route("/removebg",methods=["POST"])
446
+ def removebg():
447
 
448
+ file=request.files["image"]
449
 
450
+ img=Image.open(file.stream)
 
 
 
 
 
 
451
 
452
+ img=img.convert("RGBA")
453
 
454
+ datas=img.getdata()
455
 
456
+ newData=[]
457
 
458
+ for item in datas:
459
 
460
+ if item[0]>240 and item[1]>240 and item[2]>240:
461
+ newData.append((255,255,255,0))
462
+ else:
463
+ newData.append(item)
464
+
465
+ img.putdata(newData)
466
+
467
+ path=OUTPUT_DIR+"/nobg.png"
468
+
469
+ img.save(path)
470
+
471
+ return send_file(path,mimetype="image/png")
472
+
473
+ # =============================
474
+ # GALLERY
475
+ # =============================
476
+
477
+ @app.route("/gallery")
478
+ def gallery():
479
+
480
+ files=os.listdir(OUTPUT_DIR)
481
+
482
+ html="<h1>Gallery</h1>"
483
+
484
+ for f in files:
485
 
486
+ html+=f'<img src="/outputs/{f}" width="300"><br>'
487
 
488
+ return html
489
 
490
+ # =============================
491
  # START SERVER
492
+ # =============================
493
 
494
  if __name__=="__main__":
495
 
496
+ print("SERVER READY")
497
 
498
+ app.run(host="0.0.0.0",port=7860)