machinesarenotpeople commited on
Commit
e4bf77f
1 Parent(s): 94bc65e

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +16 -49
  2. requirements.txt +5 -5
app.py CHANGED
@@ -1,68 +1,35 @@
 
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("Running properly")
57
- print(MY_SECRET_TOKEN)
58
-
59
- title="Img2Img Stable Diffusion CPU"
60
- 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 /> </b></p>"
61
 
62
- gr.Interface(fn=infer, inputs=[source_img,
63
- "text",
64
  gr.Slider(2, 15, value = 7, label = 'Guidence Scale'),
65
  gr.Slider(10, 50, value = 25, step = 1, label = 'Number of Iterations'),
66
- gr.Slider(label = "Seed", minimum = 0, maximum = 2147483647, step = 1, randomize = True),
67
- gr.Slider(label='Strength', minimum = 0, maximum = 1, step = .05, value = .75)],
68
- outputs=gallery,title=title,description=description, allow_flagging="manual", flagging_dir="flagged").queue(max_size=100).launch(enable_queue=True)
 
 
 
 
 
1
+ import streamlit as st
2
  import gradio as gr
3
  import torch
 
 
4
  from PIL import Image
5
  import numpy as np
6
  from io import BytesIO
 
 
 
 
7
  from diffusers import StableDiffusionImg2ImgPipeline
8
 
 
 
 
 
9
  device="cpu"
10
 
11
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token = st.secrets['USER_TOKEN'])
12
+ pipe.to(device)
 
 
 
 
 
 
13
 
14
  def resize(value,img):
 
15
  img = Image.open(img)
16
+ img = img.resize((value,value))
 
 
 
17
  return img
18
 
19
+ def infer(source_img, prompt, guide, steps, seed, Strength):
20
+ generator = torch.Generator("cpu").manual_seed(seed)
 
 
21
  source_image = resize(512, source_img)
22
  source_image.save('source.png')
23
+ image = pipe([prompt], init_image=source_image, strength=Strength, guidance_scale=guide, num_inference_steps=steps).images[0]
24
+ return image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ gr.Interface(fn=infer, inputs=[gr.Image(source="upload", type="filepath", label="Raw Image"), gr.Textbox(label = 'Prompt Input Text'),
 
27
  gr.Slider(2, 15, value = 7, label = 'Guidence Scale'),
28
  gr.Slider(10, 50, value = 25, step = 1, label = 'Number of Iterations'),
29
+ gr.Slider(
30
+ label = "Seed",
31
+ minimum = 0,
32
+ maximum = 2147483647,
33
+ step = 1,
34
+ randomize = True), gr.Slider(label='Strength', minimum = 0, maximum = 1, step = .05, value = .5)
35
+ ], outputs='image', title = "Stable Diffusion Image to Image Pipeline CPU", description = "Upload an Image (must be .PNG and 512x512-2048x2048) enter a Prompt, or let it just do its Thing, then click submit. 10 Iterations takes about 300 seconds currently. For more informationon about Stable Diffusion or Suggestions for prompts, keywords, artists or styles see https://github.com/Maks-s/sd-akashic", article = "Code Monkey: <a href=\"https://huggingface.co/Manjushri\">Manjushri</a>").queue(max_size=10).launch(enable_queue=True, debug=True)
requirements.txt CHANGED
@@ -1,6 +1,6 @@
1
- torch
2
- transformers
3
- scipy
4
- ftfy
5
  accelerate
6
- git+https://github.com/huggingface/diffusers
 
 
 
 
 
 
 
 
 
1
  accelerate
2
+ diffusers
3
+ ftfy
4
+ scipy
5
+ transformers
6
+ torch