Update app.py
Browse files
app.py
CHANGED
|
@@ -1,424 +1,424 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
-
import os
|
| 4 |
-
import time
|
| 5 |
-
from PIL import Image
|
| 6 |
-
import io
|
| 7 |
-
import base64
|
| 8 |
-
import json
|
| 9 |
-
|
| 10 |
-
# Configuration
|
| 11 |
-
MIRAGIC_API_URL = os.getenv("MIRAGIC_API_URL")
|
| 12 |
-
COMPANY_NAME = "Miragic"
|
| 13 |
-
COMPANY_URL = "https://www.miragic.ai"
|
| 14 |
-
CONTACT_EMAIL = "info@miragic.ai"
|
| 15 |
-
|
| 16 |
-
def generate_image_via_api(prompt, model, width, height, seed, image_url, enhance, safe):
|
| 17 |
-
"""
|
| 18 |
-
Call the Flask API to generate an image
|
| 19 |
-
"""
|
| 20 |
-
try:
|
| 21 |
-
# Prepare the request data
|
| 22 |
-
data = {
|
| 23 |
-
"prompt": prompt,
|
| 24 |
-
"model": model,
|
| 25 |
-
"width": width,
|
| 26 |
-
"height": height,
|
| 27 |
-
"enhance": enhance,
|
| 28 |
-
"safe": safe
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
if seed and seed != -1:
|
| 32 |
-
data["seed"] = seed
|
| 33 |
-
|
| 34 |
-
if image_url:
|
| 35 |
-
data["image_url"] = image_url
|
| 36 |
-
|
| 37 |
-
# Validate kontext model requirements
|
| 38 |
-
if model.lower() == "kontext" and not image_url:
|
| 39 |
-
raise gr.Error("Image URL is required for kontext model (image-to-image generation)")
|
| 40 |
-
|
| 41 |
-
# Make the API request
|
| 42 |
-
response = requests.post(
|
| 43 |
-
f"{MIRAGIC_API_URL}/generate_image",
|
| 44 |
-
json=data,
|
| 45 |
-
timeout=300 # Increased timeout for image generation
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
if response.status_code == 200:
|
| 49 |
-
result = response.json()
|
| 50 |
-
if result["status"] == "success":
|
| 51 |
-
# Download the image from Dropbox link
|
| 52 |
-
image_url = result["link"]
|
| 53 |
-
image_response = requests.get(image_url, timeout=30)
|
| 54 |
-
|
| 55 |
-
if image_response.status_code == 200:
|
| 56 |
-
# Convert to PIL Image
|
| 57 |
-
image = Image.open(io.BytesIO(image_response.content))
|
| 58 |
-
return image, gr.update(visible=True)
|
| 59 |
-
else:
|
| 60 |
-
return None, gr.update(visible=False)
|
| 61 |
-
else:
|
| 62 |
-
raise gr.Error(f"API Error: {result.get('error', 'Unknown error')}")
|
| 63 |
-
else:
|
| 64 |
-
raise gr.Error(f"HTTP Error: {response.status_code} - {response.text}")
|
| 65 |
-
|
| 66 |
-
except requests.exceptions.RequestException as e:
|
| 67 |
-
raise gr.Error(f"Request Error: {str(e)}")
|
| 68 |
-
except Exception as e:
|
| 69 |
-
raise gr.Error(f"Unexpected Error: {str(e)}")
|
| 70 |
-
|
| 71 |
-
def get_available_models():
|
| 72 |
-
"""
|
| 73 |
-
Get available models from the Flask API
|
| 74 |
-
"""
|
| 75 |
-
try:
|
| 76 |
-
response = requests.get(f"{MIRAGIC_API_URL}/available_models", timeout=10)
|
| 77 |
-
if response.status_code == 200:
|
| 78 |
-
result = response.json()
|
| 79 |
-
if result["status"] == "success":
|
| 80 |
-
return result["models"]
|
| 81 |
-
return ["flux", "kontext", "dreamshaper", "realistic", "anime"]
|
| 82 |
-
except:
|
| 83 |
-
return ["flux", "kontext", "dreamshaper", "realistic", "anime"]
|
| 84 |
-
|
| 85 |
-
def handle_like_action():
|
| 86 |
-
"""Handle like button click"""
|
| 87 |
-
return gr.update(
|
| 88 |
-
value="β€οΈ Thanks for liking! Please star our repo!",
|
| 89 |
-
interactive=False,
|
| 90 |
-
variant="secondary"
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
def create_footer_html():
|
| 94 |
-
"""Create footer HTML"""
|
| 95 |
-
return f"""
|
| 96 |
-
<div style="text-align: center; margin-top: 30px; padding: 15px; border-radius: 8px;">
|
| 97 |
-
<p style="margin: 5px 0;">Β© 2025 {COMPANY_NAME}. All rights reserved.</p>
|
| 98 |
-
<p style="margin: 5px 0;">
|
| 99 |
-
<a href="{COMPANY_URL}" target="_blank" style="color: #0984e3; text-decoration: none;">Website</a> |
|
| 100 |
-
<a href="mailto:{CONTACT_EMAIL}" style="color: #0984e3; text-decoration: none;">Contact Us</a> |
|
| 101 |
-
<a href="{COMPANY_URL}/privacy-policy" target="_blank" style="color: #0984e3; text-decoration: none;">Privacy Policy</a>
|
| 102 |
-
</p>
|
| 103 |
-
</div>
|
| 104 |
-
"""
|
| 105 |
-
|
| 106 |
-
def create_instructions_html():
|
| 107 |
-
"""Create instructions HTML"""
|
| 108 |
-
return """
|
| 109 |
-
<div style="padding: 15px; border-radius: 8px; margin-bottom: 20px;">
|
| 110 |
-
<h2 style="margin-top: 0;">How to use:</h2>
|
| 111 |
-
<ol style="color: #636e72;">
|
| 112 |
-
<li>Enter a detailed description of the image you want to generate</li>
|
| 113 |
-
<li>Select a model (different models have different styles)</li>
|
| 114 |
-
<li>For 'kontext' model, provide an image URL for image-to-image generation</li>
|
| 115 |
-
<li>Adjust the image dimensions and other parameters</li>
|
| 116 |
-
<li>Click "Generate Image" to create your AI artwork</li>
|
| 117 |
-
</ol>
|
| 118 |
-
<p style="margin-bottom: 0;"><strong>Tip:</strong> Be specific in your prompts for better results!</p>
|
| 119 |
-
</div>
|
| 120 |
-
"""
|
| 121 |
-
|
| 122 |
-
def get_css_styles():
|
| 123 |
-
"""Get CSS styles for the interface"""
|
| 124 |
-
return """
|
| 125 |
-
footer {visibility: hidden}
|
| 126 |
-
.banner {
|
| 127 |
-
background-color: #f8f9fa;
|
| 128 |
-
padding: 10px;
|
| 129 |
-
border-radius: 5px;
|
| 130 |
-
margin-bottom: 20px;
|
| 131 |
-
text-align: center;
|
| 132 |
-
}
|
| 133 |
-
.button-gradient {
|
| 134 |
-
background: linear-gradient(45deg, #ff416c, #ff4b2b, #ff9b00, #ff416c);
|
| 135 |
-
background-size: 400% 400%;
|
| 136 |
-
border: none;
|
| 137 |
-
padding: 14px 28px;
|
| 138 |
-
font-size: 16px;
|
| 139 |
-
font-weight: bold;
|
| 140 |
-
color: white;
|
| 141 |
-
border-radius: 10px;
|
| 142 |
-
cursor: pointer;
|
| 143 |
-
transition: 0.3s ease-in-out;
|
| 144 |
-
animation: gradientAnimation 2s infinite linear;
|
| 145 |
-
box-shadow: 0 4px 10px rgba(255, 65, 108, 0.6);
|
| 146 |
-
}
|
| 147 |
-
@keyframes gradientAnimation {
|
| 148 |
-
0% { background-position: 0% 50%; }
|
| 149 |
-
100% { background-position: 100% 50%; }
|
| 150 |
-
}
|
| 151 |
-
.button-gradient:hover {
|
| 152 |
-
transform: scale(1.05);
|
| 153 |
-
box-shadow: 0 6px 15px rgba(255, 75, 43, 0.8);
|
| 154 |
-
}
|
| 155 |
-
.signup-container {
|
| 156 |
-
text-align: center;
|
| 157 |
-
padding: 20px;
|
| 158 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 159 |
-
border-radius: 8px;
|
| 160 |
-
margin-top: 10px;
|
| 161 |
-
color: white;
|
| 162 |
-
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
|
| 163 |
-
}
|
| 164 |
-
.signup-container h3 {
|
| 165 |
-
margin-bottom: 10px;
|
| 166 |
-
color: white;
|
| 167 |
-
}
|
| 168 |
-
.signup-container p {
|
| 169 |
-
margin-bottom: 15px;
|
| 170 |
-
color: #f0f0f0;
|
| 171 |
-
}
|
| 172 |
-
.signup-button {
|
| 173 |
-
background: linear-gradient(45deg, #ff416c, #ff4b2b);
|
| 174 |
-
border: none;
|
| 175 |
-
padding: 12px 25px;
|
| 176 |
-
font-size: 16px;
|
| 177 |
-
font-weight: bold;
|
| 178 |
-
color: white;
|
| 179 |
-
border-radius: 8px;
|
| 180 |
-
text-decoration: none;
|
| 181 |
-
display: inline-block;
|
| 182 |
-
transition: all 0.3s ease;
|
| 183 |
-
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
|
| 184 |
-
}
|
| 185 |
-
.signup-button:hover {
|
| 186 |
-
transform: translateY(-2px);
|
| 187 |
-
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
|
| 188 |
-
}
|
| 189 |
-
#col-container {
|
| 190 |
-
max-width: 1400px;
|
| 191 |
-
margin: 0 auto;
|
| 192 |
-
}
|
| 193 |
-
.step-column {
|
| 194 |
-
padding: 20px;
|
| 195 |
-
border-radius: 8px;
|
| 196 |
-
box-shadow: var(--card-shadow);
|
| 197 |
-
margin: 10px;
|
| 198 |
-
}
|
| 199 |
-
.step-title {
|
| 200 |
-
color: var(--primary-color);
|
| 201 |
-
text-align: center;
|
| 202 |
-
margin-bottom: 15px;
|
| 203 |
-
}
|
| 204 |
-
.image-preview {
|
| 205 |
-
border-radius: 8px;
|
| 206 |
-
overflow: hidden;
|
| 207 |
-
box-shadow: var(--card-shadow);
|
| 208 |
-
}
|
| 209 |
-
.result-section {
|
| 210 |
-
background-color: white;
|
| 211 |
-
padding: 20px;
|
| 212 |
-
border-radius: 8px;
|
| 213 |
-
box-shadow: var(--card-shadow);
|
| 214 |
-
}
|
| 215 |
-
.footer {
|
| 216 |
-
margin-top: 30px;
|
| 217 |
-
text-align: center;
|
| 218 |
-
}
|
| 219 |
-
.like-button {
|
| 220 |
-
background: linear-gradient(45deg, #ff6b6b, #ff8e8e);
|
| 221 |
-
border: none;
|
| 222 |
-
padding: 10px 20px;
|
| 223 |
-
font-size: 14px;
|
| 224 |
-
font-weight: bold;
|
| 225 |
-
color: white;
|
| 226 |
-
border-radius: 8px;
|
| 227 |
-
cursor: pointer;
|
| 228 |
-
transition: all 0.3s ease;
|
| 229 |
-
box-shadow: 0 2px 8px rgba(255, 107, 107, 0.3);
|
| 230 |
-
}
|
| 231 |
-
.like-button:hover {
|
| 232 |
-
transform: translateY(-2px);
|
| 233 |
-
box-shadow: 0 4px 12px rgba(255, 107, 107, 0.5);
|
| 234 |
-
}
|
| 235 |
-
.interaction-section {
|
| 236 |
-
text-align: center;
|
| 237 |
-
padding: 20px;
|
| 238 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 239 |
-
border-radius: 8px;
|
| 240 |
-
margin-top: 20px;
|
| 241 |
-
color: white;
|
| 242 |
-
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
|
| 243 |
-
}
|
| 244 |
-
.kontext-notice {
|
| 245 |
-
background: #fff3cd;
|
| 246 |
-
padding: 10px;
|
| 247 |
-
border-radius: 5px;
|
| 248 |
-
margin: 10px 0;
|
| 249 |
-
border-left: 4px solid #ffc107;
|
| 250 |
-
color: #856404;
|
| 251 |
-
}
|
| 252 |
-
"""
|
| 253 |
-
|
| 254 |
-
# Create the Gradio interface
|
| 255 |
-
with gr.Blocks(css=get_css_styles(), title=f"{COMPANY_NAME} AI Image Generator", theme=gr.themes.Ocean()) as demo:
|
| 256 |
-
with gr.Row():
|
| 257 |
-
with gr.Column(0.7):
|
| 258 |
-
gr.Markdown(f"""
|
| 259 |
-
<div style="display: flex; align-items: center;">
|
| 260 |
-
<img src="https://avatars.githubusercontent.com/u/211682198?s=200&v=4" style="width: 80px; margin-right: 20px;"/>
|
| 261 |
-
<div>
|
| 262 |
-
<h1 style="margin-bottom: 0;">{COMPANY_NAME} AI Image Generator π¨</h1>
|
| 263 |
-
<p style="margin-top: 0; color: #636e72;">Create stunning AI-generated images with text prompts</p>
|
| 264 |
-
</div>
|
| 265 |
-
</div>
|
| 266 |
-
""")
|
| 267 |
-
|
| 268 |
-
gr.HTML(create_instructions_html())
|
| 269 |
-
with gr.Column(0.3):
|
| 270 |
-
gr.Markdown(f"""
|
| 271 |
-
<div style="display: flex; align-items: center;">
|
| 272 |
-
<img src="https://miragic.ai/products/qrcode-vto.png" style="width: 450px; margin-left: 100px;"/>
|
| 273 |
-
</div>
|
| 274 |
-
""")
|
| 275 |
-
|
| 276 |
-
with gr.Row(elem_id="col-container"):
|
| 277 |
-
# Step 1: Prompt and Settings
|
| 278 |
-
with gr.Column(elem_classes="step-column"):
|
| 279 |
-
gr.HTML("""
|
| 280 |
-
<div class="step-title">
|
| 281 |
-
<span style="font-size: 24px;">1. Enter Prompt & Settings</span><br>
|
| 282 |
-
</div>
|
| 283 |
-
""")
|
| 284 |
-
|
| 285 |
-
prompt = gr.Textbox(
|
| 286 |
-
label="Prompt",
|
| 287 |
-
lines=3,
|
| 288 |
-
placeholder="Describe the image you want to generate...",
|
| 289 |
-
value=""
|
| 290 |
-
)
|
| 291 |
-
|
| 292 |
-
with gr.Row():
|
| 293 |
-
model = gr.Dropdown(
|
| 294 |
-
choices=get_available_models(),
|
| 295 |
-
value="flux",
|
| 296 |
-
label="Model"
|
| 297 |
-
)
|
| 298 |
-
seed = gr.Number(
|
| 299 |
-
label="Seed",
|
| 300 |
-
value=-1,
|
| 301 |
-
info="-1 for random seed"
|
| 302 |
-
)
|
| 303 |
-
|
| 304 |
-
with gr.Row():
|
| 305 |
-
width = gr.Slider(
|
| 306 |
-
minimum=256,
|
| 307 |
-
maximum=2048,
|
| 308 |
-
value=1024,
|
| 309 |
-
step=64,
|
| 310 |
-
label="Width"
|
| 311 |
-
)
|
| 312 |
-
height = gr.Slider(
|
| 313 |
-
minimum=256,
|
| 314 |
-
maximum=2048,
|
| 315 |
-
value=1024,
|
| 316 |
-
step=64,
|
| 317 |
-
label="Height"
|
| 318 |
-
)
|
| 319 |
-
|
| 320 |
-
image_url = gr.Textbox(
|
| 321 |
-
label="Image URL (for kontext model only)",
|
| 322 |
-
placeholder="https://example.com/image.jpg",
|
| 323 |
-
info="Required for image-to-image generation with kontext model"
|
| 324 |
-
)
|
| 325 |
-
|
| 326 |
-
with gr.Row():
|
| 327 |
-
enhance = gr.Checkbox(label="Enhance Prompt", value=False, info="Use AI to enhance your prompt")
|
| 328 |
-
safe = gr.Checkbox(label="Safe Filter", value=False, info="Enable strict NSFW filtering")
|
| 329 |
-
|
| 330 |
-
gr.HTML("""
|
| 331 |
-
<div class="kontext-notice">
|
| 332 |
-
<strong>Note:</strong> For the 'kontext' model, you must provide an Image URL for image-to-image generation.
|
| 333 |
-
Example: "bake_a_cake_from_this_logo" with image URL of a logo.
|
| 334 |
-
</div>
|
| 335 |
-
""")
|
| 336 |
-
|
| 337 |
-
generate_btn = gr.Button(
|
| 338 |
-
"Generate Image π¨",
|
| 339 |
-
elem_classes="button-gradient"
|
| 340 |
-
)
|
| 341 |
-
|
| 342 |
-
# Step 2: Results
|
| 343 |
-
with gr.Column(elem_classes="step-column"):
|
| 344 |
-
gr.HTML("""
|
| 345 |
-
<div class="step-title">
|
| 346 |
-
<span style="font-size: 24px;">2. Generated Image</span><br>
|
| 347 |
-
</div>
|
| 348 |
-
""")
|
| 349 |
-
|
| 350 |
-
result_img = gr.Image(
|
| 351 |
-
label="Generated Image",
|
| 352 |
-
interactive=False,
|
| 353 |
-
elem_classes="image-preview",
|
| 354 |
-
height=400
|
| 355 |
-
)
|
| 356 |
-
|
| 357 |
-
with gr.Row():
|
| 358 |
-
like_button = gr.Button(
|
| 359 |
-
"π Like this result!",
|
| 360 |
-
elem_classes="like-button",
|
| 361 |
-
visible=False
|
| 362 |
-
)
|
| 363 |
-
|
| 364 |
-
gr.HTML("""
|
| 365 |
-
<div class="interaction-section">
|
| 366 |
-
<p style="margin: 5px 0;">If you like our AI Image Generator, please give us a β€οΈ into our space!</p>
|
| 367 |
-
</div>
|
| 368 |
-
""")
|
| 369 |
-
|
| 370 |
-
signup_prompt = gr.HTML(
|
| 371 |
-
visible=True,
|
| 372 |
-
value=f"""<div class="signup-container">
|
| 373 |
-
<h3>π Want more AI tools?</h3>
|
| 374 |
-
<p>Visit {COMPANY_NAME}.ai for unlimited access to all our AI tools!</p>
|
| 375 |
-
<a href='{COMPANY_URL}/
|
| 376 |
-
Explore More Tools π
|
| 377 |
-
</a>
|
| 378 |
-
</div>"""
|
| 379 |
-
)
|
| 380 |
-
|
| 381 |
-
# Examples
|
| 382 |
-
examples = [
|
| 383 |
-
["An epic fantasy landscape with floating islands, cascading waterfalls, ancient ruins, magical aurora borealis in the sky, digital painting, concept art, unreal engine 5, 4K wallpaper", "flux", 2048, 1024, -1, "", False, False],
|
| 384 |
-
["A serene Japanese garden with cherry blossoms, koi pond, traditional pagoda, morning mist, sunlight filtering through trees, peaceful atmosphere, ultra detailed", "flux", 2048, 1024, -1, "", True, False],
|
| 385 |
-
["bake_a_cake_from_this_logo", "kontext", 1024, 1024, -1, "https://avatars.githubusercontent.com/u/86964862", False, False],
|
| 386 |
-
["A minimalist modern living room with large windows, Scandinavian design, plants, cozy atmosphere, architectural rendering", "turbo", 1024, 1024, 42, "", False, True],
|
| 387 |
-
["A beautiful tropical beach with turquoise water, white sand, palm trees, sunset colors, relaxing vibe, 4K wallpaper", "turbo", 2048, 1024, -1, "", True, False],
|
| 388 |
-
]
|
| 389 |
-
|
| 390 |
-
gr.Examples(
|
| 391 |
-
examples=examples,
|
| 392 |
-
inputs=[prompt, model, width, height, seed, image_url, enhance, safe],
|
| 393 |
-
outputs=[result_img, like_button],
|
| 394 |
-
fn=generate_image_via_api,
|
| 395 |
-
cache_examples=False,
|
| 396 |
-
label="Example Prompts"
|
| 397 |
-
)
|
| 398 |
-
|
| 399 |
-
# Button actions
|
| 400 |
-
generate_btn.click(
|
| 401 |
-
fn=generate_image_via_api,
|
| 402 |
-
inputs=[prompt, model, width, height, seed, image_url, enhance, safe],
|
| 403 |
-
outputs=[result_img, like_button],
|
| 404 |
-
api_name="generate_image"
|
| 405 |
-
)
|
| 406 |
-
|
| 407 |
-
like_button.click(
|
| 408 |
-
fn=handle_like_action,
|
| 409 |
-
inputs=[],
|
| 410 |
-
outputs=[like_button]
|
| 411 |
-
)
|
| 412 |
-
|
| 413 |
-
# Visitor badge
|
| 414 |
-
gr.HTML(f'<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2F{COMPANY_NAME}-AI%2F{COMPANY_NAME}-AI-Image-Generator"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2F{COMPANY_NAME}-AI%2F{COMPANY_NAME}-AI-Image-Generator&label=VISITORS&labelColor=%2337d67a&countColor=%23f47373&style=plastic&labelStyle=upper" /></a>')
|
| 415 |
-
|
| 416 |
-
# Footer
|
| 417 |
-
gr.HTML(create_footer_html())
|
| 418 |
-
|
| 419 |
-
if __name__ == "__main__":
|
| 420 |
-
demo.launch(
|
| 421 |
-
show_api=False,
|
| 422 |
-
server_name="0.0.0.0",
|
| 423 |
-
server_port=7860
|
| 424 |
)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
import base64
|
| 8 |
+
import json
|
| 9 |
+
|
| 10 |
+
# Configuration
|
| 11 |
+
MIRAGIC_API_URL = os.getenv("MIRAGIC_API_URL")
|
| 12 |
+
COMPANY_NAME = "Miragic"
|
| 13 |
+
COMPANY_URL = "https://www.miragic.ai"
|
| 14 |
+
CONTACT_EMAIL = "info@miragic.ai"
|
| 15 |
+
|
| 16 |
+
def generate_image_via_api(prompt, model, width, height, seed, image_url, enhance, safe):
|
| 17 |
+
"""
|
| 18 |
+
Call the Flask API to generate an image
|
| 19 |
+
"""
|
| 20 |
+
try:
|
| 21 |
+
# Prepare the request data
|
| 22 |
+
data = {
|
| 23 |
+
"prompt": prompt,
|
| 24 |
+
"model": model,
|
| 25 |
+
"width": width,
|
| 26 |
+
"height": height,
|
| 27 |
+
"enhance": enhance,
|
| 28 |
+
"safe": safe
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
if seed and seed != -1:
|
| 32 |
+
data["seed"] = seed
|
| 33 |
+
|
| 34 |
+
if image_url:
|
| 35 |
+
data["image_url"] = image_url
|
| 36 |
+
|
| 37 |
+
# Validate kontext model requirements
|
| 38 |
+
if model.lower() == "kontext" and not image_url:
|
| 39 |
+
raise gr.Error("Image URL is required for kontext model (image-to-image generation)")
|
| 40 |
+
|
| 41 |
+
# Make the API request
|
| 42 |
+
response = requests.post(
|
| 43 |
+
f"{MIRAGIC_API_URL}/generate_image",
|
| 44 |
+
json=data,
|
| 45 |
+
timeout=300 # Increased timeout for image generation
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
if response.status_code == 200:
|
| 49 |
+
result = response.json()
|
| 50 |
+
if result["status"] == "success":
|
| 51 |
+
# Download the image from Dropbox link
|
| 52 |
+
image_url = result["link"]
|
| 53 |
+
image_response = requests.get(image_url, timeout=30)
|
| 54 |
+
|
| 55 |
+
if image_response.status_code == 200:
|
| 56 |
+
# Convert to PIL Image
|
| 57 |
+
image = Image.open(io.BytesIO(image_response.content))
|
| 58 |
+
return image, gr.update(visible=True)
|
| 59 |
+
else:
|
| 60 |
+
return None, gr.update(visible=False)
|
| 61 |
+
else:
|
| 62 |
+
raise gr.Error(f"API Error: {result.get('error', 'Unknown error')}")
|
| 63 |
+
else:
|
| 64 |
+
raise gr.Error(f"HTTP Error: {response.status_code} - {response.text}")
|
| 65 |
+
|
| 66 |
+
except requests.exceptions.RequestException as e:
|
| 67 |
+
raise gr.Error(f"Request Error: {str(e)}")
|
| 68 |
+
except Exception as e:
|
| 69 |
+
raise gr.Error(f"Unexpected Error: {str(e)}")
|
| 70 |
+
|
| 71 |
+
def get_available_models():
|
| 72 |
+
"""
|
| 73 |
+
Get available models from the Flask API
|
| 74 |
+
"""
|
| 75 |
+
try:
|
| 76 |
+
response = requests.get(f"{MIRAGIC_API_URL}/available_models", timeout=10)
|
| 77 |
+
if response.status_code == 200:
|
| 78 |
+
result = response.json()
|
| 79 |
+
if result["status"] == "success":
|
| 80 |
+
return result["models"]
|
| 81 |
+
return ["flux", "kontext", "dreamshaper", "realistic", "anime"]
|
| 82 |
+
except:
|
| 83 |
+
return ["flux", "kontext", "dreamshaper", "realistic", "anime"]
|
| 84 |
+
|
| 85 |
+
def handle_like_action():
|
| 86 |
+
"""Handle like button click"""
|
| 87 |
+
return gr.update(
|
| 88 |
+
value="β€οΈ Thanks for liking! Please star our repo!",
|
| 89 |
+
interactive=False,
|
| 90 |
+
variant="secondary"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
def create_footer_html():
|
| 94 |
+
"""Create footer HTML"""
|
| 95 |
+
return f"""
|
| 96 |
+
<div style="text-align: center; margin-top: 30px; padding: 15px; border-radius: 8px;">
|
| 97 |
+
<p style="margin: 5px 0;">Β© 2025 {COMPANY_NAME}. All rights reserved.</p>
|
| 98 |
+
<p style="margin: 5px 0;">
|
| 99 |
+
<a href="{COMPANY_URL}" target="_blank" style="color: #0984e3; text-decoration: none;">Website</a> |
|
| 100 |
+
<a href="mailto:{CONTACT_EMAIL}" style="color: #0984e3; text-decoration: none;">Contact Us</a> |
|
| 101 |
+
<a href="{COMPANY_URL}/privacy-policy" target="_blank" style="color: #0984e3; text-decoration: none;">Privacy Policy</a>
|
| 102 |
+
</p>
|
| 103 |
+
</div>
|
| 104 |
+
"""
|
| 105 |
+
|
| 106 |
+
def create_instructions_html():
|
| 107 |
+
"""Create instructions HTML"""
|
| 108 |
+
return """
|
| 109 |
+
<div style="padding: 15px; border-radius: 8px; margin-bottom: 20px;">
|
| 110 |
+
<h2 style="margin-top: 0;">How to use:</h2>
|
| 111 |
+
<ol style="color: #636e72;">
|
| 112 |
+
<li>Enter a detailed description of the image you want to generate</li>
|
| 113 |
+
<li>Select a model (different models have different styles)</li>
|
| 114 |
+
<li>For 'kontext' model, provide an image URL for image-to-image generation</li>
|
| 115 |
+
<li>Adjust the image dimensions and other parameters</li>
|
| 116 |
+
<li>Click "Generate Image" to create your AI artwork</li>
|
| 117 |
+
</ol>
|
| 118 |
+
<p style="margin-bottom: 0;"><strong>Tip:</strong> Be specific in your prompts for better results!</p>
|
| 119 |
+
</div>
|
| 120 |
+
"""
|
| 121 |
+
|
| 122 |
+
def get_css_styles():
|
| 123 |
+
"""Get CSS styles for the interface"""
|
| 124 |
+
return """
|
| 125 |
+
footer {visibility: hidden}
|
| 126 |
+
.banner {
|
| 127 |
+
background-color: #f8f9fa;
|
| 128 |
+
padding: 10px;
|
| 129 |
+
border-radius: 5px;
|
| 130 |
+
margin-bottom: 20px;
|
| 131 |
+
text-align: center;
|
| 132 |
+
}
|
| 133 |
+
.button-gradient {
|
| 134 |
+
background: linear-gradient(45deg, #ff416c, #ff4b2b, #ff9b00, #ff416c);
|
| 135 |
+
background-size: 400% 400%;
|
| 136 |
+
border: none;
|
| 137 |
+
padding: 14px 28px;
|
| 138 |
+
font-size: 16px;
|
| 139 |
+
font-weight: bold;
|
| 140 |
+
color: white;
|
| 141 |
+
border-radius: 10px;
|
| 142 |
+
cursor: pointer;
|
| 143 |
+
transition: 0.3s ease-in-out;
|
| 144 |
+
animation: gradientAnimation 2s infinite linear;
|
| 145 |
+
box-shadow: 0 4px 10px rgba(255, 65, 108, 0.6);
|
| 146 |
+
}
|
| 147 |
+
@keyframes gradientAnimation {
|
| 148 |
+
0% { background-position: 0% 50%; }
|
| 149 |
+
100% { background-position: 100% 50%; }
|
| 150 |
+
}
|
| 151 |
+
.button-gradient:hover {
|
| 152 |
+
transform: scale(1.05);
|
| 153 |
+
box-shadow: 0 6px 15px rgba(255, 75, 43, 0.8);
|
| 154 |
+
}
|
| 155 |
+
.signup-container {
|
| 156 |
+
text-align: center;
|
| 157 |
+
padding: 20px;
|
| 158 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 159 |
+
border-radius: 8px;
|
| 160 |
+
margin-top: 10px;
|
| 161 |
+
color: white;
|
| 162 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
|
| 163 |
+
}
|
| 164 |
+
.signup-container h3 {
|
| 165 |
+
margin-bottom: 10px;
|
| 166 |
+
color: white;
|
| 167 |
+
}
|
| 168 |
+
.signup-container p {
|
| 169 |
+
margin-bottom: 15px;
|
| 170 |
+
color: #f0f0f0;
|
| 171 |
+
}
|
| 172 |
+
.signup-button {
|
| 173 |
+
background: linear-gradient(45deg, #ff416c, #ff4b2b);
|
| 174 |
+
border: none;
|
| 175 |
+
padding: 12px 25px;
|
| 176 |
+
font-size: 16px;
|
| 177 |
+
font-weight: bold;
|
| 178 |
+
color: white;
|
| 179 |
+
border-radius: 8px;
|
| 180 |
+
text-decoration: none;
|
| 181 |
+
display: inline-block;
|
| 182 |
+
transition: all 0.3s ease;
|
| 183 |
+
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
|
| 184 |
+
}
|
| 185 |
+
.signup-button:hover {
|
| 186 |
+
transform: translateY(-2px);
|
| 187 |
+
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
|
| 188 |
+
}
|
| 189 |
+
#col-container {
|
| 190 |
+
max-width: 1400px;
|
| 191 |
+
margin: 0 auto;
|
| 192 |
+
}
|
| 193 |
+
.step-column {
|
| 194 |
+
padding: 20px;
|
| 195 |
+
border-radius: 8px;
|
| 196 |
+
box-shadow: var(--card-shadow);
|
| 197 |
+
margin: 10px;
|
| 198 |
+
}
|
| 199 |
+
.step-title {
|
| 200 |
+
color: var(--primary-color);
|
| 201 |
+
text-align: center;
|
| 202 |
+
margin-bottom: 15px;
|
| 203 |
+
}
|
| 204 |
+
.image-preview {
|
| 205 |
+
border-radius: 8px;
|
| 206 |
+
overflow: hidden;
|
| 207 |
+
box-shadow: var(--card-shadow);
|
| 208 |
+
}
|
| 209 |
+
.result-section {
|
| 210 |
+
background-color: white;
|
| 211 |
+
padding: 20px;
|
| 212 |
+
border-radius: 8px;
|
| 213 |
+
box-shadow: var(--card-shadow);
|
| 214 |
+
}
|
| 215 |
+
.footer {
|
| 216 |
+
margin-top: 30px;
|
| 217 |
+
text-align: center;
|
| 218 |
+
}
|
| 219 |
+
.like-button {
|
| 220 |
+
background: linear-gradient(45deg, #ff6b6b, #ff8e8e);
|
| 221 |
+
border: none;
|
| 222 |
+
padding: 10px 20px;
|
| 223 |
+
font-size: 14px;
|
| 224 |
+
font-weight: bold;
|
| 225 |
+
color: white;
|
| 226 |
+
border-radius: 8px;
|
| 227 |
+
cursor: pointer;
|
| 228 |
+
transition: all 0.3s ease;
|
| 229 |
+
box-shadow: 0 2px 8px rgba(255, 107, 107, 0.3);
|
| 230 |
+
}
|
| 231 |
+
.like-button:hover {
|
| 232 |
+
transform: translateY(-2px);
|
| 233 |
+
box-shadow: 0 4px 12px rgba(255, 107, 107, 0.5);
|
| 234 |
+
}
|
| 235 |
+
.interaction-section {
|
| 236 |
+
text-align: center;
|
| 237 |
+
padding: 20px;
|
| 238 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 239 |
+
border-radius: 8px;
|
| 240 |
+
margin-top: 20px;
|
| 241 |
+
color: white;
|
| 242 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
|
| 243 |
+
}
|
| 244 |
+
.kontext-notice {
|
| 245 |
+
background: #fff3cd;
|
| 246 |
+
padding: 10px;
|
| 247 |
+
border-radius: 5px;
|
| 248 |
+
margin: 10px 0;
|
| 249 |
+
border-left: 4px solid #ffc107;
|
| 250 |
+
color: #856404;
|
| 251 |
+
}
|
| 252 |
+
"""
|
| 253 |
+
|
| 254 |
+
# Create the Gradio interface
|
| 255 |
+
with gr.Blocks(css=get_css_styles(), title=f"{COMPANY_NAME} AI Image Generator", theme=gr.themes.Ocean()) as demo:
|
| 256 |
+
with gr.Row():
|
| 257 |
+
with gr.Column(0.7):
|
| 258 |
+
gr.Markdown(f"""
|
| 259 |
+
<div style="display: flex; align-items: center;">
|
| 260 |
+
<img src="https://avatars.githubusercontent.com/u/211682198?s=200&v=4" style="width: 80px; margin-right: 20px;"/>
|
| 261 |
+
<div>
|
| 262 |
+
<h1 style="margin-bottom: 0;">{COMPANY_NAME} AI Image Generator π¨</h1>
|
| 263 |
+
<p style="margin-top: 0; color: #636e72;">Create stunning AI-generated images with text prompts</p>
|
| 264 |
+
</div>
|
| 265 |
+
</div>
|
| 266 |
+
""")
|
| 267 |
+
|
| 268 |
+
gr.HTML(create_instructions_html())
|
| 269 |
+
with gr.Column(0.3):
|
| 270 |
+
gr.Markdown(f"""
|
| 271 |
+
<div style="display: flex; align-items: center;">
|
| 272 |
+
<img src="https://miragic.ai/products/qrcode-vto.png" style="width: 450px; margin-left: 100px;"/>
|
| 273 |
+
</div>
|
| 274 |
+
""")
|
| 275 |
+
|
| 276 |
+
with gr.Row(elem_id="col-container"):
|
| 277 |
+
# Step 1: Prompt and Settings
|
| 278 |
+
with gr.Column(elem_classes="step-column"):
|
| 279 |
+
gr.HTML("""
|
| 280 |
+
<div class="step-title">
|
| 281 |
+
<span style="font-size: 24px;">1. Enter Prompt & Settings</span><br>
|
| 282 |
+
</div>
|
| 283 |
+
""")
|
| 284 |
+
|
| 285 |
+
prompt = gr.Textbox(
|
| 286 |
+
label="Prompt",
|
| 287 |
+
lines=3,
|
| 288 |
+
placeholder="Describe the image you want to generate...",
|
| 289 |
+
value=""
|
| 290 |
+
)
|
| 291 |
+
|
| 292 |
+
with gr.Row():
|
| 293 |
+
model = gr.Dropdown(
|
| 294 |
+
choices=get_available_models(),
|
| 295 |
+
value="flux",
|
| 296 |
+
label="Model"
|
| 297 |
+
)
|
| 298 |
+
seed = gr.Number(
|
| 299 |
+
label="Seed",
|
| 300 |
+
value=-1,
|
| 301 |
+
info="-1 for random seed"
|
| 302 |
+
)
|
| 303 |
+
|
| 304 |
+
with gr.Row():
|
| 305 |
+
width = gr.Slider(
|
| 306 |
+
minimum=256,
|
| 307 |
+
maximum=2048,
|
| 308 |
+
value=1024,
|
| 309 |
+
step=64,
|
| 310 |
+
label="Width"
|
| 311 |
+
)
|
| 312 |
+
height = gr.Slider(
|
| 313 |
+
minimum=256,
|
| 314 |
+
maximum=2048,
|
| 315 |
+
value=1024,
|
| 316 |
+
step=64,
|
| 317 |
+
label="Height"
|
| 318 |
+
)
|
| 319 |
+
|
| 320 |
+
image_url = gr.Textbox(
|
| 321 |
+
label="Image URL (for kontext model only)",
|
| 322 |
+
placeholder="https://example.com/image.jpg",
|
| 323 |
+
info="Required for image-to-image generation with kontext model"
|
| 324 |
+
)
|
| 325 |
+
|
| 326 |
+
with gr.Row():
|
| 327 |
+
enhance = gr.Checkbox(label="Enhance Prompt", value=False, info="Use AI to enhance your prompt")
|
| 328 |
+
safe = gr.Checkbox(label="Safe Filter", value=False, info="Enable strict NSFW filtering")
|
| 329 |
+
|
| 330 |
+
gr.HTML("""
|
| 331 |
+
<div class="kontext-notice">
|
| 332 |
+
<strong>Note:</strong> For the 'kontext' model, you must provide an Image URL for image-to-image generation.
|
| 333 |
+
Example: "bake_a_cake_from_this_logo" with image URL of a logo.
|
| 334 |
+
</div>
|
| 335 |
+
""")
|
| 336 |
+
|
| 337 |
+
generate_btn = gr.Button(
|
| 338 |
+
"Generate Image π¨",
|
| 339 |
+
elem_classes="button-gradient"
|
| 340 |
+
)
|
| 341 |
+
|
| 342 |
+
# Step 2: Results
|
| 343 |
+
with gr.Column(elem_classes="step-column"):
|
| 344 |
+
gr.HTML("""
|
| 345 |
+
<div class="step-title">
|
| 346 |
+
<span style="font-size: 24px;">2. Generated Image</span><br>
|
| 347 |
+
</div>
|
| 348 |
+
""")
|
| 349 |
+
|
| 350 |
+
result_img = gr.Image(
|
| 351 |
+
label="Generated Image",
|
| 352 |
+
interactive=False,
|
| 353 |
+
elem_classes="image-preview",
|
| 354 |
+
height=400
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
with gr.Row():
|
| 358 |
+
like_button = gr.Button(
|
| 359 |
+
"π Like this result!",
|
| 360 |
+
elem_classes="like-button",
|
| 361 |
+
visible=False
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
+
gr.HTML("""
|
| 365 |
+
<div class="interaction-section">
|
| 366 |
+
<p style="margin: 5px 0;">If you like our AI Image Generator, please give us a β€οΈ into our space!</p>
|
| 367 |
+
</div>
|
| 368 |
+
""")
|
| 369 |
+
|
| 370 |
+
signup_prompt = gr.HTML(
|
| 371 |
+
visible=True,
|
| 372 |
+
value=f"""<div class="signup-container">
|
| 373 |
+
<h3>π Want more AI tools?</h3>
|
| 374 |
+
<p>Visit {COMPANY_NAME}.ai for unlimited access to all our AI tools!</p>
|
| 375 |
+
<a href='{COMPANY_URL}/' target='_blank' class="signup-button">
|
| 376 |
+
Explore More Tools π
|
| 377 |
+
</a>
|
| 378 |
+
</div>"""
|
| 379 |
+
)
|
| 380 |
+
|
| 381 |
+
# Examples
|
| 382 |
+
examples = [
|
| 383 |
+
["An epic fantasy landscape with floating islands, cascading waterfalls, ancient ruins, magical aurora borealis in the sky, digital painting, concept art, unreal engine 5, 4K wallpaper", "flux", 2048, 1024, -1, "", False, False],
|
| 384 |
+
["A serene Japanese garden with cherry blossoms, koi pond, traditional pagoda, morning mist, sunlight filtering through trees, peaceful atmosphere, ultra detailed", "flux", 2048, 1024, -1, "", True, False],
|
| 385 |
+
["bake_a_cake_from_this_logo", "kontext", 1024, 1024, -1, "https://avatars.githubusercontent.com/u/86964862", False, False],
|
| 386 |
+
["A minimalist modern living room with large windows, Scandinavian design, plants, cozy atmosphere, architectural rendering", "turbo", 1024, 1024, 42, "", False, True],
|
| 387 |
+
["A beautiful tropical beach with turquoise water, white sand, palm trees, sunset colors, relaxing vibe, 4K wallpaper", "turbo", 2048, 1024, -1, "", True, False],
|
| 388 |
+
]
|
| 389 |
+
|
| 390 |
+
gr.Examples(
|
| 391 |
+
examples=examples,
|
| 392 |
+
inputs=[prompt, model, width, height, seed, image_url, enhance, safe],
|
| 393 |
+
outputs=[result_img, like_button],
|
| 394 |
+
fn=generate_image_via_api,
|
| 395 |
+
cache_examples=False,
|
| 396 |
+
label="Example Prompts"
|
| 397 |
+
)
|
| 398 |
+
|
| 399 |
+
# Button actions
|
| 400 |
+
generate_btn.click(
|
| 401 |
+
fn=generate_image_via_api,
|
| 402 |
+
inputs=[prompt, model, width, height, seed, image_url, enhance, safe],
|
| 403 |
+
outputs=[result_img, like_button],
|
| 404 |
+
api_name="generate_image"
|
| 405 |
+
)
|
| 406 |
+
|
| 407 |
+
like_button.click(
|
| 408 |
+
fn=handle_like_action,
|
| 409 |
+
inputs=[],
|
| 410 |
+
outputs=[like_button]
|
| 411 |
+
)
|
| 412 |
+
|
| 413 |
+
# Visitor badge
|
| 414 |
+
gr.HTML(f'<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2F{COMPANY_NAME}-AI%2F{COMPANY_NAME}-AI-Image-Generator"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2F{COMPANY_NAME}-AI%2F{COMPANY_NAME}-AI-Image-Generator&label=VISITORS&labelColor=%2337d67a&countColor=%23f47373&style=plastic&labelStyle=upper" /></a>')
|
| 415 |
+
|
| 416 |
+
# Footer
|
| 417 |
+
gr.HTML(create_footer_html())
|
| 418 |
+
|
| 419 |
+
if __name__ == "__main__":
|
| 420 |
+
demo.launch(
|
| 421 |
+
show_api=False,
|
| 422 |
+
server_name="0.0.0.0",
|
| 423 |
+
server_port=7860
|
| 424 |
)
|