tahirsher commited on
Commit
302cd1a
1 Parent(s): 36f8896

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -8
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  from diffusers import DiffusionPipeline
 
3
  from PIL import Image
4
  import torch
5
 
@@ -7,7 +8,16 @@ import torch
7
  @st.cache_resource
8
  def load_pipeline():
9
  pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
10
- pipe.load_lora_weights("Melonie/text_to_image_finetuned")
 
 
 
 
 
 
 
 
 
11
  return pipe
12
 
13
  pipe = load_pipeline()
@@ -16,17 +26,23 @@ pipe = load_pipeline()
16
  st.title("Text-to-Image Generation App")
17
 
18
  # User input for prompt
19
- user_prompt = st.text_input("Enter your image prompt", value="Astronaut in a jungle, cold color palette, muted colors, detailed, 8k")
 
 
 
 
20
 
21
  # Button to generate the image
22
  if st.button("Generate Image"):
23
  if user_prompt:
24
  with st.spinner("Generating image..."):
25
- # Generate the image
26
- image = pipe(user_prompt).images[0]
27
-
28
- # Display the generated image
29
- st.image(image, caption="Generated Image", use_column_width=True)
 
 
 
30
  else:
31
  st.error("Please enter a valid prompt.")
32
-
 
1
  import streamlit as st
2
  from diffusers import DiffusionPipeline
3
+ from peft import PeftModel # Import PEFT model loader
4
  from PIL import Image
5
  import torch
6
 
 
8
  @st.cache_resource
9
  def load_pipeline():
10
  pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
11
+
12
+ # Ensure PEFT is available before loading LoRA weights
13
+ try:
14
+ pipe.load_lora_weights("Melonie/text_to_image_finetuned")
15
+ except ValueError as e:
16
+ st.error("PEFT backend is required but not properly set up.")
17
+ raise e
18
+
19
+ if torch.cuda.is_available():
20
+ pipe.to("cuda") # Move pipeline to GPU if available
21
  return pipe
22
 
23
  pipe = load_pipeline()
 
26
  st.title("Text-to-Image Generation App")
27
 
28
  # User input for prompt
29
+ user_prompt = st.text_input(
30
+ "Enter your image prompt",
31
+ value="Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
32
+ help="Provide a detailed description of the image you'd like to generate."
33
+ )
34
 
35
  # Button to generate the image
36
  if st.button("Generate Image"):
37
  if user_prompt:
38
  with st.spinner("Generating image..."):
39
+ try:
40
+ # Generate the image
41
+ image = pipe(user_prompt).images[0]
42
+
43
+ # Display the generated image
44
+ st.image(image, caption="Generated Image", use_column_width=True)
45
+ except Exception as e:
46
+ st.error(f"Error generating image: {str(e)}")
47
  else:
48
  st.error("Please enter a valid prompt.")