|
|
import gradio as gr |
|
|
import torch |
|
|
|
|
|
|
|
|
from PIL import Image |
|
|
import numpy as np |
|
|
from io import BytesIO |
|
|
import os |
|
|
MY_SECRET_TOKEN=os.environ.get('HF_TOKEN_SD') |
|
|
|
|
|
|
|
|
from diffusers import StableDiffusionImg2ImgPipeline |
|
|
|
|
|
print("hello") |
|
|
|
|
|
YOUR_TOKEN=MY_SECRET_TOKEN |
|
|
|
|
|
device="cpu" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained( |
|
|
"Linaqruf/anything-v3-1", |
|
|
use_auth_token=YOUR_TOKEN, |
|
|
safety_checker=None, |
|
|
) |
|
|
img_pipe.to(device) |
|
|
|
|
|
source_img = gr.Image(sources=["upload", "webcam", "clipboard"], type="filepath", label="Add Image for Text Guided Editing and Sketch") |
|
|
gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[1], height="auto") |
|
|
|
|
|
def resize(value,img): |
|
|
|
|
|
img = Image.open(img) |
|
|
|
|
|
|
|
|
|
|
|
img = img.resize((value,value), Image.Resampling.LANCZOS) |
|
|
return img |
|
|
|
|
|
def infer(source_img, prompt, guide, steps, seed, strength): |
|
|
generator = torch.Generator("cpu").manual_seed(seed) |
|
|
|
|
|
source_image = Image.open(source_img).convert("RGB") |
|
|
source_image = source_image.resize((1024, 1024), Image.Resampling.LANCZOS) |
|
|
|
|
|
result = img_pipe( |
|
|
[prompt], |
|
|
image=source_image, |
|
|
strength=strength, |
|
|
guidance_scale=guide, |
|
|
num_inference_steps=steps, |
|
|
generator=generator |
|
|
) |
|
|
|
|
|
output_images = result["images"] |
|
|
output_paths = [] |
|
|
|
|
|
for idx, img in enumerate(output_images): |
|
|
filename = f"output_{seed}_{idx}.png" |
|
|
save_path = os.path.join("outputs", filename) |
|
|
os.makedirs("outputs", exist_ok=True) |
|
|
img.save(save_path) |
|
|
print(f"Saved image to: {save_path}") |
|
|
output_paths.append(save_path) |
|
|
|
|
|
|
|
|
return output_images |
|
|
|
|
|
print("Great ! Everything is working fine !") |
|
|
|
|
|
title="Text Guided Image Editing" |
|
|
description="<p style='text-align: center;'>Text Guided Image Editing via Stable Diffusion Image to Image using CPU and HF token. <br />Warning: CPU processing is slow... 6/7 min inference time.</p>" |
|
|
|
|
|
gr.Interface(fn=infer, inputs=[source_img, |
|
|
"text", |
|
|
gr.Slider(2, 15, value = 6.7, label = 'Guidence Scale'), |
|
|
gr.Slider(10, 50, value = 13, step = 1, label = 'Number of Iterations'), |
|
|
gr.Slider(label = "Seed", minimum = 0, maximum = 676767, step = 67, randomize = False), |
|
|
gr.Slider(label='Strength', minimum = 0, maximum = 1, step = .05, value = .67)], |
|
|
outputs=gallery,title=title,description=description, allow_flagging="never", flagging_dir="flagged").queue(max_size=67).launch(enable_queue=True, footer_links=[]) |