vincenthugging commited on
Commit
7955fdc
·
1 Parent(s): 775014f

initial commit

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. app.py +97 -124
  3. example0.webp +0 -0
  4. requirements.txt +5 -4
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Text-to-Image Gradio Template
3
  emoji: 🖼
4
  colorFrom: purple
5
  colorTo: red
 
1
  ---
2
+ title: Flux Dev Leijun
3
  emoji: 🖼
4
  colorFrom: purple
5
  colorTo: red
app.py CHANGED
@@ -1,142 +1,115 @@
 
 
 
1
  import gradio as gr
2
- import numpy as np
3
- import random
4
- #import spaces #[uncomment to use ZeroGPU]
5
- from diffusers import DiffusionPipeline
6
  import torch
 
 
 
7
 
8
- device = "cuda" if torch.cuda.is_available() else "cpu"
9
- model_repo_id = "stabilityai/sdxl-turbo" #Replace to the model you would like to use
10
 
11
- if torch.cuda.is_available():
12
- torch_dtype = torch.float16
 
 
 
 
13
  else:
14
- torch_dtype = torch.float32
 
 
 
 
15
 
16
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
17
- pipe = pipe.to(device)
 
18
 
19
- MAX_SEED = np.iinfo(np.int32).max
20
- MAX_IMAGE_SIZE = 1024
21
 
22
- #@spaces.GPU #[uncomment to use ZeroGPU]
23
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
24
 
 
 
 
25
  if randomize_seed:
26
  seed = random.randint(0, MAX_SEED)
27
-
28
- generator = torch.Generator().manual_seed(seed)
29
-
 
 
 
 
 
 
 
 
 
30
  image = pipe(
31
- prompt = prompt,
32
- negative_prompt = negative_prompt,
33
- guidance_scale = guidance_scale,
34
- num_inference_steps = num_inference_steps,
35
- width = width,
36
- height = height,
37
- generator = generator
38
- ).images[0]
39
-
40
- return image, seed
41
-
42
- examples = [
43
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
44
- "An astronaut riding a green horse",
45
- "A delicious ceviche cheesecake slice",
46
- ]
47
-
48
- css="""
49
- #col-container {
50
- margin: 0 auto;
51
- max-width: 640px;
52
- }
53
- """
54
-
55
- with gr.Blocks(css=css) as demo:
56
-
57
- with gr.Column(elem_id="col-container"):
58
- gr.Markdown(f"""
59
- # Text-to-Image Gradio Template
60
- """)
61
-
62
- with gr.Row():
63
-
64
- prompt = gr.Text(
65
- label="Prompt",
66
- show_label=False,
67
- max_lines=1,
68
- placeholder="Enter your prompt",
69
- container=False,
70
- )
71
-
72
- run_button = gr.Button("Run", scale=0)
73
-
74
- result = gr.Image(label="Result", show_label=False)
75
 
76
- with gr.Accordion("Advanced Settings", open=False):
77
-
78
- negative_prompt = gr.Text(
79
- label="Negative prompt",
80
- max_lines=1,
81
- placeholder="Enter a negative prompt",
82
- visible=False,
83
- )
84
-
85
- seed = gr.Slider(
86
- label="Seed",
87
- minimum=0,
88
- maximum=MAX_SEED,
89
- step=1,
90
- value=0,
91
- )
92
-
93
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
94
-
95
- with gr.Row():
96
-
97
- width = gr.Slider(
98
- label="Width",
99
- minimum=256,
100
- maximum=MAX_IMAGE_SIZE,
101
- step=32,
102
- value=1024, #Replace with defaults that work for your model
103
- )
104
-
105
- height = gr.Slider(
106
- label="Height",
107
- minimum=256,
108
- maximum=MAX_IMAGE_SIZE,
109
- step=32,
110
- value=1024, #Replace with defaults that work for your model
111
- )
112
 
113
- with gr.Row():
 
 
 
 
 
 
114
 
115
- guidance_scale = gr.Slider(
116
- label="Guidance scale",
117
- minimum=0.0,
118
- maximum=10.0,
119
- step=0.1,
120
- value=0.0, #Replace with defaults that work for your model
121
- )
122
-
123
- num_inference_steps = gr.Slider(
124
- label="Number of inference steps",
125
- minimum=1,
126
- maximum=50,
127
- step=1,
128
- value=2, #Replace with defaults that work for your model
129
- )
130
 
131
- gr.Examples(
132
- examples = examples,
133
- inputs = [prompt]
134
- )
135
- gr.on(
136
- triggers=[run_button.click, prompt.submit],
137
- fn = infer,
138
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
139
- outputs = [result, seed]
 
 
140
  )
141
 
142
- demo.queue().launch()
 
 
1
+ import os
2
+ from huggingface_hub import login
3
+ import spaces
4
  import gradio as gr
 
 
 
 
