3morixd commited on
Commit
3b41516
·
verified ·
1 Parent(s): 5a3d81f

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +127 -44
app.py CHANGED
@@ -1,9 +1,14 @@
 
 
 
 
 
 
1
  import os
2
  import io
3
- import random
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)
@@ -11,59 +16,103 @@ 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")
@@ -72,34 +121,56 @@ def download_image(img):
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):
@@ -111,7 +182,7 @@ with gr.Blocks(
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()),
@@ -119,8 +190,13 @@ with gr.Blocks(
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",
@@ -135,18 +211,25 @@ with gr.Blocks(
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()
 
1
+ """
2
+ Dispatch AI — Wallpaper Engine
3
+ Gradio app generating phone wallpapers with FLUX.1-schnell.
4
+ Presets: UAE Sunset, Cyberpunk, Minimal Dark, Arabic Calligraphy + more.
5
+ """
6
+
7
  import os
8
  import io
 
9
  import gradio as gr
10
  from huggingface_hub import InferenceClient
11
+ from PIL import Image, ImageDraw
12
 
13
  # --- Configuration -----------------------------------------------------------
14
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
 
16
 
17
  client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
18
 
 
19
  BG_COLOR = "#0A0F1A"
20
  ACCENT = "#1FE0E6"
21
 
22
  # Aspect ratio -> (width, height)
23
  ASPECT_RATIOS = {
24
+ "9:16 (Phone Wallpaper)": (720, 1280),
25
+ "9:16 HD (Phone)": (1080, 1920),
26
  "1:1 (Square)": (1024, 1024),
27
  "16:9 (Wide)": (1280, 720),
28
+ "4:5 (Portrait)": (1024, 1280),
29
  }
30
 
31
+ # Presets: name -> (prompt, emoji)
32
  PRESETS = {
33
+ "UAE Sunset": (
34
+ "A breathtaking UAE desert sunset, golden sand dunes rolling under a vibrant orange and purple sky, "
35
+ "silhouette of a lone camel walking, distant wind tower, cinematic lighting, ultra detailed, 4k wallpaper",
36
+ "🏜️",
37
+ ),
38
+ "Cyberpunk City": (
39
+ "A neon-soaked cyberpunk city at night, towering skyscrapers with glowing cyan and magenta lights, "
40
+ "flying cars, rain-slicked streets reflecting neon, blade runner aesthetic, ultra detailed wallpaper",
41
+ "🌃",
42
+ ),
43
+ "Minimal Dark": (
44
+ "Minimalist dark abstract wallpaper, deep black background with subtle geometric cyan lines, "
45
+ "smooth gradients, modern elegant design, 4k",
46
+ "⬛",
47
+ ),
48
+ "Arabic Calligraphy": (
49
+ "Beautiful Arabic calligraphy art wallpaper, elegant flowing script in gold on dark textured background, "
50
+ "intricate decorative borders, luxurious design, 4k",
51
+ "✒️",
52
+ ),
53
+ "Desert Stars": (
54
+ "A serene Arabian desert at night under a sky full of stars, Milky Way galaxy visible, "
55
+ "soft sand dunes in foreground, peaceful cosmic atmosphere, ultra detailed wallpaper",
56
+ "🌌",
57
+ ),
58
+ "Dubai Skyline": (
59
+ "Dubai skyline at golden hour, Burj Khalifa towering above modern skyscrapers, warm sunlight, "
60
+ "palm trees, luxury cars on Sheikh Zayed Road, cinematic, ultra detailed, 4k wallpaper",
61
+ "🏙️",
62
+ ),
63
+ "Ocean Waves": (
64
+ "Tranquil ocean waves wallpaper, deep blue water with white foam, sunlight sparkling on the surface, "
65
+ "minimalist calming aesthetic, top-down view, ultra detailed, 4k",
66
+ "🌊",
67
+ ),
68
+ "Mountain Peak": (
69
+ "Majestic snow-capped mountain peak at dawn, alpine glow, dramatic clouds, pristine landscape, "
70
+ "ultra detailed nature wallpaper, 4k",
71
+ "🏔️",
72
+ ),
73
+ "Abstract Neon": (
74
+ "Abstract neon wallpaper, flowing liquid metal shapes in cyan and purple, dark background, "
75
+ "futuristic 3D render, ultra sharp, 4k",
76
+ "🎨",
77
+ ),
78
+ "Falcon Majesty": (
79
+ "A majestic falcon perched on a rock in the Arabian desert, piercing eyes, detailed feathers, "
80
+ "warm sunset light, ultra detailed wildlife wallpaper, 4k",
81
+ "🦅",
82
+ ),
83
  }
84
 
85
 
86
+ def generate_wallpaper(prompt, aspect, preset, num_steps):
 
87
  """Generate a wallpaper via FLUX.1-schnell."""
 
88
  if preset and preset in PRESETS:
89
+ final_prompt = PRESETS[preset][0]
90
  elif prompt and prompt.strip():
91
  final_prompt = prompt.strip()
92
  else:
93
+ final_prompt = PRESETS["UAE Sunset"][0]
94
 
95
+ w, h = ASPECT_RATIOS.get(aspect, (720, 1280))
96
+ steps = int(num_steps) if num_steps else 4
97
 
98
  try:
 
99
  image = client.text_to_image(
100
  final_prompt,
101
  width=w,
102
  height=h,
103
+ num_inference_steps=steps,
104
  )
 
105
  if not isinstance(image, Image.Image):
106
+ image = Image.open(io.BytesIO(image)) if hasattr(image, "read") else Image.open(image)
107
  return image, "✅ Wallpaper generated successfully!"
108
  except Exception as e:
 
109
  img = Image.new("RGB", (w, h), BG_COLOR)
110
+ d = ImageDraw.Draw(img)
111
+ d.text((w // 4, h // 2), f"Error: {str(e)[:60]}", fill=ACCENT)
112
  return img, f"❌ Error: {str(e)}"
113
 
114
 
115
  def download_image(img):
 
116
  if img is None:
117
  return None
118
  path = os.path.join(os.getcwd(), "wallpaper_output.png")
 
121
 
122
 
123
  def pick_preset_prompt(preset_name):
124
+ return PRESETS.get(preset_name, ("", ""))[0]
 
125
 
126
 
127
  # --- UI -----------------------------------------------------------------------
128
+ CSS = """
129
+ #dispatch-header h1 {
130
+ color: #FFFFFF; font-size: 2.2rem; margin: 0;
131
+ background: linear-gradient(90deg, #1FE0E6 0%, #FFFFFF 60%);
132
+ -webkit-background-clip: text; -webkit-text-fill-color: transparent;
133
+ }
134
+ #dispatch-header p { color: #1FE0E6; font-size: 1.05rem; margin: 6px 0 0 0; }
135
+ .dispatch-footer { text-align: center; color: #8A8F9C; font-size: 0.9rem; padding-top: 8px; }
136
  """
137
 
138
  with gr.Blocks(
139
+ title="Dispatch AI Wallpaper Engine",
140
  theme=gr.themes.Base(
141
  primary_hue="cyan",
142
+ secondary_hue="cyan",
143
  neutral_hue="slate",
144
+ font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui"],
145
+ ).set(
146
+ body_background_fill="#0A0F1A",
147
+ body_background_fill_dark="#0A0F1A",
148
+ body_text_color="#FFFFFF",
149
+ body_text_color_dark="#FFFFFF",
150
+ block_background_fill="#0E1424",
151
+ block_background_fill_dark="#0E1424",
152
+ block_border_color="#1FE0E6",
153
+ block_border_width="1px",
154
+ block_label_text_color="#1FE0E6",
155
+ block_title_text_color="#1FE0E6",
156
+ button_primary_background_fill="#1FE0E6",
157
+ button_primary_background_fill_dark="#1FE0E6",
158
+ button_primary_text_color="#0A0F1A",
159
+ button_primary_border_color="#1FE0E6",
160
+ input_background_fill="#0E1424",
161
+ input_background_fill_dark="#0E1424",
162
+ input_border_color="#1FE0E6",
163
+ input_border_width="1px",
164
  ),
165
+ css=CSS,
166
  ) as demo:
167
+ with gr.Column(elem_id="dispatch-header"):
168
+ gr.Markdown(
169
+ """
170
+ # Dispatch AI Wallpaper Engine
171
+ Generate stunning phone wallpapers with FLUX.1-schnell · Dispatch AI (FZE) · UAE
172
+ """
173
+ )
 
174
 
175
  with gr.Row():
176
  with gr.Column(scale=1):
 
182
  aspect_select = gr.Radio(
183
  list(ASPECT_RATIOS.keys()),
184
  label="Aspect Ratio",
185
+ value="9:16 (Phone Wallpaper)",
186
  )
187
  preset_select = gr.Dropdown(
188
  list(PRESETS.keys()),
 
190
  value=None,
191
  info="Select a preset to auto-fill the prompt",
192
  )
193
+ steps_slider = gr.Slider(
194
+ minimum=1, maximum=8, value=4, step=1,
195
+ label="Inference Steps (schnell works well at 4)",
196
+ )
197
+ with gr.Row():
198
+ load_preset_btn = gr.Button("Load Preset", variant="secondary")
199
+ generate_btn = gr.Button("🎨 Generate Wallpaper", variant="primary")
200
  with gr.Column(scale=2):
201
  output_image = gr.Image(
202
  label="Generated Wallpaper",
 
211
  load_preset_btn.click(pick_preset_prompt, inputs=preset_select, outputs=prompt_input)
212
  generate_btn.click(
213
  generate_wallpaper,
214
+ inputs=[prompt_input, aspect_select, preset_select, steps_slider],
215
  outputs=[output_image, status_box],
216
  )
217
  download_btn.click(download_image, inputs=output_image, outputs=download_file)
218
 
219
+ gr.Examples(
220
+ examples=[[v[0], "9:16 (Phone Wallpaper)", k, 4] for k, v in PRESETS.items()],
221
+ inputs=[prompt_input, aspect_select, preset_select, steps_slider],
222
+ label="Preset Examples — click to load",
223
+ )
224
+
225
  gr.Markdown(
226
  """
227
+ <div class="dispatch-footer">
228
+ © 2026 Dispatch AI (FZE) · UAE · License 10818 · Model: FLUX.1-schnell by Black Forest Labs
229
  </div>
230
  """
231
  )
232
 
233
  if __name__ == "__main__":
234
+ demo.queue()
235
  demo.launch()