Spaces:
Running
on
Zero
Running
on
Zero
Update raw.py
Browse files
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 |
-
|
10 |
-
|
11 |
)
|
12 |
pipe = FluxControlNetPipeline.from_pretrained(
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
16 |
)
|
17 |
pipe.to("cuda")
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|