Wootang01/stable_diffuser_3
0
1import gradio as gr2#import torch3#from torch import autocast // only for GPU4 5from PIL import Image6import numpy as np7from io import BytesIO8import os9MY_SECRET_TOKEN=os.environ.get('HF_TOKEN_SD')10 11from diffusers import StableDiffusionImg2ImgPipeline12 13print("hello sylvain")14 15YOUR_TOKEN=MY_SECRET_TOKEN16 17device="cpu"18 19#prompt_pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", use_auth_token=YOUR_TOKEN)20#prompt_pipe.to(device)21 22img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", use_auth_token=YOUR_TOKEN)23img_pipe.to(device)24 25source_img = gr.Image(source="upload", type="filepath", label="init_img | 512*512 px")26gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[2], height="auto")27 28def resize(value,img):29 #baseheight = value30 img = Image.open(img)31 #hpercent = (baseheight/float(img.size[1]))32 #wsize = int((float(img.size[0])*float(hpercent)))33 #img = img.resize((wsize,baseheight), Image.Resampling.LANCZOS)34 img = img.resize((value,value), Image.Resampling.LANCZOS)35 return img36 37 38def infer(prompt, source_img): 39 40 source_image = resize(512, source_img)41 source_image.save('source.png')42 images_list = img_pipe([prompt] * 2, init_image=source_image, strength=0.75)43 images = []44 safe_image = Image.open(r"unsafe.png")45 for i, image in enumerate(images_list["sample"]):46 if(images_list["nsfw_content_detected"][i]):47 images.append(safe_image)48 else:49 images.append(image) 50 return images51 52print("Great sylvain ! Everything is working fine !")53 54title="Img2Img Stable Diffusion CPU"55description="Img2Img Stable Diffusion example using CPU and HF token. <br />Warning: Slow process... ~5/10 min inference time. <b>NSFW filter enabled.</b>" 56 57gr.Interface(fn=infer, inputs=["text", source_img], outputs=gallery,title=title,description=description).queue(max_size=100).launch(enable_queue=True)58#from torch import autocast59#import requests60#import torch61#from PIL import Image62#from io import BytesIO63#import os64#MY_SECRET_TOKEN = os.environ.get('HF_TOKEN_SD')65 66#from diffusers import StableDiffusionImg2ImgPipeline67 68#YOUR_TOKEN = MY_SECRET_TOKEN69# load the pipeline70#device = "cuda"71#model_id_or_path = "CompVis/stable-diffusion-v1-4"72 73# pipe = StableDiffusionImg2ImgPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token = YOUR_TOKEN)74 75#pipe = StableDiffusionImg2ImgPipeline.from_pretrained(76# model_id_or_path,77# revision="fp16", 78# torch_dtype=torch.float16,79# use_auth_token=YOUR_TOKEN80#)81# or download via git clone https://huggingface.co/CompVis/stable-diffusion-v1-482# and pass `model_id_or_path="./stable-diffusion-v1-4"` without having to use `use_auth_token=True`.83#pipe = pipe.to(device)84 85# let's download an initial image86#url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"87 88#response = requests.get(url)89#init_image = Image.open(BytesIO(response.content)).convert("RGB")90#init_image = init_image.resize((768, 512))91 92#prompt = "Lively, illustration of a [[[<king::4>]]], portrait, fantasy, intricate, Scenic, hyperdetailed, hyper realistic <king-hearthstone>, unreal engine, 4k, smooth, sharp focus, intricate, cinematic lighting, highly detailed, octane, digital painting, artstation, concept art, vibrant colors, Cinema4D, WLOP, 3d render, in the style of hearthstone::5 art by Artgerm and greg rutkowski and magali villeneuve, martina jackova, Giger"93 94#with autocast("cuda"):95# images = pipe(prompt=prompt, init_image=init_image, strength=0.75, guidance_scale=7.5).images96 97#images[0].save("fantasy_landscape.png")