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| 4 |
+
import gradio as gr
|
| 5 |
+
from huggingface_hub import InferenceClient
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# --- Configuration -----------------------------------------------------------
|
| 9 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 10 |
+
MODEL_ID = "black-forest-labs/FLUX.1-schnell"
|
| 11 |
+
|
| 12 |
+
client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
|
| 13 |
+
|
| 14 |
+
# Dark theme colors
|
| 15 |
+
BG_COLOR = "#0A0F1A"
|
| 16 |
+
ACCENT = "#1FE0E6"
|
| 17 |
+
|
| 18 |
+
# Aspect ratio -> (width, height)
|
| 19 |
+
ASPECT_RATIOS = {
|
| 20 |
+
"9:16 (Phone)": (720, 1280),
|
| 21 |
+
"1:1 (Square)": (1024, 1024),
|
| 22 |
+
"16:9 (Wide)": (1280, 720),
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# Presets: name -> prompt
|
| 26 |
+
PRESETS = {
|
| 27 |
+
"UAE Sunset": "A breathtaking UAE desert sunset, golden sand dunes rolling under a vibrant orange and purple sky, silhouette of a lone camel, cinematic lighting, ultra detailed, 4k wallpaper",
|
| 28 |
+
"Cyberpunk City": "A neon-soaked cyberpunk city at night, towering skyscrapers with glowing cyan and magenta lights, flying cars, rain-slicked streets reflecting neon, blade runner aesthetic, ultra detailed wallpaper",
|
| 29 |
+
"Minimal Dark": "Minimalist dark abstract wallpaper, deep black background with subtle geometric cyan lines, smooth gradients, modern elegant design, 4k",
|
| 30 |
+
"Arabic Calligraphy": "Beautiful Arabic calligraphy art wallpaper, elegant flowing script in gold on dark textured background, intricate decorative borders, luxurious design, 4k",
|
| 31 |
+
"Desert Stars": "A serene Arabian desert at night under a sky full of stars, Milky Way galaxy visible, soft sand dunes in foreground, peaceful cosmic atmosphere, ultra detailed wallpaper",
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# --- Core function -----------------------------------------------------------
|
| 36 |
+
def generate_wallpaper(prompt, aspect, preset):
|
| 37 |
+
"""Generate a wallpaper via FLUX.1-schnell."""
|
| 38 |
+
# If a preset is selected, use its prompt (override user prompt if empty)
|
| 39 |
+
if preset and preset in PRESETS:
|
| 40 |
+
final_prompt = PRESETS[preset]
|
| 41 |
+
elif prompt and prompt.strip():
|
| 42 |
+
final_prompt = prompt.strip()
|
| 43 |
+
else:
|
| 44 |
+
final_prompt = PRESETS["UAE Sunset"]
|
| 45 |
+
|
| 46 |
+
w, h = ASPECT_RATIOS.get(aspect, (1024, 1024))
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
# FLUX.1-schnell supports image generation via inference
|
| 50 |
+
image = client.text_to_image(
|
| 51 |
+
final_prompt,
|
| 52 |
+
width=w,
|
| 53 |
+
height=h,
|
| 54 |
+
)
|
| 55 |
+
# Ensure PIL Image
|
| 56 |
+
if not isinstance(image, Image.Image):
|
| 57 |
+
image = Image.open(image) if hasattr(image, "read") else Image.frombytes("RGB", image.size, image.tobytes()) if hasattr(image, "size") else Image.open(io.BytesIO(image))
|
| 58 |
+
return image, "✅ Wallpaper generated successfully!"
|
| 59 |
+
except Exception as e:
|
| 60 |
+
# Return a placeholder error image
|
| 61 |
+
img = Image.new("RGB", (w, h), BG_COLOR)
|
| 62 |
+
return img, f"❌ Error: {str(e)}"
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def download_image(img):
|
| 66 |
+
"""Provide downloadable file path."""
|
| 67 |
+
if img is None:
|
| 68 |
+
return None
|
| 69 |
+
path = os.path.join(os.getcwd(), "wallpaper_output.png")
|
| 70 |
+
img.save(path)
|
| 71 |
+
return path
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def pick_preset_prompt(preset_name):
|
| 75 |
+
"""Load preset prompt into the text box."""
|
| 76 |
+
return PRESETS.get(preset_name, "")
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# --- UI -----------------------------------------------------------------------
|
| 80 |
+
CUSTOM_CSS = f"""
|
| 81 |
+
#main {{background-color: {BG_COLOR};}}
|
| 82 |
+
.gradio-container {{background-color: {BG_COLOR} !important;}}
|
| 83 |
+
h1, h2, h3 {{color: {ACCENT} !important;}}
|
| 84 |
+
"""
|
| 85 |
+
|
| 86 |
+
with gr.Blocks(
|
| 87 |
+
title="Dispatch AI Wallpaper Engine",
|
| 88 |
+
theme=gr.themes.Base(
|
| 89 |
+
primary_hue="cyan",
|
| 90 |
+
neutral_hue="slate",
|
| 91 |
+
font=("Inter", "system-ui", "sans-serif"),
|
| 92 |
+
),
|
| 93 |
+
css=CUSTOM_CSS,
|
| 94 |
+
) as demo:
|
| 95 |
+
gr.Markdown(
|
| 96 |
+
f"""
|
| 97 |
+
<div style="text-align:center;">
|
| 98 |
+
<h1>Dispatch AI Wallpaper Engine</h1>
|
| 99 |
+
<p style="color:{ACCENT};">Generate stunning phone wallpapers with FLUX.1-schnell · Powered by Dispatch AI (FZE)</p>
|
| 100 |
+
</div>
|
| 101 |
+
"""
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
with gr.Row():
|
| 105 |
+
with gr.Column(scale=1):
|
| 106 |
+
prompt_input = gr.Textbox(
|
| 107 |
+
label="Prompt",
|
| 108 |
+
placeholder="Describe your wallpaper... or pick a preset below",
|
| 109 |
+
lines=3,
|
| 110 |
+
)
|
| 111 |
+
aspect_select = gr.Radio(
|
| 112 |
+
list(ASPECT_RATIOS.keys()),
|
| 113 |
+
label="Aspect Ratio",
|
| 114 |
+
value="9:16 (Phone)",
|
| 115 |
+
)
|
| 116 |
+
preset_select = gr.Dropdown(
|
| 117 |
+
list(PRESETS.keys()),
|
| 118 |
+
label="Preset",
|
| 119 |
+
value=None,
|
| 120 |
+
info="Select a preset to auto-fill the prompt",
|
| 121 |
+
)
|
| 122 |
+
load_preset_btn = gr.Button("Load Preset Prompt", variant="secondary")
|
| 123 |
+
generate_btn = gr.Button("🎨 Generate Wallpaper", variant="primary")
|
| 124 |
+
with gr.Column(scale=2):
|
| 125 |
+
output_image = gr.Image(
|
| 126 |
+
label="Generated Wallpaper",
|
| 127 |
+
type="pil",
|
| 128 |
+
show_download_button=True,
|
| 129 |
+
)
|
| 130 |
+
status_box = gr.Textbox(label="Status", interactive=False)
|
| 131 |
+
download_btn = gr.Button("⬇️ Download PNG", variant="secondary")
|
| 132 |
+
download_file = gr.File(label="Download")
|
| 133 |
+
|
| 134 |
+
# Events
|
| 135 |
+
load_preset_btn.click(pick_preset_prompt, inputs=preset_select, outputs=prompt_input)
|
| 136 |
+
generate_btn.click(
|
| 137 |
+
generate_wallpaper,
|
| 138 |
+
inputs=[prompt_input, aspect_select, preset_select],
|
| 139 |
+
outputs=[output_image, status_box],
|
| 140 |
+
)
|
| 141 |
+
download_btn.click(download_image, inputs=output_image, outputs=download_file)
|
| 142 |
+
|
| 143 |
+
gr.Markdown(
|
| 144 |
+
"""
|
| 145 |
+
<div style="text-align:center; opacity:0.6; padding-top:20px;">
|
| 146 |
+
<small>Dispatch AI (FZE) · UAE · Model: FLUX.1-schnell by Black Forest Labs</small>
|
| 147 |
+
</div>
|
| 148 |
+
"""
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
if __name__ == "__main__":
|
| 152 |
+
demo.launch()
|