Spaces:
Sleeping
Sleeping
Commit
·
a70e57e
1
Parent(s):
17c456b
Initial commit
Browse files- app.py +91 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers import StableDiffusionPipeline
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# Load model
|
| 7 |
+
#model_id = "stabilityai/stable-diffusion-2-1"
|
| 8 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
|
| 11 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 12 |
+
model_id,
|
| 13 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 14 |
+
).to(device)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Function to generate mockup image from text prompt
|
| 18 |
+
def generate_mockup(prompt, num_inference_steps=50, guidance_scale=7.5):
|
| 19 |
+
if device == "cuda":
|
| 20 |
+
image = pipe(
|
| 21 |
+
prompt,
|
| 22 |
+
num_inference_steps=num_inference_steps,
|
| 23 |
+
guidance_scale=guidance_scale
|
| 24 |
+
).images[0]
|
| 25 |
+
else:
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
image = pipe(
|
| 28 |
+
prompt,
|
| 29 |
+
num_inference_steps=num_inference_steps,
|
| 30 |
+
guidance_scale=guidance_scale
|
| 31 |
+
).images[0]
|
| 32 |
+
|
| 33 |
+
# Simple info text under the image
|
| 34 |
+
info = f"Image Size: {image.size[0]}×{image.size[1]} — Model: Stable Diffusion-v1-5"
|
| 35 |
+
|
| 36 |
+
return image, info
|
| 37 |
+
|
| 38 |
+
# Custom Theme
|
| 39 |
+
theme = gr.themes.Soft(
|
| 40 |
+
primary_hue="pink",
|
| 41 |
+
secondary_hue="gray",
|
| 42 |
+
neutral_hue="slate",
|
| 43 |
+
font=["Inter", "sans-serif"],
|
| 44 |
+
).set(
|
| 45 |
+
button_primary_background_fill="linear-gradient(90deg, #6366F1, #3B82F6)",
|
| 46 |
+
button_primary_text_color="white",
|
| 47 |
+
button_primary_background_fill_hover="linear-gradient(90deg, #4F46E5, #2563EB)",
|
| 48 |
+
button_border_width="0px",
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Interface
|
| 52 |
+
with gr.Blocks(theme=theme, css="""
|
| 53 |
+
#title {text-align:center; font-size:1.8em; font-weight:700; margin-bottom:0.4em;}
|
| 54 |
+
#subtitle {text-align:center; color:#6B7280; font-size:1em; margin-bottom:1.2em;}
|
| 55 |
+
.footer {text-align:center; font-size:0.85em; color:#9CA3AF; margin-top:1.5em;}
|
| 56 |
+
""") as demo:
|
| 57 |
+
|
| 58 |
+
gr.Markdown("<div id='title'>AI Product Mockup Generator</div>")
|
| 59 |
+
gr.Markdown("<div id='subtitle'>Turn your text ideas into professional product mockups instantly!</div>")
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
with gr.Column(scale=1):
|
| 63 |
+
prompt = gr.Textbox(
|
| 64 |
+
label="Enter your product description",
|
| 65 |
+
placeholder="e.g., A modern smartwatch on a white background",
|
| 66 |
+
lines=2
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Advanced settings hidden under an accordion
|
| 70 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 71 |
+
steps = gr.Slider(1, 50, value=10, step=5, label="Inference Steps")
|
| 72 |
+
guidance = gr.Slider(1.0, 15.0, value=7.5, step=0.1, label="Guidance Scale")
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
generate_btn = gr.Button("Generate Mockup", variant="primary")
|
| 76 |
+
clear_btn = gr.Button("Clear")
|
| 77 |
+
|
| 78 |
+
with gr.Column(scale=1):
|
| 79 |
+
output_image = gr.Image(type="pil", label="Generated Mockup", show_label=False)
|
| 80 |
+
image_info = gr.Markdown("")
|
| 81 |
+
|
| 82 |
+
# Button functions
|
| 83 |
+
generate_btn.click(generate_mockup, [prompt, steps, guidance], [output_image, image_info])
|
| 84 |
+
|
| 85 |
+
# Clear button resets everything
|
| 86 |
+
clear_btn.click(lambda: ("", None, ""), None, [prompt, output_image, image_info])
|
| 87 |
+
|
| 88 |
+
#gr.Markdown("<div class='footer'>Created with Stable Diffusion 2.1 and Gradio</div>")
|
| 89 |
+
|
| 90 |
+
# Launch App
|
| 91 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
torchvision
|
| 4 |
+
diffusers
|
| 5 |
+
gradio
|