| import gradio as gr |
| from diffusers import ControlNetModel, StableDiffusionControlNetPipeline |
| import torch |
| import os |
|
|
| |
| HF_TOKEN = os.getenv("HF_TOKEN") |
|
|
| |
| controlnet = ControlNetModel.from_pretrained( |
| "lllyasviel/control_v11p_sd15_canny", |
| use_auth_token=HF_TOKEN |
| ) |
| pipeline = StableDiffusionControlNetPipeline.from_pretrained( |
| "runwayml/stable-diffusion-v1-5", |
| controlnet=controlnet, |
| use_auth_token=HF_TOKEN |
| ) |
|
|
| |
| def generate_image(image, prompt, num_inference_steps=50, guidance_scale=7.5): |
| if image is None: |
| return None |
| |
| |
| result = pipeline( |
| prompt=prompt, |
| image=image, |
| num_inference_steps=num_inference_steps, |
| guidance_scale=guidance_scale |
| ).images[0] |
|
|
| return result |
|
|
| css = """ |
| footer { |
| visibility: hidden; |
| } |
| """ |
|
|
|
|
| |
| demo = gr.Interface(theme="Nymbo/Nymbo_Theme", css=css, |
| fn=generate_image, |
| inputs=[ |
| gr.Image(type="pil", label="Upload an Image"), |
| gr.Textbox(lines=1, placeholder="Enter a prompt", label="Prompt"), |
| gr.Slider(minimum=10, maximum=100, value=50, label="Inference Steps"), |
| gr.Slider(minimum=1.0, maximum=20.0, value=7.5, label="Guidance Scale") |
| ], |
| outputs=gr.Image(label="Generated Image"), |
| title="Stable Diffusion with ControlNet", |
| description="Upload an image and enter a prompt to generate a new image with the same face but different context.", |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|