Spaces:
Running
on
Zero
Running
on
Zero
Ahmad Basyouni
commited on
Commit
•
aa7f465
1
Parent(s):
d793609
Add application file
Browse files- app.py +133 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler, DDIMScheduler, PNDMScheduler
|
3 |
+
import torch
|
4 |
+
from PIL import ImageEnhance, Image
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
# Load Stable Diffusion pipeline
|
8 |
+
model_id = "CompVis/stable-diffusion-v1-4"
|
9 |
+
default_scheduler = EulerAncestralDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
|
10 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=default_scheduler, torch_dtype=torch.float16)
|
11 |
+
pipe = pipe.to("cuda")
|
12 |
+
|
13 |
+
# Scheduler options
|
14 |
+
schedulers = {
|
15 |
+
"Artistic & Imaginative (Euler Ancestral) - Recommended for creative scenes, moderate speed": EulerAncestralDiscreteScheduler,
|
16 |
+
"Photo-Realistic (PNDM) - Best for realistic details, moderate speed": PNDMScheduler,
|
17 |
+
"High-Definition & Fast (DDIM) - Good quality with fastest speed": DDIMScheduler,
|
18 |
+
}
|
19 |
+
|
20 |
+
# Main image generation function with dynamic scheduling and size option
|
21 |
+
def generate_image(prompt, use_categories, genre, style, theme, lighting, scheduler_choice, quality, size):
|
22 |
+
# Check if additional categories should be added to the prompt
|
23 |
+
if use_categories:
|
24 |
+
prompt_text = (
|
25 |
+
f"{prompt.strip()} in a {genre.lower()} wallpaper style, "
|
26 |
+
f"with {style.lower()} visuals, focusing on a {theme.lower()} theme "
|
27 |
+
f"and {lighting.lower()} lighting."
|
28 |
+
)
|
29 |
+
else:
|
30 |
+
prompt_text = prompt.strip()
|
31 |
+
|
32 |
+
# Set the scheduler based on user choice
|
33 |
+
scheduler = schedulers[scheduler_choice].from_pretrained(model_id, subfolder="scheduler")
|
34 |
+
pipe.scheduler = scheduler
|
35 |
+
|
36 |
+
# Set output size based on selection
|
37 |
+
image_size = (512, 512) if size == "Profile Picture" else (1024, 768)
|
38 |
+
|
39 |
+
# Generate image with specified quality and size
|
40 |
+
with torch.no_grad():
|
41 |
+
image = pipe(prompt_text, num_inference_steps=quality, guidance_scale=7.5).images[0]
|
42 |
+
image = image.resize(image_size) # Resize image to fit selected dimensions
|
43 |
+
|
44 |
+
return np.array(image) # Return as NumPy array for Gradio
|
45 |
+
|
46 |
+
# Post-processing function for brightness and contrast
|
47 |
+
def adjust_brightness_contrast(image, brightness, contrast):
|
48 |
+
image = Image.fromarray(image.astype('uint8'), 'RGB')
|
49 |
+
image = ImageEnhance.Brightness(image).enhance(brightness)
|
50 |
+
image = ImageEnhance.Contrast(image).enhance(contrast)
|
51 |
+
return np.array(image)
|
52 |
+
|
53 |
+
# Warning function to show a message if the user selects a high value for quality
|
54 |
+
def show_warning(quality):
|
55 |
+
if quality > 80:
|
56 |
+
return "⚠️ High Quality: This setting may slow down generation and might not provide additional visual improvement. Consider using 50-80 steps for best results."
|
57 |
+
return ""
|
58 |
+
|
59 |
+
# Build Gradio Interface
|
60 |
+
with gr.Blocks() as demo:
|
61 |
+
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.")
|
62 |
+
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.")
|
63 |
+
|
64 |
+
# Image Generation Section
|
65 |
+
with gr.Tab("Image Generator"):
|
66 |
+
gr.Markdown("## Generate an Image")
|
67 |
+
|
68 |
+
with gr.Row():
|
69 |
+
with gr.Column():
|
70 |
+
custom_prompt = gr.Textbox(label="Custom Prompt", placeholder="Describe your image (e.g., 'A forest at sunset')")
|
71 |
+
|
72 |
+
# Toggle for using additional categories
|
73 |
+
use_categories = gr.Checkbox(label="Enable Advanced Settings (Genre, Style, Theme, Lighting)", value=False)
|
74 |
+
|
75 |
+
# Additional categories, hidden by default and shown only if use_categories is checked
|
76 |
+
with gr.Accordion("Advanced Settings", open=False):
|
77 |
+
genre = gr.Dropdown(["Futuristic", "Nature", "Abstract", "Fantasy", "Sci-Fi", "Cyberpunk"], label="Genre")
|
78 |
+
style = gr.Dropdown(["Realistic", "Surreal", "Digital Art", "Cartoon", "Photorealistic"], label="Style")
|
79 |
+
theme = gr.Dropdown(["Landscape", "Portrait", "Abstract Patterns", "Architecture"], label="Theme")
|
80 |
+
lighting = gr.Dropdown(["Warm", "Cool", "Cinematic", "Soft", "Neon"], label="Lighting")
|
81 |
+
|
82 |
+
quality = gr.Slider(20, 150, value=80, step=10, label="Image Quality", info="Higher values yield more detail but take longer to generate.")
|
83 |
+
warning_message = gr.Markdown("")
|
84 |
+
|
85 |
+
# Scheduler selection with default option
|
86 |
+
scheduler_choice = gr.Dropdown(
|
87 |
+
[
|
88 |
+
"Artistic & Imaginative (Euler Ancestral) - Recommended for creative scenes, moderate speed",
|
89 |
+
"Photo-Realistic (PNDM) - Best for realistic details, moderate speed",
|
90 |
+
"High-Definition & Fast (DDIM) - Good quality with fastest speed"
|
91 |
+
],
|
92 |
+
label="Artistic Style & Speed",
|
93 |
+
value="Artistic & Imaginative (Euler Ancestral) - Recommended for creative scenes, moderate speed"
|
94 |
+
)
|
95 |
+
|
96 |
+
size = gr.Dropdown(["Profile Picture", "Wallpaper"], label="Image Size", value="Profile Picture")
|
97 |
+
generate_button = gr.Button("Generate Image")
|
98 |
+
|
99 |
+
with gr.Column():
|
100 |
+
generated_image = gr.Image(label="Generated Image", interactive=False)
|
101 |
+
|
102 |
+
# Display warning message for high-quality settings
|
103 |
+
quality.change(show_warning, inputs=[quality], outputs=warning_message)
|
104 |
+
|
105 |
+
# Bind the generate function to the generate button
|
106 |
+
generate_button.click(
|
107 |
+
fn=generate_image,
|
108 |
+
inputs=[custom_prompt, use_categories, genre, style, theme, lighting, scheduler_choice, quality, size],
|
109 |
+
outputs=generated_image
|
110 |
+
)
|
111 |
+
|
112 |
+
# Post-Processing Section
|
113 |
+
with gr.Tab("Edit Generated Image"):
|
114 |
+
gr.Markdown("## Adjust Brightness & Contrast")
|
115 |
+
|
116 |
+
with gr.Row():
|
117 |
+
with gr.Column():
|
118 |
+
brightness_slider = gr.Slider(0.5, 2.0, value=1.0, label="Brightness")
|
119 |
+
contrast_slider = gr.Slider(0.5, 2.0, value=1.0, label="Contrast")
|
120 |
+
apply_adjustments = gr.Button("Apply Adjustments")
|
121 |
+
|
122 |
+
with gr.Column():
|
123 |
+
output_image = gr.Image(label="Adjusted Image", interactive=False)
|
124 |
+
|
125 |
+
# Bind the brightness and contrast adjustment function to the Apply Adjustments button
|
126 |
+
apply_adjustments.click(
|
127 |
+
fn=adjust_brightness_contrast,
|
128 |
+
inputs=[generated_image, brightness_slider, contrast_slider],
|
129 |
+
outputs=output_image
|
130 |
+
)
|
131 |
+
|
132 |
+
# Launch with a public shareable link
|
133 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
diffusers
|
2 |
+
torch
|
3 |
+
gradio
|
4 |
+
pillow
|