Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
from torchvision import transforms
|
6 |
+
from PIL import Image
|
7 |
+
import numpy as np
|
8 |
+
from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler
|
9 |
+
from io import BytesIO
|
10 |
+
|
11 |
+
# Set up model cache directory
|
12 |
+
MODEL_CACHE = "model_cache"
|
13 |
+
os.makedirs(MODEL_CACHE, exist_ok=True)
|
14 |
+
|
15 |
+
# Initialize model pipeline
|
16 |
+
def get_pipeline():
|
17 |
+
model_id = "OFA-Sys/small-stable-diffusion-v0"
|
18 |
+
scheduler = EulerAncestralDiscreteScheduler.from_pretrained(
|
19 |
+
model_id,
|
20 |
+
subfolder="scheduler",
|
21 |
+
cache_dir=MODEL_CACHE
|
22 |
+
)
|
23 |
+
|
24 |
+
return StableDiffusionPipeline.from_pretrained(
|
25 |
+
model_id,
|
26 |
+
scheduler=scheduler,
|
27 |
+
torch_dtype=torch.float32,
|
28 |
+
cache_dir=MODEL_CACHE,
|
29 |
+
safety_checker=None
|
30 |
+
).to("cpu")
|
31 |
+
|
32 |
+
# Load model once at startup
|
33 |
+
pipeline = get_pipeline()
|
34 |
+
|
35 |
+
def generate_image(
|
36 |
+
prompt: str,
|
37 |
+
width: int,
|
38 |
+
height: int,
|
39 |
+
seed: int,
|
40 |
+
randomize_seed: bool,
|
41 |
+
guidance_scale: float,
|
42 |
+
num_inference_steps: int
|
43 |
+
):
|
44 |
+
# Handle seed randomization
|
45 |
+
if randomize_seed:
|
46 |
+
seed = random.randint(0, 2147483647)
|
47 |
+
generator = torch.Generator(device="cpu").manual_seed(seed)
|
48 |
+
|
49 |
+
# Generate image
|
50 |
+
image = pipeline(
|
51 |
+
prompt,
|
52 |
+
width=width,
|
53 |
+
height=height,
|
54 |
+
guidance_scale=guidance_scale,
|
55 |
+
num_inference_steps=num_inference_steps,
|
56 |
+
generator=generator
|
57 |
+
).images[0]
|
58 |
+
|
59 |
+
return image, seed
|
60 |
+
|
61 |
+
# Style presets
|
62 |
+
STYLE_PRESETS = {
|
63 |
+
"Realistic": "photorealistic, 8k, detailed, sharp focus",
|
64 |
+
"Anime": "anime style, vibrant colors, cel shading",
|
65 |
+
"Oil Painting": "oil painting, brush strokes, textured",
|
66 |
+
"Cyberpunk": "neon lights, cyberpunk, futuristic, rain",
|
67 |
+
"Minimalist": "minimalist, simple shapes, flat colors"
|
68 |
+
}
|
69 |
+
|
70 |
+
def apply_style(prompt, style_name):
|
71 |
+
return f"{STYLE_PRESETS[style_name]}, {prompt}"
|
72 |
+
|
73 |
+
# UI Components
|
74 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
75 |
+
gr.Markdown("""
|
76 |
+
# ⚡ FLUX Turbo Generator
|
77 |
+
**CPU-Optimized Image Generation** · No APIs · No Limits
|
78 |
+
""")
|
79 |
+
|
80 |
+
with gr.Row():
|
81 |
+
with gr.Column(scale=3):
|
82 |
+
prompt = gr.Textbox(
|
83 |
+
label="Prompt",
|
84 |
+
placeholder="Describe what you want to create...",
|
85 |
+
lines=3
|
86 |
+
)
|
87 |
+
style = gr.Dropdown(
|
88 |
+
label="Style Preset",
|
89 |
+
choices=list(STYLE_PRESETS.keys()),
|
90 |
+
value="Realistic"
|
91 |
+
)
|
92 |
+
generate_btn = gr.Button("Generate", variant="primary")
|
93 |
+
|
94 |
+
with gr.Accordion("Advanced Settings", open=False):
|
95 |
+
with gr.Row():
|
96 |
+
width = gr.Slider(384, 768, value=512, step=64, label="Width")
|
97 |
+
height = gr.Slider(384, 768, value=512, step=64, label="Height")
|
98 |
+
guidance = gr.Slider(1.0, 20.0, value=7.5, step=0.5, label="Creativity")
|
99 |
+
steps = gr.Slider(5, 50, value=20, step=5, label="Generation Steps")
|
100 |
+
|
101 |
+
with gr.Row():
|
102 |
+
seed = gr.Number(label="Seed", value=0)
|
103 |
+
random_seed = gr.Checkbox(label="Random Seed", value=True)
|
104 |
+
|
105 |
+
with gr.Column(scale=2):
|
106 |
+
output_image = gr.Image(label="Generated Image", type="pil")
|
107 |
+
used_seed = gr.Textbox(label="Used Seed", interactive=False)
|
108 |
+
gr.Markdown("**Tip:** Use specific descriptions for better results")
|
109 |
+
|
110 |
+
# Style application
|
111 |
+
style.change(
|
112 |
+
fn=apply_style,
|
113 |
+
inputs=[prompt, style],
|
114 |
+
outputs=prompt
|
115 |
+
)
|
116 |
+
|
117 |
+
# Generation handler
|
118 |
+
generate_btn.click(
|
119 |
+
fn=generate_image,
|
120 |
+
inputs=[prompt, width, height, seed, random_seed, guidance, steps],
|
121 |
+
outputs=[output_image, used_seed]
|
122 |
+
)
|
123 |
+
|
124 |
+
# Examples
|
125 |
+
gr.Examples(
|
126 |
+
examples=[
|
127 |
+
["majestic mountain landscape at sunset, snow-capped peaks", "Realistic", 512, 512],
|
128 |
+
["cyberpunk city street at night, neon signs, rain puddles", "Cyberpunk", 512, 512],
|
129 |
+
["cute anime cat warrior wearing armor, fantasy setting", "Anime", 512, 512]
|
130 |
+
],
|
131 |
+
inputs=[prompt, style, width, height],
|
132 |
+
label="Example Prompts"
|
133 |
+
)
|
134 |
+
|
135 |
+
# Launch settings
|
136 |
+
if __name__ == "__main__":
|
137 |
+
demo.launch(
|
138 |
+
server_name="0.0.0.0",
|
139 |
+
server_port=int(os.getenv("PORT", 7860)),
|
140 |
+
enable_queue=True
|
141 |
+
)
|