SakuraD commited on
Commit
19dbe32
Β·
1 Parent(s): fa16113
Files changed (6) hide show
  1. README.md +5 -6
  2. app.py +67 -0
  3. flagged/readme.txt +0 -0
  4. requirements.txt +5 -0
  5. stable-diffusion-img2img +1 -0
  6. unsafe.png +0 -0
README.md CHANGED
@@ -1,13 +1,12 @@
1
  ---
2
- title: Stable Diffusion Demo
3
- emoji: πŸ‘€
4
- colorFrom: purple
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 3.12.0
8
  app_file: app.py
9
  pinned: false
10
- license: mit
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Stable Diffusion Img2img CPU
3
+ emoji: 🎨🌠
4
+ colorFrom: green
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 3.6
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ #from torch import autocast // only for GPU
4
+
5
+ from PIL import Image
6
+ import numpy as np
7
+ from io import BytesIO
8
+ import os
9
+ MY_SECRET_TOKEN=os.environ.get('HF_TOKEN_SD')
10
+
11
+ #from diffusers import StableDiffusionPipeline
12
+ from diffusers import StableDiffusionImg2ImgPipeline
13
+
14
+ print("hello sylvain")
15
+
16
+ YOUR_TOKEN=MY_SECRET_TOKEN
17
+
18
+ device="cpu"
19
+
20
+ #prompt_pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=YOUR_TOKEN)
21
+ #prompt_pipe.to(device)
22
+
23
+ img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", use_auth_token=YOUR_TOKEN)
24
+ img_pipe.to(device)
25
+
26
+ source_img = gr.Image(source="upload", type="filepath", label="init_img | 512*512 px")
27
+ gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[1], height="auto")
28
+
29
+ def resize(value,img):
30
+ #baseheight = value
31
+ img = Image.open(img)
32
+ #hpercent = (baseheight/float(img.size[1]))
33
+ #wsize = int((float(img.size[0])*float(hpercent)))
34
+ #img = img.resize((wsize,baseheight), Image.Resampling.LANCZOS)
35
+ img = img.resize((value,value), Image.Resampling.LANCZOS)
36
+ return img
37
+
38
+
39
+ def infer(source_img, prompt, guide, steps, seed, strength):
40
+ generator = torch.Generator('cpu').manual_seed(seed)
41
+
42
+ source_image = resize(512, source_img)
43
+ source_image.save('source.png')
44
+
45
+ images_list = img_pipe([prompt] * 1, init_image=source_image, strength=strength, guidance_scale=guide, num_inference_steps=steps)
46
+ images = []
47
+ safe_image = Image.open(r"unsafe.png")
48
+
49
+ for i, image in enumerate(images_list["images"]):
50
+ if(images_list["nsfw_content_detected"][i]):
51
+ images.append(safe_image)
52
+ else:
53
+ images.append(image)
54
+ return images
55
+
56
+ print("Great sylvain ! Everything is working fine !")
57
+
58
+ title="Img2Img Stable Diffusion CPU"
59
+ description="<p style='text-align: center;'>Img2Img Stable Diffusion example using CPU and HF token. <br />Warning: Slow process... ~5/10 min inference time. <b>NSFW filter enabled. <br /> <img id='visitor-badge' alt='visitor badge' src='https://visitor-badge.glitch.me/badge?page_id=gradio-blocks.stable-diffusion-img2img' style='display: inline-block'/></b></p>"
60
+
61
+ gr.Interface(fn=infer, inputs=[source_img,
62
+ "text",
63
+ gr.Slider(2, 15, value = 7, label = 'Guidence Scale'),
64
+ gr.Slider(10, 50, value = 25, step = 1, label = 'Number of Iterations'),
65
+ gr.Slider(label = "Seed", minimum = 0, maximum = 2147483647, step = 1, randomize = True),
66
+ gr.Slider(label='Strength', minimum = 0, maximum = 1, step = .05, value = .75)],
67
+ outputs=gallery,title=title,description=description, allow_flagging="manual", flagging_dir="flagged").queue(max_size=100).launch(enable_queue=True)
flagged/readme.txt ADDED
File without changes
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ scipy
4
+ ftfy
5
+ git+https://github.com/huggingface/diffusers
stable-diffusion-img2img ADDED
@@ -0,0 +1 @@
 
 
1
+ Subproject commit 102198819ddcbb3976017e23062f30eb069e752a
unsafe.png ADDED