Waseem7711 commited on
Commit
738528e
·
verified ·
1 Parent(s): 44769ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -51
app.py CHANGED
@@ -1,53 +1,53 @@
1
- # prompt: i found this error please fix this "File "/home/user/app/app.py", line 14
2
- # !pip install --quiet --upgrade diffusers transformers accelerate invisible_watermark mediapy
3
- # ^
4
- # SyntaxError: invalid syntax"
5
-
6
- import mediapy as media
7
- import random
8
- import sys
9
- import torch
10
- from diffusers import DiffusionPipeline
11
  import streamlit as st
12
- # Remove this line:
13
- # %pip install --quiet --upgrade diffusers transformers accelerate invisible_watermark mediapy
14
-
15
- # Install necessary libraries if not already installed
16
- try:
17
- import diffusers
18
- import transformers
19
- import accelerate
20
- import invisible_watermark
21
- import mediapy
22
- import streamlit
23
- import torch
24
- except ImportError:
25
- !pip install --quiet --upgrade diffusers transformers accelerate invisible_watermark mediapy streamlit torch
26
-
27
- use_refiner = False
28
-
29
-
30
- pipe = DiffusionPipeline.from_pretrained(
31
- "stabilityai/stable-diffusion-xl-base-1.0",
32
- torch_dtype=torch.float16,
33
- use_safetensors=True,
34
- variant="fp16",
35
  )
36
-
37
- if use_refiner:
38
- refiner = DiffusionPipeline.from_pretrained(
39
- "stabilityai/stable-diffusion-xl-refiner-1.0",
40
- text_encoder_2=pipe.text_encoder_2,
41
- vae=pipe.vae,
42
- torch_dtype=torch.float16,
43
- use_safetensors=True,
44
- variant="fp16",
45
- )
46
-
47
- refiner = refiner.to("cuda")
48
-
49
- pipe.enable_model_cpu_offload()
50
- else:
51
- pipe = pipe.to("cuda")
52
-
53
- # ... (rest of your code)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from diffusers import StableDiffusionPipeline
3
+ import torch
4
+ from PIL import Image
5
+
6
+ # Title and description of the app
7
+ st.title("🖼️ Stable Diffusion Image Generator")
8
+ st.write("Generate images from text using the Stable Diffusion v1.5 model!")
9
+
10
+ # Sidebar for user inputs
11
+ st.sidebar.title("Input Options")
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
+ # Load the pipeline when the app starts
16
+ @st.cache_resource
17
+ def load_pipeline():
18
+ # Use a smaller model or a more efficient pipeline
19
+ pipe = StableDiffusionPipeline.from_pretrained(
20
+ "runwayml/stable-diffusion-v1-5",
21
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
22
+ revision="fp16" if torch.cuda.is_available() else None
 
 
23
  )
24
+ device = "cuda" if torch.cuda.is_available() else "cpu"
25
+ pipe = pipe.to(device)
26
+ return pipe
27
+
28
+ pipe = load_pipeline()
29
+
30
+ # Generate image when button is clicked
31
+ if generate_button:
32
+ st.write(f"### Prompt: {prompt}")
33
+ with st.spinner("Generating image... Please wait."):
34
+ try:
35
+ # Generate the image
36
+ with torch.autocast("cuda" if torch.cuda.is_available() else "cpu"):
37
+ image = pipe(prompt).images[0]
38
+
39
+ # Display the generated image
40
+ st.image(image, caption="Generated Image", use_column_width=True)
41
+
42
+ # Option to download the image
43
+ img_path = "generated_image.png"
44
+ image.save(img_path)
45
+ with open(img_path, "rb") as img_file:
46
+ st.download_button(
47
+ label="Download Image",
48
+ data=img_file,
49
+ file_name="generated_image.png",
50
+ mime="image/png"
51
+ )
52
+ except Exception as e:
53
+ st.error(f"An error occurred: {e}")