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

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +185 -175
api.py CHANGED
@@ -1,20 +1,24 @@
1
  import os
2
  import torch
3
- from flask import Flask, request, send_file, jsonify, render_template_string
4
  from diffusers import StableDiffusionPipeline
5
  from PIL import Image, ImageFilter
6
  from deep_translator import GoogleTranslator
7
 
8
- print("IMAGE PRO AI STUDIO MAX STARTING")
9
 
10
- CACHE_DIR="./models"
 
 
 
 
11
  OUTPUT_DIR="outputs"
12
 
13
  os.makedirs(OUTPUT_DIR,exist_ok=True)
14
 
15
- # =====================================
16
  # TRANSLATOR
17
- # =====================================
18
 
19
  def translate(prompt):
20
 
@@ -23,93 +27,74 @@ def translate(prompt):
23
  except:
24
  return prompt
25
 
26
- # =====================================
27
  # LOAD MODEL
28
- # =====================================
29
 
30
- print("Loading model...")
31
 
32
  pipe=StableDiffusionPipeline.from_pretrained(
33
- "SG161222/Realistic_Vision_V5.1_noVAE",
34
- cache_dir=CACHE_DIR,
35
- torch_dtype=torch.float32,
36
- safety_checker=None
37
  )
38
 
39
  pipe.enable_attention_slicing()
40
 
41
  print("MODEL READY")
42
 
43
- # =====================================
44
  # FLASK
45
- # =====================================
46
 
47
  app=Flask(__name__)
48
 
49
- # =====================================
50
- # HTML UI
51
- # =====================================
52
 
53
  HTML="""
54
-
55
  <html>
56
-
57
  <head>
58
-
59
- <title>IMAGE PRO AI STUDIO MAX</title>
60
-
61
  <style>
62
 
63
  body{
64
- background:#0b0b0b;
65
  color:white;
66
- font-family:Arial;
67
  text-align:center;
68
- }
69
-
70
- h1{
71
- font-size:42px;
72
  }
73
 
74
  input{
75
- width:420px;
76
- padding:12px;
77
- font-size:16px;
78
  }
79
 
80
  button{
81
- padding:12px 22px;
82
- margin:4px;
83
- background:#ff6a00;
84
  border:none;
85
- color:white;
86
- font-size:15px;
87
  cursor:pointer;
88
  }
89
 
90
  img{
91
- max-width:512px;
92
  margin-top:20px;
93
  }
94
 
95
- #gallery img{
96
- width:200px;
97
- margin:10px;
98
- }
99
-
100
  </style>
101
-
102
  </head>
103
 
104
  <body>
105
 
106
  <h1>IMAGE PRO AI STUDIO MAX</h1>
107
 
108
- <input id="prompt" placeholder="np luksusowa reklama perfum">
109
 
110
  <br><br>
111
 
112
- <button onclick="generate()">IMAGE</button>
113
  <button onclick="product()">PRODUCT</button>
114
  <button onclick="logo()">LOGO</button>
115
  <button onclick="banner()">BANNER</button>
@@ -117,258 +102,243 @@ margin:10px;
117
 
118
  <br><br>
119
 
120
- <input type="file" id="upload">
121
 
122
- <br>
123
 
124
  <button onclick="restore()">RESTORE</button>
125
  <button onclick="upscale()">UPSCALE</button>
126
- <button onclick="colorize()">COLORIZE</button>
127
 
128
  <h2>RESULT</h2>
129
 
130
  <img id="result">
131
 
132
- <h2>GALLERY</h2>
133
-
134
- <div id="gallery"></div>
135
-
136
  <script>
137
 
138
- function show(blob){
139
-
140
- url=URL.createObjectURL(blob)
141
-
142
- document.getElementById("result").src=url
143
-
144
- img=document.createElement("img")
145
- img.src=url
146
-
147
- document.getElementById("gallery").appendChild(img)
148
-
149
- }
150
-
151
- async function generate(){
152
-
153
- prompt=document.getElementById("prompt").value
154
 
155
- r=await fetch("/generate",{
156
  method:"POST",
157
  headers:{"Content-Type":"application/json"},
158
- body:JSON.stringify({prompt:prompt})
 
 
 
 
159
  })
160
-
161
- show(await r.blob())
162
 
163
  }
164
 
165
- async function product(){
166
-
167
- prompt=document.getElementById("prompt").value
168
 
169
- r=await fetch("/product",{method:"POST",
 
170
  headers:{"Content-Type":"application/json"},
171
- body:JSON.stringify({prompt:prompt})})
172
-
173
- show(await r.blob())
 
 
 
174
 
175
  }
176
 
177
- async function logo(){
178
-
179
- prompt=document.getElementById("prompt").value
180
 
181
- r=await fetch("/logo",{method:"POST",
 
182
  headers:{"Content-Type":"application/json"},
183
- body:JSON.stringify({prompt:prompt})})
184
-
185
- show(await r.blob())
 
 
 
186
 
187
  }
188
 
189
- async function banner(){
190
-
191
- prompt=document.getElementById("prompt").value
192
 
193
- r=await fetch("/banner",{method:"POST",
 
194
  headers:{"Content-Type":"application/json"},
195
- body:JSON.stringify({prompt:prompt})})
196
-
197
- show(await r.blob())
 
 
 
198
 
199
  }
200
 
201
- async function social(){
202
-
203
- prompt=document.getElementById("prompt").value
204
 
205
- r=await fetch("/social",{method:"POST",
 
206
  headers:{"Content-Type":"application/json"},
207
- body:JSON.stringify({prompt:prompt})})
208
-
209
- show(await r.blob())
 
 
 
210
 
211
  }
212
 
213
- async function restore(){
214
-
215
- file=document.getElementById("upload").files[0]
216
-
217
- f=new FormData()
218
 
219
- f.append("image",file)
220
 
221
- r=await fetch("/restore",{method:"POST",body:f})
 
222
 
223
- show(await r.blob())
 
 
 
 
224
 
225
  }
226
 
227
- async function upscale(){
228
-
229
- file=document.getElementById("upload").files[0]
230
-
231
- f=new FormData()
232
 
233
- f.append("image",file)
234
 
235
- r=await fetch("/upscale",{method:"POST",body:f})
 
236
 
237
- show(await r.blob())
 
 
 
 
238
 
239
  }
240
 
241
- async function colorize(){
242
-
243
- file=document.getElementById("upload").files[0]
244
 
245
- f=new FormData()
246
 
247
- f.append("image",file)
 
248
 
249
- r=await fetch("/colorize",{method:"POST",body:f})
250
-
251
- show(await r.blob())
 
 
252
 
253
  }
254
 
255
  </script>
256
 
257
  </body>
258
-
259
  </html>
260
-
261
  """
