Spaces:
Running
Running
Update logoGenertor.py
Browse files- logoGenertor.py +72 -34
logoGenertor.py
CHANGED
@@ -1,34 +1,72 @@
|
|
1 |
-
import torch
|
2 |
-
from diffusers import StableDiffusionPipeline
|
3 |
-
|
4 |
-
import
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from diffusers import StableDiffusionPipeline
|
3 |
+
import gradio as gr
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Load model
|
8 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
9 |
+
"stabilityai/stable-diffusion-2",
|
10 |
+
torch_dtype=torch.float16,
|
11 |
+
revision="fp16"
|
12 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
+
|
14 |
+
# Style mapping
|
15 |
+
STYLE_PRESETS = {
|
16 |
+
"Minimal": "minimal flat logo, vector style",
|
17 |
+
"Modern": "modern logo, professional design",
|
18 |
+
"Retro": "vintage retro logo, 90s style",
|
19 |
+
"Tech": "technology startup logo, digital design",
|
20 |
+
"Luxury": "luxury brand logo, elegant and premium"
|
21 |
+
}
|
22 |
+
|
23 |
+
# Convert to transparent PNG if background is white-ish
|
24 |
+
def remove_white_background(pil_image):
|
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 |
+
if not prompt:
|
37 |
+
return None
|
38 |
+
styled_prompt = f"{prompt}, {STYLE_PRESETS[style]}"
|
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 = remove_white_background(image)
|
44 |
+
|
45 |
+
return image
|
46 |
+
|
47 |
+
# Build UI
|
48 |
+
def create_ui():
|
49 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=".dark-mode body { background-color: #1e1e1e; color: white; }") as demo:
|
50 |
+
gr.Markdown("# 🎨 AI Logo Generator")
|
51 |
+
gr.Markdown("Enter a description to generate a high-quality logo")
|
52 |
+
|
53 |
+
with gr.Row():
|
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 |
+
output = gr.Image(label="Generated Logo", show_download_button=True, type="pil")
|
64 |
+
submit = gr.Button("Generate")
|
65 |
+
|
66 |
+
submit.click(fn=generate_logo, inputs=[prompt, style, resolution, transparent], outputs=output)
|
67 |
+
|
68 |
+
return demo
|
69 |
+
|
70 |
+
if __name__ == "__main__":
|
71 |
+
ui = create_ui()
|
72 |
+
ui.launch(share=True)
|