Skip to content

Instantly share code, notes, and snippets.

@pleabargain
Created October 25, 2024 09:15
Show Gist options
  • Save pleabargain/558d5b05bbb58aabe14aee1ae047a799 to your computer and use it in GitHub Desktop.
Save pleabargain/558d5b05bbb58aabe14aee1ae047a799 to your computer and use it in GitHub Desktop.
streamlit image generator using replicate
#on windows 11
# python -m streamlit run app.py
import streamlit as st
import replicate
import time
from dotenv import load_dotenv
import requests
from PIL import Image
from io import BytesIO
load_dotenv()
st.title("AI Image Generator")
prompt = st.text_input("Enter a prompt for the image:")
# Add a button to generate the image
if st.button("Generate Image"):
with st.spinner('Generating image...'):
start_time = time.time()
output = replicate.run(
"stability-ai/sdxl:39ed52f2a78e934b3ba6e2a89f5b1c712de7dfea535525255b1aa35c5565e08b",
input={
"width": 1024,
"height": 1024,
"prompt": prompt,
"refine": "expert_ensemble_refiner",
"num_outputs": 1,
"apply_watermark": False,
"negative_prompt": "low quality, worst quality",
"num_inference_steps": 25
}
)
# The output is typically a list with one or more image URLs
if output and len(output) > 0:
# Get the first image URL
image_url = output[0]
# Download the image
response = requests.get(image_url)
if response.status_code == 200:
# Convert the image data to a format Streamlit can display
image = Image.open(BytesIO(response.content))
# Display the generated image
st.image(image)
end_time = time.time()
elapsed_time = end_time - start_time
st.write(f"Image generated in {elapsed_time:.2f} seconds")
else:
st.error("Failed to download the generated image")
else:
st.error("No image was generated")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment