Spaces:
Running
Running
Update logoGenertor.py
Browse files- logoGenertor.py +55 -51
logoGenertor.py
CHANGED
@@ -1,72 +1,76 @@
|
|
1 |
-
import torch
|
2 |
-
from diffusers import StableDiffusionPipeline
|
3 |
import gradio as gr
|
4 |
-
|
|
|
5 |
from PIL import Image
|
|
|
6 |
|
7 |
-
# Load model
|
8 |
pipe = StableDiffusionPipeline.from_pretrained(
|
9 |
-
"stabilityai/
|
10 |
-
|
11 |
-
|
12 |
-
).to("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
|
14 |
-
# Style
|
15 |
STYLE_PRESETS = {
|
16 |
-
"Minimal": "minimal
|
17 |
-
"Modern": "modern
|
18 |
-
"Retro": "
|
19 |
-
"Tech": "technology startup logo, digital design",
|
20 |
-
"Luxury": "luxury brand logo, elegant and premium"
|
21 |
}
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
image = pil_image.convert("RGBA")
|
26 |
-
data = np.array(image)
|
27 |
-
r, g, b, a = data.T
|
28 |
-
|
29 |
-
white_areas = (r > 240) & (g > 240) & (b > 240)
|
30 |
-
data[..., :-1][white_areas.T] = (255, 255, 255)
|
31 |
-
data[..., -1][white_areas.T] = 0 # transparent
|
32 |
-
return Image.fromarray(data)
|
33 |
|
34 |
-
# Generate image
|
35 |
def generate_logo(prompt, style, resolution, transparent):
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
width, height = map(int, resolution.split('x'))
|
40 |
-
image = pipe(styled_prompt, height=height, width=width).images[0]
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
if transparent:
|
43 |
-
image =
|
44 |
|
45 |
return image
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
prompt = gr.Textbox(label="Logo Description", placeholder="e.g., A minimalist owl logo for a tech startup")
|
55 |
-
style = gr.Dropdown(choices=list(STYLE_PRESETS.keys()), label="Style", value="Minimal")
|
56 |
-
resolution = gr.Dropdown(choices=["512x512", "768x768"], label="Resolution", value="512x512")
|
57 |
-
|
58 |
-
with gr.Row():
|
59 |
-
transparent = gr.Checkbox(label="Make background transparent", value=True)
|
60 |
-
dark_mode = gr.Checkbox(label="Dark Mode UI", value=False)
|
61 |
-
share_space = gr.Checkbox(label="Make app public (Gradio share link)", value=False)
|
62 |
|
63 |
-
|
64 |
-
|
65 |
|
66 |
-
|
|
|
|
|
|
|
67 |
|
68 |
-
|
|
|
|
|
|
|
|
|
69 |
|
70 |
if __name__ == "__main__":
|
71 |
-
|
72 |
-
ui.launch(share=True)
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from diffusers import StableDiffusionPipeline
|
3 |
+
import torch
|
4 |
from PIL import Image
|
5 |
+
import io
|
6 |
|
7 |
+
# Load the model globally once
|
8 |
pipe = StableDiffusionPipeline.from_pretrained(
|
9 |
+
"stabilityai/sd-turbo"
|
10 |
+
)
|
11 |
+
pipe.to("cpu") # Free hardware: CPU
|
|
|
12 |
|
13 |
+
# Style presets
|
14 |
STYLE_PRESETS = {
|
15 |
+
"Minimal": "minimal, clean lines, flat design",
|
16 |
+
"Modern": "modern, bold, gradient",
|
17 |
+
"Retro": "retro, vintage, 90s style"
|
|
|
|
|
18 |
}
|
19 |
|
20 |
+
# Resolutions
|
21 |
+
RESOLUTIONS = ["256x256", "384x384", "512x512", "768x768"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
|
|
23 |
def generate_logo(prompt, style, resolution, transparent):
|
24 |
+
# Apply style
|
25 |
+
style_prompt = STYLE_PRESETS.get(style, "")
|
26 |
+
full_prompt = f"{style_prompt}, {prompt}".strip()
|
|
|
|
|
27 |
|
28 |
+
# Resolution
|
29 |
+
width, height = map(int, resolution.split("x"))
|
30 |
+
|
31 |
+
# Run pipeline
|
32 |
+
image = pipe(full_prompt, height=height, width=width, num_inference_steps=15).images[0]
|
33 |
+
|
34 |
+
# Convert to RGBA if transparent
|
35 |
if transparent:
|
36 |
+
image = image.convert("RGBA")
|
37 |
|
38 |
return image
|
39 |
|
40 |
+
def get_image_bytes(img):
|
41 |
+
buffer = io.BytesIO()
|
42 |
+
img.save(buffer, format="PNG")
|
43 |
+
buffer.seek(0)
|
44 |
+
return buffer
|
45 |
+
|
46 |
+
with gr.Blocks(theme=gr.themes.Default(), title="🎨 AI Logo Generator") as demo:
|
47 |
+
gr.Markdown("## 🎨 AI Logo Generator\nEnter a description to generate a high-quality logo")
|
48 |
+
|
49 |
+
with gr.Row():
|
50 |
+
prompt = gr.Textbox(label="Logo Description", placeholder="e.g., A minimalist logo of owl for tech startup")
|
51 |
+
style = gr.Dropdown(label="Style", choices=list(STYLE_PRESETS.keys()), value="Minimal")
|
52 |
+
resolution = gr.Dropdown(label="Resolution", choices=RESOLUTIONS, value="512x512")
|
53 |
+
|
54 |
+
with gr.Row():
|
55 |
+
transparent = gr.Checkbox(label="Make background transparent", value=False)
|
56 |
+
dark_mode = gr.Checkbox(label="Dark Mode UI", value=False)
|
57 |
+
share_link = gr.Checkbox(label="Make app public (Gradio share link)", value=True)
|
58 |
|
59 |
+
btn = gr.Button("Generate")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
output_img = gr.Image(label="Generated Logo", type="pil")
|
62 |
+
download_btn = gr.File(label="Download Logo (PNG)")
|
63 |
|
64 |
+
def process_all(prompt, style, resolution, transparent, dark_mode, share_link):
|
65 |
+
image = generate_logo(prompt, style, resolution, transparent)
|
66 |
+
file = get_image_bytes(image)
|
67 |
+
return image, (file, "logo.png")
|
68 |
|
69 |
+
btn.click(
|
70 |
+
fn=process_all,
|
71 |
+
inputs=[prompt, style, resolution, transparent, dark_mode, share_link],
|
72 |
+
outputs=[output_img, download_btn]
|
73 |
+
)
|
74 |
|
75 |
if __name__ == "__main__":
|
76 |
+
demo.launch(share=True)
|
|