Create new file
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pip install -U git+https://github.com/huggingface/diffusers
|
| 2 |
+
import gradio as gr
|
| 3 |
+
#import torch
|
| 4 |
+
#from torch import autocast // only for GPU
|
| 5 |
+
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 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 |
+
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=YOUR_TOKEN)
|
| 21 |
+
pipe.to(device)
|
| 22 |
+
|
| 23 |
+
gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[2], height="auto")
|
| 24 |
+
|
| 25 |
+
def infer(prompt):
|
| 26 |
+
|
| 27 |
+
#image = pipe(prompt, init_image=init_image)["sample"][0]
|
| 28 |
+
images_list = pipe([prompt] * 4)
|
| 29 |
+
images = []
|
| 30 |
+
safe_image = Image.open(r"unsafe.png")
|
| 31 |
+
for i, image in enumerate(images_list["sample"]):
|
| 32 |
+
if(images_list["nsfw_content_detected"][i]):
|
| 33 |
+
images.append(safe_image)
|
| 34 |
+
else:
|
| 35 |
+
images.append(image)
|
| 36 |
+
|
| 37 |
+
return images
|
| 38 |
+
|
| 39 |
+
print("Great sylvain ! Everything is working fine !")
|
| 40 |
+
|
| 41 |
+
title="Stable Diffusion CPU"
|
| 42 |
+
description="Stable Diffusion example using CPU and HF token. <br />Warning: Slow process... ~5/10 min inference time. <b>NSFW filter enabled.</b>"
|
| 43 |
+
|
| 44 |
+
gr.Interface(fn=infer, inputs="text", outputs=gallery,title=title,description=description).launch(enable_queue=True)
|