Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Sagar_Flux Image Generator
|
2 |
+
# To use this notebook:
|
3 |
+
# 1. Make sure you're using a GPU runtime: Runtime > Change runtime type > GPU
|
4 |
+
# 2. Run each cell in order
|
5 |
+
|
6 |
+
# Install required libraries
|
7 |
+
!pip install -q diffusers transformers torch gradio
|
8 |
+
|
9 |
+
import gradio as gr
|
10 |
+
import torch
|
11 |
+
from PIL import Image
|
12 |
+
from diffusers import DiffusionPipeline
|
13 |
+
import random
|
14 |
+
|
15 |
+
# Initialize the base model and specific LoRA
|
16 |
+
base_model = "black-forest-labs/FLUX.1-dev"
|
17 |
+
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.float16)
|
18 |
+
pipe.to("cuda")
|
19 |
+
|
20 |
+
lora_repo = "sagar007/sagar_flux"
|
21 |
+
trigger_word = "sagar" # Use "sagar" as the trigger word
|
22 |
+
pipe.load_lora_weights(lora_repo)
|
23 |
+
|
24 |
+
MAX_SEED = 2**32-1
|
25 |
+
|
26 |
+
def run_lora(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
|
27 |
+
# Set random seed for reproducibility
|
28 |
+
if randomize_seed:
|
29 |
+
seed = random.randint(0, MAX_SEED)
|
30 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
31 |
+
|
32 |
+
# Update progress bar
|
33 |
+
progress(0, "Starting image generation...")
|
34 |
+
|
35 |
+
# Generate image with progress updates
|
36 |
+
for i in range(1, steps + 1):
|
37 |
+
if i % (steps // 10) == 0:
|
38 |
+
progress(i / steps * 100, f"Processing step {i} of {steps}...")
|
39 |
+
|
40 |
+
# Generate image using the pipeline
|
41 |
+
image = pipe(
|
42 |
+
prompt=f"{prompt} {trigger_word}",
|
43 |
+
num_inference_steps=steps,
|
44 |
+
guidance_scale=cfg_scale,
|
45 |
+
width=width,
|
46 |
+
height=height,
|
47 |
+
generator=generator,
|
48 |
+
cross_attention_kwargs={"scale": lora_scale},
|
49 |
+
).images[0]
|
50 |
+
|
51 |
+
# Final update
|
52 |
+
progress(100, "Completed!")
|
53 |
+
|
54 |
+
return image, seed
|
55 |
+
|
56 |
+
# Gradio interface
|
57 |
+
with gr.Blocks() as app:
|
58 |
+
gr.Markdown("# Sagar_Flux Image Generator")
|
59 |
+
with gr.Row():
|
60 |
+
with gr.Column(scale=3):
|
61 |
+
prompt = gr.TextArea(label="Prompt", placeholder="Type a prompt (include 'sagar' for best results)", lines=5)
|
62 |
+
generate_button = gr.Button("Generate")
|
63 |
+
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=7)
|
64 |
+
steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=30)
|
65 |
+
width = gr.Slider(label="Width", minimum=256, maximum=1024, step=64, value=512)
|
66 |
+
height = gr.Slider(label="Height", minimum=256, maximum=1024, step=64, value=512)
|
67 |
+
randomize_seed = gr.Checkbox(False, label="Randomize seed")
|
68 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42)
|
69 |
+
lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=1, step=0.01, value=0.75)
|
70 |
+
with gr.Column(scale=1):
|
71 |
+
result = gr.Image(label="Generated Image")
|
72 |
+
gr.Markdown("Generate images using Sagar_Flux and a text prompt.")
|
73 |
+
|
74 |
+
generate_button.click(
|
75 |
+
run_lora,
|
76 |
+
inputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale],
|
77 |
+
outputs=[result, seed]
|
78 |
+
)
|
79 |
+
|
80 |
+
app.launch()
|