LPX55 commited on
Commit
aedf846
·
verified ·
1 Parent(s): 198b340

Update raw.py

Browse files
Files changed (1) hide show
  1. raw.py +43 -26
raw.py CHANGED
@@ -1,38 +1,55 @@
1
  import torch
2
- from diffusers.utils import load_image
3
- from diffusers import FluxControlNetModel
4
- from diffusers.pipelines import FluxControlNetPipeline
5
  import spaces
 
 
 
 
 
6
 
7
  # Load pipeline
8
  controlnet = FluxControlNetModel.from_pretrained(
9
- "jasperai/Flux.1-dev-Controlnet-Upscaler",
10
- torch_dtype=torch.bfloat16
11
  )
12
  pipe = FluxControlNetPipeline.from_pretrained(
13
- "black-forest-labs/FLUX.1-dev",
14
- controlnet=controlnet,
15
- torch_dtype=torch.bfloat16
 
16
  )
17
  pipe.to("cuda")
18
 
19
- # Load a control image
20
- control_image = load_image(
21
- "https://img.getimg.ai/generated/img-HvTBfR1W1RUhykk7gmBUYp.png"
22
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- w, h = control_image.size
25
-
26
- # Upscale x4
27
- control_image = control_image.resize((w * 2, h * 2))
 
 
 
 
 
 
 
 
 
28
 
29
- image = pipe(
30
- prompt="",
31
- control_image=control_image,
32
- controlnet_conditioning_scale=0.6,
33
- num_inference_steps=14,
34
- guidance_scale=3.5,
35
- height=control_image.size[1],
36
- width=control_image.size[0]
37
- ).images[0]
38
- image
 
1
  import torch
 
 
 
2
  import spaces
3
+ import os
4
+ from diffusers.utils import load_image
5
+ from diffusers import FluxControlNetModel, FluxControlNetPipeline
6
+ import gradio as gr
7
+ huggingface_token = os.getenv("HUGGINFACE_TOKEN")
8
 
9
  # Load pipeline
10
  controlnet = FluxControlNetModel.from_pretrained(
11
+ "jasperai/Flux.1-dev-Controlnet-Upscaler",
12
+ torch_dtype=torch.bfloat16
13
  )
14
  pipe = FluxControlNetPipeline.from_pretrained(
15
+ "black-forest-labs/FLUX.1-dev",
16
+ controlnet=controlnet,
17
+ torch_dtype=torch.bfloat16,
18
+ token=huggingface_token
19
  )
20
  pipe.to("cuda")
21
 
22
+ @spaces.GPU
23
+ def generate_image(prompt, control_image):
24
+ # Load control image
25
+ control_image = load_image(control_image.name)
26
+ w, h = control_image.size
27
+ # Upscale x4
28
+ control_image = control_image.resize((w * 2, h * 2))
29
+ image = pipe(
30
+ prompt=prompt,
31
+ control_image=control_image,
32
+ controlnet_conditioning_scale=0.6,
33
+ num_inference_steps=14,
34
+ guidance_scale=3.5,
35
+ height=control_image.size[1],
36
+ width=control_image.size[0]
37
+ ).images[0]
38
+ return image
39
 
40
+ # Create Gradio interface
41
+ iface = gr.Interface(
42
+ fn=generate_image,
43
+ inputs=[
44
+ gr.inputs.Textbox(lines=2, placeholder="Enter your prompt here..."),
45
+ gr.inputs.Image(type="pil", label="Control Image"),
46
+ ],
47
+ outputs=[
48
+ gr.outputs.Image(type="pil", label="Generated Image"),
49
+ ],
50
+ title="FLUX ControlNet Image Generation",
51
+ description="Generate images using the FluxControlNetPipeline. Upload a control image and enter a prompt to create an image.",
52
+ )
53
 
54
+ # Launch the app
55
+ iface.launch()