webnowa commited on
Commit
497f32f
·
verified ·
1 Parent(s): 294b6e2

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +41 -212
api.py CHANGED
@@ -1,35 +1,13 @@
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("STARTING IMAGE PRO AI")
 
9
 
10
- OUTPUT_DIR="outputs"
11
- os.makedirs(OUTPUT_DIR,exist_ok=True)
12
-
13
- # ======================
14
- # TRANSLATE
15
- # ======================
16
-
17
- def translate(text):
18
-
19
- try:
20
- return GoogleTranslator(source="pl",target="en").translate(text)
21
- except:
22
- return text
23
-
24
- def enhance(text):
25
-
26
- return text+", ultra realistic, cinematic lighting, high detail, 8k"
27
-
28
- # ======================
29
- # MODEL
30
- # ======================
31
-
32
- print("Loading Stable Diffusion...")
33
 
34
  pipe=StableDiffusionPipeline.from_pretrained(
35
  "runwayml/stable-diffusion-v1-5",
@@ -41,25 +19,22 @@ pipe.enable_attention_slicing()
41
 
42
  print("MODEL READY")
43
 
44
- # ======================
45
- # APP
46
- # ======================
47
-
48
  app=Flask(__name__)
49
 
50
  HTML="""
 
51
  <html>
52
  <head>
53
 
54
- <title>IMAGE PRO AI STUDIO</title>
55
 
56
  <style>
57
 
58
  body{
59
  background:black;
60
  color:white;
61
- text-align:center;
62
  font-family:Arial;
 
63
  }
64
 
65
  button{
@@ -71,8 +46,8 @@ cursor:pointer;
71
  }
72
 
73
  input{
74
- width:400px;
75
  padding:10px;
 
76
  }
77
 
78
  img{
@@ -93,10 +68,6 @@ margin-top:20px;
93
  <br><br>
94
 
95
  <button onclick="generate()">IMAGE</button>
96
- <button onclick="product()">PRODUCT</button>
97
- <button onclick="logo()">LOGO</button>
98
- <button onclick="banner()">BANNER</button>
99
- <button onclick="social()">SOCIAL</button>
100
 
101
  <br><br>
102
 
@@ -106,9 +77,9 @@ margin-top:20px;
106
 
107
  <button onclick="restore()">RESTORE</button>
108
  <button onclick="upscale()">UPSCALE</button>
109
- <button onclick="color()">COLORIZE</button>
110
 
111
- <h2>RESULT</h2>
112
 
113
  <img id="result">
114
 
@@ -116,39 +87,9 @@ margin-top:20px;
116
 
117
  function generate(){
118
 
119
- fetch("/generate",{
120
- method:"POST",
121
- headers:{"Content-Type":"application/json"},
122
- body:JSON.stringify({
123
- prompt:document.getElementById("prompt").value
124
- })
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({
139
- prompt:document.getElementById("prompt").value
140
- })
141
- })
142
- .then(r=>r.blob())
143
- .then(b=>{
144
- document.getElementById("result").src=URL.createObjectURL(b)
145
- })
146
-
147
- }
148
-
149
- function logo(){
150
-
151
- fetch("/logo",{
152
  method:"POST",
153
  headers:{"Content-Type":"application/json"},
154
  body:JSON.stringify({
@@ -157,56 +98,28 @@ prompt:document.getElementById("prompt").value
157
  })
158
  .then(r=>r.blob())
159
  .then(b=>{
160
- document.getElementById("result").src=URL.createObjectURL(b)
161
- })
162
-
163
- }
164
-
165
- function banner(){
166
 
167
- fetch("/banner",{
168
- method:"POST",
169
- headers:{"Content-Type":"application/json"},
170
- body:JSON.stringify({
171
- prompt:document.getElementById("prompt").value
172
- })
173
- })
174
- .then(r=>r.blob())
175
- .then(b=>{
176
  document.getElementById("result").src=URL.createObjectURL(b)
177
- })
178
-
179
- }
180
 
181
- function social(){
182
 
183
- fetch("/social",{
184
- method:"POST",
185
- headers:{"Content-Type":"application/json"},
186
- body:JSON.stringify({
187
- prompt:document.getElementById("prompt").value
188
- })
189
- })
190
- .then(r=>r.blob())
191
- .then(b=>{
192
- document.getElementById("result").src=URL.createObjectURL(b)
193
  })
194
 
195
  }
196
 
197
  function restore(){
198
 
199
- let file=document.getElementById("file").files[0]
200
 
201
- if(!file){
202
- alert("Select image first")
203
  return
204
  }
205
 
206
- let data=new FormData()
207
- data.append("image",file)
208
 
209
- fetch("/restore",{method:"POST",body:data})
210
  .then(r=>r.blob())
211
  .then(b=>{
212
  document.getElementById("result").src=URL.createObjectURL(b)
@@ -216,17 +129,17 @@ document.getElementById("result").src=URL.createObjectURL(b)
216
 
217
  function upscale(){
218
 
219
- let file=document.getElementById("file").files[0]
220
 
221
- if(!file){
222
- alert("Select image first")
223
  return
224
  }
225
 
226
- let data=new FormData()
227
- data.append("image",file)
228
 
229
- fetch("/upscale",{method:"POST",body:data})
230
  .then(r=>r.blob())
231
  .then(b=>{
232
  document.getElementById("result").src=URL.createObjectURL(b)
@@ -234,19 +147,19 @@ document.getElementById("result").src=URL.createObjectURL(b)
234
 
235
  }
236
 
237
- function color(){
238
 
239
- let file=document.getElementById("file").files[0]
240
 
241
- if(!file){
242
- alert("Select image first")
243
  return
244
  }
245
 
246
- let data=new FormData()
247
- data.append("image",file)
248
 
249
- fetch("/colorize",{method:"POST",body:data})
250
  .then(r=>r.blob())
251
  .then(b=>{
252
  document.getElementById("result").src=URL.createObjectURL(b)
@@ -258,108 +171,30 @@ document.getElementById("result").src=URL.createObjectURL(b)
258
 
259
  </body>
260
  </html>
 
261
  """
262
 
263
  @app.route("/")
264
  def home():
265
  return render_template_string(HTML)
266
 
267
- # ======================
268
  # GENERATE
269
- # ======================
270
 
271
  @app.route("/generate",methods=["POST"])
272
  def generate():
273
 
274
- try:
275
-
276
- data=request.json
277
- prompt=data.get("prompt","")
278
-
279
- prompt=enhance(translate(prompt))
280
-
281
- image=pipe(prompt,num_inference_steps=20).images[0]
282
-
283
- path=OUTPUT_DIR+"/image.png"
284
- image.save(path)
285
-
286
- return send_file(path,mimetype="image/png")
287
-
288
- except Exception as e:
289
-
290
- return jsonify({"error":str(e)})
291
-
292
- # ======================
293
- # PRODUCT
294
- # ======================
295
-
296
- @app.route("/product",methods=["POST"])
297
- def product():
298
 
299
- prompt=request.json["prompt"]
300
- prompt=translate(prompt)+", product photography"
301
 
302
- image=pipe(prompt).images[0]
303
 
304
- path=OUTPUT_DIR+"/product.png"
305
  image.save(path)
306
 
307
  return send_file(path,mimetype="image/png")
308
 
309
- # ======================
310
- # LOGO
311
- # ======================
312
-
313
- @app.route("/logo",methods=["POST"])
314
- def logo():
315
-
316
- prompt=request.json["prompt"]
317
- prompt=translate(prompt)+", minimalist vector logo"
318
-
319
- image=pipe(prompt).images[0]
320
-
321
- path=OUTPUT_DIR+"/logo.png"
322
- image.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=request.json["prompt"]
334
- prompt=translate(prompt)+", website banner design"
335
-
336
- image=pipe(prompt).images[0]
337
-
338
- path=OUTPUT_DIR+"/banner.png"
339
- image.save(path)
340
-
341
- return send_file(path,mimetype="image/png")
342
-
343
- # ======================
344
- # SOCIAL
345
- # ======================
346
-
347
- @app.route("/social",methods=["POST"])
348
- def social():
349
-
350
- prompt=request.json["prompt"]
351
- prompt=translate(prompt)+", instagram post"
352
-
353
- image=pipe(prompt).images[0]
354
-
355
- path=OUTPUT_DIR+"/social.png"
356
- image.save(path)
357
-
358
- return send_file(path,mimetype="image/png")
359
-
360
- # ======================
361
  # RESTORE
362
- # ======================
363
 
364
  @app.route("/restore",methods=["POST"])
365
  def restore():
@@ -373,15 +208,13 @@ def restore():
373
 
374
  img=img.filter(ImageFilter.SHARPEN)
375
 
376
- path=OUTPUT_DIR+"/restore.png"
377
 
378
  img.save(path)
379
 
380
  return send_file(path,mimetype="image/png")
381
 
382
- # ======================
383
  # UPSCALE
384
- # ======================
385
 
386
  @app.route("/upscale",methods=["POST"])
387
  def upscale():
@@ -395,17 +228,15 @@ def upscale():
395
 
396
  w,h=img.size
397
 
398
- img=img.resize((w*2,h*2),Image.LANCZOS)
399
 
400
- path=OUTPUT_DIR+"/upscale.png"
401
 
402
  img.save(path)
403
 
404
  return send_file(path,mimetype="image/png")
405
 
406
- # ======================
407
  # COLORIZE
408
- # ======================
409
 
410
  @app.route("/colorize",methods=["POST"])
411
  def colorize():
@@ -419,14 +250,12 @@ def colorize():
419
 
420
  img=img.convert("RGB")
421
 
422
- path=OUTPUT_DIR+"/color.png"
423
 
424
  img.save(path)
425
 
426
  return send_file(path,mimetype="image/png")
427
 
428
- # ======================
429
-
430
  if __name__=="__main__":
431
 
432
  app.run(host="0.0.0.0",port=7860)
 
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
 
7
+ OUTPUT="outputs"
8
+ os.makedirs(OUTPUT,exist_ok=True)
9
 
10
+ print("Loading model...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  pipe=StableDiffusionPipeline.from_pretrained(
13
  "runwayml/stable-diffusion-v1-5",
 
19
 
20
  print("MODEL READY")
21
 
 
 
 
 
22
  app=Flask(__name__)
23
 
24
  HTML="""
25
+
26
  <html>
27
  <head>
28
 
29
+ <title>IMAGE PRO AI</title>
30
 
31
  <style>
32
 
33
  body{
34
  background:black;
35
  color:white;
 
36
  font-family:Arial;
37
+ text-align:center;
38
  }
39
 
40
  button{
 
46
  }
47
 
48
  input{
 
49
  padding:10px;
50
+ width:400px;
51
  }
52
 
53
  img{
 
68
  <br><br>
69
 
70
  <button onclick="generate()">IMAGE</button>
 
 
 
 
71
 
72
  <br><br>
73
 
 
77
 
78
  <button onclick="restore()">RESTORE</button>
79
  <button onclick="upscale()">UPSCALE</button>
80
+ <button onclick="colorize()">COLORIZE</button>
81
 
82
+ <h2 id="status"></h2>
83
 
84
  <img id="result">
85
 
 
87
 
88
  function generate(){
89
 
90
+ document.getElementById("status").innerText="GENERATING IMAGE..."
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
+ fetch("/generate",{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  method:"POST",
94
  headers:{"Content-Type":"application/json"},
95
  body:JSON.stringify({
 
98
  })
99
  .then(r=>r.blob())
100
  .then(b=>{
 
 
 
 
 
 
101
 
 
 
 
 
 
 
 
 
 
102
  document.getElementById("result").src=URL.createObjectURL(b)
 
 
 
103
 
104
+ document.getElementById("status").innerText="DONE"
105
 
 
 
 
 
 
 
 
 
 
 
106
  })
107
 
108
  }
109
 
110
  function restore(){
111
 
112
+ let f=document.getElementById("file").files[0]
113
 
114
+ if(!f){
115
+ alert("select file")
116
  return
117
  }
118
 
119
+ let d=new FormData()
120
+ d.append("image",f)
121
 
122
+ fetch("/restore",{method:"POST",body:d})
123
  .then(r=>r.blob())
124
  .then(b=>{
125
  document.getElementById("result").src=URL.createObjectURL(b)
 
129
 
130
  function upscale(){
131
 
132
+ let f=document.getElementById("file").files[0]
133
 
134
+ if(!f){
135
+ alert("select file")
136
  return
137
  }
138
 
139
+ let d=new FormData()
140
+ d.append("image",f)
141
 
142
+ fetch("/upscale",{method:"POST",body:d})
143
  .then(r=>r.blob())
144
  .then(b=>{
145
  document.getElementById("result").src=URL.createObjectURL(b)
 
147
 
148
  }
149
 
150
+ function colorize(){
151
 
152
+ let f=document.getElementById("file").files[0]
153
 
154
+ if(!f){
155
+ alert("select file")
156
  return
157
  }
158
 
159
+ let d=new FormData()
160
+ d.append("image",f)
161
 
162
+ fetch("/colorize",{method:"POST",body:d})
163
  .then(r=>r.blob())
164
  .then(b=>{
165
  document.getElementById("result").src=URL.createObjectURL(b)
 
171
 
172
  </body>
173
  </html>
174
+
175
  """
176
 
177
  @app.route("/")
178
  def home():
179
  return render_template_string(HTML)
180
 
 
181
  # GENERATE
 
182
 
183
  @app.route("/generate",methods=["POST"])
184
  def generate():
185
 
186
+ data=request.json
187
+ prompt=data["prompt"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
 
189
+ image=pipe(prompt,num_inference_steps=20).images[0]
 
190
 
191
+ path=OUTPUT+"/image.png"
192
 
 
193
  image.save(path)
194
 
195
  return send_file(path,mimetype="image/png")
196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
  # RESTORE
 
198
 
199
  @app.route("/restore",methods=["POST"])
200
  def restore():
 
208
 
209
  img=img.filter(ImageFilter.SHARPEN)
210
 
211
+ path=OUTPUT+"/restore.png"
212
 
213
  img.save(path)
214
 
215
  return send_file(path,mimetype="image/png")
216
 
 
217
  # UPSCALE
 
218
 
219
  @app.route("/upscale",methods=["POST"])
220
  def upscale():
 
228
 
229
  w,h=img.size
230
 
231
+ img=img.resize((w*2,h*2))
232
 
233
+ path=OUTPUT+"/upscale.png"
234
 
235
  img.save(path)
236
 
237
  return send_file(path,mimetype="image/png")
238
 
 
239
  # COLORIZE
 
240
 
241
  @app.route("/colorize",methods=["POST"])
242
  def colorize():
 
250
 
251
  img=img.convert("RGB")
252
 
253
+ path=OUTPUT+"/color.png"
254
 
255
  img.save(path)
256
 
257
  return send_file(path,mimetype="image/png")
258
 
 
 
259
  if __name__=="__main__":
260
 
261
  app.run(host="0.0.0.0",port=7860)