LPDoctor commited on
Commit
b3650ac
1 Parent(s): 24840c1

initial commit

Browse files
Files changed (5) hide show
  1. .DS_Store +0 -0
  2. README.md +6 -6
  3. app.py +146 -0
  4. .gitattributes → gitattributes.txt +0 -0
  5. requirements.txt +7 -0
.DS_Store ADDED
Binary file (6.15 kB). View file
 
README.md CHANGED
@@ -1,12 +1,12 @@
1
  ---
2
- title: Flux Half Illustration
3
- emoji: 🏆
4
- colorFrom: blue
5
- colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 4.41.0
8
  app_file: app.py
9
- pinned: false
10
  license: mit
11
  ---
12
 
 
1
  ---
2
+ title: FLUX-HALF-ILLUSTRATION
3
+ emoji: 🖥️
4
+ colorFrom: red
5
+ colorTo: red
6
  sdk: gradio
7
+ sdk_version: 4.40.0
8
  app_file: app.py
9
+ pinned: true
10
  license: mit
11
  ---
12
 
app.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ import spaces
5
+ import torch
6
+ from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
7
+ from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
8
+ from huggingface_hub import hf_hub_download
9
+ import os
10
+
11
+ dtype = torch.bfloat16
12
+ device = "cuda" if torch.cuda.is_available() else "cpu"
13
+
14
+ MAX_SEED = np.iinfo(np.int32).max
15
+ MAX_IMAGE_SIZE = 2048
16
+
17
+ # Initialize the pipeline globally
18
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to(device)
19
+
20
+ @spaces.GPU(duration=300)
21
+ def infer(prompt, lora_model, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=5.0, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
22
+ global pipe
23
+
24
+ # Load LoRA if specified
25
+ if lora_model:
26
+ try:
27
+ pipe.load_lora_weights(lora_model)
28
+ except Exception as e:
29
+ return None, seed, f"Failed to load LoRA model: {str(e)}"
30
+
31
+ if randomize_seed:
32
+ seed = random.randint(0, MAX_SEED)
33
+ generator = torch.Generator().manual_seed(seed)
34
+
35
+ try:
36
+ image = pipe(
37
+ prompt=prompt,
38
+ width=width,
39
+ height=height,
40
+ num_inference_steps=num_inference_steps,
41
+ generator=generator,
42
+ guidance_scale=guidance_scale
43
+ ).images[0]
44
+
45
+ # Unload LoRA weights after generation
46
+ if lora_model:
47
+ pipe.unload_lora_weights()
48
+
49
+ return image, seed, "Image generated successfully."
50
+ except Exception as e:
51
+ return None, seed, f"Error during image generation: {str(e)}"
52
+
53
+ examples = [
54
+ ["a tiny astronaut hatching from an egg on the moon", ""],
55
+ ["a cat holding a sign that says hello world", ""],
56
+ ["an anime illustration of a wiener schnitzel", ""],
57
+ ]
58
+
59
+ css = """
60
+ #col-container {
61
+ margin: 0 auto;
62
+ max-width: 520px;
63
+ }
64
+ """
65
+
66
+ with gr.Blocks(css=css) as demo:
67
+ with gr.Column(elem_id="col-container"):
68
+ gr.Markdown(f"""# FLUX.1 [dev] with LoRA Support
69
+ 12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
70
+ [[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
71
+ """)
72
+
73
+ with gr.Row():
74
+ prompt = gr.Text(
75
+ label="Prompt",
76
+ show_label=False,
77
+ max_lines=1,
78
+ placeholder="Enter your prompt",
79
+ container=False,
80
+ )
81
+ run_button = gr.Button("Run", scale=0)
82
+
83
+ lora_model = gr.Text(
84
+ label="LoRA Model ID (optional)",
85
+ placeholder="Enter Hugging Face LoRA model ID",
86
+ )
87
+
88
+ result = gr.Image(label="Result", show_label=False)
89
+ output_message = gr.Textbox(label="Output Message")
90
+
91
+ with gr.Accordion("Advanced Settings", open=False):
92
+ seed = gr.Slider(
93
+ label="Seed",
94
+ minimum=0,
95
+ maximum=MAX_SEED,
96
+ step=1,
97
+ value=0,
98
+ )
99
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
100
+ with gr.Row():
101
+ width = gr.Slider(
102
+ label="Width",
103
+ minimum=256,
104
+ maximum=MAX_IMAGE_SIZE,
105
+ step=32,
106
+ value=1024,
107
+ )
108
+ height = gr.Slider(
109
+ label="Height",
110
+ minimum=256,
111
+ maximum=MAX_IMAGE_SIZE,
112
+ step=32,
113
+ value=1024,
114
+ )
115
+ with gr.Row():
116
+ guidance_scale = gr.Slider(
117
+ label="Guidance Scale",
118
+ minimum=1,
119
+ maximum=15,
120
+ step=0.1,
121
+ value=3.5,
122
+ )
123
+ num_inference_steps = gr.Slider(
124
+ label="Number of inference steps",
125
+ minimum=1,
126
+ maximum=50,
127
+ step=1,
128
+ value=28,
129
+ )
130
+
131
+ gr.Examples(
132
+ examples=examples,
133
+ fn=infer,
134
+ inputs=[prompt, lora_model],
135
+ outputs=[result, seed, output_message],
136
+ cache_examples="lazy"
137
+ )
138
+
139
+ gr.on(
140
+ triggers=[run_button.click, prompt.submit],
141
+ fn=infer,
142
+ inputs=[prompt, lora_model, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
143
+ outputs=[result, seed, output_message]
144
+ )
145
+
146
+ demo.launch()
.gitattributes → gitattributes.txt RENAMED
File without changes
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ accelerate
2
+ git+https://github.com/huggingface/diffusers
3
+ torch
4
+ transformers==4.42.4
5
+ xformers
6
+ sentencepiece
7
+ peft