Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import FluxPipeline
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="Flux Image Generator", layout="centered")
|
| 7 |
+
|
| 8 |
+
st.title("🎨 AI Image Generator")
|
| 9 |
+
st.caption("Powered by FLUX.1 [schnell] - Optimized for speed and quality")
|
| 10 |
+
|
| 11 |
+
# Load the model with caching to avoid reloading on every interaction
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_pipeline():
|
| 14 |
+
pipe = FluxPipeline.from_pretrained(
|
| 15 |
+
"black-forest-labs/FLUX.1-schnell",
|
| 16 |
+
torch_dtype=torch.bfloat16
|
| 17 |
+
)
|
| 18 |
+
# Use CPU offload to save memory on Hugging Face's free tier
|
| 19 |
+
pipe.enable_model_cpu_offload()
|
| 20 |
+
return pipe
|
| 21 |
+
|
| 22 |
+
pipeline = load_pipeline()
|
| 23 |
+
|
| 24 |
+
# Sidebar for settings
|
| 25 |
+
with st.sidebar:
|
| 26 |
+
st.header("Settings")
|
| 27 |
+
width = st.slider("Width", 512, 1024, 1024, step=128)
|
| 28 |
+
height = st.slider("Height", 512, 1024, 1024, step=128)
|
| 29 |
+
num_steps = st.slider("Inference Steps", 1, 4, 4)
|
| 30 |
+
|
| 31 |
+
# User Input
|
| 32 |
+
prompt = st.text_area("Enter your prompt:", "A futuristic mechanical engine, detailed blueprint style, blue neon lines")
|
| 33 |
+
|
| 34 |
+
if st.button("Generate Image"):
|
| 35 |
+
if prompt:
|
| 36 |
+
with st.spinner("Generating... please wait."):
|
| 37 |
+
# Generate the image
|
| 38 |
+
image = pipeline(
|
| 39 |
+
prompt,
|
| 40 |
+
num_inference_steps=num_steps,
|
| 41 |
+
guidance_scale=0.0,
|
| 42 |
+
width=width,
|
| 43 |
+
height=height,
|
| 44 |
+
max_sequence_length=256
|
| 45 |
+
).images[0]
|
| 46 |
+
|
| 47 |
+
# Display image
|
| 48 |
+
st.image(image, caption="Generated Result", use_column_width=True)
|
| 49 |
+
|
| 50 |
+
# Download button
|
| 51 |
+
buf = io.BytesIO()
|
| 52 |
+
image.save(buf, format="PNG")
|
| 53 |
+
byte_im = buf.getvalue()
|
| 54 |
+
st.download_button(label="Download Image", data=byte_im, file_name="generated.png", mime="image/png")
|
| 55 |
+
else:
|
| 56 |
+
st.warning("Please enter a prompt first!")
|