-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
78 lines (66 loc) · 2.09 KB
/
app.py
File metadata and controls
78 lines (66 loc) · 2.09 KB
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
import streamlit as st
from diffusers import StableDiffusionPipeline
import torch
from PIL import Image
import os
import uuid
import datetime
# --------------------------
# Page Configuration
# --------------------------
st.set_page_config(
page_title="Text2ImageGen-AI",
layout="centered",
page_icon="🎨",
)
@st.cache_resource
def load_model():
model_id = "runwayml/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(
model_id,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
)
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
return pipe
pipe = load_model()
# --------------------------
# UI Title
# --------------------------
st.title("🎨 Text2ImageGen-AI")
st.write("Generate high-quality images from text using Stable Diffusion v1.5")
# --------------------------
# Prompt Input
# --------------------------
prompt = st.text_area(
"Enter your prompt",
placeholder="Example: A futuristic cyberpunk city with neon lights, 8K highly detailed"
)
steps = st.slider("Inference Steps", 20, 60, 30)
guidance_scale = st.slider("Guidance Scale", 1.0, 12.0, 7.5)
generate_btn = st.button("Generate Image 🚀")
# --------------------------
# Processing
# --------------------------
if generate_btn:
if not prompt.strip():
st.warning("Please enter a prompt.")
st.stop()
with st.spinner("Generating image... this can take a few seconds ⏳"):
image = pipe(
prompt,
num_inference_steps=steps,
guidance_scale=guidance_scale
).images[0]
# Save Output
os.makedirs("outputs", exist_ok=True)
filename = f"outputs/{uuid.uuid4().hex}.png"
image.save(filename)
st.image(image, caption="Generated Image", width='stretch')
st.success("Image generated successfully!")
with open(filename, "rb") as img_file:
st.download_button(
label="Download Image",
data=img_file,
file_name=f"generated_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.png",
mime="image/png"
)