flxip / app.py
fantos's picture
Update app.py
ddc6cae verified
import gradio as gr
from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
import torch
import os
# Hugging Face ํ† ํฐ ์„ค์ •
HF_TOKEN = os.getenv("HF_TOKEN")
# ์˜ฌ๋ฐ”๋ฅธ ๋ชจ๋ธ ์ด๋ฆ„์œผ๋กœ ๋ณ€๊ฒฝ (์˜ˆ์‹œ: "lllyasviel/control_v11p_sd15_canny")
controlnet = ControlNetModel.from_pretrained(
"lllyasviel/control_v11p_sd15_canny",
use_auth_token=HF_TOKEN
)
pipeline = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", # base model example
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;
}
"""
# Gradio ์ธํ„ฐํŽ˜์ด์Šค
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()