Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- .env.example +3 -0
- .gitignore +12 -0
- README.md +95 -5
- app.py +327 -0
- leonardo/__init__.py +3 -0
- leonardo/client.py +219 -0
- requirements.txt +5 -0
.env.example
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Leonardo AI API Key
|
| 2 |
+
# Get your API key from: https://app.leonardo.ai → User Menu → API Access
|
| 3 |
+
LEONARDO_API_KEY=your_api_key_here
|
.gitignore
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.py[cod]
|
| 3 |
+
*$py.class
|
| 4 |
+
*.so
|
| 5 |
+
.Python
|
| 6 |
+
env/
|
| 7 |
+
venv/
|
| 8 |
+
.venv/
|
| 9 |
+
.env
|
| 10 |
+
.DS_Store
|
| 11 |
+
*.log
|
| 12 |
+
flagged/
|
README.md
CHANGED
|
@@ -1,12 +1,102 @@
|
|
| 1 |
---
|
| 2 |
-
title: Leonardo
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: purple
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Leonardo AI Design Generator
|
| 3 |
+
emoji: 🎨
|
| 4 |
colorFrom: purple
|
| 5 |
+
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# 🎨 Leonardo AI Design Generator
|
| 13 |
+
|
| 14 |
+
Test Space for AI Software Factory - Leonardo AI Integration
|
| 15 |
+
|
| 16 |
+
## Description
|
| 17 |
+
|
| 18 |
+
This HuggingFace Space demonstrates the integration with Leonardo AI for generating high-quality UI/UX design mockups. It's part of the AI Software Factory project for automated Flutter app development.
|
| 19 |
+
|
| 20 |
+
## Features
|
| 21 |
+
|
| 22 |
+
- **Mobile App UI Mockups**: Generate professional mobile screen designs
|
| 23 |
+
- **Web Dashboard Designs**: Create desktop application interfaces
|
| 24 |
+
- **Landing Page Layouts**: Design modern landing page sections
|
| 25 |
+
- **Customizable Prompts**: Full control over design parameters
|
| 26 |
+
- **Real-time Credit Tracking**: Monitor your Leonardo AI credits
|
| 27 |
+
|
| 28 |
+
## How to Use
|
| 29 |
+
|
| 30 |
+
1. **Check Credits**: Click "Check Credits" to see your available Leonardo AI credits
|
| 31 |
+
2. **Configure Design**:
|
| 32 |
+
- Choose a template type (Mobile App UI, Web Dashboard, or Landing Page)
|
| 33 |
+
- Enter app type, screen type, visual style
|
| 34 |
+
- Set color palette and layout elements
|
| 35 |
+
3. **Advanced Options**:
|
| 36 |
+
- Override with custom prompt if needed
|
| 37 |
+
- Adjust image dimensions (must be multiple of 8)
|
| 38 |
+
- Choose number of variations (1-4)
|
| 39 |
+
4. **Generate**: Click "Generate Design" and wait for the result
|
| 40 |
+
|
| 41 |
+
## Setup
|
| 42 |
+
|
| 43 |
+
To run this Space, you need a Leonardo AI API key:
|
| 44 |
+
|
| 45 |
+
1. Go to [Leonardo AI](https://app.leonardo.ai)
|
| 46 |
+
2. Navigate to User Menu → API Access
|
| 47 |
+
3. Generate an API Key
|
| 48 |
+
4. Add it as a **Secret** in your HuggingFace Space settings:
|
| 49 |
+
- Name: `LEONARDO_API_KEY`
|
| 50 |
+
- Value: Your API key
|
| 51 |
+
|
| 52 |
+
## API Endpoints
|
| 53 |
+
|
| 54 |
+
When deployed, this Space exposes a REST API:
|
| 55 |
+
|
| 56 |
+
```bash
|
| 57 |
+
POST /api/predict
|
| 58 |
+
{
|
| 59 |
+
"data": [
|
| 60 |
+
"Mobile App UI", # template_type
|
| 61 |
+
"delivery app", # app_type
|
| 62 |
+
"home screen", # screen_type
|
| 63 |
+
"modern", # style
|
| 64 |
+
"#FF0000, #00FF00", # colors
|
| 65 |
+
"header, cards", # elements
|
| 66 |
+
"", # custom_prompt
|
| 67 |
+
375, # width
|
| 68 |
+
812, # height
|
| 69 |
+
1 # num_images
|
| 70 |
+
]
|
| 71 |
+
}
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
## Cost
|
| 75 |
+
|
| 76 |
+
Each image generation costs approximately **10-20 Leonardo AI credits**, depending on:
|
| 77 |
+
- Image resolution
|
| 78 |
+
- Number of images
|
| 79 |
+
- Alchemy/Prompt Magic settings
|
| 80 |
+
|
| 81 |
+
## Technologies
|
| 82 |
+
|
| 83 |
+
- **Leonardo AI**: Phoenix model for image generation
|
| 84 |
+
- **Gradio**: Web interface framework
|
| 85 |
+
- **Python**: Backend logic
|
| 86 |
+
- **HuggingFace Spaces**: Deployment platform
|
| 87 |
+
|
| 88 |
+
## Related
|
| 89 |
+
|
| 90 |
+
This Space is part of the [AI Software Factory](https://github.com/sabatino81/AISoftwareFactory) project:
|
| 91 |
+
- 15 specialized agent Spaces on HuggingFace
|
| 92 |
+
- Automated Flutter app generation
|
| 93 |
+
- N8N workflow orchestration
|
| 94 |
+
- Full CI/CD pipeline
|
| 95 |
+
|
| 96 |
+
## License
|
| 97 |
+
|
| 98 |
+
MIT
|
| 99 |
+
|
| 100 |
+
## Author
|
| 101 |
+
|
| 102 |
+
AI Software Factory Team
|
app.py
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Leonardo AI Design Generator - Test Space
|
| 3 |
+
HuggingFace Space for testing Leonardo AI integration
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import asyncio
|
| 9 |
+
from leonardo.client import LeonardoClient
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
|
| 12 |
+
# Load environment variables
|
| 13 |
+
load_dotenv()
|
| 14 |
+
|
| 15 |
+
# Initialize Leonardo client
|
| 16 |
+
try:
|
| 17 |
+
leo_client = LeonardoClient()
|
| 18 |
+
client_ready = True
|
| 19 |
+
except Exception as e:
|
| 20 |
+
leo_client = None
|
| 21 |
+
client_ready = False
|
| 22 |
+
error_message = str(e)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# Prompt templates
|
| 26 |
+
DESIGN_TEMPLATES = {
|
| 27 |
+
"Mobile App UI": """
|
| 28 |
+
Professional mobile app UI design mockup,
|
| 29 |
+
{app_type} application,
|
| 30 |
+
{screen_type} screen,
|
| 31 |
+
|
| 32 |
+
Visual style: {style},
|
| 33 |
+
Design system: Material Design 3,
|
| 34 |
+
Color palette: {colors},
|
| 35 |
+
|
| 36 |
+
Layout elements: {elements},
|
| 37 |
+
|
| 38 |
+
Quality: high fidelity, professional mockup,
|
| 39 |
+
smartphone screen portrait orientation,
|
| 40 |
+
ultra detailed, Dribbble quality,
|
| 41 |
+
modern design guidelines
|
| 42 |
+
""".strip(),
|
| 43 |
+
|
| 44 |
+
"Web Dashboard": """
|
| 45 |
+
Professional web dashboard UI design,
|
| 46 |
+
{app_type} application,
|
| 47 |
+
{screen_type} view,
|
| 48 |
+
|
| 49 |
+
Visual style: {style},
|
| 50 |
+
Design system: modern web design,
|
| 51 |
+
Color palette: {colors},
|
| 52 |
+
|
| 53 |
+
Layout elements: {elements},
|
| 54 |
+
|
| 55 |
+
Quality: high fidelity, professional mockup,
|
| 56 |
+
desktop view, wide screen,
|
| 57 |
+
ultra detailed, Behance quality
|
| 58 |
+
""".strip(),
|
| 59 |
+
|
| 60 |
+
"Landing Page": """
|
| 61 |
+
Modern landing page design,
|
| 62 |
+
{app_type} website,
|
| 63 |
+
{section} section,
|
| 64 |
+
|
| 65 |
+
Visual style: {style},
|
| 66 |
+
Color palette: {colors},
|
| 67 |
+
|
| 68 |
+
Layout elements: {elements},
|
| 69 |
+
|
| 70 |
+
Quality: high fidelity, professional mockup,
|
| 71 |
+
responsive web design,
|
| 72 |
+
ultra detailed, Awwwards quality
|
| 73 |
+
""".strip()
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
NEGATIVE_PROMPT = """
|
| 77 |
+
blurry, low quality, distorted,
|
| 78 |
+
lorem ipsum, placeholder text, dummy content,
|
| 79 |
+
watermark, logo, branding,
|
| 80 |
+
pixelated, artifacts, noise,
|
| 81 |
+
multiple screens, inconsistent
|
| 82 |
+
""".strip()
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
async def check_credits():
|
| 86 |
+
"""Check Leonardo AI credits"""
|
| 87 |
+
if not client_ready:
|
| 88 |
+
return f"❌ Client not initialized: {error_message}"
|
| 89 |
+
|
| 90 |
+
try:
|
| 91 |
+
user_info = await leo_client.get_user_info()
|
| 92 |
+
total_credits = user_info['api_credits'] + user_info['subscription_tokens']
|
| 93 |
+
return f"✅ Available credits: {total_credits} (API: {user_info['api_credits']}, Subscription: {user_info['subscription_tokens']})"
|
| 94 |
+
except Exception as e:
|
| 95 |
+
return f"❌ Error: {str(e)}"
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
async def generate_design(
|
| 99 |
+
template_type: str,
|
| 100 |
+
app_type: str,
|
| 101 |
+
screen_type: str,
|
| 102 |
+
style: str,
|
| 103 |
+
colors: str,
|
| 104 |
+
elements: str,
|
| 105 |
+
custom_prompt: str,
|
| 106 |
+
width: int,
|
| 107 |
+
height: int,
|
| 108 |
+
num_images: int,
|
| 109 |
+
progress=gr.Progress()
|
| 110 |
+
):
|
| 111 |
+
"""Generate design mockup with Leonardo AI"""
|
| 112 |
+
|
| 113 |
+
if not client_ready:
|
| 114 |
+
return None, f"❌ Leonardo client not initialized: {error_message}"
|
| 115 |
+
|
| 116 |
+
try:
|
| 117 |
+
progress(0.1, desc="Preparing prompt...")
|
| 118 |
+
|
| 119 |
+
# Build prompt
|
| 120 |
+
if custom_prompt.strip():
|
| 121 |
+
final_prompt = custom_prompt
|
| 122 |
+
else:
|
| 123 |
+
template = DESIGN_TEMPLATES[template_type]
|
| 124 |
+
final_prompt = template.format(
|
| 125 |
+
app_type=app_type,
|
| 126 |
+
screen_type=screen_type,
|
| 127 |
+
style=style,
|
| 128 |
+
colors=colors,
|
| 129 |
+
elements=elements,
|
| 130 |
+
section=screen_type
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
progress(0.2, desc="Sending to Leonardo AI...")
|
| 134 |
+
|
| 135 |
+
# Generate
|
| 136 |
+
gen_result = await leo_client.generate_image(
|
| 137 |
+
prompt=final_prompt,
|
| 138 |
+
negative_prompt=NEGATIVE_PROMPT,
|
| 139 |
+
width=width,
|
| 140 |
+
height=height,
|
| 141 |
+
num_images=num_images,
|
| 142 |
+
guidance_scale=7,
|
| 143 |
+
prompt_magic=True,
|
| 144 |
+
alchemy=True
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
generation_id = gen_result["generation_id"]
|
| 148 |
+
cost = gen_result["api_credit_cost"]
|
| 149 |
+
|
| 150 |
+
progress(0.4, desc=f"Generating... (Cost: {cost} credits)")
|
| 151 |
+
|
| 152 |
+
# Wait for completion
|
| 153 |
+
result = await leo_client.wait_for_generation(generation_id, max_wait_seconds=120)
|
| 154 |
+
|
| 155 |
+
progress(0.9, desc="Processing results...")
|
| 156 |
+
|
| 157 |
+
if result["status"] == "COMPLETE" and result["images"]:
|
| 158 |
+
image_urls = [img["url"] for img in result["images"]]
|
| 159 |
+
|
| 160 |
+
# Return first image and info
|
| 161 |
+
info = f"""✅ Generation Complete!
|
| 162 |
+
|
| 163 |
+
Generation ID: {generation_id}
|
| 164 |
+
Cost: {cost} credits
|
| 165 |
+
Images generated: {len(image_urls)}
|
| 166 |
+
|
| 167 |
+
Image URLs:
|
| 168 |
+
{chr(10).join(f'- {url}' for url in image_urls)}
|
| 169 |
+
|
| 170 |
+
Prompt used:
|
| 171 |
+
{final_prompt}
|
| 172 |
+
"""
|
| 173 |
+
|
| 174 |
+
progress(1.0, desc="Done!")
|
| 175 |
+
return image_urls[0] if image_urls else None, info
|
| 176 |
+
else:
|
| 177 |
+
return None, f"❌ Generation failed or no images produced"
|
| 178 |
+
|
| 179 |
+
except Exception as e:
|
| 180 |
+
return None, f"❌ Error: {str(e)}"
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
# Gradio Interface
|
| 184 |
+
with gr.Blocks(title="Leonardo AI Design Generator", theme=gr.themes.Soft()) as demo:
|
| 185 |
+
|
| 186 |
+
gr.Markdown("""
|
| 187 |
+
# 🎨 Leonardo AI Design Generator
|
| 188 |
+
|
| 189 |
+
Test space for generating UI/UX design mockups using Leonardo AI.
|
| 190 |
+
|
| 191 |
+
**Features:**
|
| 192 |
+
- Mobile app UI mockups
|
| 193 |
+
- Web dashboard designs
|
| 194 |
+
- Landing page layouts
|
| 195 |
+
- Customizable prompts
|
| 196 |
+
""")
|
| 197 |
+
|
| 198 |
+
with gr.Row():
|
| 199 |
+
with gr.Column():
|
| 200 |
+
gr.Markdown("### Credits Info")
|
| 201 |
+
credits_btn = gr.Button("🔍 Check Credits", variant="secondary")
|
| 202 |
+
credits_output = gr.Textbox(label="Credits Status", lines=2)
|
| 203 |
+
|
| 204 |
+
gr.Markdown("---")
|
| 205 |
+
|
| 206 |
+
with gr.Row():
|
| 207 |
+
with gr.Column(scale=1):
|
| 208 |
+
gr.Markdown("### Design Settings")
|
| 209 |
+
|
| 210 |
+
template_type = gr.Dropdown(
|
| 211 |
+
choices=list(DESIGN_TEMPLATES.keys()),
|
| 212 |
+
value="Mobile App UI",
|
| 213 |
+
label="Template Type"
|
| 214 |
+
)
|
| 215 |
+
|
| 216 |
+
app_type = gr.Textbox(
|
| 217 |
+
value="delivery rider gamification",
|
| 218 |
+
label="App Type",
|
| 219 |
+
placeholder="e.g., delivery rider gamification, e-commerce, social media"
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
screen_type = gr.Textbox(
|
| 223 |
+
value="home dashboard",
|
| 224 |
+
label="Screen/Section Type",
|
| 225 |
+
placeholder="e.g., home dashboard, login, profile"
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
style = gr.Textbox(
|
| 229 |
+
value="energetic gaming aesthetic, modern, vibrant",
|
| 230 |
+
label="Visual Style",
|
| 231 |
+
placeholder="e.g., minimalist, energetic gaming, corporate"
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
colors = gr.Textbox(
|
| 235 |
+
value="primary #FF3B30, secondary #FFD60A, background #0A0A0A",
|
| 236 |
+
label="Color Palette",
|
| 237 |
+
placeholder="e.g., primary #FF0000, secondary #00FF00"
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
elements = gr.Textbox(
|
| 241 |
+
value="stats cards, performance chart, recent orders list",
|
| 242 |
+
label="Layout Elements",
|
| 243 |
+
placeholder="e.g., header, cards, buttons, charts",
|
| 244 |
+
lines=3
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
gr.Markdown("### Advanced Options")
|
| 248 |
+
|
| 249 |
+
custom_prompt = gr.Textbox(
|
| 250 |
+
label="Custom Prompt (optional - overrides template)",
|
| 251 |
+
placeholder="Enter your own Leonardo AI prompt...",
|
| 252 |
+
lines=5
|
| 253 |
+
)
|
| 254 |
+
|
| 255 |
+
with gr.Row():
|
| 256 |
+
width = gr.Slider(
|
| 257 |
+
minimum=320,
|
| 258 |
+
maximum=1024,
|
| 259 |
+
value=375,
|
| 260 |
+
step=8,
|
| 261 |
+
label="Width (must be multiple of 8)"
|
| 262 |
+
)
|
| 263 |
+
height = gr.Slider(
|
| 264 |
+
minimum=320,
|
| 265 |
+
maximum=1024,
|
| 266 |
+
value=812,
|
| 267 |
+
step=8,
|
| 268 |
+
label="Height (must be multiple of 8)"
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
num_images = gr.Slider(
|
| 272 |
+
minimum=1,
|
| 273 |
+
maximum=4,
|
| 274 |
+
value=1,
|
| 275 |
+
step=1,
|
| 276 |
+
label="Number of Images"
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
generate_btn = gr.Button("🚀 Generate Design", variant="primary", size="lg")
|
| 280 |
+
|
| 281 |
+
with gr.Column(scale=1):
|
| 282 |
+
gr.Markdown("### Generated Design")
|
| 283 |
+
|
| 284 |
+
output_image = gr.Image(label="Design Mockup", type="filepath")
|
| 285 |
+
output_info = gr.Textbox(label="Generation Info", lines=15)
|
| 286 |
+
|
| 287 |
+
# Event handlers
|
| 288 |
+
credits_btn.click(
|
| 289 |
+
fn=check_credits,
|
| 290 |
+
outputs=credits_output
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
generate_btn.click(
|
| 294 |
+
fn=generate_design,
|
| 295 |
+
inputs=[
|
| 296 |
+
template_type,
|
| 297 |
+
app_type,
|
| 298 |
+
screen_type,
|
| 299 |
+
style,
|
| 300 |
+
colors,
|
| 301 |
+
elements,
|
| 302 |
+
custom_prompt,
|
| 303 |
+
width,
|
| 304 |
+
height,
|
| 305 |
+
num_images
|
| 306 |
+
],
|
| 307 |
+
outputs=[output_image, output_info]
|
| 308 |
+
)
|
| 309 |
+
|
| 310 |
+
gr.Markdown("""
|
| 311 |
+
---
|
| 312 |
+
|
| 313 |
+
### Tips:
|
| 314 |
+
- Check your credits before generating
|
| 315 |
+
- Mobile screens are typically 375x812 (iPhone) or 360x800 (Android)
|
| 316 |
+
- Desktop mockups work well at 1024x768
|
| 317 |
+
- Be specific in your prompts for better results
|
| 318 |
+
- Use negative prompts to avoid unwanted elements
|
| 319 |
+
|
| 320 |
+
### Cost:
|
| 321 |
+
- Each generation costs approximately 10-20 credits
|
| 322 |
+
- Higher resolution and more images = higher cost
|
| 323 |
+
""")
|
| 324 |
+
|
| 325 |
+
# Launch
|
| 326 |
+
if __name__ == "__main__":
|
| 327 |
+
demo.launch()
|
leonardo/__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .client import LeonardoClient
|
| 2 |
+
|
| 3 |
+
__all__ = ["LeonardoClient"]
|
leonardo/client.py
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Leonardo AI API Client
|
| 3 |
+
Handles image generation with Leonardo.ai API
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import time
|
| 8 |
+
import httpx
|
| 9 |
+
import asyncio
|
| 10 |
+
from typing import Dict, List, Optional
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class LeonardoClient:
|
| 14 |
+
"""
|
| 15 |
+
Leonardo.ai API client for image generation
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
BASE_URL = "https://cloud.leonardo.ai/api/rest/v1"
|
| 19 |
+
|
| 20 |
+
# Model IDs
|
| 21 |
+
PHOENIX_MODEL_ID = "5c232a9e-9061-4777-980a-ddc8e65647c6" # Phoenix
|
| 22 |
+
KINO_MODEL_ID = "aa77f04e-3eec-4034-9c07-d0f619684628" # Kino XL
|
| 23 |
+
|
| 24 |
+
def __init__(self, api_key: Optional[str] = None):
|
| 25 |
+
self.api_key = api_key or os.getenv("LEONARDO_API_KEY")
|
| 26 |
+
if not self.api_key:
|
| 27 |
+
raise ValueError("Leonardo API key not found. Set LEONARDO_API_KEY environment variable.")
|
| 28 |
+
|
| 29 |
+
self.headers = {
|
| 30 |
+
"Authorization": f"Bearer {self.api_key}",
|
| 31 |
+
"Content-Type": "application/json"
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
async def generate_image(
|
| 35 |
+
self,
|
| 36 |
+
prompt: str,
|
| 37 |
+
negative_prompt: str = "",
|
| 38 |
+
width: int = 375,
|
| 39 |
+
height: int = 812,
|
| 40 |
+
model_id: str = None,
|
| 41 |
+
num_images: int = 1,
|
| 42 |
+
guidance_scale: int = 7,
|
| 43 |
+
prompt_magic: bool = True,
|
| 44 |
+
alchemy: bool = True
|
| 45 |
+
) -> Dict:
|
| 46 |
+
"""
|
| 47 |
+
Generate image with Leonardo.ai
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
prompt: Detailed image description
|
| 51 |
+
negative_prompt: What to avoid
|
| 52 |
+
width: Image width (must be multiple of 8)
|
| 53 |
+
height: Image height (must be multiple of 8)
|
| 54 |
+
model_id: Model to use (default: Phoenix)
|
| 55 |
+
num_images: Number of variations (1-4)
|
| 56 |
+
guidance_scale: How closely to follow prompt (1-20)
|
| 57 |
+
prompt_magic: Enable prompt enhancement
|
| 58 |
+
alchemy: Enable alchemy (better quality)
|
| 59 |
+
|
| 60 |
+
Returns:
|
| 61 |
+
{
|
| 62 |
+
"generation_id": "xxx",
|
| 63 |
+
"status": "PENDING",
|
| 64 |
+
"api_credit_cost": 10
|
| 65 |
+
}
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
model_id = model_id or self.PHOENIX_MODEL_ID
|
| 69 |
+
|
| 70 |
+
payload = {
|
| 71 |
+
"modelId": model_id,
|
| 72 |
+
"prompt": prompt,
|
| 73 |
+
"negative_prompt": negative_prompt,
|
| 74 |
+
"width": width,
|
| 75 |
+
"height": height,
|
| 76 |
+
"num_images": num_images,
|
| 77 |
+
"guidance_scale": guidance_scale,
|
| 78 |
+
"sd_version": "SDXL_1_0",
|
| 79 |
+
"presetStyle": "DYNAMIC",
|
| 80 |
+
"promptMagic": prompt_magic,
|
| 81 |
+
"alchemy": alchemy,
|
| 82 |
+
"photoReal": False,
|
| 83 |
+
"public": False
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 87 |
+
response = await client.post(
|
| 88 |
+
f"{self.BASE_URL}/generations",
|
| 89 |
+
headers=self.headers,
|
| 90 |
+
json=payload
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
response.raise_for_status()
|
| 94 |
+
data = response.json()
|
| 95 |
+
|
| 96 |
+
return {
|
| 97 |
+
"generation_id": data["sdGenerationJob"]["generationId"],
|
| 98 |
+
"status": "PENDING",
|
| 99 |
+
"api_credit_cost": data["sdGenerationJob"].get("apiCreditCost", 0)
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
async def get_generation(self, generation_id: str) -> Dict:
|
| 103 |
+
"""
|
| 104 |
+
Get generation status and results
|
| 105 |
+
|
| 106 |
+
Returns:
|
| 107 |
+
{
|
| 108 |
+
"status": "PENDING|COMPLETE|FAILED",
|
| 109 |
+
"images": [
|
| 110 |
+
{
|
| 111 |
+
"url": "https://...",
|
| 112 |
+
"id": "xxx"
|
| 113 |
+
}
|
| 114 |
+
]
|
| 115 |
+
}
|
| 116 |
+
"""
|
| 117 |
+
|
| 118 |
+
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 119 |
+
response = await client.get(
|
| 120 |
+
f"{self.BASE_URL}/generations/{generation_id}",
|
| 121 |
+
headers=self.headers
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
response.raise_for_status()
|
| 125 |
+
data = response.json()
|
| 126 |
+
|
| 127 |
+
generation = data["generations_by_pk"]
|
| 128 |
+
|
| 129 |
+
return {
|
| 130 |
+
"status": generation["status"],
|
| 131 |
+
"images": [
|
| 132 |
+
{
|
| 133 |
+
"url": img["url"],
|
| 134 |
+
"id": img["id"]
|
| 135 |
+
}
|
| 136 |
+
for img in generation.get("generated_images", [])
|
| 137 |
+
]
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
async def wait_for_generation(
|
| 141 |
+
self,
|
| 142 |
+
generation_id: str,
|
| 143 |
+
max_wait_seconds: int = 120,
|
| 144 |
+
poll_interval: int = 3
|
| 145 |
+
) -> Dict:
|
| 146 |
+
"""
|
| 147 |
+
Wait for generation to complete
|
| 148 |
+
|
| 149 |
+
Returns:
|
| 150 |
+
Same as get_generation() when status is COMPLETE
|
| 151 |
+
"""
|
| 152 |
+
|
| 153 |
+
start_time = time.time()
|
| 154 |
+
|
| 155 |
+
while True:
|
| 156 |
+
result = await self.get_generation(generation_id)
|
| 157 |
+
|
| 158 |
+
if result["status"] == "COMPLETE":
|
| 159 |
+
return result
|
| 160 |
+
|
| 161 |
+
if result["status"] == "FAILED":
|
| 162 |
+
raise Exception("Leonardo generation failed")
|
| 163 |
+
|
| 164 |
+
elapsed = time.time() - start_time
|
| 165 |
+
if elapsed > max_wait_seconds:
|
| 166 |
+
raise TimeoutError(f"Generation timeout after {max_wait_seconds}s")
|
| 167 |
+
|
| 168 |
+
await asyncio.sleep(poll_interval)
|
| 169 |
+
|
| 170 |
+
async def generate_and_wait(
|
| 171 |
+
self,
|
| 172 |
+
prompt: str,
|
| 173 |
+
**kwargs
|
| 174 |
+
) -> List[str]:
|
| 175 |
+
"""
|
| 176 |
+
Generate image and wait for completion
|
| 177 |
+
|
| 178 |
+
Returns:
|
| 179 |
+
List of image URLs
|
| 180 |
+
"""
|
| 181 |
+
|
| 182 |
+
# Start generation
|
| 183 |
+
gen_result = await self.generate_image(prompt, **kwargs)
|
| 184 |
+
generation_id = gen_result["generation_id"]
|
| 185 |
+
|
| 186 |
+
# Wait for completion
|
| 187 |
+
result = await self.wait_for_generation(generation_id)
|
| 188 |
+
|
| 189 |
+
# Return URLs
|
| 190 |
+
return [img["url"] for img in result["images"]]
|
| 191 |
+
|
| 192 |
+
async def get_user_info(self) -> Dict:
|
| 193 |
+
"""
|
| 194 |
+
Get user information and credit balance
|
| 195 |
+
|
| 196 |
+
Returns:
|
| 197 |
+
{
|
| 198 |
+
"username": "xxx",
|
| 199 |
+
"api_credits": 1000,
|
| 200 |
+
"subscription_tokens": 500
|
| 201 |
+
}
|
| 202 |
+
"""
|
| 203 |
+
|
| 204 |
+
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 205 |
+
response = await client.get(
|
| 206 |
+
f"{self.BASE_URL}/me",
|
| 207 |
+
headers=self.headers
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
response.raise_for_status()
|
| 211 |
+
data = response.json()
|
| 212 |
+
|
| 213 |
+
user = data["user_details"][0]
|
| 214 |
+
|
| 215 |
+
return {
|
| 216 |
+
"username": user["user"]["username"],
|
| 217 |
+
"api_credits": user.get("apiCreditBalance", 0),
|
| 218 |
+
"subscription_tokens": user.get("subscriptionTokens", 0)
|
| 219 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.44.0
|
| 2 |
+
httpx==0.27.0
|
| 3 |
+
python-dotenv==1.0.0
|
| 4 |
+
Pillow==10.4.0
|
| 5 |
+
huggingface-hub>=0.24.0
|