|
import os
|
|
import torch
|
|
from PIL import Image
|
|
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
|
import gradio as gr
|
|
|
|
|
|
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
|
|
|
|
|
torch.cuda.empty_cache()
|
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
if device == "cuda":
|
|
print("CUDA is available. Device count:", torch.cuda.device_count())
|
|
print("Current device:", torch.cuda.current_device())
|
|
print("Device name:", torch.cuda.get_device_name(torch.cuda.current_device()))
|
|
else:
|
|
print("CUDA is not available. Using CPU.")
|
|
|
|
|
|
controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_openpose", torch_dtype=torch.float16)
|
|
|
|
|
|
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
|
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
|
|
).to(device)
|
|
|
|
|
|
def generate_image(prompt, target_image, pose_image):
|
|
try:
|
|
|
|
target_image = target_image.resize((512, 512))
|
|
pose_image = pose_image.resize((512, 512))
|
|
|
|
|
|
output = pipe(prompt=prompt, image=target_image, control_image=pose_image, num_inference_steps=50)
|
|
|
|
|
|
return output["sample"][0]
|
|
except Exception as e:
|
|
print(f"Error during image generation: {e}")
|
|
return None
|
|
|
|
|
|
interface = gr.Interface(
|
|
fn=generate_image,
|
|
inputs=[
|
|
gr.Textbox(label="Prompt"),
|
|
gr.Image(label="Target Image", type="pil"),
|
|
gr.Image(label="Pose Image (Reference)", type="pil")
|
|
],
|
|
outputs=gr.Image(label="Generated Image")
|
|
)
|
|
|
|
|
|
interface.launch() |