Spaces:
Running
on
Zero
Running
on
Zero
Ahmad Basyouni
commited on
Commit
•
b5164a5
1
Parent(s):
89c448c
Add application file
Browse files
app.py
CHANGED
@@ -1,27 +1,27 @@
|
|
1 |
import gradio as gr
|
2 |
-
from diffusers import StableDiffusionPipeline,
|
3 |
import torch
|
4 |
from PIL import ImageEnhance, Image
|
5 |
import numpy as np
|
|
|
6 |
|
|
|
7 |
model_id = "CompVis/stable-diffusion-v1-4"
|
8 |
-
default_scheduler =
|
9 |
|
10 |
-
# Set device based on availability
|
11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
13 |
-
|
14 |
-
# Load pipeline with appropriate torch dtype
|
15 |
pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=default_scheduler, torch_dtype=torch_dtype).to(device)
|
16 |
|
17 |
-
# Scheduler options
|
18 |
schedulers = {
|
19 |
-
"Artistic & Imaginative (Euler Ancestral) - Recommended for creative scenes, moderate speed": EulerAncestralDiscreteScheduler,
|
20 |
-
"Photo-Realistic (PNDM) - Best for realistic details, moderate speed": PNDMScheduler,
|
21 |
"High-Definition & Fast (DDIM) - Good quality with fastest speed": DDIMScheduler,
|
|
|
|
|
22 |
}
|
23 |
|
24 |
-
# Main image generation function with
|
|
|
25 |
def generate_image(prompt, use_categories, genre, style, theme, lighting, scheduler_choice, quality, size):
|
26 |
# Check if additional categories should be added to the prompt
|
27 |
if use_categories:
|
@@ -38,7 +38,7 @@ def generate_image(prompt, use_categories, genre, style, theme, lighting, schedu
|
|
38 |
pipe.scheduler = scheduler
|
39 |
|
40 |
# Set output size based on selection
|
41 |
-
image_size = (512, 512) if size == "Profile Picture" else (
|
42 |
|
43 |
# Generate image with specified quality and size
|
44 |
with torch.no_grad():
|
@@ -54,16 +54,15 @@ def adjust_brightness_contrast(image, brightness, contrast):
|
|
54 |
image = ImageEnhance.Contrast(image).enhance(contrast)
|
55 |
return np.array(image)
|
56 |
|
57 |
-
# Warning function
|
58 |
def show_warning(quality):
|
59 |
-
if quality >
|
60 |
-
return "⚠️ High Quality: This
|
61 |
return ""
|
62 |
|
63 |
-
# Build Gradio Interface
|
64 |
with gr.Blocks() as demo:
|
65 |
gr.Markdown("# ✨ AI-Powered Wallpaper/Profile Picture Generator\n🖼️ A tool to generate and fine-tune AI-created wallpapers and profile pictures with adjustable styles and effects.")
|
66 |
-
gr.Markdown("⚠️ **Live effects and advanced prompt engineering coming soon! Disclaimer**: Results may not always be accurate or perfectly aligned with your prompt. Experiment with prompt adjustments and settings to get the best results.")
|
67 |
|
68 |
# Image Generation Section
|
69 |
with gr.Tab("Image Generator"):
|
@@ -83,18 +82,19 @@ with gr.Blocks() as demo:
|
|
83 |
theme = gr.Dropdown(["Landscape", "Portrait", "Abstract Patterns", "Architecture"], label="Theme")
|
84 |
lighting = gr.Dropdown(["Warm", "Cool", "Cinematic", "Soft", "Neon"], label="Lighting")
|
85 |
|
86 |
-
|
|
|
87 |
warning_message = gr.Markdown("")
|
88 |
|
89 |
-
# Scheduler selection with default option
|
90 |
scheduler_choice = gr.Dropdown(
|
91 |
[
|
92 |
-
"
|
93 |
"Photo-Realistic (PNDM) - Best for realistic details, moderate speed",
|
94 |
-
"
|
95 |
],
|
96 |
label="Artistic Style & Speed",
|
97 |
-
value="
|
98 |
)
|
99 |
|
100 |
size = gr.Dropdown(["Profile Picture", "Wallpaper"], label="Image Size", value="Profile Picture")
|
|
|
1 |
import gradio as gr
|
2 |
+
from diffusers import StableDiffusionPipeline, DDIMScheduler
|
3 |
import torch
|
4 |
from PIL import ImageEnhance, Image
|
5 |
import numpy as np
|
6 |
+
import spaces # For using ZeroGPU decorator
|
7 |
|
8 |
+
# Load Stable Diffusion pipeline with efficient defaults
|
9 |
model_id = "CompVis/stable-diffusion-v1-4"
|
10 |
+
default_scheduler = DDIMScheduler.from_pretrained(model_id, subfolder="scheduler")
|
11 |
|
|
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
|
|
|
|
14 |
pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=default_scheduler, torch_dtype=torch_dtype).to(device)
|
15 |
|
16 |
+
# Scheduler options (setting DDIM as the default for speed)
|
17 |
schedulers = {
|
|
|
|
|
18 |
"High-Definition & Fast (DDIM) - Good quality with fastest speed": DDIMScheduler,
|
19 |
+
"Photo-Realistic (PNDM) - Best for realistic details, moderate speed": PNDMScheduler,
|
20 |
+
"Artistic & Imaginative (Euler Ancestral) - Good for creative scenes, moderate speed": EulerAncestralDiscreteScheduler,
|
21 |
}
|
22 |
|
23 |
+
# Main image generation function with 60-second GPU duration limit
|
24 |
+
@spaces.GPU(duration=60)
|
25 |
def generate_image(prompt, use_categories, genre, style, theme, lighting, scheduler_choice, quality, size):
|
26 |
# Check if additional categories should be added to the prompt
|
27 |
if use_categories:
|
|
|
38 |
pipe.scheduler = scheduler
|
39 |
|
40 |
# Set output size based on selection
|
41 |
+
image_size = (512, 512) if size == "Profile Picture" else (512, 512) # Using smaller size for faster generation
|
42 |
|
43 |
# Generate image with specified quality and size
|
44 |
with torch.no_grad():
|
|
|
54 |
image = ImageEnhance.Contrast(image).enhance(contrast)
|
55 |
return np.array(image)
|
56 |
|
57 |
+
# Warning function for high-quality settings
|
58 |
def show_warning(quality):
|
59 |
+
if quality > 40: # Lower threshold for warning
|
60 |
+
return "⚠️ High Quality: This may slow down generation. Consider using values below 40 for faster results."
|
61 |
return ""
|
62 |
|
63 |
+
# Build Gradio Interface with adjusted defaults
|
64 |
with gr.Blocks() as demo:
|
65 |
gr.Markdown("# ✨ AI-Powered Wallpaper/Profile Picture Generator\n🖼️ A tool to generate and fine-tune AI-created wallpapers and profile pictures with adjustable styles and effects.")
|
|
|
66 |
|
67 |
# Image Generation Section
|
68 |
with gr.Tab("Image Generator"):
|
|
|
82 |
theme = gr.Dropdown(["Landscape", "Portrait", "Abstract Patterns", "Architecture"], label="Theme")
|
83 |
lighting = gr.Dropdown(["Warm", "Cool", "Cinematic", "Soft", "Neon"], label="Lighting")
|
84 |
|
85 |
+
# Reduced quality for faster generation
|
86 |
+
quality = gr.Slider(20, 80, value=30, step=10, label="Image Quality", info="Lower values for faster generation.")
|
87 |
warning_message = gr.Markdown("")
|
88 |
|
89 |
+
# Scheduler selection with default option as DDIM for speed
|
90 |
scheduler_choice = gr.Dropdown(
|
91 |
[
|
92 |
+
"High-Definition & Fast (DDIM) - Good quality with fastest speed",
|
93 |
"Photo-Realistic (PNDM) - Best for realistic details, moderate speed",
|
94 |
+
"Artistic & Imaginative (Euler Ancestral) - Good for creative scenes, moderate speed"
|
95 |
],
|
96 |
label="Artistic Style & Speed",
|
97 |
+
value="High-Definition & Fast (DDIM) - Good quality with fastest speed"
|
98 |
)
|
99 |
|
100 |
size = gr.Dropdown(["Profile Picture", "Wallpaper"], label="Image Size", value="Profile Picture")
|