5
  import torch
6
+ from PIL import Image
7
+ from diffusers import DiffusionPipeline
8
+ import random
9
 
 
 
10
 
11
+ # get access token
12
+ access_token = os.environ.get("ACCESS_TOKEN")
13
+
14
+ # login with access token
15
+ if access_token:
16
+ login(token=access_token)
17
  else:
18
+ print("warning: no access token found")
19
+
20
+ # Initialize the base model and specific LoRA
21
+ base_model = "black-forest-labs/FLUX.1-dev"
22
+ pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
23
 
24
+ lora_repo = "vincenthugging/flux-lora-leijun"
25
+ trigger_word = "leijun" # Leave trigger_word blank if not used.
26
+ pipe.load_lora_weights(lora_repo)
27
 
28
+ pipe.to("cuda")
 
29
 
30
+ MAX_SEED = 2**32-1
 
31
 
32
+ @spaces.GPU(duration=80)
33
+ def run_lora(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
34
+ # Set random seed for reproducibility
35
  if randomize_seed:
36
  seed = random.randint(0, MAX_SEED)
37
+ generator = torch.Generator(device="cuda").manual_seed(seed)
38
+
39
+ # Update progress bar (0% saat mulai)
40
+ progress(0, "Starting image generation...")
41
+
42
+ # Generate image with progress updates
43
+ for i in range(1, steps + 1):
44
+ # Simulate the processing step (in a real scenario, you would integrate this with your image generation process)
45
+ if i % (steps // 10) == 0: # Update every 10% of the steps
46
+ progress(i / steps * 100, f"Processing step {i} of {steps}...")
47
+
48
+ # Generate image using the pipeline
49
  image = pipe(
50
+ prompt=f"{prompt} {trigger_word}",
51
+ num_inference_steps=steps,
52
+ guidance_scale=cfg_scale,
53
+ width=width,
54
+ height=height,
55
+ generator=generator,
56
+ joint_attention_kwargs={"scale": lora_scale},
57
+ ).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
+ # Final update (100%)
60
+ progress(100, "Completed!")
61
+
62
+ yield image, seed
63
+
64
+ # Example cached image and settings
65
+ example_image_path = "example0.webp" # Replace with the actual path to the example image
66
+ example_prompt = """photo of leijun, black t-shirt with Logo of Xiaomi Company , face detailed, fit body,
67
+ looking at the viewer,holding a handwritten white sign with orange text that says, “Are you OK ?”,
68
+ light background,studio light"""
69
+
70
+ # example settings
71
+ example_cfg_scale = 3.5
72
+ example_steps = 28
73
+ example_width = 1024
74
+ example_height = 1024
75
+ example_seed = None
76
+ example_lora_scale = 0.85
77
+
78
+ def load_example():
79
+ # Load example image from file
80
+ example_image = Image.open(example_image_path)
81
+ return example_prompt, example_cfg_scale, example_steps, True, example_seed, example_width, example_height, example_lora_scale, example_image
82
+
83
+ with gr.Blocks() as app:
84
+ gr.Markdown("# Flux Image Generator for Leijun")
85
+ with gr.Row():
86
+ with gr.Column(scale=3):
87
+ prompt = gr.TextArea(label="Prompt", placeholder="Type a prompt", lines=5)
88
+ generate_button = gr.Button("Generate")
 
 
 
 
 
 
89
 
90
+ # Advanced options,with default values
91
+ with gr.Accordion("Advanced options", open=False):
92
+ cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=example_cfg_scale)
93
+ steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=example_steps)
94
+ width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=example_width)
95
+ height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=example_height)
96
+ randomize_seed = gr.Checkbox(True, label="Randomize seed")
97
 
98
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=example_seed)
99
+ lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=1, step=0.01, value=example_lora_scale)
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
+ with gr.Column(scale=1):
102
+ result = gr.Image(label="Generated Image")
103
+ gr.Markdown("Generate images using Lora and a text prompt.\n[[non-commercial license, Flux.1 Dev](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)]")
104
+
105
+ # Automatically load example data and image when the interface is launched
106
+ app.load(load_example, inputs=[], outputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, result])
107
+
108
+ generate_button.click(
109
+ run_lora,
110
+ inputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale],
111
+ outputs=[result, seed]
112
  )
113
 
114
+ app.queue()
115
+ app.launch()
example0.webp ADDED
requirements.txt CHANGED
@@ -1,6 +1,7 @@
1
- accelerate
2
- diffusers
3
- invisible_watermark
4
  torch
 
 
5
  transformers
6
- xformers
 
 
 
 
 
 
1
  torch
2
+ diffusers
3
+ spaces
4
  transformers
5
+ peft
6
+ sentencepiece
7
+ huggingface_hub