File size: 1,549 Bytes
8893107
fd39a5a
302cd1a
cd9d635
 
8893107
36f8896
8893107
6aea10a
36f8896
302cd1a
 
 
 
 
 
 
 
 
 
6aea10a
8893107
6aea10a
8893107
 
36f8896
8893107
 
302cd1a
 
 
 
 
8893107
 
 
 
 
302cd1a
 
 
 
 
 
 
 
8893107
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import streamlit as st
from diffusers import DiffusionPipeline
from peft import PeftModel  # Import PEFT model loader
from PIL import Image
import torch

# Load the diffusion pipeline model
@st.cache_resource
def load_pipeline():
    pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
    
    # Ensure PEFT is available before loading LoRA weights
    try:
        pipe.load_lora_weights("Melonie/text_to_image_finetuned")
    except ValueError as e:
        st.error("PEFT backend is required but not properly set up.")
        raise e
    
    if torch.cuda.is_available():
        pipe.to("cuda")  # Move pipeline to GPU if available
    return pipe

pipe = load_pipeline()

# Streamlit app
st.title("Text-to-Image Generation App")

# User input for prompt
user_prompt = st.text_input(
    "Enter your image prompt", 
    value="Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
    help="Provide a detailed description of the image you'd like to generate."
)

# Button to generate the image
if st.button("Generate Image"):
    if user_prompt:
        with st.spinner("Generating image..."):
            try:
                # Generate the image
                image = pipe(user_prompt).images[0]
                
                # Display the generated image
                st.image(image, caption="Generated Image", use_column_width=True)
            except Exception as e:
                st.error(f"Error generating image: {str(e)}")
    else:
        st.error("Please enter a valid prompt.")