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

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +136 -31
api.py CHANGED
@@ -42,10 +42,10 @@ def enhance(prompt):
42
  print("Loading AI model...")
43
 
44
  pipe=StableDiffusionPipeline.from_pretrained(
45
- "runwayml/stable-diffusion-v1-5",
46
- torch_dtype=torch.float32
47
  )
48
 
 
49
  pipe.enable_attention_slicing()
50
 
51
  print("MODEL READY")
@@ -139,96 +139,185 @@ margin-top:20px;
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>
@@ -249,14 +338,22 @@ def home():
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
  # =============================
@@ -267,11 +364,13 @@ def generate():
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")
@@ -284,11 +383,13 @@ def product():
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")
@@ -301,11 +402,13 @@ def logo():
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")
@@ -318,11 +421,13 @@ def banner():
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")
 
42
  print("Loading AI model...")
43
 
44
  pipe=StableDiffusionPipeline.from_pretrained(
45
+ "runwayml/stable-diffusion-v1-5"
 
46
  )
47
 
48
+ pipe=pipe.to("cpu")
49
  pipe.enable_attention_slicing()
50
 
51
  print("MODEL READY")
 
139
  <script>
140
 
141
  function gen(){
142
+
143
+ fetch("/generate",{
144
+ method:"POST",
145
+ headers:{"Content-Type":"application/json"},
146
+ body:JSON.stringify({
147
+ prompt:document.getElementById("prompt").value
148
+ })
149
+ })
150
  .then(r=>r.blob())
151
+ .then(b=>{
152
+ document.getElementById("result").src=URL.createObjectURL(b)
153
+ })
154
+
155
  }
156
 
157
  function product(){
158
+
159
+ fetch("/product",{
160
+ method:"POST",
161
+ headers:{"Content-Type":"application/json"},
162
+ body:JSON.stringify({
163
+ prompt:document.getElementById("prompt").value
164
+ })
165
+ })
166
  .then(r=>r.blob())
167
+ .then(b=>{
168
+ document.getElementById("result").src=URL.createObjectURL(b)
169
+ })
170
+
171
  }
172
 
173
  function logo(){
174
+
175
+ fetch("/logo",{
176
+ method:"POST",
177
+ headers:{"Content-Type":"application/json"},
178
+ body:JSON.stringify({
179
+ prompt:document.getElementById("prompt").value
180
+ })
181
+ })
182
  .then(r=>r.blob())
183
+ .then(b=>{
184
+ document.getElementById("result").src=URL.createObjectURL(b)
185
+ })
186
+
187
  }
188
 
189
  function banner(){
190
+
191
+ fetch("/banner",{
192
+ method:"POST",
193
+ headers:{"Content-Type":"application/json"},
194
+ body:JSON.stringify({
195
+ prompt:document.getElementById("prompt").value
196
+ })
197
+ })
198
  .then(r=>r.blob())
199
+ .then(b=>{
200
+ document.getElementById("result").src=URL.createObjectURL(b)
201
+ })
202
+
203
  }
204
 
205
  function social(){
206
+
207
+ fetch("/social",{
208
+ method:"POST",
209
+ headers:{"Content-Type":"application/json"},
210
+ body:JSON.stringify({
211
+ prompt:document.getElementById("prompt").value
212
+ })
213
+ })
214
  .then(r=>r.blob())
215
+ .then(b=>{
216
+ document.getElementById("result").src=URL.createObjectURL(b)
217
+ })
218
+
219
  }
220
 
221
  function batch(){
222
+
223
+ fetch("/batch",{
224
+ method:"POST",
225
+ headers:{"Content-Type":"application/json"},
226
+ body:JSON.stringify({
227
+ prompt:document.getElementById("prompt").value
228
+ })
229
+ })
230
  .then(r=>r.json())
231
+ .then(data=>{
232
+ alert("Generated "+data.length+" images")
233
+ })
234
+
235
  }
236
 
