Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from diffusers import
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import numpy as np
|
| 6 |
-
import cv2
|
| 7 |
|
| 8 |
-
#
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
}
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
model_name = models.get(style, "CompVis/stable-diffusion-v1-4")
|
| 20 |
-
return StableDiffusionPipeline.from_pretrained(model_name).to(device)
|
| 21 |
-
|
| 22 |
-
image_model = load_image_model("fooocusv2")
|
| 23 |
-
inpaint_model = StableDiffusionInpaintPipeline.from_pretrained("CompVis/stable-diffusion-v1-4-inpainting").to(device)
|
| 24 |
-
|
| 25 |
-
def generate_image(prompt, style):
|
| 26 |
-
try:
|
| 27 |
-
image_model = load_image_model(style)
|
| 28 |
-
with torch.no_grad():
|
| 29 |
-
image = image_model(prompt).images[0]
|
| 30 |
-
return image, None
|
| 31 |
-
except Exception as e:
|
| 32 |
-
return None, f"Error generating image: {str(e)}"
|
| 33 |
-
|
| 34 |
-
def face_swap(image1, image2):
|
| 35 |
-
try:
|
| 36 |
-
if image1 is None or image2 is None:
|
| 37 |
-
return None, "Images for face swap are required"
|
| 38 |
-
image1 = cv2.cvtColor(np.array(image1), cv2.COLOR_RGB2BGR)
|
| 39 |
-
image2 = cv2.cvtColor(np.array(image2), cv2.COLOR_RGB2BGR)
|
| 40 |
-
swapped_image = image1 # Placeholder implementation
|
| 41 |
-
return cv2.cvtColor(swapped_image, cv2.COLOR_BGR2RGB), None
|
| 42 |
-
except Exception as e:
|
| 43 |
-
return None, f"Error during face swap: {str(e)}"
|
| 44 |
-
|
| 45 |
-
def upscale_image(image, scale_factor=2):
|
| 46 |
try:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
return None, f"Error during upscaling: {str(e)}"
|
| 56 |
-
|
| 57 |
-
def inpaint_image(image, mask):
|
| 58 |
-
try:
|
| 59 |
-
if image is None or mask is None:
|
| 60 |
-
return None, "Image and mask are required for inpainting"
|
| 61 |
-
image = Image.fromarray(np.array(image))
|
| 62 |
-
mask = Image.fromarray(np.array(mask))
|
| 63 |
-
inpainted_image = inpaint_model(prompt="inpainting", image=image, mask_image=mask).images[0]
|
| 64 |
-
return inpainted_image, None
|
| 65 |
-
except Exception as e:
|
| 66 |
-
return None, f"Error during inpainting: {str(e)}"
|
| 67 |
-
|
| 68 |
-
def process_image(prompt, style, image1=None, image2=None, mask=None, scale_factor=2):
|
| 69 |
-
try:
|
| 70 |
-
if prompt:
|
| 71 |
-
generated_image, error = generate_image(prompt, style)
|
| 72 |
-
if error:
|
| 73 |
-
return None, error
|
| 74 |
-
return generated_image, None
|
| 75 |
-
elif image1 and image2:
|
| 76 |
-
swapped_image, error = face_swap(image1, image2)
|
| 77 |
-
if error:
|
| 78 |
-
return None, error
|
| 79 |
-
upscaled_image, error = upscale_image(swapped_image, scale_factor)
|
| 80 |
-
if error:
|
| 81 |
-
return None, error
|
| 82 |
-
return upscaled_image, None
|
| 83 |
-
elif image1 and mask:
|
| 84 |
-
inpainted_image, error = inpaint_image(image1, mask)
|
| 85 |
-
if error:
|
| 86 |
-
return None, error
|
| 87 |
-
return inpainted_image, None
|
| 88 |
else:
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
except Exception as e:
|
| 91 |
-
return
|
| 92 |
-
|
| 93 |
-
# Gradio interface
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from diffusers import DiffusionPipeline
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Set up device
|
| 6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
|
| 8 |
+
# Initialize the inpainting model
|
| 9 |
+
try:
|
| 10 |
+
inpaint_model = DiffusionPipeline.from_pretrained("diffusers/stable-diffusion-xl-1.0-inpainting-0.1").to(device)
|
| 11 |
+
except Exception as e:
|
| 12 |
+
print(f"Error initializing model: {e}")
|
|
|
|
| 13 |
|
| 14 |
+
def process_image(prompt, image, style, upscale_factor, inpaint):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
+
# Convert the image to the appropriate format if needed
|
| 17 |
+
if image is not None:
|
| 18 |
+
image = image.convert("RGB")
|
| 19 |
+
|
| 20 |
+
# Example placeholder logic for using the pipeline
|
| 21 |
+
# Here we assume the pipeline can handle both image and prompt; adjust as needed
|
| 22 |
+
if inpaint:
|
| 23 |
+
result = inpaint_model(prompt=prompt, image=image, guidance_scale=7.5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
else:
|
| 25 |
+
result = inpaint_model(prompt=prompt, guidance_scale=7.5)
|
| 26 |
+
|
| 27 |
+
return result.images[0] # Return the first image from the result
|
| 28 |
except Exception as e:
|
| 29 |
+
return f"Error in process_image function: {e}"
|
| 30 |
+
|
| 31 |
+
# Define the Gradio interface
|
| 32 |
+
with gr.Blocks() as demo:
|
| 33 |
+
with gr.Row():
|
| 34 |
+
with gr.Column():
|
| 35 |
+
prompt_input = gr.Textbox(label="Enter your prompt")
|
| 36 |
+
image_input = gr.Image(label="Image (for inpainting)", type="pil", optional=True)
|
| 37 |
+
style_input = gr.Dropdown(choices=["Fooocus Style", "SAI Anime"], label="Select Style")
|
| 38 |
+
upscale_input = gr.Slider(minimum=1, maximum=4, step=1, default=2, label="Upscale Factor")
|
| 39 |
+
inpaint_input = gr.Checkbox(label="Enable Inpainting")
|
| 40 |
+
|
| 41 |
+
output = gr.Image(label="Generated Image")
|
| 42 |
+
|
| 43 |
+
generate_button = gr.Button("Generate Image")
|
| 44 |
+
generate_button.click(
|
| 45 |
+
process_image,
|
| 46 |
+
inputs=[prompt_input, image_input, style_input, upscale_input, inpaint_input],
|
| 47 |
+
outputs=output
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Launch the interface
|
| 51 |
+
demo.launch()
|