Spaces:
Sleeping
Sleeping
File size: 21,474 Bytes
0ae8b34 f151732 0ae8b34 f151732 0ae8b34 f151732 0ae8b34 f151732 0ae8b34 f151732 0ae8b34 f151732 0ae8b34 f151732 0ae8b34 f151732 0ae8b34 f151732 0ae8b34 bb1ef18 fa7a43a bb1ef18 0ae8b34 fa7a43a 0ae8b34 83fecbd f151732 83fecbd 0ae8b34 83fecbd 0ae8b34 f151732 0ae8b34 83fecbd 0ae8b34 f151732 0ae8b34 f151732 0ae8b34 83fecbd f151732 83fecbd 0ae8b34 83fecbd 0ae8b34 83fecbd 0ae8b34 83fecbd bb1ef18 83fecbd 0ae8b34 83fecbd 0ae8b34 83fecbd bb1ef18 83fecbd 0ae8b34 83fecbd f151732 0ae8b34 83fecbd 0ae8b34 f151732 83fecbd 0ae8b34 fa7a43a 0ae8b34 83fecbd 0ae8b34 fa7a43a 0ae8b34 fa7a43a 0ae8b34 fa7a43a f151732 fa7a43a 0ae8b34 f151732 0ae8b34 83fecbd 0ae8b34 fa7a43a 0ae8b34 f151732 0ae8b34 f151732 0ae8b34 f151732 0ae8b34 bb1ef18 0ae8b34 bb1ef18 0ae8b34 f151732 0ae8b34 f151732 0ae8b34 fa7a43a 0ae8b34 f151732 0ae8b34 83fecbd 0ae8b34 fa7a43a 0ae8b34 fa7a43a 0ae8b34 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 |
import base64
import io
from typing import List, Tuple, Optional
import gradio as gr
from PIL import Image
# -----------------------
# OpenAI + Google helpers
# -----------------------
def _get_openai_client(api_key: str):
from openai import OpenAI # local import so app still loads if lib missing
return OpenAI(api_key=api_key)
def _configure_google(api_key: str):
import google.generativeai as genai
genai.configure(api_key=api_key)
return genai
# -----------------------
# Prompt / preset logic
# -----------------------
def apply_preset_to_prompt(
base_prompt: str,
preset: str,
style: str,
content_type: str,
) -> str:
base_prompt = base_prompt.strip()
preset_addons = {
"None": "",
"ZEN Glass Dashboard": (
" ultra-detailed UI, glassmorphism, prismatic alloy panels, "
"neon cyan and magenta HUD overlays, high-end enterprise dashboard"
),
"Palantir / Anduril Infographic": (
" dark enterprise command-center aesthetic, clean vector infographics, "
"military-grade analytics overlays, sharp typography, high contrast, "
"minimal but dense information layout"
),
"Youth AI Literacy Poster": (
" vibrant educational poster for teens, clean icons, diverse students, "
"friendly but serious tone, clear typography, classroom-ready layout"
),
"ZEN AI Arena Card": (
" holographic trading card style, quantum glass edges, subtle glow, "
"sharp logo lockup, futuristic typography, dramatic lighting"
),
"Blueprint / Systems Diagram": (
" technical blueprint, white lines on deep navy background, callout labels, "
"flow arrows, system nodes, engineering drawing style"
),
}
style_addons = {
"Default": "",
"Photoreal": " hyper-realistic photography, physically based lighting",
"Illustration": " clean vector illustration style, flat colors, crisp lines",
"Futuristic UI": " futuristic interface design, HUD, holographic widgets",
"Blueprint": " blueprint drawing, schematic lines, engineering grid",
"Cinematic": " cinematic lighting, dramatic composition, filmic contrast",
}
if content_type == "Image":
ct_addon = " high-resolution concept art,"
elif content_type == "Infographic Spec":
ct_addon = (
" detailed infographic design specification, including layout regions, "
"sections, labels, and visual hierarchy,"
)
else:
ct_addon = ""
extra = " ".join(
x
for x in [
ct_addon,
preset_addons.get(preset, ""),
style_addons.get(style, ""),
]
if x
)
if extra:
if base_prompt:
return f"{base_prompt}, {extra}"
else:
return extra.strip()
return base_prompt or "high quality image"
# -----------------------
# OpenAI text + images
# -----------------------
def generate_text_openai(
api_key: str,
prompt: str,
mode: str,
) -> str:
client = _get_openai_client(api_key)
system_msg = (
"You are an expert creator for the ZEN AI ecosystem. "
"Write clear, concise, high-leverage content. "
"If mode is 'Infographic Spec', output a structured outline with sections, "
"titles, short captions, and suggested visual elements."
)
if mode == "Infographic Spec":
user_prompt = (
"Create a Palantir/Anduril-level infographic specification based on this topic:\n\n"
f"{prompt}\n\n"
"Return:\n"
"1) Title options\n"
"2) 3β5 main sections\n"
"3) Bullet points for each section\n"
"4) Suggested charts/visuals\n"
"5) Color and typography recommendations."
)
else:
user_prompt = prompt
resp = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[
{"role": "system", "content": system_msg},
{"role": "user", "content": user_prompt},
],
temperature=0.7,
)
return resp.choices[0].message.content
def decode_b64_images(b64_list: List[str]) -> List[Image.Image]:
images: List[Image.Image] = []
for b64 in b64_list:
raw = base64.b64decode(b64)
img = Image.open(io.BytesIO(raw)).convert("RGB")
images.append(img)
return images
def generate_image_openai(
api_key: str,
model: str,
prompt: str,
size: str,
quality: str,
n_images: int,
seed: Optional[int],
) -> List[Image.Image]:
client = _get_openai_client(api_key)
size_map = {
"Square (1024x1024)": "1024x1024",
"Portrait (1024x1792)": "1024x1792",
"Landscape (1792x1024)": "1792x1024",
}
size_param = size_map.get(size, "1024x1024")
kwargs = {
"model": model,
"prompt": prompt,
"size": size_param,
"n": n_images,
}
# Allowed values from API: low, medium, high, auto
allowed_qualities = {"low", "medium", "high", "auto"}
if quality in allowed_qualities:
kwargs["quality"] = quality
if seed is not None:
kwargs["seed"] = seed
resp = client.images.generate(**kwargs)
b64_list = [d.b64_json for d in resp.data]
return decode_b64_images(b64_list)
# -----------------------
# Google (Gemini / Nano-Banana)
# -----------------------
def generate_text_google(
api_key: str,
prompt: str,
mode: str,
) -> str:
genai = _configure_google(api_key)
model = genai.GenerativeModel("gemini-1.5-pro")
if mode == "Infographic Spec":
content = (
"You are an expert enterprise communicator. "
"Create a Palantir/Anduril-grade infographic spec.\n\n"
f"Topic / prompt:\n{prompt}\n\n"
"Return:\n"
"1) Title options\n"
"2) Main sections with bullet points\n"
"3) Visual layout ideas\n"
"4) Chart/visualization suggestions\n"
"5) Palette & typography notes."
)
else:
content = prompt
resp = model.generate_content(content)
return resp.text
def generate_image_google(
api_key: str,
google_image_model: str,
prompt: str,
n_images: int,
seed: Optional[int],
) -> List[Image.Image]:
"""
Uses a Google / Gemini image-capable model that returns inline image bytes.
If your Nano-Banana model behaves differently, adjust this function.
"""
genai = _configure_google(api_key)
model = genai.GenerativeModel(google_image_model)
images: List[Image.Image] = []
for i in range(n_images):
generation_config = {}
if seed is not None:
generation_config["seed"] = seed + i
resp = model.generate_content(
prompt,
generation_config=generation_config or None,
)
candidates = getattr(resp, "candidates", []) or []
for cand in candidates:
content = getattr(cand, "content", None)
if not content:
continue
parts = getattr(content, "parts", []) or []
for part in parts:
inline = getattr(part, "inline_data", None)
if inline and getattr(inline, "data", None):
try:
raw = base64.b64decode(inline.data)
img = Image.open(io.BytesIO(raw)).convert("RGB")
images.append(img)
except Exception:
continue
return images
# -----------------------
# Core callback with provider fallback
# -----------------------
def run_generation(
openai_key: str,
google_key: str,
task_type: str,
provider: str,
base_prompt: str,
negative_prompt: str,
preset: str,
style: str,
size: str,
quality: str,
n_images: int,
seed: int,
use_seed: bool,
google_image_model: str,
google_text_model_hint: str, # currently just logged
) -> Tuple[str, List[Image.Image], str]:
text_output = ""
images: List[Image.Image] = []
debug_lines = []
if not base_prompt.strip():
return "Please enter a prompt.", [], "No prompt provided."
content_type = "Image" if task_type == "Image" else task_type
full_prompt = apply_preset_to_prompt(
base_prompt=base_prompt,
preset=preset,
style=style,
content_type=content_type,
)
if negative_prompt.strip():
full_prompt += f". Avoid: {negative_prompt.strip()}"
debug_lines.append(f"Task: {task_type}")
debug_lines.append(f"Provider selected: {provider}")
debug_lines.append(f"Preset: {preset}, Style: {style}")
debug_lines.append(f"OpenAI size: {size}, quality: {quality}")
debug_lines.append(f"Google image model: {google_image_model}")
debug_lines.append(f"Google text model hint: {google_text_model_hint}")
debug_lines.append(f"Seed enabled: {use_seed}, seed: {seed if use_seed else 'None'}")
seed_val: Optional[int] = seed if use_seed else None
try:
# TEXT / INFOGRAPHIC
if task_type in ["Text", "Infographic Spec"]:
if provider == "OpenAI":
if not openai_key.strip():
return "Missing OpenAI API key.", [], "OpenAI key not provided."
text_output = generate_text_openai(
api_key=openai_key.strip(),
prompt=full_prompt,
mode=task_type,
)
else:
if not google_key.strip():
return "Missing Google API key.", [], "Google key not provided."
text_output = generate_text_google(
api_key=google_key.strip(),
prompt=full_prompt,
mode=task_type,
)
# IMAGE
if task_type == "Image":
primary = provider
secondary = "OpenAI" if provider.startswith("Google") else "Google"
# Helper to attempt OpenAI
def try_openai() -> Tuple[List[Image.Image], str]:
if not openai_key.strip():
raise ValueError("OpenAI key missing for OpenAI image generation.")
image_model = "gpt-image-1"
if "Palantir" in preset:
image_model = "dall-e-3"
imgs = generate_image_openai(
api_key=openai_key.strip(),
model=image_model,
prompt=full_prompt,
size=size,
quality=quality,
n_images=n_images,
seed=seed_val,
)
return imgs, image_model
# Helper to attempt Google
def try_google() -> List[Image.Image]:
if not google_key.strip():
raise ValueError("Google key missing for Google image generation.")
model_id = google_image_model.strip() or "gemini-1.5-flash"
return generate_image_google(
api_key=google_key.strip(),
google_image_model=model_id,
prompt=full_prompt,
n_images=n_images,
seed=seed_val,
)
image_model_used = None
try:
if primary == "OpenAI":
images, image_model_used = try_openai()
else: # Google primary
images = try_google()
except Exception as e_primary:
debug_lines.append(f"Primary provider {primary} error: {e_primary}")
# Fallback if possible
try:
if secondary == "OpenAI":
images, image_model_used = try_openai()
else:
images = try_google()
debug_lines.append(f"Fallback provider {secondary} succeeded.")
except Exception as e_secondary:
debug_lines.append(f"Fallback provider {secondary} error: {e_secondary}")
raise RuntimeError(
f"Both providers failed. Primary: {e_primary} | Secondary: {e_secondary}"
)
if image_model_used:
debug_lines.append(f"OpenAI image model used: {image_model_used}")
if not text_output and task_type == "Image":
text_output = (
"Image(s) generated. Use Text or Infographic Spec mode to "
"generate captions, copy, or layout specs."
)
if task_type == "Image" and not images:
debug_lines.append("No images returned from any provider.")
return text_output, images, "\n".join(debug_lines)
except Exception as e:
debug_lines.append(f"Exception: {e}")
return f"Error during generation: {e}", [], "\n".join(debug_lines)
# -----------------------
# Starter prompts helper
# -----------------------
STARTER_PROMPTS = {
"None": "",
"ZEN Glass Arena Card": (
"ZEN AI Arena holographic credential card showcasing a youth AI pioneer, "
"glassmorphism border, quantum prism edges, subtle neon glow, "
"nameplate and role, dark control-room background"
),
"AI Pioneer Infographic": (
"Infographic showing the AI Pioneer Program journey from idea to deployment, "
"timeline of modules, icons for coding, Hugging Face Spaces, and blockchain credentials, "
"Palantir-style layout with three main columns"
),
"Youth AI Literacy Poster": (
"Poster inviting teens to join the AI Pioneer Program, diverse students, laptops, "
"cloud-hosted AI agents floating as holograms, bold headline and simple CTA, "
"modern but serious aesthetic"
),
"Vanguard Systems Blueprint": (
"Blueprint diagram of the ZEN ecosystem: AI Pioneer Program, ZEN Arena, "
"blockchain credentials, ZEN dashboards, arrows showing data flow and automations, "
"technical engineering style"
),
"Instructor Training Card": (
"Training card for ZEN Vanguard instructors with modules listed, clean UI, "
"minimal layout, white card on dark background, subtle gradient border, "
"space for QR code and URL"
),
}
def load_starter_prompt(choice: str) -> str:
return STARTER_PROMPTS.get(choice, "")
def clear_outputs():
return "", [], ""
# -----------------------
# UI
# -----------------------
with gr.Blocks() as demo:
gr.Markdown(
"""
# 𧬠ZEN Module 2 Section 2.11 β Omni Studio
A multi-provider creator used in the **ZEN Vanguard Program**.
- π Bring your own **OpenAI** and **Google (Gemini / Nano-Banana)** keys
- π¨ Generate **images** with presets + fine-grained controls
- π§ Generate **text** and **infographic specs** for ZEN dashboards, cards, and posters
"""
)
with gr.Row():
with gr.Column():
gr.Markdown("### π API Keys (local to this session)")
openai_key = gr.Textbox(
label="OPENAI_API_KEY",
type="password",
placeholder="sk-...",
)
google_key = gr.Textbox(
label="GOOGLE_API_KEY (Gemini / Nano-Banana)",
type="password",
placeholder="AIza...",
)
gr.Markdown("### π― Task & Provider")
task_type = gr.Radio(
["Image", "Text", "Infographic Spec"],
value="Image",
label="Task Type",
)
provider = gr.Radio(
["OpenAI", "Google (Nano-Banana / Gemini)"],
value="OpenAI",
label="Primary Provider",
)
with gr.Accordion("Starter Prompts (ZEN Vanguard)", open=False):
starter_choice = gr.Dropdown(
list(STARTER_PROMPTS.keys()),
value="None",
label="Choose a starter prompt",
)
load_prompt_btn = gr.Button("Load Starter Prompt")
gr.Markdown(
"""
Use starter prompts to quickly explore:
- **ZEN Glass Arena Card** β holographic card-style image
- **AI Pioneer Infographic** β program journey and outcomes
- **Youth AI Literacy Poster** β outreach poster for teens
- **Vanguard Systems Blueprint** β systems-thinking diagram
- **Instructor Training Card** β card UI for trainers
"""
)
base_prompt = gr.Textbox(
label="Main Prompt",
lines=5,
placeholder="Describe the ZEN image, text, or infographic you want.",
)
negative_prompt = gr.Textbox(
label="Negative Prompt (optional)",
lines=2,
placeholder="Things to avoid: low-res, clutter, warped text, etc.",
)
with gr.Row():
preset = gr.Dropdown(
[
"None",
"ZEN Glass Dashboard",
"Palantir / Anduril Infographic",
"Youth AI Literacy Poster",
"ZEN AI Arena Card",
"Blueprint / Systems Diagram",
],
value="ZEN Glass Dashboard",
label="Visual Preset",
)
style = gr.Dropdown(
[
"Default",
"Photoreal",
"Illustration",
"Futuristic UI",
"Blueprint",
"Cinematic",
],
value="Futuristic UI",
label="Style Accent",
)
gr.Markdown("### π OpenAI Image Controls")
with gr.Row():
size = gr.Dropdown(
[
"Square (1024x1024)",
"Portrait (1024x1792)",
"Landscape (1792x1024)",
],
value="Square (1024x1024)",
label="Aspect Ratio / Size",
)
quality = gr.Dropdown(
["auto", "low", "medium", "high"],
value="high",
label="Quality (OpenAI)",
)
n_images = gr.Slider(
minimum=1,
maximum=4,
value=1,
step=1,
label="Number of Images",
)
with gr.Row():
use_seed = gr.Checkbox(
value=False,
label="Lock Seed (repeatable outputs)",
)
seed = gr.Slider(
minimum=1,
maximum=2**31 - 1,
value=12345,
step=1,
label="Seed",
)
gr.Markdown("### π§ͺ Google Image / Text Model Hints")
google_image_model = gr.Textbox(
label="Google Image Model (default: gemini-1.5-flash)",
value="gemini-1.5-flash",
placeholder="e.g. your Nano-Banana model id or another image-capable model",
)
google_text_model_hint = gr.Textbox(
label="Google Text Model Hint",
value="gemini-1.5-pro",
placeholder="Used internally as default text model.",
)
with gr.Row():
generate_btn = gr.Button("π Generate", variant="primary")
clear_btn = gr.Button("Clear Outputs")
with gr.Column():
gr.Markdown("### π Text / Spec Output")
text_output = gr.Markdown()
gr.Markdown("### πΌ Image Output")
image_gallery = gr.Gallery(
show_label=False,
columns=2,
height=500,
)
gr.Markdown("### π§Ύ Debug / Logs")
debug_output = gr.Textbox(
label="Debug Info",
lines=12,
)
# Wire up callbacks
generate_btn.click(
fn=run_generation,
inputs=[
openai_key,
google_key,
task_type,
provider,
base_prompt,
negative_prompt,
preset,
style,
size,
quality,
n_images,
seed,
use_seed,
google_image_model,
google_text_model_hint,
],
outputs=[text_output, image_gallery, debug_output],
)
load_prompt_btn.click(
fn=load_starter_prompt,
inputs=[starter_choice],
outputs=[base_prompt],
)
clear_btn.click(
fn=clear_outputs,
inputs=[],
outputs=[text_output, image_gallery, debug_output],
)
if __name__ == "__main__":
demo.launch()
|