Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import torch | |
| from diffusers import StableDiffusionXLImg2ImgPipeline | |
| from diffusers.utils import load_image | |
| from PIL import Image | |
| import requests | |
| #from diffusers import DiffusionPipeline | |
| ''' | |
| pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained( | |
| "stabilityai/stable-diffusion-xl-refiner-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True | |
| ) | |
| pipe = pipe.to("cpu") | |
| url = "https://huggingface.co/datasets/patrickvonplaten/images/resolve/main/aa_xl/000000009.png" | |
| def run_fn(img_url): | |
| init_image = load_image(url).convert("RGB") | |
| prompt = "a photo of an astronaut riding a horse on mars" | |
| image = pipe(prompt, image=init_image).images | |
| return image | |
| ''' | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| #pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", torch_dtype=torch.float16) if torch.cuda.is_available() else DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0") | |
| pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", torch_dtype=torch.float16) if torch.cuda.is_available() else StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0") | |
| pipe = pipe.to(device) | |
| def resize(value,img): | |
| img = Image.open(requests.get(img, stream=True).raw) | |
| img.save("tmp_im.png") | |
| img = Image.open("tmp_im.png") | |
| #img = img.resize((value,value)) | |
| return img | |
| def infer(source_img, prompt, negative_prompt, guide, steps, seed, Strength): | |
| #source_img = load_image(source_img).convert("RGB") | |
| generator = torch.Generator(device).manual_seed(seed) | |
| source_image = resize(768, source_img) | |
| source_image.save('source.png') | |
| image = pipe(prompt, negative_prompt=negative_prompt, image=source_image, strength=Strength, guidance_scale=guide, num_inference_steps=steps).images[0] | |
| return image | |
| gr.Interface(fn=infer, inputs=[gr.Textbox(), gr.Textbox(label = 'Prompt Input Text. 77 Token (Keyword or Symbol) Maximum'), gr.Textbox(label='What you Do Not want the AI to generate.'), | |
| gr.Slider(2, 15, value = 7, label = 'Guidance Scale'), | |
| gr.Slider(1, 25, value = 10, step = 1, label = 'Number of Iterations'), | |
| gr.Slider(label = "Seed", minimum = 0, maximum = 987654321987654321, step = 1, randomize = True), | |
| gr.Slider(label='Strength', minimum = 0, maximum = 1, step = .05, value = .5)], | |
| outputs='image').launch() | |