Download app.py from tahirsher/Versatile_Text_to_Image_Generator: direct link, hf CLI and curl.
- Browser
- Download file 1.55 kB
-
https://huggingface.co/spaces/tahirsher/Versatile_Text_to_Image_Generator/resolve/main/app.py
- Command line
-
hf download hf://spaces/tahirsher/Versatile_Text_to_Image_Generator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/tahirsher/Versatile_Text_to_Image_Generator/resolve/main/app.py
1.55 kB
| 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 | |
| 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.") | |