Commit
·
0a68e6e
1
Parent(s):
1ea548a
modified app.py
Browse files- app.py +71 -1
- requirements.txt +4 -4
app.py
CHANGED
@@ -1,3 +1,73 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import DiffusionPipeline
|
4 |
+
import time
|
5 |
|
6 |
+
# Initialize the base model
|
7 |
+
base_model = "black-forest-labs/FLUX.1-dev"
|
8 |
+
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
|
9 |
+
|
10 |
+
MAX_SEED = 2**32-1
|
11 |
+
|
12 |
+
class calculateDuration:
|
13 |
+
def __init__(self, activity_name=""):
|
14 |
+
self.activity_name = activity_name
|
15 |
+
|
16 |
+
def __enter__(self):
|
17 |
+
self.start_time = time.time()
|
18 |
+
return self
|
19 |
+
|
20 |
+
def __exit__(self, exc_type, exc_value, traceback):
|
21 |
+
self.end_time = time.time()
|
22 |
+
self.elapsed_time = self.end_time - self.start_time
|
23 |
+
if self.activity_name:
|
24 |
+
print(f"Elapsed time for {self.activity_name}: {self.elapsed_time:.6f} seconds")
|
25 |
+
else:
|
26 |
+
print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
|
27 |
+
|
28 |
+
def generate_image(prompt, steps, seed, cfg_scale, width, height):
|
29 |
+
pipe.to("cuda")
|
30 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
31 |
+
|
32 |
+
with calculateDuration("Generating image"):
|
33 |
+
# Generate image
|
34 |
+
image = pipe(
|
35 |
+
prompt=prompt,
|
36 |
+
num_inference_steps=steps,
|
37 |
+
guidance_scale=cfg_scale,
|
38 |
+
width=width,
|
39 |
+
height=height,
|
40 |
+
generator=generator
|
41 |
+
).images[0]
|
42 |
+
return image
|
43 |
+
|
44 |
+
def run_model(prompt, cfg_scale, steps, randomize_seed, seed, width, height):
|
45 |
+
if randomize_seed:
|
46 |
+
seed = torch.randint(0, MAX_SEED, (1,)).item()
|
47 |
+
|
48 |
+
image = generate_image(prompt, steps, seed, cfg_scale, width, height)
|
49 |
+
return image, seed
|
50 |
+
|
51 |
+
with gr.Blocks() as app:
|
52 |
+
gr.Row():
|
53 |
+
with gr.Column():
|
54 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Type a prompt here")
|
55 |
+
generate_button = gr.Button("Generate")
|
56 |
+
with gr.Row():
|
57 |
+
result = gr.Image(label="Generated Image")
|
58 |
+
|
59 |
+
with gr.Row():
|
60 |
+
with gr.Column():
|
61 |
+
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=3.5)
|
62 |
+
steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=28)
|
63 |
+
width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=1024)
|
64 |
+
height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=1024)
|
65 |
+
randomize_seed = gr.Checkbox(True, label="Randomize seed")
|
66 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
67 |
+
|
68 |
+
gr.Interface(
|
69 |
+
fn=run_model,
|
70 |
+
inputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height],
|
71 |
+
outputs=[result, seed],
|
72 |
+
live=True
|
73 |
+
).launch()
|
requirements.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
accelerate
|
2 |
-
diffusers
|
3 |
-
invisible_watermark
|
4 |
torch
|
|
|
|
|
5 |
transformers
|
6 |
-
|
|
|
|
|
|
|
|
|
|
1 |
torch
|
2 |
+
git+https://github.com/huggingface/diffusers
|
3 |
+
spaces
|
4 |
transformers
|
5 |
+
peft
|
6 |
+
sentencepiece
|