262
 
263
- # =====================================
264
- # HOME
265
- # =====================================
266
-
267
  @app.route("/")
268
  def home():
269
  return render_template_string(HTML)
270
 
271
- # =====================================
272
- # GENERATE
273
- # =====================================
274
 
275
  @app.route("/generate",methods=["POST"])
276
  def generate():
277
 
278
  prompt=translate(request.json["prompt"])
279
 
280
- img=pipe(prompt).images[0]
281
 
282
  path=OUTPUT_DIR+"/image.png"
283
-
284
- img.save(path)
285
 
286
  return send_file(path,mimetype="image/png")
287
 
288
- # =====================================
289
  # PRODUCT PHOTO
290
- # =====================================
291
 
292
  @app.route("/product",methods=["POST"])
293
  def product():
294
 
295
  prompt=translate(request.json["prompt"])
296
 
297
- prompt+=", luxury product photography"
298
 
299
- img=pipe(prompt).images[0]
300
 
301
  path=OUTPUT_DIR+"/product.png"
302
-
303
- img.save(path)
304
 
305
  return send_file(path,mimetype="image/png")
306
 
307
- # =====================================
308
  # LOGO
309
- # =====================================
310
 
311
  @app.route("/logo",methods=["POST"])
312
  def logo():
313
 
314
  prompt=translate(request.json["prompt"])
315
 
316
- prompt+=", modern vector logo"
317
 
318
- img=pipe(prompt).images[0]
319
 
320
  path=OUTPUT_DIR+"/logo.png"
321
-
322
- img.save(path)
323
 
324
  return send_file(path,mimetype="image/png")
325
 
326
- # =====================================
327
  # BANNER
328
- # =====================================
329
 
330
  @app.route("/banner",methods=["POST"])
331
  def banner():
332
 
333
  prompt=translate(request.json["prompt"])
334
 
335
- prompt+=", marketing banner"
336
 
337
- img=pipe(prompt).images[0]
338
 
339
  path=OUTPUT_DIR+"/banner.png"
340
-
341
- img.save(path)
342
 
