File size: 1,540 Bytes
97c4252
e39b8e2
1dbe873
97c4252
e39b8e2
1dbe873
e39b8e2
1dbe873
 
 
 
 
 
e39b8e2
fa8cbc2
1dbe873
 
 
e39b8e2
1dbe873
 
 
e39b8e2
 
fa8cbc2
1dbe873
e39b8e2
fa8cbc2
 
 
 
e39b8e2
1dbe873
 
 
e39b8e2
fa8cbc2
e39b8e2
1dbe873
5f83b50
fa8cbc2
1dbe873
fa8cbc2
1dbe873
bf472b6
1dbe873
 
 
7f974d4
fa8cbc2
97c4252
 
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
import gradio as gr
from diffusers import StableDiffusionPipeline
import torch
from PIL import Image
import os
import uuid

# Load Stable Diffusion v1.5
pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5", 
    safety_checker=None, 
    torch_dtype=torch.float32
).to("cpu")

# Output folder
os.makedirs("outputs", exist_ok=True)

# Aspect ratio mapping
aspect_ratios = {
    "Square (512x512)": (512, 512),
    "Portrait (512x768)": (512, 768),
    "Landscape (768x512)": (768, 512)
}

def generate_image(user_prompt, ratio):
    width, height = aspect_ratios[ratio]

    # Enhance prompt internally for better quality
    enhanced_prompt = f"{user_prompt}, highly detailed, ultra realistic, cinematic lighting, sharp focus"

    image = pipe(prompt=enhanced_prompt, height=height, width=width, guidance_scale=7.5).images[0]

    # Save image
    filename = f"outputs/{uuid.uuid4().hex}.png"
    image.save(filename)

    return image

# UI
with gr.Blocks() as demo:
    gr.Markdown("# 🌌 Stable Diffusion v1.5 - Image Generator")
    with gr.Row():
        prompt = gr.Textbox(label="Prompt", placeholder="A shining star in the sky")
        ratio = gr.Radio(list(aspect_ratios.keys()), label="Aspect Ratio", value="Square (512x512)")
    with gr.Row():
        generate_btn = gr.Button("Generate Image")
    with gr.Column():
        output_img = gr.Image(label="Generated Image", interactive=False)

    generate_btn.click(fn=generate_image, inputs=[prompt, ratio], outputs=output_img)

demo.launch()