akhaliq HF Staff commited on
Commit
755b512
·
verified ·
1 Parent(s): 23b3eed

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +126 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import spaces
4
+ from diffusers import FluxPipeline
5
+ from safetensors.torch import load_file
6
+
7
+ # Load the model
8
+ pipe = FluxPipeline.from_pretrained(
9
+ 'black-forest-labs/FLUX.1-dev',
10
+ torch_dtype=torch.bfloat16,
11
+ use_safetensors=True
12
+ )
13
+ pipe.to('cuda')
14
+
15
+ # Load SRPO weights
16
+ state_dict = load_file("diffusion_pytorch_model.safetensors")
17
+ pipe.transformer.load_state_dict(state_dict, strict=False)
18
+
19
+ @spaces.GPU(duration=120)
20
+ def generate_image(
21
+ prompt,
22
+ negative_prompt="",
23
+ width=1024,
24
+ height=1024,
25
+ guidance_scale=3.5,
26
+ num_inference_steps=50,
27
+ seed=-1
28
+ ):
29
+ if seed == -1:
30
+ seed = torch.randint(0, 2**32, (1,)).item()
31
+
32
+ generator = torch.Generator(device='cuda').manual_seed(seed)
33
+
34
+ image = pipe(
35
+ prompt=prompt,
36
+ negative_prompt=negative_prompt if negative_prompt else None,
37
+ guidance_scale=guidance_scale,
38
+ height=height,
39
+ width=width,
40
+ num_inference_steps=num_inference_steps,
41
+ max_sequence_length=512,
42
+ generator=generator
43
+ ).images[0]
44
+
45
+ return image, seed
46
+
47
+ with gr.Blocks(title="FLUX SRPO Text-to-Image") as demo:
48
+ gr.Markdown("# FLUX with SRPO (Self-Regulating Preference Optimization)")
49
+ gr.Markdown("Generate high-quality images using FLUX model enhanced with Tencent's SRPO technique")
50
+
51
+ with gr.Row():
52
+ with gr.Column(scale=3):
53
+ prompt = gr.Textbox(
54
+ label="Prompt",
55
+ placeholder="Describe the image you want to generate...",
56
+ lines=3
57
+ )
58
+ negative_prompt = gr.Textbox(
59
+ label="Negative Prompt (optional)",
60
+ placeholder="What you don't want to see in the image...",
61
+ lines=2
62
+ )
63
+
64
+ with gr.Row():
65
+ width = gr.Slider(
66
+ minimum=256,
67
+ maximum=2048,
68
+ value=1024,
69
+ step=64,
70
+ label="Width"
71
+ )
72
+ height = gr.Slider(
73
+ minimum=256,
74
+ maximum=2048,
75
+ value=1024,
76
+ step=64,
77
+ label="Height"
78
+ )
79
+
80
+ with gr.Row():
81
+ guidance_scale = gr.Slider(
82
+ minimum=1.0,
83
+ maximum=20.0,
84
+ value=3.5,
85
+ step=0.5,
86
+ label="Guidance Scale"
87
+ )
88
+ num_inference_steps = gr.Slider(
89
+ minimum=10,
90
+ maximum=100,
91
+ value=50,
92
+ step=5,
93
+ label="Inference Steps"
94
+ )
95
+
96
+ seed = gr.Number(
97
+ label="Seed (-1 for random)",
98
+ value=-1,
99
+ precision=0
100
+ )
101
+
102
+ generate_btn = gr.Button("Generate Image", variant="primary", size="lg")
103
+
104
+ with gr.Column(scale=4):
105
+ output_image = gr.Image(label="Generated Image", type="pil")
106
+ used_seed = gr.Number(label="Seed Used", precision=0)
107
+
108
+ gr.Examples(
109
+ examples=[
110
+ ["The Death of Ophelia by John Everett Millais, Pre-Raphaelite painting, Ophelia floating in a river surrounded by flowers, detailed natural elements, melancholic and tragic atmosphere"],
111
+ ["A serene Japanese garden with cherry blossoms, koi pond, traditional wooden bridge, soft morning light, photorealistic"],
112
+ ["Cyberpunk cityscape at night, neon lights, flying cars, rain-slicked streets, blade runner aesthetic, highly detailed"],
113
+ ["Portrait of a majestic lion in golden hour light, detailed fur texture, intense gaze, African savanna background"],
114
+ ["Abstract colorful explosion of paint in water, high speed photography, vibrant colors mixing, dramatic lighting"],
115
+ ],
116
+ inputs=prompt,
117
+ label="Example Prompts"
118
+ )
119
+
120
+ generate_btn.click(
121
+ fn=generate_image,
122
+ inputs=[prompt, negative_prompt, width, height, guidance_scale, num_inference_steps, seed],
123
+ outputs=[output_image, used_seed]
124
+ )
125
+
126
+ demo.launch()