ariG23498 HF staff commited on
Commit
6052413
·
verified ·
1 Parent(s): 8a35237

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import spaces
4
+
5
+ from diffusers import FluxControlPipeline, FluxTransformer2DModel
6
+
7
+ ####################################
8
+ # Load the model(s) on CPU #
9
+ ####################################
10
+ edit_transformer = FluxTransformer2DModel.from_pretrained(
11
+ "sayakpaul/FLUX.1-dev-edit-v0",
12
+ torch_dtype=torch.float32
13
+ )
14
+ pipeline = FluxControlPipeline.from_pretrained(
15
+ "black-forest-labs/FLUX.1-dev",
16
+ transformer=edit_transformer,
17
+ torch_dtype=torch.float32
18
+ )
19
+
20
+ #####################################
21
+ # The function for our Gradio app #
22
+ #####################################
23
+ @spaces.GPU(duration=120)
24
+ def generate(prompt, input_image):
25
+ """
26
+ Runs the Flux Control pipeline for editing the given `input_image`
27
+ with the specified `prompt`. The pipeline is on CPU by default.
28
+ """
29
+ # Perform inference
30
+ output_image = pipeline(
31
+ control_image=input_image,
32
+ prompt=prompt,
33
+ guidance_scale=30.0,
34
+ num_inference_steps=50,
35
+ max_sequence_length=512,
36
+ height=input_image.height,
37
+ width=input_image.width,
38
+ generator=torch.manual_seed(0),
39
+ ).images[0]
40
+
41
+ return output_image
42
+
43
+
44
+ def launch_app():
45
+ with gr.Blocks() as demo:
46
+ gr.Markdown(
47
+ """
48
+ # Flux Control Editing
49
+
50
+ This demo uses the [FLUX.1-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev)
51
+ pipeline with an edit transformer from [Sayak Paul](https://huggingface.co/sayakpaul).
52
+
53
+ **Acknowledgements**:
54
+ - [Sayak Paul](https://huggingface.co/sayakpaul) for open-sourcing FLUX.1-dev-edit-v0
55
+ - [black-forest-labs](https://huggingface.co/black-forest-labs) for FLUX.1-dev
56
+ """
57
+ )
58
+
59
+ with gr.Row():
60
+ prompt = gr.Textbox(
61
+ label="Prompt",
62
+ placeholder="e.g. 'Edit a certain thing in the image'"
63
+ )
64
+ input_image = gr.Image(
65
+ label="Image",
66
+ type="pil",
67
+ )
68
+
69
+ generate_button = gr.Button("Generate")
70
+ output_image = gr.Image(label="Edited Image")
71
+
72
+ # Connect button to function
73
+ generate_button.click(
74
+ fn=generate,
75
+ inputs=[prompt, input_image],
76
+ outputs=[output_image],
77
+ )
78
+
79
+ gr.Examples(
80
+ examples=[
81
+ ["Turn the color of the mushroom to gray", "mushroom.jpg"],
82
+ ["Make the mushroom polka-dotted", "mushroom.jpg"],
83
+ ],
84
+ inputs=[prompt, input_image],
85
+ )
86
+
87
+ return demo
88
+
89
+
90
+ if __name__ == "__main__":
91
+ demo = launch_app()
92
+ demo.launch()