Khalil09 commited on
Commit
54e5814
·
verified ·
1 Parent(s): d01bbe1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -26
app.py CHANGED
@@ -2,55 +2,73 @@ 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!")
 
2
  import torch
3
  from diffusers import FluxPipeline
4
  import io
5
+ import os
6
 
7
+ # Page configuration
8
  st.set_page_config(page_title="Flux Image Generator", layout="centered")
9
 
10
  st.title("🎨 AI Image Generator")
11
+ st.caption("Powered by FLUX.1 [schnell]")
12
 
13
+ # 1. THE LOAD FUNCTION
14
  @st.cache_resource
15
  def load_pipeline():
16
+ # This pulls the secret you named 'HF_TOKEN' from your Space Settings
17
+ token = os.getenv("HF_TOKEN")
18
+
19
+ # Loading the model with the access token
20
  pipe = FluxPipeline.from_pretrained(
21
  "black-forest-labs/FLUX.1-schnell",
22
+ torch_dtype=torch.bfloat16,
23
+ token=token
24
  )
25
+
26
+ # Enables memory saving for the Hugging Face free tier
27
  pipe.enable_model_cpu_offload()
28
  return pipe
29
 
30
+ # Initialize the pipeline
31
+ try:
32
+ pipeline = load_pipeline()
33
+ except Exception as e:
34
+ st.error("Could not load the model. Make sure you accepted the terms on the model page and added your HF_TOKEN to secrets.")
35
+ st.stop()
36
 
37
+ # 2. SIDEBAR SETTINGS
38
  with st.sidebar:
39
  st.header("Settings")
40
  width = st.slider("Width", 512, 1024, 1024, step=128)
41
  height = st.slider("Height", 512, 1024, 1024, step=128)
42
  num_steps = st.slider("Inference Steps", 1, 4, 4)
43
+ st.info("Tip: Use 4 steps for the best quality.")
44
 
45
+ # 3. USER INTERFACE
46
+ prompt = st.text_area("Enter your prompt:", "A futuristic robotic arm building a circuit board, cinematic lighting, 8k")
47
 
48
  if st.button("Generate Image"):
49
  if prompt:
50
+ with st.spinner("Generating... this may take a minute."):
51
+ try:
52
+ # Generate the image
53
+ image = pipeline(
54
+ prompt,
55
+ num_inference_steps=num_steps,
56
+ guidance_scale=0.0,
57
+ width=width,
58
+ height=height,
59
+ max_sequence_length=256
60
+ ).images[0]
61
+
62
+ # Display image
63
+ st.image(image, caption="Generated Result", use_column_width=True)
64
+
65
+ # Download button
66
+ buf = io.BytesIO()
67
+ image.save(buf, format="PNG")
68
+ byte_im = buf.getvalue()
69
+ st.download_button(label="Download Image", data=byte_im, file_name="generated.png", mime="image/png")
70
 
71
+ except Exception as e:
72
+ st.error(f"Error during generation: {e}")
 
 
 
 
 
 
73
  else:
74
  st.warning("Please enter a prompt first!")