feat: initial commit of WMOC CEO portfolio with cruise ship parallax and seafarer document compliance platform
This commit is contained in:
175
.agents/skills/design/scripts/logo/core.py
Normal file
175
.agents/skills/design/scripts/logo/core.py
Normal file
@@ -0,0 +1,175 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Logo Design Core - BM25 search engine for logo design guidelines
|
||||
"""
|
||||
|
||||
import csv
|
||||
import re
|
||||
from pathlib import Path
|
||||
from math import log
|
||||
from collections import defaultdict
|
||||
|
||||
# ============ CONFIGURATION ============
|
||||
DATA_DIR = Path(__file__).parent.parent.parent / "data" / "logo"
|
||||
MAX_RESULTS = 3
|
||||
|
||||
CSV_CONFIG = {
|
||||
"style": {
|
||||
"file": "styles.csv",
|
||||
"search_cols": ["Style Name", "Category", "Keywords", "Best For"],
|
||||
"output_cols": ["Style Name", "Category", "Keywords", "Primary Colors", "Secondary Colors", "Typography", "Effects", "Best For", "Avoid For", "Complexity", "Era"]
|
||||
},
|
||||
"color": {
|
||||
"file": "colors.csv",
|
||||
"search_cols": ["Palette Name", "Category", "Keywords", "Psychology", "Best For"],
|
||||
"output_cols": ["Palette Name", "Category", "Keywords", "Primary Hex", "Secondary Hex", "Accent Hex", "Background Hex", "Text Hex", "Psychology", "Best For", "Avoid For"]
|
||||
},
|
||||
"industry": {
|
||||
"file": "industries.csv",
|
||||
"search_cols": ["Industry", "Keywords", "Recommended Styles", "Mood"],
|
||||
"output_cols": ["Industry", "Keywords", "Recommended Styles", "Primary Colors", "Typography", "Common Symbols", "Mood", "Best Practices", "Avoid"]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# ============ BM25 IMPLEMENTATION ============
|
||||
class BM25:
|
||||
"""BM25 ranking algorithm for text search"""
|
||||
|
||||
def __init__(self, k1=1.5, b=0.75):
|
||||
self.k1 = k1
|
||||
self.b = b
|
||||
self.corpus = []
|
||||
self.doc_lengths = []
|
||||
self.avgdl = 0
|
||||
self.idf = {}
|
||||
self.doc_freqs = defaultdict(int)
|
||||
self.N = 0
|
||||
|
||||
def tokenize(self, text):
|
||||
"""Lowercase, split, remove punctuation, filter short words"""
|
||||
text = re.sub(r'[^\w\s]', ' ', str(text).lower())
|
||||
return [w for w in text.split() if len(w) > 2]
|
||||
|
||||
def fit(self, documents):
|
||||
"""Build BM25 index from documents"""
|
||||
self.corpus = [self.tokenize(doc) for doc in documents]
|
||||
self.N = len(self.corpus)
|
||||
if self.N == 0:
|
||||
return
|
||||
self.doc_lengths = [len(doc) for doc in self.corpus]
|
||||
self.avgdl = sum(self.doc_lengths) / self.N
|
||||
|
||||
for doc in self.corpus:
|
||||
seen = set()
|
||||
for word in doc:
|
||||
if word not in seen:
|
||||
self.doc_freqs[word] += 1
|
||||
seen.add(word)
|
||||
|
||||
for word, freq in self.doc_freqs.items():
|
||||
self.idf[word] = log((self.N - freq + 0.5) / (freq + 0.5) + 1)
|
||||
|
||||
def score(self, query):
|
||||
"""Score all documents against query"""
|
||||
query_tokens = self.tokenize(query)
|
||||
scores = []
|
||||
|
||||
for idx, doc in enumerate(self.corpus):
|
||||
score = 0
|
||||
doc_len = self.doc_lengths[idx]
|
||||
term_freqs = defaultdict(int)
|
||||
for word in doc:
|
||||
term_freqs[word] += 1
|
||||
|
||||
for token in query_tokens:
|
||||
if token in self.idf:
|
||||
tf = term_freqs[token]
|
||||
idf = self.idf[token]
|
||||
numerator = tf * (self.k1 + 1)
|
||||
denominator = tf + self.k1 * (1 - self.b + self.b * doc_len / self.avgdl)
|
||||
score += idf * numerator / denominator
|
||||
|
||||
scores.append((idx, score))
|
||||
|
||||
return sorted(scores, key=lambda x: x[1], reverse=True)
|
||||
|
||||
|
||||
# ============ SEARCH FUNCTIONS ============
|
||||
def _load_csv(filepath):
|
||||
"""Load CSV and return list of dicts"""
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
return list(csv.DictReader(f))
|
||||
|
||||
|
||||
def _search_csv(filepath, search_cols, output_cols, query, max_results):
|
||||
"""Core search function using BM25"""
|
||||
if not filepath.exists():
|
||||
return []
|
||||
|
||||
data = _load_csv(filepath)
|
||||
|
||||
# Build documents from search columns
|
||||
documents = [" ".join(str(row.get(col, "")) for col in search_cols) for row in data]
|
||||
|
||||
# BM25 search
|
||||
bm25 = BM25()
|
||||
bm25.fit(documents)
|
||||
ranked = bm25.score(query)
|
||||
|
||||
# Get top results with score > 0
|
||||
results = []
|
||||
for idx, score in ranked[:max_results]:
|
||||
if score > 0:
|
||||
row = data[idx]
|
||||
results.append({col: row.get(col, "") for col in output_cols if col in row})
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def detect_domain(query):
|
||||
"""Auto-detect the most relevant domain from query"""
|
||||
query_lower = query.lower()
|
||||
|
||||
domain_keywords = {
|
||||
"style": ["style", "minimalist", "vintage", "modern", "retro", "geometric", "abstract", "emblem", "badge", "wordmark", "mascot", "luxury", "playful", "corporate"],
|
||||
"color": ["color", "palette", "hex", "#", "rgb", "blue", "red", "green", "gold", "warm", "cool", "vibrant", "pastel"],
|
||||
"industry": ["tech", "healthcare", "finance", "legal", "restaurant", "food", "fashion", "beauty", "education", "sports", "fitness", "real estate", "crypto", "gaming"]
|
||||
}
|
||||
|
||||
scores = {domain: sum(1 for kw in keywords if kw in query_lower) for domain, keywords in domain_keywords.items()}
|
||||
best = max(scores, key=scores.get)
|
||||
return best if scores[best] > 0 else "style"
|
||||
|
||||
|
||||
def search(query, domain=None, max_results=MAX_RESULTS):
|
||||
"""Main search function with auto-domain detection"""
|
||||
if domain is None:
|
||||
domain = detect_domain(query)
|
||||
|
||||
config = CSV_CONFIG.get(domain, CSV_CONFIG["style"])
|
||||
filepath = DATA_DIR / config["file"]
|
||||
|
||||
if not filepath.exists():
|
||||
return {"error": f"File not found: {filepath}", "domain": domain}
|
||||
|
||||
results = _search_csv(filepath, config["search_cols"], config["output_cols"], query, max_results)
|
||||
|
||||
return {
|
||||
"domain": domain,
|
||||
"query": query,
|
||||
"file": config["file"],
|
||||
"count": len(results),
|
||||
"results": results
|
||||
}
|
||||
|
||||
|
||||
def search_all(query, max_results=2):
|
||||
"""Search across all domains and combine results"""
|
||||
all_results = {}
|
||||
for domain in CSV_CONFIG.keys():
|
||||
result = search(query, domain, max_results)
|
||||
if result.get("results"):
|
||||
all_results[domain] = result["results"]
|
||||
return all_results
|
||||
769
.agents/skills/design/scripts/logo/generate.py
Normal file
769
.agents/skills/design/scripts/logo/generate.py
Normal file
@@ -0,0 +1,769 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Logo generation with Gemini, Atlas Cloud, or MuAPI.
|
||||
|
||||
Gemini remains the default provider. Atlas Cloud is opt-in with
|
||||
``--provider atlas`` and uses its asynchronous image generation API. MuAPI is
|
||||
opt-in with ``--provider muapi`` and uses its asynchronous image generation API
|
||||
with the selected model's prompt/aspect-ratio contract.
|
||||
|
||||
Models:
|
||||
- Nano Banana (default): gemini-2.5-flash-image - fast, high-volume, low-latency
|
||||
- Nano Banana Pro (--pro): gemini-3-pro-image-preview - professional quality, advanced reasoning
|
||||
- MuAPI Nano Banana (--provider muapi): nano-banana - hosted asynchronous image generation
|
||||
|
||||
Usage:
|
||||
python generate.py --prompt "tech startup logo minimalist blue"
|
||||
python generate.py --prompt "coffee shop vintage badge" --style vintage --output logo.png
|
||||
python generate.py --brand "TechFlow" --industry tech --style minimalist
|
||||
python generate.py --brand "TechFlow" --pro # Use Nano Banana Pro model
|
||||
python generate.py --brand "TechFlow" --provider atlas
|
||||
python generate.py --brand "TechFlow" --provider muapi
|
||||
python generate.py --brand "TechFlow" --provider muapi --muapi-model nano-banana-pro
|
||||
|
||||
Batch mode (generates multiple variants):
|
||||
python generate.py --brand "Unikorn" --batch 9 --output-dir ./logos --pro
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import ipaddress
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.parse import urlparse
|
||||
from urllib.request import HTTPRedirectHandler, Request, build_opener
|
||||
|
||||
|
||||
# Load environment variables
|
||||
def load_env():
|
||||
"""Load .env files in priority order"""
|
||||
env_paths = [
|
||||
Path(__file__).parent.parent.parent / ".env",
|
||||
Path.home() / ".claude" / "skills" / ".env",
|
||||
Path.home() / ".claude" / ".env",
|
||||
]
|
||||
|
||||
for env_path in env_paths:
|
||||
if env_path.exists():
|
||||
with open(env_path) as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line and not line.startswith("#") and "=" in line:
|
||||
key, value = line.split("=", 1)
|
||||
if key not in os.environ:
|
||||
os.environ[key] = value.strip("\"'")
|
||||
|
||||
|
||||
load_env()
|
||||
|
||||
|
||||
# ============ CONFIGURATION ============
|
||||
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
||||
ATLASCLOUD_API_KEY = os.environ.get("ATLASCLOUD_API_KEY")
|
||||
MUAPI_API_KEY = os.environ.get("MUAPI_API_KEY")
|
||||
|
||||
# Gemini "Nano Banana" model configurations for image generation
|
||||
GEMINI_FLASH = "gemini-2.5-flash-image" # Nano Banana: fast, high-volume, low-latency
|
||||
GEMINI_PRO = "gemini-3-pro-image-preview" # Nano Banana Pro: professional quality, advanced reasoning
|
||||
|
||||
# Atlas Cloud model validated against the live model catalog and schema.
|
||||
ATLAS_MODEL = "google/nano-banana-2-lite/text-to-image"
|
||||
ATLAS_API_BASE = "https://api.atlascloud.ai/api/v1"
|
||||
MUAPI_MODEL = "nano-banana"
|
||||
MUAPI_MODELS = ("nano-banana", "nano-banana-pro")
|
||||
MUAPI_API_BASE = "https://api.muapi.ai/api/v1"
|
||||
HTTP_USER_AGENT = "ui-ux-pro-max/2.5 (logo generation)"
|
||||
ATLAS_POLL_INTERVAL = 2
|
||||
ATLAS_MAX_POLLS = 90
|
||||
MUAPI_POLL_INTERVAL = 2
|
||||
MUAPI_MAX_POLLS = 90
|
||||
|
||||
# Supported aspect ratios
|
||||
ASPECT_RATIOS = ["1:1", "16:9", "9:16", "4:3", "3:4"]
|
||||
DEFAULT_ASPECT_RATIO = "1:1" # Square is ideal for logos
|
||||
|
||||
# Logo-specific prompt templates
|
||||
LOGO_PROMPT_TEMPLATE = """Generate a professional logo image: {prompt}
|
||||
|
||||
Style requirements:
|
||||
- Clean vector-style illustration suitable for a logo
|
||||
- Simple, scalable design that works at any size
|
||||
- Clear silhouette and recognizable shape
|
||||
- Professional quality suitable for business use
|
||||
- Centered composition on plain white or transparent background
|
||||
- No text unless specifically requested
|
||||
- High contrast and clear edges
|
||||
- Square format, perfectly centered
|
||||
- Output as a clean, high-quality logo image
|
||||
"""
|
||||
|
||||
STYLE_MODIFIERS = {
|
||||
"minimalist": "minimalist, simple geometric shapes, clean lines, lots of white space, single color or limited palette",
|
||||
"vintage": "vintage, retro, badge style, distressed texture, heritage feel, warm earth tones",
|
||||
"modern": "modern, sleek, gradient colors, tech-forward, innovative feel",
|
||||
"luxury": "luxury, elegant, gold accents, refined, premium feel, serif typography",
|
||||
"playful": "playful, fun, colorful, friendly, approachable, rounded shapes",
|
||||
"corporate": "corporate, professional, trustworthy, stable, conservative colors",
|
||||
"organic": "organic, natural, flowing lines, earth tones, sustainable feel",
|
||||
"geometric": "geometric, abstract, mathematical precision, symmetrical",
|
||||
"hand-drawn": "hand-drawn, artisan, sketch-like, authentic, imperfect lines",
|
||||
"3d": "3D, dimensional, depth, shadows, isometric perspective",
|
||||
"abstract": "abstract mark, conceptual, symbolic, non-literal representation, artistic interpretation",
|
||||
"lettermark": "lettermark, single letter or initials, typographic, monogram style, distinctive character",
|
||||
"wordmark": "wordmark, logotype, custom typography, brand name as logo, distinctive lettering",
|
||||
"emblem": "emblem, badge, crest style, enclosed design, traditional, authoritative feel",
|
||||
"mascot": "mascot, character, friendly face, personified, memorable figure",
|
||||
"gradient": "gradient, color transition, vibrant, modern digital feel, smooth color flow",
|
||||
"lineart": "line art, single stroke, continuous line, elegant simplicity, wire-frame style",
|
||||
"negative-space": "negative space, clever use of white space, hidden meaning, dual imagery, optical illusion",
|
||||
}
|
||||
|
||||
INDUSTRY_PROMPTS = {
|
||||
"tech": "technology company, digital, innovative, modern, circuit-like elements",
|
||||
"healthcare": "healthcare, medical, caring, trust, cross or heart symbol",
|
||||
"finance": "financial services, stable, trustworthy, growth, upward elements",
|
||||
"food": "food and beverage, appetizing, warm colors, welcoming",
|
||||
"fashion": "fashion brand, elegant, stylish, refined, artistic",
|
||||
"fitness": "fitness and sports, dynamic, energetic, powerful, movement",
|
||||
"eco": "eco-friendly, sustainable, natural, green, leaf or earth elements",
|
||||
"education": "education, knowledge, growth, learning, book or cap symbol",
|
||||
"real-estate": "real estate, property, home, roof or building silhouette",
|
||||
"creative": "creative agency, artistic, unique, expressive, colorful",
|
||||
}
|
||||
|
||||
|
||||
def enhance_prompt(base_prompt, style=None, industry=None, brand_name=None):
|
||||
"""Enhance the logo prompt with style and industry modifiers"""
|
||||
prompt_parts = [base_prompt]
|
||||
|
||||
if style and style in STYLE_MODIFIERS:
|
||||
prompt_parts.append(STYLE_MODIFIERS[style])
|
||||
|
||||
if industry and industry in INDUSTRY_PROMPTS:
|
||||
prompt_parts.append(INDUSTRY_PROMPTS[industry])
|
||||
|
||||
if brand_name:
|
||||
prompt_parts.insert(0, f"Logo for '{brand_name}':")
|
||||
|
||||
combined = ", ".join(prompt_parts)
|
||||
return LOGO_PROMPT_TEMPLATE.format(prompt=combined)
|
||||
|
||||
|
||||
class _SafeRedirectHandler(HTTPRedirectHandler):
|
||||
"""Reject redirects to non-public or non-HTTPS destinations."""
|
||||
|
||||
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
||||
_validate_public_https_url(newurl)
|
||||
return super().redirect_request(req, fp, code, msg, headers, newurl)
|
||||
|
||||
|
||||
def _validate_public_https_url(url):
|
||||
parsed = urlparse(url)
|
||||
if (
|
||||
parsed.scheme != "https"
|
||||
or not parsed.hostname
|
||||
or parsed.username
|
||||
or parsed.password
|
||||
):
|
||||
raise ValueError("Provider returned an invalid media URL")
|
||||
|
||||
hostname = parsed.hostname.lower().rstrip(".")
|
||||
if hostname == "localhost" or hostname.endswith(
|
||||
(".localhost", ".local", ".internal")
|
||||
):
|
||||
raise ValueError("Provider media URL used a local hostname")
|
||||
|
||||
try:
|
||||
ip = ipaddress.ip_address(hostname)
|
||||
except ValueError:
|
||||
return
|
||||
else:
|
||||
if not ip.is_global:
|
||||
raise ValueError("Provider media URL used a non-public address")
|
||||
|
||||
|
||||
def _json_request(
|
||||
url, api_key, method="GET", payload=None, api_key_header="Authorization"
|
||||
):
|
||||
if api_key_header == "Authorization":
|
||||
auth_value = f"Bearer {api_key}"
|
||||
elif api_key_header == "x-api-key":
|
||||
auth_value = api_key
|
||||
else:
|
||||
raise ValueError("Unsupported API key header")
|
||||
|
||||
body = json.dumps(payload).encode("utf-8") if payload is not None else None
|
||||
request = Request(
|
||||
url,
|
||||
data=body,
|
||||
method=method,
|
||||
headers={
|
||||
api_key_header: auth_value,
|
||||
"Accept": "application/json",
|
||||
"User-Agent": HTTP_USER_AGENT,
|
||||
**({"Content-Type": "application/json"} if body is not None else {}),
|
||||
},
|
||||
)
|
||||
try:
|
||||
with build_opener(_SafeRedirectHandler()).open(request, timeout=60) as response:
|
||||
return json.loads(response.read().decode("utf-8"))
|
||||
except HTTPError as exc:
|
||||
detail = exc.read().decode("utf-8", errors="replace")
|
||||
raise RuntimeError(
|
||||
f"Provider request failed ({exc.code}): {detail[:300]}"
|
||||
) from exc
|
||||
except (URLError, TimeoutError, json.JSONDecodeError) as exc:
|
||||
raise RuntimeError(f"Provider request failed: {exc}") from exc
|
||||
|
||||
|
||||
def _atlas_prediction_data(response):
|
||||
if not isinstance(response, dict):
|
||||
raise TypeError("Atlas Cloud returned an invalid response")
|
||||
if response.get("code") not in (None, 0, 200):
|
||||
raise RuntimeError(response.get("message") or "Atlas Cloud request failed")
|
||||
data = response.get("data")
|
||||
if not isinstance(data, dict):
|
||||
raise TypeError("Atlas Cloud response did not include prediction data")
|
||||
return data
|
||||
|
||||
|
||||
def _download_atlas_image(url, output_path):
|
||||
_download_image(url, output_path, "image provider")
|
||||
|
||||
|
||||
def _download_image(url, output_path, provider_name):
|
||||
_validate_public_https_url(url)
|
||||
request = Request(
|
||||
url,
|
||||
headers={"Accept": "image/*", "User-Agent": HTTP_USER_AGENT},
|
||||
)
|
||||
try:
|
||||
with build_opener(_SafeRedirectHandler()).open(
|
||||
request, timeout=120
|
||||
) as response:
|
||||
content_type = response.headers.get_content_type()
|
||||
if not content_type.startswith("image/"):
|
||||
raise RuntimeError(
|
||||
f"{provider_name} output is not an image ({content_type})"
|
||||
)
|
||||
image_data = response.read()
|
||||
except (HTTPError, URLError, TimeoutError) as exc:
|
||||
raise RuntimeError(f"Unable to download {provider_name} image: {exc}") from exc
|
||||
|
||||
if not image_data:
|
||||
raise RuntimeError(f"{provider_name} returned an empty image")
|
||||
with open(output_path, "wb") as output_file:
|
||||
output_file.write(image_data)
|
||||
|
||||
|
||||
def _generate_with_atlas(prompt, output_path, aspect_ratio, api_key, model):
|
||||
if not api_key:
|
||||
raise RuntimeError("ATLASCLOUD_API_KEY not set")
|
||||
|
||||
payload = {
|
||||
"model": model,
|
||||
"prompt": prompt,
|
||||
"aspect_ratio": aspect_ratio,
|
||||
}
|
||||
response = _json_request(
|
||||
f"{ATLAS_API_BASE}/model/generateImage",
|
||||
api_key,
|
||||
method="POST",
|
||||
payload=payload,
|
||||
)
|
||||
data = _atlas_prediction_data(response)
|
||||
prediction_id = data.get("id")
|
||||
if not prediction_id:
|
||||
raise RuntimeError("Atlas Cloud did not return a prediction ID")
|
||||
|
||||
for poll_number in range(ATLAS_MAX_POLLS + 1):
|
||||
status = str(data.get("status", "")).lower()
|
||||
if status == "completed":
|
||||
outputs = data.get("outputs")
|
||||
if (
|
||||
not isinstance(outputs, list)
|
||||
or not outputs
|
||||
or not isinstance(outputs[0], str)
|
||||
):
|
||||
raise RuntimeError("Atlas Cloud completed without an image URL")
|
||||
_download_atlas_image(outputs[0], output_path)
|
||||
return
|
||||
if status in {"failed", "timeout", "canceled", "cancelled"}:
|
||||
raise RuntimeError(data.get("error") or f"Atlas Cloud prediction {status}")
|
||||
if poll_number == ATLAS_MAX_POLLS:
|
||||
break
|
||||
time.sleep(ATLAS_POLL_INTERVAL)
|
||||
data = _atlas_prediction_data(
|
||||
_json_request(
|
||||
f"{ATLAS_API_BASE}/model/prediction/{prediction_id}",
|
||||
api_key,
|
||||
)
|
||||
)
|
||||
|
||||
raise RuntimeError("Atlas Cloud prediction timed out while polling")
|
||||
|
||||
|
||||
def _muapi_response_objects(response):
|
||||
"""Return the response and common MuAPI envelopes without guessing fields."""
|
||||
if not isinstance(response, dict):
|
||||
raise TypeError("MuAPI returned an invalid response")
|
||||
|
||||
objects = [response]
|
||||
for key in ("data", "output", "result"):
|
||||
value = response.get(key)
|
||||
if isinstance(value, dict) and value not in objects:
|
||||
objects.append(value)
|
||||
return objects
|
||||
|
||||
|
||||
def _muapi_response_value(response, keys):
|
||||
for item in _muapi_response_objects(response):
|
||||
for key in keys:
|
||||
value = item.get(key)
|
||||
if value not in (None, ""):
|
||||
return value
|
||||
return None
|
||||
|
||||
|
||||
def _muapi_error(response):
|
||||
value = _muapi_response_value(response, ("error", "message", "detail"))
|
||||
if isinstance(value, str):
|
||||
return value[:300]
|
||||
return "MuAPI request failed"
|
||||
|
||||
|
||||
def _muapi_result_url(response):
|
||||
"""Return the documented result URL from the creation response."""
|
||||
for item in _muapi_response_objects(response):
|
||||
urls = item.get("urls")
|
||||
if not isinstance(urls, dict) or "get" not in urls:
|
||||
continue
|
||||
|
||||
result_url = urls.get("get")
|
||||
if not isinstance(result_url, str) or not result_url:
|
||||
raise RuntimeError(
|
||||
"MuAPI creation response did not include a valid HTTPS result URL"
|
||||
)
|
||||
try:
|
||||
_validate_public_https_url(result_url)
|
||||
except ValueError as exc:
|
||||
raise RuntimeError(
|
||||
"MuAPI creation response did not include a valid HTTPS result URL"
|
||||
) from exc
|
||||
return result_url
|
||||
|
||||
raise RuntimeError(
|
||||
"MuAPI creation response did not include a valid HTTPS result URL"
|
||||
)
|
||||
|
||||
|
||||
def _muapi_output_url(response):
|
||||
for item in _muapi_response_objects(response):
|
||||
outputs = item.get("outputs")
|
||||
if isinstance(outputs, list):
|
||||
for output in outputs:
|
||||
if isinstance(output, str) and output.startswith("https://"):
|
||||
return output
|
||||
if isinstance(output, dict):
|
||||
for key in ("url", "image_url"):
|
||||
value = output.get(key)
|
||||
if isinstance(value, str) and value.startswith("https://"):
|
||||
return value
|
||||
raise RuntimeError("MuAPI completed without an HTTPS image URL")
|
||||
|
||||
|
||||
def _download_muapi_image(url, output_path):
|
||||
_download_image(url, output_path, "MuAPI")
|
||||
|
||||
|
||||
def _generate_with_muapi(prompt, output_path, aspect_ratio, api_key, model):
|
||||
if not api_key:
|
||||
raise RuntimeError("MUAPI_API_KEY not set")
|
||||
if model not in MUAPI_MODELS:
|
||||
raise RuntimeError(
|
||||
f"Unsupported MuAPI logo model: {model}. "
|
||||
f"Choose one of: {', '.join(MUAPI_MODELS)}"
|
||||
)
|
||||
|
||||
payload = {
|
||||
"prompt": prompt,
|
||||
"aspect_ratio": aspect_ratio,
|
||||
}
|
||||
response = _json_request(
|
||||
f"{MUAPI_API_BASE}/{model}",
|
||||
api_key,
|
||||
method="POST",
|
||||
payload=payload,
|
||||
api_key_header="x-api-key",
|
||||
)
|
||||
request_id = _muapi_response_value(response, ("request_id", "id"))
|
||||
if not isinstance(request_id, str) or not request_id:
|
||||
raise RuntimeError("MuAPI did not return a request ID")
|
||||
result_url = _muapi_result_url(response)
|
||||
|
||||
data = response
|
||||
for poll_number in range(MUAPI_MAX_POLLS + 1):
|
||||
status = _muapi_response_value(data, ("status",))
|
||||
normalized_status = str(status or "").lower()
|
||||
if normalized_status in {"completed", "succeeded", "success"}:
|
||||
_download_muapi_image(_muapi_output_url(data), output_path)
|
||||
return
|
||||
if normalized_status in {
|
||||
"failed",
|
||||
"error",
|
||||
"timeout",
|
||||
"canceled",
|
||||
"cancelled",
|
||||
}:
|
||||
raise RuntimeError(f"MuAPI generation {normalized_status}: {_muapi_error(data)}")
|
||||
if poll_number == MUAPI_MAX_POLLS:
|
||||
break
|
||||
|
||||
time.sleep(MUAPI_POLL_INTERVAL)
|
||||
data = _json_request(
|
||||
result_url,
|
||||
api_key,
|
||||
api_key_header="x-api-key",
|
||||
)
|
||||
|
||||
raise RuntimeError("MuAPI prediction timed out while polling")
|
||||
|
||||
|
||||
def _generate_with_gemini(prompt, output_path, aspect_ratio, use_pro):
|
||||
if not GEMINI_API_KEY:
|
||||
raise RuntimeError("GEMINI_API_KEY not set")
|
||||
|
||||
try:
|
||||
from google import genai
|
||||
from google.genai import types
|
||||
except ImportError as exc:
|
||||
raise RuntimeError(
|
||||
"google-genai package not installed; run: pip install google-genai"
|
||||
) from exc
|
||||
|
||||
client = genai.Client(api_key=GEMINI_API_KEY)
|
||||
model = GEMINI_PRO if use_pro else GEMINI_FLASH
|
||||
response = client.models.generate_content(
|
||||
model=model,
|
||||
contents=prompt,
|
||||
config=types.GenerateContentConfig(
|
||||
response_modalities=["IMAGE", "TEXT"],
|
||||
image_config=types.ImageConfig(aspect_ratio=aspect_ratio),
|
||||
safety_settings=[
|
||||
types.SafetySetting(
|
||||
category="HARM_CATEGORY_HATE_SPEECH",
|
||||
threshold="BLOCK_LOW_AND_ABOVE",
|
||||
),
|
||||
types.SafetySetting(
|
||||
category="HARM_CATEGORY_DANGEROUS_CONTENT",
|
||||
threshold="BLOCK_LOW_AND_ABOVE",
|
||||
),
|
||||
types.SafetySetting(
|
||||
category="HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
||||
threshold="BLOCK_LOW_AND_ABOVE",
|
||||
),
|
||||
types.SafetySetting(
|
||||
category="HARM_CATEGORY_HARASSMENT",
|
||||
threshold="BLOCK_LOW_AND_ABOVE",
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
|
||||
for part in response.candidates[0].content.parts:
|
||||
if (
|
||||
hasattr(part, "inline_data")
|
||||
and part.inline_data
|
||||
and part.inline_data.mime_type.startswith("image/")
|
||||
):
|
||||
with open(output_path, "wb") as output_file:
|
||||
output_file.write(part.inline_data.data)
|
||||
return
|
||||
raise RuntimeError("Gemini did not return an image")
|
||||
|
||||
|
||||
def generate_logo(
|
||||
prompt,
|
||||
style=None,
|
||||
industry=None,
|
||||
brand_name=None,
|
||||
output_path=None,
|
||||
use_pro=False,
|
||||
aspect_ratio=None,
|
||||
provider="gemini",
|
||||
atlas_model=ATLAS_MODEL,
|
||||
muapi_model=MUAPI_MODEL,
|
||||
):
|
||||
"""Generate a logo using Gemini, Atlas Cloud, or MuAPI image generation.
|
||||
|
||||
Args:
|
||||
aspect_ratio: Image aspect ratio. Options: "1:1", "16:9", "9:16", "4:3", "3:4"
|
||||
Default is "1:1" (square) for logos.
|
||||
"""
|
||||
|
||||
# Enhance the prompt
|
||||
full_prompt = enhance_prompt(prompt, style, industry, brand_name)
|
||||
|
||||
# Set aspect ratio (default to 1:1 for logos)
|
||||
ratio = aspect_ratio if aspect_ratio in ASPECT_RATIOS else DEFAULT_ASPECT_RATIO
|
||||
|
||||
if output_path is None:
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") # noqa: DTZ005
|
||||
brand_slug = brand_name.lower().replace(" ", "_") if brand_name else "logo"
|
||||
output_path = f"{brand_slug}_{timestamp}.png"
|
||||
|
||||
if provider == "atlas":
|
||||
model_label = f"Atlas Cloud ({atlas_model})"
|
||||
elif provider == "muapi":
|
||||
model_label = f"MuAPI ({muapi_model})"
|
||||
else:
|
||||
model_label = (
|
||||
"Nano Banana Pro (gemini-3-pro-image-preview)"
|
||||
if use_pro
|
||||
else "Nano Banana (gemini-2.5-flash-image)"
|
||||
)
|
||||
|
||||
print(f"Generating logo with {model_label}...")
|
||||
print(f"Aspect ratio: {ratio}")
|
||||
print(f"Prompt: {full_prompt[:150]}...")
|
||||
print()
|
||||
|
||||
try:
|
||||
if provider == "atlas":
|
||||
_generate_with_atlas(
|
||||
full_prompt,
|
||||
output_path,
|
||||
ratio,
|
||||
ATLASCLOUD_API_KEY,
|
||||
atlas_model,
|
||||
)
|
||||
elif provider == "muapi":
|
||||
_generate_with_muapi(
|
||||
full_prompt,
|
||||
output_path,
|
||||
ratio,
|
||||
MUAPI_API_KEY,
|
||||
muapi_model,
|
||||
)
|
||||
else:
|
||||
_generate_with_gemini(full_prompt, output_path, ratio, use_pro)
|
||||
|
||||
print(f"Logo saved to: {output_path}")
|
||||
return output_path
|
||||
|
||||
except Exception as exc: # noqa: BLE001 - provider SDK errors are not standardized
|
||||
print(f"Error generating logo: {exc}")
|
||||
return None
|
||||
|
||||
|
||||
def generate_batch(
|
||||
prompt,
|
||||
brand_name,
|
||||
count,
|
||||
output_dir,
|
||||
use_pro=False,
|
||||
brand_context=None,
|
||||
aspect_ratio=None,
|
||||
provider="gemini",
|
||||
atlas_model=ATLAS_MODEL,
|
||||
muapi_model=MUAPI_MODEL,
|
||||
):
|
||||
"""Generate multiple logo variants with different styles"""
|
||||
|
||||
# Select appropriate styles for batch generation
|
||||
batch_styles = [
|
||||
("minimalist", "Clean, simple geometric shape with minimal details"),
|
||||
("modern", "Sleek gradient with tech-forward aesthetic"),
|
||||
("geometric", "Abstract geometric patterns, mathematical precision"),
|
||||
("gradient", "Vibrant color transitions, modern digital feel"),
|
||||
("abstract", "Conceptual symbolic representation"),
|
||||
("lettermark", "Stylized letter 'U' as monogram"),
|
||||
("negative-space", "Clever use of negative space, hidden meaning"),
|
||||
("lineart", "Single stroke continuous line design"),
|
||||
("3d", "Dimensional design with depth and shadows"),
|
||||
]
|
||||
|
||||
# Ensure output directory exists
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
results = []
|
||||
model_label = (
|
||||
f"Atlas Cloud ({atlas_model})"
|
||||
if provider == "atlas"
|
||||
else f"MuAPI ({muapi_model})"
|
||||
if provider == "muapi"
|
||||
else f"Nano Banana {'Pro' if use_pro else 'Flash'}"
|
||||
)
|
||||
ratio = aspect_ratio if aspect_ratio in ASPECT_RATIOS else DEFAULT_ASPECT_RATIO
|
||||
|
||||
print(f"\n{'=' * 60}")
|
||||
print(f" BATCH LOGO GENERATION: {brand_name}")
|
||||
print(f" Model: {model_label}")
|
||||
print(f" Aspect Ratio: {ratio}")
|
||||
print(f" Variants: {count}")
|
||||
print(f" Output: {output_dir}")
|
||||
print(f"{'=' * 60}\n")
|
||||
|
||||
for i in range(min(count, len(batch_styles))):
|
||||
style_key, style_desc = batch_styles[i]
|
||||
|
||||
# Build enhanced prompt with brand context
|
||||
enhanced_prompt = f"{prompt}, {style_desc}"
|
||||
if brand_context:
|
||||
enhanced_prompt = f"{brand_context}, {enhanced_prompt}"
|
||||
|
||||
# Generate filename
|
||||
filename = f"{brand_name.lower().replace(' ', '_')}_{style_key}_{i + 1:02d}.png"
|
||||
output_path = os.path.join(output_dir, filename)
|
||||
|
||||
print(f"[{i + 1}/{count}] Generating {style_key} variant...")
|
||||
|
||||
result = generate_logo(
|
||||
prompt=enhanced_prompt,
|
||||
style=style_key,
|
||||
industry="tech",
|
||||
brand_name=brand_name,
|
||||
output_path=output_path,
|
||||
use_pro=use_pro,
|
||||
aspect_ratio=aspect_ratio,
|
||||
provider=provider,
|
||||
atlas_model=atlas_model,
|
||||
muapi_model=muapi_model,
|
||||
)
|
||||
|
||||
if result:
|
||||
results.append(result)
|
||||
print(f" ✓ Saved: {filename}\n")
|
||||
else:
|
||||
print(f" ✗ Failed: {style_key}\n")
|
||||
|
||||
# Rate limiting between requests
|
||||
if i < count - 1:
|
||||
time.sleep(2)
|
||||
|
||||
print(f"\n{'=' * 60}")
|
||||
print(f" BATCH COMPLETE: {len(results)}/{count} logos generated")
|
||||
print(f"{'=' * 60}\n")
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Generate logos using Gemini, Atlas Cloud, or MuAPI"
|
||||
)
|
||||
parser.add_argument("--prompt", "-p", type=str, help="Logo description prompt")
|
||||
parser.add_argument("--brand", "-b", type=str, help="Brand name")
|
||||
parser.add_argument(
|
||||
"--style", "-s", choices=list(STYLE_MODIFIERS.keys()), help="Logo style"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--industry", "-i", choices=list(INDUSTRY_PROMPTS.keys()), help="Industry type"
|
||||
)
|
||||
parser.add_argument("--output", "-o", type=str, help="Output file path")
|
||||
parser.add_argument(
|
||||
"--output-dir", type=str, help="Output directory for batch generation"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--batch", type=int, help="Number of logo variants to generate (batch mode)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--brand-context", type=str, help="Additional brand context for prompts"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--pro",
|
||||
action="store_true",
|
||||
help="Use Nano Banana Pro (gemini-3-pro-image-preview) for professional quality",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--provider",
|
||||
choices=["gemini", "atlas", "muapi"],
|
||||
default="gemini",
|
||||
help="Image provider (default: gemini)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--atlas-model",
|
||||
default=ATLAS_MODEL,
|
||||
help=f"Atlas Cloud image model (default: {ATLAS_MODEL})",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--muapi-model",
|
||||
choices=MUAPI_MODELS,
|
||||
default=MUAPI_MODEL,
|
||||
help=f"MuAPI image model (default: {MUAPI_MODEL})",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--aspect-ratio",
|
||||
"-r",
|
||||
choices=ASPECT_RATIOS,
|
||||
default=DEFAULT_ASPECT_RATIO,
|
||||
help=f"Image aspect ratio (default: {DEFAULT_ASPECT_RATIO} for logos)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--list-styles", action="store_true", help="List available styles"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--list-industries", action="store_true", help="List available industries"
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.provider != "gemini" and args.pro:
|
||||
parser.error(
|
||||
"--pro is only available with --provider gemini; "
|
||||
"use --muapi-model nano-banana-pro for MuAPI"
|
||||
)
|
||||
|
||||
if args.list_styles:
|
||||
print("Available styles:")
|
||||
for style, desc in STYLE_MODIFIERS.items():
|
||||
print(f" {style}: {desc[:60]}...")
|
||||
return
|
||||
|
||||
if args.list_industries:
|
||||
print("Available industries:")
|
||||
for industry, desc in INDUSTRY_PROMPTS.items():
|
||||
print(f" {industry}: {desc[:60]}...")
|
||||
return
|
||||
|
||||
if not args.prompt and not args.brand:
|
||||
parser.error("Either --prompt or --brand is required")
|
||||
|
||||
prompt = args.prompt or "professional logo"
|
||||
|
||||
# Batch mode
|
||||
if args.batch:
|
||||
output_dir = (
|
||||
args.output_dir or f"./{args.brand.lower().replace(' ', '_')}_logos"
|
||||
)
|
||||
generate_batch(
|
||||
prompt=prompt,
|
||||
brand_name=args.brand or "Logo",
|
||||
count=args.batch,
|
||||
output_dir=output_dir,
|
||||
use_pro=args.pro,
|
||||
brand_context=args.brand_context,
|
||||
aspect_ratio=args.aspect_ratio,
|
||||
provider=args.provider,
|
||||
atlas_model=args.atlas_model,
|
||||
muapi_model=args.muapi_model,
|
||||
)
|
||||
else:
|
||||
generate_logo(
|
||||
prompt=prompt,
|
||||
style=args.style,
|
||||
industry=args.industry,
|
||||
brand_name=args.brand,
|
||||
output_path=args.output,
|
||||
use_pro=args.pro,
|
||||
aspect_ratio=args.aspect_ratio,
|
||||
provider=args.provider,
|
||||
atlas_model=args.atlas_model,
|
||||
muapi_model=args.muapi_model,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
114
.agents/skills/design/scripts/logo/search.py
Normal file
114
.agents/skills/design/scripts/logo/search.py
Normal file
@@ -0,0 +1,114 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Logo Design Search - CLI for searching logo design guidelines
|
||||
Usage: python search.py "<query>" [--domain <domain>] [--max-results 3]
|
||||
python search.py "<query>" --design-brief [-p "Brand Name"]
|
||||
|
||||
Domains: style, color, industry
|
||||
"""
|
||||
|
||||
import argparse
|
||||
from core import CSV_CONFIG, MAX_RESULTS, search, search_all
|
||||
|
||||
|
||||
def format_output(result):
|
||||
"""Format results for Claude consumption (token-optimized)"""
|
||||
if "error" in result:
|
||||
return f"Error: {result['error']}"
|
||||
|
||||
output = []
|
||||
output.append(f"## Logo Design Search Results")
|
||||
output.append(f"**Domain:** {result['domain']} | **Query:** {result['query']}")
|
||||
output.append(f"**Source:** {result['file']} | **Found:** {result['count']} results\n")
|
||||
|
||||
for i, row in enumerate(result['results'], 1):
|
||||
output.append(f"### Result {i}")
|
||||
for key, value in row.items():
|
||||
value_str = str(value)
|
||||
if len(value_str) > 300:
|
||||
value_str = value_str[:300] + "..."
|
||||
output.append(f"- **{key}:** {value_str}")
|
||||
output.append("")
|
||||
|
||||
return "\n".join(output)
|
||||
|
||||
|
||||
def generate_design_brief(query, brand_name=None):
|
||||
"""Generate a comprehensive logo design brief based on query"""
|
||||
results = search_all(query, max_results=2)
|
||||
|
||||
output = []
|
||||
output.append("=" * 60)
|
||||
if brand_name:
|
||||
output.append(f" LOGO DESIGN BRIEF: {brand_name.upper()}")
|
||||
else:
|
||||
output.append(" LOGO DESIGN BRIEF")
|
||||
output.append("=" * 60)
|
||||
output.append(f" Query: {query}")
|
||||
output.append("=" * 60)
|
||||
output.append("")
|
||||
|
||||
# Industry recommendations
|
||||
if "industry" in results:
|
||||
output.append("## INDUSTRY ANALYSIS")
|
||||
for r in results["industry"]:
|
||||
output.append(f"**Industry:** {r.get('Industry', 'N/A')}")
|
||||
output.append(f"- Recommended Styles: {r.get('Recommended Styles', 'N/A')}")
|
||||
output.append(f"- Colors: {r.get('Primary Colors', 'N/A')}")
|
||||
output.append(f"- Typography: {r.get('Typography', 'N/A')}")
|
||||
output.append(f"- Symbols: {r.get('Common Symbols', 'N/A')}")
|
||||
output.append(f"- Mood: {r.get('Mood', 'N/A')}")
|
||||
output.append(f"- Best Practices: {r.get('Best Practices', 'N/A')}")
|
||||
output.append(f"- Avoid: {r.get('Avoid', 'N/A')}")
|
||||
output.append("")
|
||||
|
||||
# Style recommendations
|
||||
if "style" in results:
|
||||
output.append("## STYLE RECOMMENDATIONS")
|
||||
for r in results["style"]:
|
||||
output.append(f"**{r.get('Style Name', 'N/A')}** ({r.get('Category', 'N/A')})")
|
||||
output.append(f"- Colors: {r.get('Primary Colors', 'N/A')} | {r.get('Secondary Colors', 'N/A')}")
|
||||
output.append(f"- Typography: {r.get('Typography', 'N/A')}")
|
||||
output.append(f"- Effects: {r.get('Effects', 'N/A')}")
|
||||
output.append(f"- Best For: {r.get('Best For', 'N/A')}")
|
||||
output.append(f"- Complexity: {r.get('Complexity', 'N/A')}")
|
||||
output.append("")
|
||||
|
||||
# Color recommendations
|
||||
if "color" in results:
|
||||
output.append("## COLOR PALETTE OPTIONS")
|
||||
for r in results["color"]:
|
||||
output.append(f"**{r.get('Palette Name', 'N/A')}**")
|
||||
output.append(f"- Primary: {r.get('Primary Hex', 'N/A')}")
|
||||
output.append(f"- Secondary: {r.get('Secondary Hex', 'N/A')}")
|
||||
output.append(f"- Accent: {r.get('Accent Hex', 'N/A')}")
|
||||
output.append(f"- Background: {r.get('Background Hex', 'N/A')}")
|
||||
output.append(f"- Psychology: {r.get('Psychology', 'N/A')}")
|
||||
output.append("")
|
||||
|
||||
output.append("=" * 60)
|
||||
return "\n".join(output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Logo Design Search")
|
||||
parser.add_argument("query", help="Search query")
|
||||
parser.add_argument("--domain", "-d", choices=list(CSV_CONFIG.keys()), help="Search domain")
|
||||
parser.add_argument("--max-results", "-n", type=int, default=MAX_RESULTS, help="Max results (default: 3)")
|
||||
parser.add_argument("--json", action="store_true", help="Output as JSON")
|
||||
parser.add_argument("--design-brief", "-db", action="store_true", help="Generate comprehensive design brief")
|
||||
parser.add_argument("--brand-name", "-p", type=str, default=None, help="Brand name for design brief")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.design_brief:
|
||||
result = generate_design_brief(args.query, args.brand_name)
|
||||
print(result)
|
||||
else:
|
||||
result = search(args.query, args.domain, args.max_results)
|
||||
if args.json:
|
||||
import json
|
||||
print(json.dumps(result, indent=2, ensure_ascii=False))
|
||||
else:
|
||||
print(format_output(result))
|
||||
288
.agents/skills/design/scripts/logo/tests/test_generate.py
Normal file
288
.agents/skills/design/scripts/logo/tests/test_generate.py
Normal file
@@ -0,0 +1,288 @@
|
||||
import importlib.util
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import call, patch
|
||||
|
||||
MODULE_PATH = Path(__file__).parents[1] / "generate.py"
|
||||
SPEC = importlib.util.spec_from_file_location("logo_generate", MODULE_PATH)
|
||||
logo_generate = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(logo_generate)
|
||||
|
||||
|
||||
class AtlasGenerationTests(unittest.TestCase):
|
||||
@patch.object(logo_generate, "_download_atlas_image")
|
||||
@patch.object(logo_generate.time, "sleep")
|
||||
@patch.object(logo_generate, "_json_request")
|
||||
def test_atlas_submits_once_and_polls_until_completed(
|
||||
self, json_request, sleep, download
|
||||
):
|
||||
json_request.side_effect = [
|
||||
{"code": 200, "data": {"id": "pred-123", "status": "created"}},
|
||||
{"code": 200, "data": {"id": "pred-123", "status": "processing"}},
|
||||
{
|
||||
"code": 200,
|
||||
"data": {
|
||||
"id": "pred-123",
|
||||
"status": "completed",
|
||||
"outputs": ["https://media.example.com/logo.png"],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
logo_generate._generate_with_atlas(
|
||||
"logo prompt", "logo.png", "1:1", "atlas-key", "atlas/model"
|
||||
)
|
||||
|
||||
self.assertEqual(json_request.call_count, 3)
|
||||
self.assertEqual(
|
||||
json_request.call_args_list[0],
|
||||
call(
|
||||
f"{logo_generate.ATLAS_API_BASE}/model/generateImage",
|
||||
"atlas-key",
|
||||
method="POST",
|
||||
payload={
|
||||
"model": "atlas/model",
|
||||
"prompt": "logo prompt",
|
||||
"aspect_ratio": "1:1",
|
||||
},
|
||||
),
|
||||
)
|
||||
self.assertEqual(
|
||||
json_request.call_args_list[1:],
|
||||
[
|
||||
call(
|
||||
f"{logo_generate.ATLAS_API_BASE}/model/prediction/pred-123",
|
||||
"atlas-key",
|
||||
),
|
||||
call(
|
||||
f"{logo_generate.ATLAS_API_BASE}/model/prediction/pred-123",
|
||||
"atlas-key",
|
||||
),
|
||||
],
|
||||
)
|
||||
self.assertEqual(sleep.call_count, 2)
|
||||
download.assert_called_once_with(
|
||||
"https://media.example.com/logo.png", "logo.png"
|
||||
)
|
||||
|
||||
@patch.object(logo_generate, "_json_request")
|
||||
def test_atlas_does_not_retry_generation_post(self, json_request):
|
||||
json_request.side_effect = RuntimeError("network error")
|
||||
|
||||
with self.assertRaisesRegex(RuntimeError, "network error"):
|
||||
logo_generate._generate_with_atlas(
|
||||
"logo prompt", "logo.png", "1:1", "atlas-key", "atlas/model"
|
||||
)
|
||||
|
||||
json_request.assert_called_once()
|
||||
|
||||
@patch.object(logo_generate, "_validate_public_https_url")
|
||||
@patch.object(logo_generate, "build_opener")
|
||||
def test_media_download_never_forwards_api_key(self, build_opener, validate):
|
||||
class Headers:
|
||||
@staticmethod
|
||||
def get_content_type():
|
||||
return "image/png"
|
||||
|
||||
class Response:
|
||||
headers = Headers()
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def read():
|
||||
return b"png-bytes"
|
||||
|
||||
build_opener.return_value.open.return_value = Response()
|
||||
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
output = Path(temp_dir) / "logo.png"
|
||||
logo_generate._download_atlas_image(
|
||||
"https://media.example.com/logo.png", output
|
||||
)
|
||||
self.assertEqual(output.read_bytes(), b"png-bytes")
|
||||
|
||||
request = build_opener.return_value.open.call_args.args[0]
|
||||
headers = {key.lower(): value for key, value in request.header_items()}
|
||||
self.assertNotIn("authorization", headers)
|
||||
self.assertEqual(headers["accept"], "image/*")
|
||||
self.assertEqual(headers["user-agent"], logo_generate.HTTP_USER_AGENT)
|
||||
validate.assert_called_once_with("https://media.example.com/logo.png")
|
||||
|
||||
def test_atlas_requires_api_key(self):
|
||||
with self.assertRaisesRegex(RuntimeError, "ATLASCLOUD_API_KEY not set"):
|
||||
logo_generate._generate_with_atlas(
|
||||
"logo prompt", "logo.png", "1:1", None, "atlas/model"
|
||||
)
|
||||
|
||||
def test_media_url_rejects_private_addresses(self):
|
||||
with self.assertRaisesRegex(ValueError, "non-public address"):
|
||||
logo_generate._validate_public_https_url("https://127.0.0.1/logo.png")
|
||||
|
||||
with self.assertRaisesRegex(ValueError, "local hostname"):
|
||||
logo_generate._validate_public_https_url("https://assets.local/logo.png")
|
||||
|
||||
|
||||
class MuapiGenerationTests(unittest.TestCase):
|
||||
@patch.object(logo_generate, "_download_muapi_image")
|
||||
@patch.object(logo_generate.time, "sleep")
|
||||
@patch.object(logo_generate, "_json_request")
|
||||
def test_muapi_submits_once_and_polls_until_completed(
|
||||
self, json_request, sleep, download
|
||||
):
|
||||
json_request.side_effect = [
|
||||
{
|
||||
"id": "req-123",
|
||||
"status": "created",
|
||||
"output": {
|
||||
"urls": {
|
||||
"get": "https://api.muapi.ai/api/v1/results/req-123"
|
||||
}
|
||||
},
|
||||
},
|
||||
{"id": "req-123", "status": "processing"},
|
||||
{
|
||||
"id": "req-123",
|
||||
"status": "completed",
|
||||
"output": {"outputs": ["https://media.example.com/logo.png"]},
|
||||
},
|
||||
]
|
||||
|
||||
logo_generate._generate_with_muapi(
|
||||
"logo prompt", "logo.png", "1:1", "muapi-key", "nano-banana"
|
||||
)
|
||||
|
||||
self.assertEqual(json_request.call_count, 3)
|
||||
self.assertEqual(
|
||||
json_request.call_args_list[0],
|
||||
call(
|
||||
f"{logo_generate.MUAPI_API_BASE}/nano-banana",
|
||||
"muapi-key",
|
||||
method="POST",
|
||||
payload={"prompt": "logo prompt", "aspect_ratio": "1:1"},
|
||||
api_key_header="x-api-key",
|
||||
),
|
||||
)
|
||||
self.assertEqual(
|
||||
json_request.call_args_list[1:],
|
||||
[
|
||||
call(
|
||||
"https://api.muapi.ai/api/v1/results/req-123",
|
||||
"muapi-key",
|
||||
api_key_header="x-api-key",
|
||||
),
|
||||
call(
|
||||
"https://api.muapi.ai/api/v1/results/req-123",
|
||||
"muapi-key",
|
||||
api_key_header="x-api-key",
|
||||
),
|
||||
],
|
||||
)
|
||||
self.assertEqual(sleep.call_count, 2)
|
||||
download.assert_called_once_with(
|
||||
"https://media.example.com/logo.png", "logo.png"
|
||||
)
|
||||
|
||||
@patch.object(logo_generate, "_json_request")
|
||||
def test_muapi_does_not_retry_generation_post(self, json_request):
|
||||
json_request.side_effect = RuntimeError("network error")
|
||||
|
||||
with self.assertRaisesRegex(RuntimeError, "network error"):
|
||||
logo_generate._generate_with_muapi(
|
||||
"logo prompt", "logo.png", "1:1", "muapi-key", "nano-banana"
|
||||
)
|
||||
|
||||
json_request.assert_called_once()
|
||||
|
||||
def test_muapi_requires_key_and_known_model(self):
|
||||
with self.assertRaisesRegex(RuntimeError, "MUAPI_API_KEY not set"):
|
||||
logo_generate._generate_with_muapi(
|
||||
"logo prompt", "logo.png", "1:1", None, "nano-banana"
|
||||
)
|
||||
|
||||
with self.assertRaisesRegex(RuntimeError, "Unsupported MuAPI logo model"):
|
||||
logo_generate._generate_with_muapi(
|
||||
"logo prompt", "logo.png", "1:1", "muapi-key", "unknown-model"
|
||||
)
|
||||
|
||||
@patch.object(logo_generate, "build_opener")
|
||||
def test_muapi_uses_x_api_key_header(self, build_opener):
|
||||
class Response:
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def read():
|
||||
return b"{}"
|
||||
|
||||
build_opener.return_value.open.return_value = Response()
|
||||
|
||||
logo_generate._json_request(
|
||||
"https://api.muapi.ai/api/v1/nano-banana",
|
||||
"muapi-key",
|
||||
method="POST",
|
||||
payload={"prompt": "logo"},
|
||||
api_key_header="x-api-key",
|
||||
)
|
||||
|
||||
request = build_opener.return_value.open.call_args.args[0]
|
||||
headers = {key.lower(): value for key, value in request.header_items()}
|
||||
self.assertEqual(headers["x-api-key"], "muapi-key")
|
||||
self.assertNotIn("authorization", headers)
|
||||
|
||||
@patch.object(logo_generate, "_json_request")
|
||||
def test_muapi_reports_failed_prediction(self, json_request):
|
||||
json_request.side_effect = [
|
||||
{
|
||||
"request_id": "req-123",
|
||||
"output": {
|
||||
"urls": {
|
||||
"get": "https://api.muapi.ai/api/v1/results/req-123"
|
||||
}
|
||||
},
|
||||
},
|
||||
{"status": "failed", "error": "invalid prompt"},
|
||||
]
|
||||
|
||||
with self.assertRaisesRegex(RuntimeError, "invalid prompt"):
|
||||
logo_generate._generate_with_muapi(
|
||||
"logo prompt", "logo.png", "1:1", "muapi-key", "nano-banana"
|
||||
)
|
||||
|
||||
@patch.object(logo_generate, "_json_request")
|
||||
def test_muapi_requires_creation_result_url(self, json_request):
|
||||
json_request.return_value = {"request_id": "req-123", "status": "created"}
|
||||
|
||||
with self.assertRaisesRegex(RuntimeError, "valid HTTPS result URL"):
|
||||
logo_generate._generate_with_muapi(
|
||||
"logo prompt", "logo.png", "1:1", "muapi-key", "nano-banana"
|
||||
)
|
||||
|
||||
json_request.assert_called_once()
|
||||
|
||||
@patch.object(logo_generate, "_json_request")
|
||||
def test_muapi_rejects_invalid_creation_result_url(self, json_request):
|
||||
json_request.return_value = {
|
||||
"request_id": "req-123",
|
||||
"status": "created",
|
||||
"output": {"urls": {"get": "http://api.muapi.ai/results/req-123"}},
|
||||
}
|
||||
|
||||
with self.assertRaisesRegex(RuntimeError, "valid HTTPS result URL"):
|
||||
logo_generate._generate_with_muapi(
|
||||
"logo prompt", "logo.png", "1:1", "muapi-key", "nano-banana"
|
||||
)
|
||||
|
||||
json_request.assert_called_once()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user