Spaces:
Running
Running
Upload 2 files
Browse files- app.py +77 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from diffusers import StableDiffusionPipeline
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# Initialize the Stable Diffusion pipeline (do this ONCE)
|
6 |
+
@st.cache_resource # Cache the pipeline for efficiency
|
7 |
+
def load_model():
|
8 |
+
pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1")
|
9 |
+
return pipe
|
10 |
+
|
11 |
+
pipe = load_model()
|
12 |
+
|
13 |
+
# Pre-generated prompts (you can customize these)
|
14 |
+
PRE_GENERATED_PROMPTS = [
|
15 |
+
"A photorealistic image of a majestic lion in a savanna at sunset",
|
16 |
+
"A vibrant abstract painting with swirling colors",
|
17 |
+
"A futuristic cityscape with flying cars and neon lights",
|
18 |
+
"A cute cartoon cat wearing a top hat and monocle",
|
19 |
+
"A fantasy landscape with floating islands and waterfalls",
|
20 |
+
"A surreal dreamscape with melting clocks and distorted figures",
|
21 |
+
"A portrait of a person with glowing eyes and intricate tattoos",
|
22 |
+
"A science fiction scene with a spaceship landing on a distant planet",
|
23 |
+
"A whimsical illustration of a fairy tale character",
|
24 |
+
"A hyperrealistic image of a delicious-looking pizza",
|
25 |
+
]
|
26 |
+
|
27 |
+
st.title("Stable Diffusion 2.1 Image Generator")
|
28 |
+
|
29 |
+
# Layout with columns for better organization
|
30 |
+
col1, col2 = st.columns([1, 1]) # Adjust column ratios as needed
|
31 |
+
|
32 |
+
with col1:
|
33 |
+
st.header("Image Generation")
|
34 |
+
|
35 |
+
prompt = st.text_area("Prompt", height=150) # Larger text area
|
36 |
+
negative_prompt = st.text_area("Negative Prompt (Optional)", height=100)
|
37 |
+
|
38 |
+
# Prompt selection dropdown
|
39 |
+
selected_prompt = st.selectbox("Or choose a pre-generated prompt:", [""] + PRE_GENERATED_PROMPTS) # Add empty option
|
40 |
+
if selected_prompt:
|
41 |
+
prompt = selected_prompt # If a prompt is selected, update the prompt text area
|
42 |
+
|
43 |
+
num_images = st.slider("Number of Images", min_value=1, max_value=4, value=1)
|
44 |
+
#seed = st.number_input("Seed (Optional, for reproducibility)", value=None, placeholder="Enter an integer seed") # Make seed optional
|
45 |
+
seed = st.text_input("Seed (Optional, for reproducibility)", placeholder="Enter an integer seed") # Make seed optional
|
46 |
+
|
47 |
+
if st.button("Generate"):
|
48 |
+
if not prompt: # Basic validation
|
49 |
+
st.error("Please enter a prompt.")
|
50 |
+
else:
|
51 |
+
try:
|
52 |
+
images = []
|
53 |
+
for i in range(num_images):
|
54 |
+
if seed:
|
55 |
+
try:
|
56 |
+
seed = int(seed)
|
57 |
+
except ValueError:
|
58 |
+
st.error("Seed must be an integer.")
|
59 |
+
break
|
60 |
+
else:
|
61 |
+
seed = None # if seed input is empty, make seed None so it can be random
|
62 |
+
|
63 |
+
image = pipe(prompt, negative_prompt=negative_prompt, seed=seed).images[0]
|
64 |
+
images.append(image)
|
65 |
+
|
66 |
+
for image in images:
|
67 |
+
st.image(image, use_column_width=True) # Use column width for better display
|
68 |
+
except Exception as e:
|
69 |
+
st.error(f"An error occurred: {e}")
|
70 |
+
|
71 |
+
with col2:
|
72 |
+
st.header("Prompt Examples")
|
73 |
+
# Display pre-generated prompts in a more visually appealing way
|
74 |
+
for p in PRE_GENERATED_PROMPTS:
|
75 |
+
st.markdown(f"- *{p}*") # Use markdown for italics and bullet points
|
76 |
+
st.markdown("---") # Separator
|
77 |
+
st.markdown("You can use these prompts as inspiration or modify them to create your own.")
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
|