Spaces:
Sleeping
Sleeping
Commit ·
104c977
1
Parent(s): 85a30db
feat: simplified image generator with optimized settings
Browse files- .gitignore +8 -1
- app.py +26 -47
.gitignore
CHANGED
|
@@ -1,4 +1,11 @@
|
|
| 1 |
__pycache__/
|
| 2 |
*.py[cod]
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
.env
|
|
|
|
|
|
| 1 |
__pycache__/
|
| 2 |
*.py[cod]
|
| 3 |
+
*.so
|
| 4 |
+
.Python
|
| 5 |
+
env/
|
| 6 |
+
build/
|
| 7 |
+
dist/
|
| 8 |
+
eggs/
|
| 9 |
+
*.egg-info/
|
| 10 |
.env
|
| 11 |
+
generated_image.png
|
app.py
CHANGED
|
@@ -5,89 +5,68 @@ import gc
|
|
| 5 |
|
| 6 |
@st.cache_resource
|
| 7 |
def load_model():
|
| 8 |
-
# Use smaller model
|
| 9 |
-
model_id = "
|
| 10 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 11 |
model_id,
|
| 12 |
torch_dtype=torch.float32,
|
| 13 |
-
safety_checker=None,
|
| 14 |
-
requires_safety_checker=False
|
|
|
|
| 15 |
).to("cpu")
|
| 16 |
|
| 17 |
-
#
|
| 18 |
pipe.enable_attention_slicing(slice_size=1)
|
| 19 |
pipe.enable_vae_slicing()
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
gc.collect()
|
| 23 |
|
| 24 |
return pipe
|
| 25 |
|
| 26 |
-
st.title("🎨
|
| 27 |
-
st.write("
|
| 28 |
|
| 29 |
# Initialize model
|
| 30 |
pipeline = load_model()
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
with st.sidebar:
|
| 34 |
-
st.header("Generation Settings")
|
| 35 |
-
num_steps = st.slider("Steps", 20, 50, 30)
|
| 36 |
-
guidance_scale = st.slider("CFG Scale", 5.0, 15.0, 7.5)
|
| 37 |
-
width = st.select_slider("Width", options=[256, 384, 512], value=384)
|
| 38 |
-
height = st.select_slider("Height", options=[256, 384, 512], value=384)
|
| 39 |
-
|
| 40 |
-
st.info("💡 Generation time: 2-5 minutes on CPU")
|
| 41 |
-
|
| 42 |
prompt = st.text_area(
|
| 43 |
"Describe your image:",
|
| 44 |
-
"A professional photograph of a serene landscape at sunset, golden hour lighting,
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
negative_prompt = st.text_area(
|
| 48 |
-
"Negative prompt (what to avoid):",
|
| 49 |
-
"ugly, blurry, low quality, distorted, disfigured, poor details, bad anatomy, watermark"
|
| 50 |
)
|
| 51 |
|
| 52 |
if st.button("Generate Image"):
|
| 53 |
-
with st.spinner("Creating your image (
|
| 54 |
try:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
image = pipeline(
|
| 58 |
prompt=prompt,
|
| 59 |
-
negative_prompt=
|
| 60 |
-
num_inference_steps=
|
| 61 |
-
guidance_scale=
|
| 62 |
-
width=
|
| 63 |
-
height=
|
| 64 |
).images[0]
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
gc.collect()
|
| 68 |
-
|
| 69 |
-
# Display image
|
| 70 |
st.image(image, caption=prompt, use_column_width=True)
|
| 71 |
|
| 72 |
# Save and offer download
|
| 73 |
image.save("generated_image.png")
|
| 74 |
with open("generated_image.png", "rb") as file:
|
| 75 |
st.download_button(
|
| 76 |
-
label="Download
|
| 77 |
data=file,
|
| 78 |
file_name="generated_image.png",
|
| 79 |
mime="image/png"
|
| 80 |
)
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
-
st.error(f"Error
|
| 84 |
-
st.info("Try reducing image size or number of steps if you encounter memory issues")
|
| 85 |
|
| 86 |
st.markdown("""
|
| 87 |
-
### Tips for
|
| 88 |
-
- Be specific in your
|
| 89 |
-
- Include details about style
|
| 90 |
-
-
|
| 91 |
-
- Adjust steps for quality vs. speed tradeoff
|
| 92 |
-
- Use negative prompts to avoid unwanted elements
|
| 93 |
""")
|
|
|
|
| 5 |
|
| 6 |
@st.cache_resource
|
| 7 |
def load_model():
|
| 8 |
+
# Use RunwayML's smaller model
|
| 9 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 10 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 11 |
model_id,
|
| 12 |
torch_dtype=torch.float32,
|
| 13 |
+
safety_checker=None,
|
| 14 |
+
requires_safety_checker=False,
|
| 15 |
+
use_safetensors=True
|
| 16 |
).to("cpu")
|
| 17 |
|
| 18 |
+
# Optimized settings
|
| 19 |
pipe.enable_attention_slicing(slice_size=1)
|
| 20 |
pipe.enable_vae_slicing()
|
| 21 |
+
pipe.enable_sequential_cpu_offload()
|
| 22 |
+
torch.set_num_threads(6) # Optimal thread count for most systems
|
|
|
|
| 23 |
|
| 24 |
return pipe
|
| 25 |
|
| 26 |
+
st.title("🎨 AI Image Generator")
|
| 27 |
+
st.write("Create images from text descriptions")
|
| 28 |
|
| 29 |
# Initialize model
|
| 30 |
pipeline = load_model()
|
| 31 |
|
| 32 |
+
# Simple interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
prompt = st.text_area(
|
| 34 |
"Describe your image:",
|
| 35 |
+
"A professional photograph of a serene landscape at sunset, golden hour lighting, highly detailed"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
if st.button("Generate Image"):
|
| 39 |
+
with st.spinner("Creating your image... (about 1 minute)"):
|
| 40 |
try:
|
| 41 |
+
# Pre-optimized settings
|
|
|
|
| 42 |
image = pipeline(
|
| 43 |
prompt=prompt,
|
| 44 |
+
negative_prompt="ugly, blurry, low quality, distorted, disfigured",
|
| 45 |
+
num_inference_steps=20,
|
| 46 |
+
guidance_scale=7.0,
|
| 47 |
+
width=320,
|
| 48 |
+
height=320,
|
| 49 |
).images[0]
|
| 50 |
|
| 51 |
+
# Display and download
|
|
|
|
|
|
|
|
|
|
| 52 |
st.image(image, caption=prompt, use_column_width=True)
|
| 53 |
|
| 54 |
# Save and offer download
|
| 55 |
image.save("generated_image.png")
|
| 56 |
with open("generated_image.png", "rb") as file:
|
| 57 |
st.download_button(
|
| 58 |
+
label="Download Image",
|
| 59 |
data=file,
|
| 60 |
file_name="generated_image.png",
|
| 61 |
mime="image/png"
|
| 62 |
)
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
+
st.error(f"Error: {str(e)}")
|
|
|
|
| 66 |
|
| 67 |
st.markdown("""
|
| 68 |
+
### Tips for good results:
|
| 69 |
+
- Be specific in your description
|
| 70 |
+
- Include details about style and lighting
|
| 71 |
+
- Keep descriptions concise
|
|
|
|
|
|
|
| 72 |
""")
|