AP123 commited on
Commit
33d79f2
1 Parent(s): 566afe8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.launch()