Spaces:
Sleeping
Sleeping
File size: 1,426 Bytes
fc7e66d 632f9b4 fc7e66d 632f9b4 fc7e66d 632f9b4 134db1a fc7e66d 134db1a 632f9b4 fc7e66d 632f9b4 fc7e66d 632f9b4 fc7e66d c057e21 fc7e66d | 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 | # Streamlit and Machine Learning libraries
import streamlit as st
import torch
from torch import autocast
from diffusers import StableDiffusionPipeline
# Libraries for processing image
from PIL import Image
# Private modules
from authtoken import auth_token
# Download stable diffusion model from Hugging Face
modelid = "CompVis/stable-diffusion-v1-4"
stable_diffusion_model = StableDiffusionPipeline.from_pretrained(modelid, revision="fp16", dtype=torch.float16, use_auth_token=auth_token)
# Create a Streamlit app
st.set_page_config(
page_title="Text to Image App",
page_icon="🖼️",
layout="centered",
)
# Create input box on the user interface
st.write("# Text to Image app")
prompt = st.text_area("Enter your text here:", height=10, max_chars=200)
# Create a placeholder to show the generated image
img_placeholder = st.empty()
# Generate image from text
def generate_image():
if prompt:
st.write("Generating image...")
try:
with autocast():
image = stable_diffusion_model(prompt, guidance_scale=8.5)["sample"][0]
# Display the generated image on the user interface
st.image(image, caption="Generated Image", use_column_width=True)
except Exception as e:
st.error(f"Error generating the image: {str(e)}")
# Create a button to trigger image generation
if st.button("Generate Image"):
generate_image()
|