AP123 commited on
Commit
c8963b4
1 Parent(s): 2751651

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -27
app.py CHANGED
@@ -1,50 +1,78 @@
1
- import gradio as gr
2
- import torch
3
- from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline
4
  import spaces
 
 
 
5
  from PIL import Image
6
  import numpy as np
7
 
8
- # Load the ControlNet model and pipeline
9
  controlnet = ControlNetModel.from_pretrained(
10
  "briaai/BRIA-2.2-ControlNet-Recoloring",
11
  torch_dtype=torch.float16
12
- )
 
13
  pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
14
  "briaai/BRIA-2.2",
15
  controlnet=controlnet,
16
  torch_dtype=torch.float16,
17
- ).to("cuda")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- # Function to transform the image based on a prompt
20
  @spaces.GPU(enable_queue=True)
21
- def generate_image(image, prompt):
22
- # Prepare the image for processing
23
- image = image.convert("RGB")
24
- recoloring_image = Image.fromarray(np.array(image)).convert('L').convert('RGB')
 
 
 
 
25
 
26
- # Define the negative prompt
27
- negative_prompt = "Logo,Watermark,Text,Ugly,Morbid,Extra fingers,Poorly drawn hands,Mutation,Blurry,Extra limbs,Gross proportions,Missing arms,Mutated hands,Long neck,Duplicate,Mutilated,Mutilated hands,Poorly drawn face,Deformed,Bad anatomy,Cloned face,Malformed limbs,Missing legs,Too many fingers"
 
 
 
 
 
 
28
 
29
- # Generate the transformed image
30
- results = pipe(prompt=prompt, negative_prompt=negative_prompt, image=recoloring_image, controlnet_conditioning_scale=1.0, height=1024, width=1024)
31
- return results.images[0]
32
 
33
  # Gradio Interface
34
- description = """
35
- Anything to Anything, a workflow by Angrypenguinpng using the Bria Recolor ControlNet, check it out here: https://huggingface.co/briaai/BRIA-2.2-ControlNet-Recoloring
36
- """
37
 
38
  with gr.Blocks() as demo:
39
  gr.Markdown("<h1><center>Image Transformation with Bria Recolor ControlNet</center></h1>")
40
  gr.Markdown(description)
41
- with gr.Group():
42
- with gr.Row():
43
- image = gr.Image(label='Upload your image')
44
- prompt = gr.Textbox(label='Enter your prompt', placeholder="A portrait of a beautiful and playful ethereal singer, golden designs, highly detailed, blurry background")
45
- submit = gr.Button('Transform Image')
46
- output_image = gr.Image(label='Transformed Image')
47
-
48
- submit.click(fn=generate_image, inputs=[image, prompt], outputs=output_image)
 
 
49
 
50
  demo.queue().launch()
 
 
 
 
1
  import spaces
2
+ from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, EulerAncestralDiscreteScheduler
3
+ import torch
4
+ import gradio as gr
5
  from PIL import Image
6
  import numpy as np
7
 
8
+ # Load the models
9
  controlnet = ControlNetModel.from_pretrained(
10
  "briaai/BRIA-2.2-ControlNet-Recoloring",
11
  torch_dtype=torch.float16
12
+ ).to('cuda')
13
+
14
  pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
15
  "briaai/BRIA-2.2",
16
  controlnet=controlnet,
17
  torch_dtype=torch.float16,
18
+ device_map='auto',
19
+ low_cpu_mem_usage=True,
20
+ offload_state_dict=True,
21
+ ).to('cuda')
22
+
23
+ pipe.scheduler = EulerAncestralDiscreteScheduler(
24
+ beta_start=0.00085,
25
+ beta_end=0.012,
26
+ beta_schedule="scaled_linear",
27
+ num_train_timesteps=1000,
28
+ steps_offset=1
29
+ )
30
+
31
+ pipe.force_zeros_for_empty_prompt = False
32
+
33
+ def resize_image(image):
34
+ image = image.convert('RGB')
35
+ current_size = image.size
36
+ transform = gr.Image(height=1024, width=1024, keep_aspect_ratio=True, source="upload", tool="editor")
37
+ resized_image = transform.postprocess(image)
38
+ return resized_image
39
 
 
40
  @spaces.GPU(enable_queue=True)
41
+ def generate_image(input_image, prompt, controlnet_conditioning_scale):
42
+ # Always use a random seed for diversity in outputs
43
+ seed = np.random.randint(2147483647)
44
+ generator = torch.Generator("cuda").manual_seed(seed)
45
+
46
+ # Resize and prepare the image
47
+ input_image = resize_image(input_image)
48
+ grayscale_image = input_image.convert('L').convert('RGB')
49
 
50
+ # Generate the image with fixed 30 steps
51
+ images = pipe(
52
+ prompt=prompt,
53
+ image=grayscale_image,
54
+ num_inference_steps=30,
55
+ controlnet_conditioning_scale=float(controlnet_conditioning_scale),
56
+ generator=generator,
57
+ ).images
58
 
59
+ return images[0]
 
 
60
 
61
  # Gradio Interface
62
+ description = "Anything to Anything. Transform anything to anything. Allow an adjuster for controlnet scale."
 
 
63
 
64
  with gr.Blocks() as demo:
65
  gr.Markdown("<h1><center>Image Transformation with Bria Recolor ControlNet</center></h1>")
66
  gr.Markdown(description)
67
+ with gr.Row():
68
+ with gr.Column():
69
+ input_image = gr.Image(label='Upload your image', type="pil")
70
+ prompt = gr.Textbox(label='Enter your prompt')
71
+ controlnet_conditioning_scale = gr.Slider(label="ControlNet conditioning scale", minimum=0.1, maximum=2.0, value=1.0, step=0.05)
72
+ submit_button = gr.Button('Transform Image')
73
+ with gr.Column():
74
+ output_image = gr.Image(label='Transformed Image')
75
+
76
+ submit_button.click(fn=generate_image, inputs=[input_image, prompt, controlnet_conditioning_scale], outputs=output_image)
77
 
78
  demo.queue().launch()