Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,121 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ["STREAMLIT_SERVER_PORT"] = "7860"
|
| 3 |
+
os.environ["STREAMLIT_SERVER_ADDRESS"] = "0.0.0.0"
|
| 4 |
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import torch
|
| 7 |
+
from diffusers import FluxPipeline
|
| 8 |
|
| 9 |
+
# Set page config
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="FLUX.1 Image Generator",
|
| 12 |
+
page_icon="🎨",
|
| 13 |
+
layout="wide"
|
| 14 |
+
)
|
| 15 |
|
| 16 |
+
@st.cache_resource
|
| 17 |
+
def load_pipeline():
|
| 18 |
+
pipe = FluxPipeline.from_pretrained(
|
| 19 |
+
"black-forest-labs/FLUX.1-dev",
|
| 20 |
+
torch_dtype=torch.float32 # Using float32 for CPU compatibility
|
| 21 |
+
)
|
| 22 |
+
pipe = pipe.to("cpu")
|
| 23 |
+
return pipe
|
| 24 |
+
|
| 25 |
+
def main():
|
| 26 |
+
st.title("🎨 FLUX.1 Image Generator")
|
| 27 |
+
st.write("Create high-quality images using FLUX.1 [dev] model")
|
| 28 |
+
|
| 29 |
+
# User inputs
|
| 30 |
+
prompt = st.text_area("Enter your image description:", height=100)
|
| 31 |
+
|
| 32 |
+
col1, col2, col3 = st.columns(3)
|
| 33 |
+
with col1:
|
| 34 |
+
width = st.select_slider(
|
| 35 |
+
"Image Width",
|
| 36 |
+
options=[512, 768, 1024],
|
| 37 |
+
value=768
|
| 38 |
+
)
|
| 39 |
+
with col2:
|
| 40 |
+
height = st.select_slider(
|
| 41 |
+
"Image Height",
|
| 42 |
+
options=[512, 768, 1024],
|
| 43 |
+
value=768
|
| 44 |
+
)
|
| 45 |
+
with col3:
|
| 46 |
+
guidance_scale = st.slider(
|
| 47 |
+
"Guidance Scale",
|
| 48 |
+
min_value=1.0,
|
| 49 |
+
max_value=7.0,
|
| 50 |
+
value=3.5,
|
| 51 |
+
step=0.5
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
steps = st.slider(
|
| 55 |
+
"Number of Steps",
|
| 56 |
+
min_value=20,
|
| 57 |
+
max_value=50,
|
| 58 |
+
value=30
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
if st.button("Generate Image"):
|
| 62 |
+
if prompt:
|
| 63 |
+
try:
|
| 64 |
+
pipe = load_pipeline()
|
| 65 |
+
|
| 66 |
+
with st.spinner("🎨 Generating your image... (this may take several minutes on CPU)"):
|
| 67 |
+
# Set a fixed seed for reproducibility
|
| 68 |
+
generator = torch.Generator("cpu").manual_seed(42)
|
| 69 |
+
|
| 70 |
+
image = pipe(
|
| 71 |
+
prompt,
|
| 72 |
+
height=height,
|
| 73 |
+
width=width,
|
| 74 |
+
guidance_scale=guidance_scale,
|
| 75 |
+
num_inference_steps=steps,
|
| 76 |
+
max_sequence_length=512,
|
| 77 |
+
generator=generator
|
| 78 |
+
).images[0]
|
| 79 |
+
|
| 80 |
+
# Display image
|
| 81 |
+
st.image(image, caption="Generated Image")
|
| 82 |
+
|
| 83 |
+
# Save and provide download option
|
| 84 |
+
img_path = "generated_image.png"
|
| 85 |
+
image.save(img_path)
|
| 86 |
+
with open(img_path, "rb") as file:
|
| 87 |
+
st.download_button(
|
| 88 |
+
label="Download Image",
|
| 89 |
+
data=file,
|
| 90 |
+
file_name="flux_generated.png",
|
| 91 |
+
mime="image/png"
|
| 92 |
+
)
|
| 93 |
+
os.remove(img_path)
|
| 94 |
+
|
| 95 |
+
except Exception as e:
|
| 96 |
+
st.error(f"An error occurred: {str(e)}")
|
| 97 |
+
else:
|
| 98 |
+
st.warning("Please enter a prompt first!")
|
| 99 |
+
|
| 100 |
+
st.markdown("---")
|
| 101 |
+
st.markdown("Powered by FLUX.1 [dev] model")
|
| 102 |
+
st.info("⚠️ Note: Generation may take several minutes as this runs on CPU")
|
| 103 |
+
|
| 104 |
+
# Add usage guidelines
|
| 105 |
+
with st.expander("Usage Guidelines"):
|
| 106 |
+
st.markdown("""
|
| 107 |
+
**Limitations:**
|
| 108 |
+
- This model is not intended to provide factual information
|
| 109 |
+
- May amplify existing societal biases
|
| 110 |
+
- May fail to generate output that matches prompts
|
| 111 |
+
- Prompt following depends on prompting-style
|
| 112 |
+
|
| 113 |
+
**Not allowed:**
|
| 114 |
+
- Generating illegal or harmful content
|
| 115 |
+
- Creating non-consensual content
|
| 116 |
+
- Harassing or bullying individuals
|
| 117 |
+
- Generating disinformation
|
| 118 |
+
""")
|
| 119 |
+
|
| 120 |
+
if __name__ == "__main__":
|
| 121 |
+
main()
|