343
  return send_file(path,mimetype="image/png")
344
 
345
- # =====================================
346
- # SOCIAL MEDIA
347
- # =====================================
348
 
349
  @app.route("/social",methods=["POST"])
350
  def social():
351
 
352
  prompt=translate(request.json["prompt"])
353
 
354
- prompt+=", instagram ad design"
355
 
356
- img=pipe(prompt).images[0]
357
 
358
  path=OUTPUT_DIR+"/social.png"
359
-
360
- img.save(path)
361
 
362
  return send_file(path,mimetype="image/png")
363
 
364
- # =====================================
365
- # RESTORE
366
- # =====================================
367
 
368
  @app.route("/restore",methods=["POST"])
369
  def restore():
370
 
371
- img=Image.open(request.files["image"].stream)
 
 
372
 
373
  img=img.filter(ImageFilter.SHARPEN)
374
 
@@ -378,14 +348,16 @@ def restore():
378
 
379
  return send_file(path,mimetype="image/png")
380
 
381
- # =====================================
382
  # UPSCALE
383
- # =====================================
384
 
385
  @app.route("/upscale",methods=["POST"])
386
  def upscale():
387
 
388
- img=Image.open(request.files["image"].stream)
 
 
389
 
390
  w,h=img.size
391
 
@@ -397,14 +369,16 @@ def upscale():
397
 
398
  return send_file(path,mimetype="image/png")
399
 
400
- # =====================================
401
  # COLORIZE
402
- # =====================================
403
 
404
  @app.route("/colorize",methods=["POST"])
405
  def colorize():
406
 
407
- img=Image.open(request.files["image"].stream)
 
 
408
 
409
  img=img.convert("RGB")
410
 
@@ -414,10 +388,46 @@ def colorize():
414
 
415
  return send_file(path,mimetype="image/png")
416
 
417
- # =====================================
418
- # START
419
- # =====================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420
 
421
  if __name__=="__main__":
422
 
423
- app.run(host="0.0.0.0",port=7860)
 
 
 
 
 
 
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
 
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
  except:
28
  return prompt
29
 
30
+ # ===============================
31
  # LOAD MODEL
32
+ # ===============================
33
 
34
+ print("Loading AI model...")
35
 
36
  pipe=StableDiffusionPipeline.from_pretrained(
37
+ "runwayml/stable-diffusion-v1-5",
38
+ torch_dtype=torch.float32
 
 
39
  )
40
 
41
  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;
 
 
 
66
  }
67
 
68
  input{
69
+ width:400px;
70
+ padding:10px;
 
71
  }
72
 
73
  button{
74
+ background:orange;
 
 
75
  border:none;
76
+ padding:12px 20px;
77
+ margin:5px;
78
  cursor:pointer;
79
  }
80
 
81
  img{
82
+ max-width:600px;
83
  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
 
97
+ <button onclick="gen()">IMAGE</button>
98
  <button onclick="product()">PRODUCT</button>
99
  <button onclick="logo()">LOGO</button>
100
  <button onclick="banner()">BANNER</button>
 
102
 
103
  <br><br>
104
 
105
+ <input type="file" id="file">
106
 
107
+ <br><br>
108
 
109
  <button onclick="restore()">RESTORE</button>
110
  <button onclick="upscale()">UPSCALE</button>
111
+ <button onclick="color()">COLORIZE</button>
112
 
113
  <h2>RESULT</h2>
114
 
115
  <img id="result">
116
 
 
 
 
 
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():
338
 
339
+ file=request.files["image"]
340
+
341
+ img=Image.open(file.stream)
342
 
343
  img=img.filter(ImageFilter.SHARPEN)
344
 
 
348
 
349
  return send_file(path,mimetype="image/png")
350
 
351
+ # ===============================
352
  # UPSCALE
353
+ # ===============================
354
 
355
  @app.route("/upscale",methods=["POST"])
356
  def upscale():
357
 
358
+ file=request.files["image"]
359
+
360
+ img=Image.open(file.stream)
361
 
362
  w,h=img.size
363
 
 
369
 
370
  return send_file(path,mimetype="image/png")
371
 
372
+ # ===============================
373
  # COLORIZE
374
+ # ===============================
375
 
376
  @app.route("/colorize",methods=["POST"])
377
  def colorize():
378
 
379
+ file=request.files["image"]
380
+
381
+ img=Image.open(file.stream)
382
 
383
  img=img.convert("RGB")
384
 
 
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
+ )