Spaces:
Runtime error
Runtime error
File size: 10,603 Bytes
546b770 2c62118 546b770 40b6fca 04d336c 546b770 04d336c 546b770 40b6fca 04d336c f425225 b0acebf 04d336c 40b6fca 546b770 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
import gradio as gr
import numpy as np
import random
import spaces
import torch
import os
from diffusers import DiffusionPipeline
from transformers import pipeline
from huggingface_hub import login
# Login to Hugging Face Hub with token
hf_token = os.getenv("HF_TOKEN")
if hf_token:
login(token=hf_token)
else:
print("Warning: HF_TOKEN environment variable not found. Authentication may fail.")
# Translation pipeline and hardware settings
device = "cuda" if torch.cuda.is_available() else "cpu"
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en", device=device)
dtype = torch.bfloat16
# Load the model with token authentication
pipe = DiffusionPipeline.from_pretrained(
"black-forest-labs/FLUX.1-schnell",
torch_dtype=dtype,
use_auth_token=hf_token
).to(device)
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 2048
def enhance_logo_prompt(prompt):
"""Enhance prompt specifically for logo generation"""
logo_keywords = [
"minimalist logo design",
"vector graphics style",
"simple geometric shapes",
"flat design",
"logo icon",
"white background",
"centered logo composition",
"professional brand identity",
"clean lines",
"no photography",
"no realistic details",
"graphic design",
"corporate logo style"
]
# Add negative prompts to avoid photorealistic results
negative_context = "not a photograph, not realistic, not 3d render, not complex scene"
enhanced_prompt = f"{prompt}, {', '.join(logo_keywords)}, {negative_context}"
return enhanced_prompt
@spaces.GPU()
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
if randomize_seed:
seed = random.randint(0, MAX_SEED)
generator = torch.Generator().manual_seed(seed)
# Always enhance prompt for logo generation
prompt = enhance_logo_prompt(prompt)
# Korean input detection and translation
if any('\uAC00' <= char <= '\uD7A3' for char in prompt):
print("Translating Korean prompt...")
translated_prompt = translator(prompt, max_length=512)[0]['translation_text']
print("Translated prompt:", translated_prompt)
prompt = translated_prompt
image = pipe(
prompt = prompt,
width = width,
height = height,
num_inference_steps = num_inference_steps,
generator = generator,
guidance_scale=0.0
).images[0]
return image, seed
examples = [
["minimal tech company logo, circuit board pattern, blue and white, TEXT 'AI FOREVER'"],
["coffee shop logo, coffee bean icon, brown and cream colors, circular design, TEXT 'T. 123-1234'"],
["fitness gym logo, dumbbell symbol, red and black, dynamic angles, TEXT '[email protected]'"],
["eco friendly company logo, leaf design, green gradient, modern style, TEXT 'NAME CARD'"],
["fashion brand logo, elegant typography, gold and black, luxury style, TEXT 'abc.com'"],
["startup logo, rocket icon, purple and orange, innovative design, TEXT 'EVER AI'"]
]
css = """
/* Clean, minimal styling */
.container {
max-width: 1200px;
margin: auto;
padding: 20px;
}
/* Simple title */
.title {
text-align: center;
font-size: 2.5em;
font-weight: 600;
margin-bottom: 20px;
color: #333;
}
/* White background for input */
#prompt textarea {
background-color: white !important;
color: #333 !important;
border: 2px solid #e0e0e0;
border-radius: 8px;
padding: 12px;
font-size: 16px;
transition: border-color 0.3s ease;
}
#prompt textarea:focus {
border-color: #4CAF50;
outline: none;
}
/* Clean button styling */
.gr-button-primary {
background-color: #4CAF50 !important;
border: none;
padding: 12px 30px;
font-size: 16px;
font-weight: 500;
border-radius: 6px;
transition: background-color 0.3s ease;
}
.gr-button-primary:hover {
background-color: #45a049 !important;
}
/* Result image styling */
#result {
border: 1px solid #e0e0e0;
border-radius: 8px;
padding: 10px;
background-color: #f9f9f9;
}
/* Info box styling */
.info-box {
background-color: #f0f0f0;
border-left: 4px solid #4CAF50;
padding: 15px;
margin: 15px 0;
border-radius: 4px;
}
/* Accordion styling */
.gr-accordion {
background-color: #f9f9f9;
border: 1px solid #e0e0e0;
border-radius: 8px;
margin-top: 20px;
}
/* Examples section */
.gr-examples {
margin-top: 30px;
background-color: #f9f9f9;
padding: 20px;
border-radius: 8px;
border: 1px solid #e0e0e0;
}
/* Hide footer */
footer {
visibility: hidden;
}
/* Responsive design */
@media (max-width: 768px) {
.container {
padding: 15px;
}
.title {
font-size: 2em;
}
}
"""
with gr.Blocks(theme="soft", css=css) as demo:
gr.HTML(
"""
<div class='container'>
<h1 class='title'>Logo Generator AI</h1>
<p style='text-align:center; color:#666; font-size:1.1em; margin-bottom:20px;'>
Create simple, professional logos with AI
</p>
<div style='display:flex; justify-content:center; gap:12px; flex-wrap:wrap; margin-bottom:30px;'>
<a href="https://discord.gg/openfreeai" target="_blank">
<img src="https://img.shields.io/static/v1?label=Discord&message=Openfree%20AI&color=%230000ff&labelColor=%23800080&logo=discord&logoColor=white&style=for-the-badge" alt="Discord badge">
</a>
<a href="https://huggingface.co/OpenFreeAI" target="_blank">
<img src="https://img.shields.io/static/v1?label=Community&message=OpenFree_AI&color=%23800080&labelColor=%23000080&logo=HUGGINGFACE&logoColor=%23ffa500&style=for-the-badge" alt="badge">
</a>
<a href="https://huggingface.co/spaces/openfree/Best-AI" target="_blank">
<img src="https://img.shields.io/static/v1?label=OpenFree&message=BEST%20AI%20Services&color=%230000ff&labelColor=%23000080&logo=huggingface&logoColor=%23ffa500&style=for-the-badge" alt="OpenFree badge">
</a>
</div>
</div>
"""
)
with gr.Column(elem_id="container"):
# Main input section
with gr.Group():
gr.HTML("""
<div class='info-box'>
<strong>Tip:</strong> Describe your logo using simple terms like: company type, icon/symbol, colors, style.
<br>Example: "tech startup logo, lightning bolt icon, blue and silver, minimalist"
</div>
""")
prompt = gr.Textbox(
label="Logo Description",
placeholder="Describe your logo design...\nExample: tech company logo, abstract shape, blue and white, minimal design",
lines=3,
elem_id="prompt"
)
with gr.Row():
run_button = gr.Button("Generate Logo", variant="primary", scale=2)
clear_button = gr.Button("Clear", scale=1)
# Result section
with gr.Row():
with gr.Column(scale=3):
result = gr.Image(
label="Generated Logo",
show_label=True,
elem_id="result",
interactive=False
)
with gr.Column(scale=1):
seed_output = gr.Number(label="Seed Used", interactive=False)
# Advanced settings
with gr.Accordion("Advanced Settings", open=False):
with gr.Row():
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=0,
info="Set to 0 for random seed"
)
randomize_seed = gr.Checkbox(
label="Random Seed",
value=True,
info="Generate unique results each time"
)
with gr.Row():
width = gr.Slider(
label="Width",
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=32,
value=1024,
info="Logo width in pixels"
)
height = gr.Slider(
label="Height",
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=32,
value=1024,
info="Logo height in pixels"
)
num_inference_steps = gr.Slider(
label="Quality Steps",
minimum=1,
maximum=50,
step=1,
value=4,
info="Higher = better quality but slower"
)
# Examples section
gr.HTML("<div class='info-box'><strong>Example Prompts:</strong></div>")
gr.Examples(
examples=examples,
fn=infer,
inputs=[prompt],
outputs=[result, seed_output],
cache_examples="lazy"
)
# Event handlers
gr.on(
triggers=[run_button.click, prompt.submit],
fn=infer,
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
outputs=[result, seed_output]
)
# Clear button functionality
clear_button.click(
fn=lambda: (None, None),
outputs=[prompt, result]
)
# Tips section
gr.HTML("""
<div class='info-box' style='margin-top: 30px;'>
<strong>Logo Design Tips:</strong>
<ul style='margin: 10px 0; padding-left: 20px; color: #666;'>
<li>Use simple, clear descriptions</li>
<li>Specify icon type (abstract, letter, symbol)</li>
<li>Mention preferred colors</li>
<li>Include style (minimal, modern, classic)</li>
<li>Keep it simple - logos should be clean and scalable</li>
</ul>
</div>
""")
demo.launch() |