uruguayai commited on
Commit
e6a73f7
·
verified ·
1 Parent(s): 866ad2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -104
app.py CHANGED
@@ -1,113 +1,51 @@
1
  import gradio as gr
2
  import torch
3
- from diffusers import StableDiffusionPipeline, StableDiffusionInpaintPipeline
4
- from PIL import Image
5
- import numpy as np
6
- import cv2
7
 
8
- # Device setup
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
 
11
- # Load model pipelines for different styles
12
- models = {
13
- "fooocusv2": "CompVis/stable-diffusion-v1-4",
14
- "SAI Anime": "CompVis/stable-diffusion-v1-4-anime"
15
- # Add more models as needed
16
- }
17
 
18
- def load_image_model(style):
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
- if image is None:
48
- return None, "Image for upscaling is required"
49
- image = Image.fromarray(np.array(image))
50
- width, height = image.size
51
- new_size = (int(width * scale_factor), int(height * scale_factor))
52
- upscaled_image = image.resize(new_size, Image.LANCZOS)
53
- return upscaled_image, None
54
- except Exception as e:
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
- return None, "Either a prompt or two images or an image with a mask must be provided"
 
 
90
  except Exception as e:
91
- return None, f"Error in process_image function: {str(e)}"
92
-
93
- # Gradio interface setup
94
- iface = gr.Interface(
95
- fn=process_image,
96
- inputs=[
97
- gr.Textbox(label="Enter your prompt", placeholder="Type your prompt here..."),
98
- gr.Dropdown(label="Select Style", choices=list(models.keys()), default="fooocusv2"),
99
- gr.Image(label="Image 1 (for faceswap or inpainting)", type="pil", optional=True),
100
- gr.Image(label="Image 2 (for faceswap)", type="pil", optional=True),
101
- gr.Image(label="Mask (for inpainting)", type="pil", optional=True),
102
- gr.Slider(label="Upscale Factor", minimum=1, maximum=4, step=1, value=2, optional=True)
103
- ],
104
- outputs=[
105
- gr.Image(label="Output Image"),
106
- gr.Textbox(label="Error Message", placeholder="Error details will appear here...")
107
- ],
108
- title="Fooocus Image Processing",
109
- description="Generate images from prompts with style selection, swap faces, upscale images, and perform inpainting."
110
- )
111
-
112
- if __name__ == "__main__":
113
- iface.launch(server_name="0.0.0.0", server_port=7860)
 
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()