smartdigitalnetworks commited on
Commit
57779cd
·
verified ·
1 Parent(s): 385bdd1

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +6 -6
  2. app.py +83 -0
  3. requirements.txt +6 -0
  4. unsafe.png +0 -0
README.md CHANGED
@@ -1,13 +1,13 @@
1
  ---
2
- title: Stable Diffusion Image To Image Cpu
3
- emoji: 🚀
4
- colorFrom: yellow
5
- colorTo: gray
6
  sdk: gradio
7
- sdk_version: 5.49.1
8
  app_file: app.py
9
  pinned: false
10
- short_description: Stable Diffusion Image-to-Image CPU
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
+ duplicated_from: fffiloni/stable-diffusion-img2img
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(
24
+ "runwayml/stable-diffusion-v1-5",
25
+ use_auth_token=YOUR_TOKEN,
26
+ safety_checker=None, # ← disable safety checker
27
+ )
28
+ img_pipe.to(device)
29
+
30
+ source_img = gr.Image(source="upload", type="filepath", label="init_img | 512*512 px")
31
+ gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[1], height="auto")
32
+
33
+ def resize(value,img):
34
+ #baseheight = value
35
+ img = Image.open(img)
36
+ #hpercent = (baseheight/float(img.size[1]))
37
+ #wsize = int((float(img.size[0])*float(hpercent)))
38
+ #img = img.resize((wsize,baseheight), Image.Resampling.LANCZOS)
39
+ img = img.resize((value,value), Image.Resampling.LANCZOS)
40
+ return img
41
+
42
+
43
+ def infer(source_img, prompt, guide, steps, seed, strength):
44
+ generator = torch.Generator("cpu").manual_seed(seed)
45
+
46
+ source_image = Image.open(source_img).convert("RGB")
47
+ source_image = source_image.resize((512, 512), Image.Resampling.LANCZOS)
48
+
49
+ result = img_pipe(
50
+ [prompt],
51
+ image=source_image,
52
+ strength=strength,
53
+ guidance_scale=guide,
54
+ num_inference_steps=steps,
55
+ generator=generator
56
+ )
57
+
58
+ output_images = result["images"]
59
+ output_paths = []
60
+
61
+ for idx, img in enumerate(output_images):
62
+ filename = f"output_{seed}_{idx}.png"
63
+ save_path = os.path.join("outputs", filename)
64
+ os.makedirs("outputs", exist_ok=True)
65
+ img.save(save_path)
66
+ print(f"Saved image to: {save_path}")
67
+ output_paths.append(save_path)
68
+
69
+ # Optional: return paths or Gradio can render them too
70
+ return output_images
71
+
72
+ print("Great sylvain ! Everything is working fine !")
73
+
74
+ title="Img2Img Stable Diffusion CPU"
75
+ 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>"
76
+
77
+ gr.Interface(fn=infer, inputs=[source_img,
78
+ "text",
79
+ gr.Slider(2, 15, value = 7, label = 'Guidence Scale'),
80
+ gr.Slider(10, 50, value = 25, step = 1, label = 'Number of Iterations'),
81
+ gr.Slider(label = "Seed", minimum = 0, maximum = 2147483647, step = 1, randomize = True),
82
+ gr.Slider(label='Strength', minimum = 0, maximum = 1, step = .05, value = .75)],
83
+ outputs=gallery,title=title,description=description, allow_flagging="manual", flagging_dir="flagged").queue(max_size=100).launch(enable_queue=True)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ scipy
4
+ ftfy
5
+ git+https://github.com/huggingface/diffusers
6
+ accelerate
unsafe.png ADDED