Waseem7711 commited on
Commit
13cada4
·
verified ·
1 Parent(s): d749028

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -47
app.py CHANGED
@@ -1,50 +1,72 @@
 
 
1
  import streamlit as st
2
- from diffusers import StableDiffusionPipeline
 
 
3
  import torch
4
- from PIL import Image
5
-
6
- # Streamlit App Title
7
- st.title("🖼️ Stable Diffusion Image Generator")
8
- st.write("Generate images from text prompts using Stable Diffusion!")
9
-
10
- # Sidebar for prompt input and button
11
- st.sidebar.title("Image Generation Settings")
12
- prompt = st.sidebar.text_input("Enter your prompt", "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k")
13
- generate_button = st.sidebar.button("Generate Image")
14
-
15
- # Cache the Stable Diffusion model to load it only once
16
- @st.cache_resource
17
- def load_model():
18
- model = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base", torch_dtype=torch.float16)
19
- model = model.to("cuda" if torch.cuda.is_available() else "cpu")
20
- return model
21
-
22
- # Load the model
23
- pipe = load_model()
24
-
25
- # Generate the image when the button is clicked
26
- if generate_button:
27
- if prompt.strip() == "":
28
- st.error("Prompt cannot be empty. Please enter a valid text prompt.")
 
 
 
 
 
 
29
  else:
30
- st.write(f"### Prompt: {prompt}")
31
- with st.spinner("Generating image... Please wait. This may take a few moments depending on system performance."):
32
- try:
33
- # Generate the image
34
- image = pipe(prompt, num_inference_steps=50, guidance_scale=7.5).images[0]
35
-
36
- # Display the image
37
- st.image(image, caption="Generated Image", use_column_width=True)
38
-
39
- # Provide download option
40
- img_path = "generated_image.png"
41
- image.save(img_path)
42
- with open(img_path, "rb") as img_file:
43
- st.download_button(
44
- label="Download Image",
45
- data=img_file,
46
- file_name="generated_image.png",
47
- mime="image/png"
48
- )
49
- except Exception as e:
50
- st.error(f"An error occurred: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # prompt: write this notebook code for me i want to deploy this model on hugging space using streamlit
2
+
3
  import streamlit as st
4
+ import mediapy as media
5
+ import random
6
+ import sys
7
  import torch
8
+ from diffusers import DiffusionPipeline
9
+
10
+ # Install necessary libraries if not already installed
11
+ try:
12
+ import diffusers
13
+ except ImportError:
14
+ !pip install --quiet --upgrade diffusers transformers accelerate invisible_watermark mediapy
15
+
16
+ # Set use_refiner to False for initial deployment. You can add a checkbox later.
17
+ use_refiner = False
18
+
19
+ @st.cache_resource # Cache the pipeline to avoid reloading every time
20
+ def load_pipeline():
21
+ pipe = DiffusionPipeline.from_pretrained(
22
+ "stabilityai/stable-diffusion-xl-base-1.0",
23
+ torch_dtype=torch.float16,
24
+ use_safetensors=True,
25
+ variant="fp16",
26
+ )
27
+
28
+ if use_refiner: # Keep the refiner logic but disabled initially
29
+ refiner = DiffusionPipeline.from_pretrained(
30
+ "stabilityai/stable-diffusion-xl-refiner-1.0",
31
+ text_encoder_2=pipe.text_encoder_2,
32
+ vae=pipe.vae,
33
+ torch_dtype=torch.float16,
34
+ use_safetensors=True,
35
+ variant="fp16",
36
+ )
37
+ refiner = refiner.to("cuda")
38
+ pipe.enable_model_cpu_offload()
39
  else:
40
+ pipe = pipe.to("cuda")
41
+ return pipe
42
+
43
+ # Load the pipeline
44
+ pipe = load_pipeline()
45
+
46
+ st.title("Stable Diffusion XL Image Generator")
47
+
48
+ # Input text field for the prompt
49
+ prompt = st.text_input("Enter your prompt:", "a photo of Pikachu fine dining with a view to the Eiffel Tower")
50
+
51
+ # Seed input (optional)
52
+ seed = st.number_input("Enter a seed (optional):", min_value=0, value=random.randint(0, sys.maxsize))
53
+
54
+ if st.button("Generate Image"):
55
+ if not prompt:
56
+ st.warning("Please enter a prompt.")
57
+ else:
58
+ with st.spinner("Generating image..."):
59
+ images = pipe(
60
+ prompt=prompt,
61
+ output_type="latent" if use_refiner else "pil",
62
+ generator=torch.Generator("cuda").manual_seed(seed),
63
+ ).images
64
+ if use_refiner: # Condition still here, but the refiner is not used
65
+ images = refiner(prompt=prompt, image=images).images
66
+ st.write(f"Prompt:\t{prompt}\nSeed:\t{seed}")
67
+ st.image(images[0]) #Display in Streamlit
68
+ # You might want to add a download button here
69
+
70
+ # Add a checkbox for the refiner
71
+ #use_refiner = st.checkbox("Use Refiner (Experimental)", value=False)
72
+