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

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +275 -118
api.py CHANGED
@@ -5,88 +5,96 @@ from diffusers import StableDiffusionPipeline
5
  from PIL import Image, ImageFilter
6
  from deep_translator import GoogleTranslator
7
 
8
- print("IMAGE PRO AI 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(prompt):
20
 
21
  try:
22
- prompt_en = GoogleTranslator(source="pl", target="en").translate(prompt)
23
- print("PL:", prompt)
24
- print("EN:", prompt_en)
25
- return prompt_en
26
  except:
27
  return prompt
28
 
29
- # ===============================
30
  # LOAD MODEL
31
- # ===============================
32
 
33
  print("Loading model...")
34
 
35
- pipe = StableDiffusionPipeline.from_pretrained(
36
- "SG161222/Realistic_Vision_V5.1_noVAE",
37
- cache_dir=CACHE_DIR,
38
- torch_dtype=torch.float32,
39
- safety_checker=None
40
  )
41
 
42
  pipe.enable_attention_slicing()
43
 
44
  print("MODEL READY")
45
 
46
- # ===============================
47
  # FLASK
48
- # ===============================
49
 
50
- app = Flask(__name__)
51
 
52
- # ===============================
53
- # HTML PANEL
54
- # ===============================
55
 
56
- HTML = """
57
 
58
  <html>
59
 
60
  <head>
61
 
62
- <title>IMAGE PRO AI</title>
63
 
64
  <style>
65
 
66
  body{
67
- background:black;
68
  color:white;
69
  font-family:Arial;
70
  text-align:center;
71
  }
72
 
 
 
 
 
73
  input{
74
- width:400px;
75
- padding:10px;
 
76
  }
77
 
78
  button{
79
- padding:10px 20px;
80
- background:orange;
 
81
  border:none;
82
  color:white;
83
- font-size:16px;
84
  cursor:pointer;
85
  }
86
 
87
  img{
88
- margin-top:20px;
89
  max-width:512px;
 
 
 
 
 
 
90
  }
91
 
92
  </style>
@@ -95,42 +103,152 @@ max-width:512px;
95
 
96
  <body>
97
 
98
- <h1>IMAGE PRO AI</h1>
 
 
 
 
 
 
 
 
 
 
99
 
100
- <input id="prompt" placeholder="np. truskawka na stole"/>
101
  <br><br>
102
 
103
- <button onclick="generate()">GENERUJ</button>
 
 
104
 
105
- <h2>Wynik</h2>
 
 
106
 
107
- <img id="result"/>
 
 
 
 
 
 
108
 
109
  <script>
110
 
111
- async function generate(){
112
 
113
- prompt = document.getElementById("prompt").value
114
 
115
- response = await fetch("/generate",{
116
 
117
- method:"POST",
 
118
 
119
- headers:{
120
- "Content-Type":"application/json"
121
- },
122
 
123
- body:JSON.stringify({
124
- prompt:prompt
125
- })
126
 
 
 
 
 
 
 
 
 
127
  })
128
 
129
- blob = await response.blob()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
- url = URL.createObjectURL(blob)
132
 
133
- document.getElementById("result").src = url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
  }
136
 
@@ -142,125 +260,164 @@ document.getElementById("result").src = url
142
 
143
  """
144
 
145
- # ===============================
146
  # HOME
147
- # ===============================
148
 
149
  @app.route("/")
150
  def home():
151
  return render_template_string(HTML)
152
 
153
- # ===============================
154
- # GENERATE IMAGE
155
- # ===============================
156
 
157
- @app.route("/generate", methods=["POST"])
158
  def generate():
159
 
160
- data = request.json
161
- prompt = translate_prompt(data["prompt"])
162
 
163
- image = pipe(
164
- prompt,
165
- num_inference_steps=30,
166
- guidance_scale=7.5
167
- ).images[0]
168
 
169
- path = OUTPUT_DIR + "/gen.png"
170
 
171
- image.save(path)
172
 
173
- return send_file(path, mimetype="image/png")
174
 
175
- # ===============================
176
- # PRODUCT AD
177
- # ===============================
178
 
179
- @app.route("/product_ad", methods=["POST"])
180
- def product_ad():
181
 
182
- data = request.json
183
 
184
- prompt = translate_prompt(data["prompt"])
185
 
186
- style = prompt + ", luxury product photography"
187
 
188
- image = pipe(style).images[0]
189
 
190
- path = OUTPUT_DIR + "/product.png"
191
 
192
- image.save(path)
193
 
194
- return send_file(path, mimetype="image/png")
 
 
195
 
196
- # ===============================
197
- # RESTORE PHOTO
198
- # ===============================
199
 
200
- @app.route("/restore", methods=["POST"])
201
- def restore():
 
202
 
203
- file = request.files["image"]
 
 
 
 
204
 
205
- img = Image.open(file.stream)
206
 
207
- img = img.filter(ImageFilter.SHARPEN)
 
 
208
 
209
- path = OUTPUT_DIR + "/restored.png"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
 
211
  img.save(path)
212
 
213
- return send_file(path, mimetype="image/png")
214
 
215
- # ===============================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
  # UPSCALE
217
- # ===============================
218
 
219
- @app.route("/upscale", methods=["POST"])
220
  def upscale():
221
 
222
- file = request.files["image"]
223
 
224
- img = Image.open(file.stream)
225
 
226
- w,h = img.size
227
 
228
- img = img.resize((w*2,h*2), Image.LANCZOS)
229
-
230
- path = OUTPUT_DIR + "/hd.png"
231
 
232
  img.save(path)
233
 
234
- return send_file(path, mimetype="image/png")
235
 
236
- # ===============================
237
  # COLORIZE
238
- # ===============================
239
 
240
- @app.route("/colorize", methods=["POST"])
241
  def colorize():
242
 
243
- file = request.files["image"]
244
-
245
- img = Image.open(file.stream)
246
 
247
- img = img.convert("RGB")
248
 
249
- path = OUTPUT_DIR + "/color.png"
250
 
251
  img.save(path)
252
 
253
- return send_file(path, mimetype="image/png")
254
-
255
- # ===============================
256
- # START SERVER
257
- # ===============================
258
 
259
- if __name__ == "__main__":
 
 
260
 
261
- print("SERVER READY")
262
 
263
- app.run(
264
- host="0.0.0.0",
265
- port=7860
266
- )
 
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
 
21
  try:
22
+ return GoogleTranslator(source="pl",target="en").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>
 
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>
116
+ <button onclick="social()">SOCIAL</button>
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
 
 
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
+
375
+ path=OUTPUT_DIR+"/restore.png"
376
+
377
+ img.save(path)
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
 
392
+ img=img.resize((w*2,h*2),Image.LANCZOS)
393
 
394
+ path=OUTPUT_DIR+"/upscale.png"
 
 
395
 
396
  img.save(path)
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
 
411
+ path=OUTPUT_DIR+"/color.png"
412
 
413
  img.save(path)
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)