237
  function variations(){
238
+
239
+ fetch("/variations",{
240
+ method:"POST",
241
+ headers:{"Content-Type":"application/json"},
242
+ body:JSON.stringify({
243
+ prompt:document.getElementById("prompt").value
244
+ })
245
+ })
246
  .then(r=>r.json())
247
+ .then(data=>{
248
+ alert("Created "+data.length+" variations")
249
+ })
250
+
251
  }
252
 
253
  function restore(){
254
+
255
+ var f=document.getElementById("file").files[0]
256
+
257
  var d=new FormData()
258
+
259
  d.append("image",f)
260
 
261
  fetch("/restore",{method:"POST",body:d})
262
  .then(r=>r.blob())
263
+ .then(b=>{
264
+ document.getElementById("result").src=URL.createObjectURL(b)
265
+ })
266
+
267
  }
268
 
269
  function upscale(){
270
+
271
+ var f=document.getElementById("file").files[0]
272
+
273
  var d=new FormData()
274
+
275
  d.append("image",f)
276
 
277
  fetch("/upscale",{method:"POST",body:d})
278
  .then(r=>r.blob())
279
+ .then(b=>{
280
+ document.getElementById("result").src=URL.createObjectURL(b)
281
+ })
282
+
283
  }
284
 
285
  function color(){
286
+
287
+ var f=document.getElementById("file").files[0]
288
+
289
  var d=new FormData()
290
+
291
  d.append("image",f)
292
 
293
  fetch("/colorize",{method:"POST",body:d})
294
  .then(r=>r.blob())
295
+ .then(b=>{
296
+ document.getElementById("result").src=URL.createObjectURL(b)
297
+ })
298
+
299
  }
300
 
301
  function removebg(){
302
+
303
+ var f=document.getElementById("file").files[0]
304
+
305
  var d=new FormData()
306
+
307
  d.append("image",f)
308
 
309
  fetch("/removebg",{method:"POST",body:d})
310
  .then(r=>r.blob())
311
+ .then(b=>{
312
+ document.getElementById("result").src=URL.createObjectURL(b)
313
+ })
314
+
315
  }
316
 
317
  function gallery(){
318
+
319
  window.location="/gallery"
320
+
321
  }
322
 
323
  </script>
 
338
  @app.route("/generate",methods=["POST"])
339
  def generate():
340
 
341
+ print("GENERATE START")
342
+
343
  prompt=request.json["prompt"]
344
+
345
  prompt=enhance(translate(prompt))
346
 
347
+ print("PROMPT:",prompt)
348
+
349
  img=pipe(prompt,num_inference_steps=20).images[0]
350
 
351
  path=OUTPUT_DIR+"/image.png"
352
+
353
  img.save(path)
354
 
355
+ print("IMAGE SAVED")
356
+
357
  return send_file(path,mimetype="image/png")
358
 
359
  # =============================
 
364
  def product():
365
 
366
  prompt=request.json["prompt"]
367
+
368
  prompt=translate(prompt)+", luxury product photography"
369
 
370
  img=pipe(prompt).images[0]
371
 
372
  path=OUTPUT_DIR+"/product.png"
373
+
374
  img.save(path)
375
 
376
  return send_file(path,mimetype="image/png")
 
383
  def logo():
384
 
385
  prompt=request.json["prompt"]
386
+
387
  prompt=translate(prompt)+", minimalist vector logo"
388
 
389
  img=pipe(prompt).images[0]
390
 
391
  path=OUTPUT_DIR+"/logo.png"
392
+
393
  img.save(path)
394
 
395
  return send_file(path,mimetype="image/png")
 
402
  def banner():
403
 
404
  prompt=request.json["prompt"]
405
+
406
  prompt=translate(prompt)+", website banner design"
407
 
408
  img=pipe(prompt).images[0]
409
 
410
  path=OUTPUT_DIR+"/banner.png"
411
+
412
  img.save(path)
413
 
414
  return send_file(path,mimetype="image/png")
 
421
  def social():
422
 
423
  prompt=request.json["prompt"]
424
+
425
  prompt=translate(prompt)+", instagram advertising post"
426
 
427
  img=pipe(prompt).images[0]
428
 
429
  path=OUTPUT_DIR+"/social.png"
430
+
431
  img.save(path)
432
 
433
  return send_file(path,mimetype="image/png")