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

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +210 -45
api.py CHANGED
@@ -5,7 +5,7 @@ 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
  OUTPUT_DIR="outputs"
11
  os.makedirs(OUTPUT_DIR,exist_ok=True)
@@ -14,20 +14,22 @@ os.makedirs(OUTPUT_DIR,exist_ok=True)
14
  # TRANSLATE
15
  # ======================
16
 
17
- def translate(prompt):
 
18
  try:
19
- return GoogleTranslator(source="pl",target="en").translate(prompt)
20
  except:
21
- return prompt
 
 
22
 
23
- def enhance(prompt):
24
- return prompt+", ultra realistic, cinematic lighting, high detail"
25
 
26
  # ======================
27
- # LOAD MODEL
28
  # ======================
29
 
30
- print("Loading model...")
31
 
32
  pipe=StableDiffusionPipeline.from_pretrained(
33
  "runwayml/stable-diffusion-v1-5",
@@ -48,13 +50,36 @@ app=Flask(__name__)
48
  HTML="""
49
  <html>
50
  <head>
51
- <title>IMAGE PRO AI</title>
 
52
 
53
  <style>
54
- body{background:black;color:white;text-align:center;font-family:Arial}
55
- button{background:orange;border:none;padding:10px;margin:5px}
56
- input{width:400px;padding:10px}
57
- img{max-width:600px;margin-top:20px}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  </style>
59
 
60
  </head>
@@ -68,6 +93,10 @@ img{max-width:600px;margin-top:20px}
68
  <br><br>
69
 
70
  <button onclick="generate()">IMAGE</button>
 
 
 
 
71
 
72
  <br><br>
73
 
@@ -101,15 +130,83 @@ document.getElementById("result").src=URL.createObjectURL(b)
101
 
102
  }
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  function restore(){
105
 
106
- var f=document.getElementById("file").files[0]
107
 
108
- var d=new FormData()
 
 
 
109
 
110
- d.append("image",f)
 
111
 
112
- fetch("/restore",{method:"POST",body:d})
113
  .then(r=>r.blob())
114
  .then(b=>{
115
  document.getElementById("result").src=URL.createObjectURL(b)
@@ -119,13 +216,17 @@ document.getElementById("result").src=URL.createObjectURL(b)
119
 
120
  function upscale(){
121
 
122
- var f=document.getElementById("file").files[0]
123
 
124
- var d=new FormData()
 
 
 
125
 
126
- d.append("image",f)
 
127
 
128
- fetch("/upscale",{method:"POST",body:d})
129
  .then(r=>r.blob())
130
  .then(b=>{
131
  document.getElementById("result").src=URL.createObjectURL(b)
@@ -135,13 +236,17 @@ document.getElementById("result").src=URL.createObjectURL(b)
135
 
136
  function color(){
137
 
138
- var f=document.getElementById("file").files[0]
139
 
140
- var d=new FormData()
 
 
 
141
 
142
- d.append("image",f)
 
143
 
144
- fetch("/colorize",{method:"POST",body:d})
145
  .then(r=>r.blob())
146
  .then(b=>{
147
  document.getElementById("result").src=URL.createObjectURL(b)
@@ -169,33 +274,89 @@ def generate():
169
  try:
170
 
171
  data=request.json
172
-
173
  prompt=data.get("prompt","")
174
 
175
  prompt=enhance(translate(prompt))
176
 
177
- print("PROMPT:",prompt)
178
-
179
- generator=torch.Generator(device="cpu")
180
-
181
- image=pipe(
182
- prompt,
183
- num_inference_steps=20,
184
- generator=generator
185
- ).images[0]
186
-
187
- path=OUTPUT_DIR+"/gen.png"
188
 
 
189
  image.save(path)
190
 
191
  return send_file(path,mimetype="image/png")
192
 
193
  except Exception as e:
194
 
195
- print("ERROR:",e)
196
-
197
  return jsonify({"error":str(e)})
198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  # ======================
200
  # RESTORE
201
  # ======================
@@ -203,6 +364,9 @@ def generate():
203
  @app.route("/restore",methods=["POST"])
204
  def restore():
205
 
 
 
 
206
  file=request.files["image"]
207
 
208
  img=Image.open(file.stream)
@@ -222,6 +386,9 @@ def restore():
222
  @app.route("/upscale",methods=["POST"])
223
  def upscale():
224
 
 
 
 
225
  file=request.files["image"]
226
 
227
  img=Image.open(file.stream)
@@ -243,6 +410,9 @@ def upscale():
243
  @app.route("/colorize",methods=["POST"])
244
  def colorize():
245
 
 
 
 
246
  file=request.files["image"]
247
 
248
  img=Image.open(file.stream)
@@ -256,12 +426,7 @@ def colorize():
256
  return send_file(path,mimetype="image/png")
257
 
258
  # ======================
259
- # START
260
- # ======================
261
 
262
  if __name__=="__main__":
263
 
264
- app.run(
265
- host="0.0.0.0",
266
- port=7860
267
- )
 
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)
 
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",
 
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{
66
+ background:orange;
67
+ border:none;
68
+ padding:10px;
69
+ margin:5px;
70
+ cursor:pointer;
71
+ }
72
+
73
+ input{
74
+ width:400px;
75
+ padding:10px;
76
+ }
77
+
78
+ img{
79
+ max-width:600px;
80
+ margin-top:20px;
81
+ }
82
+
83
  </style>
84
 
85
  </head>
 
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
 
 
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({
155
+ prompt:document.getElementById("prompt").value
156
+ })
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
 
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)
 
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)
 
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
  # ======================
 
364
  @app.route("/restore",methods=["POST"])
365
  def restore():
366
 
367
+ if "image" not in request.files:
368
+ return "no image",400
369
+
370
  file=request.files["image"]
371
 
372
  img=Image.open(file.stream)
 
386
  @app.route("/upscale",methods=["POST"])
387
  def upscale():
388
 
389
+ if "image" not in request.files:
390
+ return "no image",400
391
+
392
  file=request.files["image"]
393
 
394
  img=Image.open(file.stream)
 
410
  @app.route("/colorize",methods=["POST"])
411
  def colorize():
412
 
413
+ if "image" not in request.files:
414
+ return "no image",400
415
+
416
  file=request.files["image"]
417
 
418
  img=Image.open(file.stream)
 
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)