-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
83 lines (67 loc) · 2.65 KB
/
app.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import streamlit as st
import io
from PIL import Image
import PIL
import requests
# from dotenv import load_dotenv
import os
dotenv_path = r'env'
# load_dotenv(dotenv_path)
# Access environment variables
HF_API = os.getenv('HF_API')
HF_API = st.secrets["HF_API"]
def generate_image(prompt, style, color_palette, image_size, background):
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
headers = {"Authorization": f"Bearer {HF_API}"}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.content
image_bytes = query({
"inputs": f"{prompt}",
"style": style,
"color_palette": color_palette,
"image_size": image_size,
"background": background,
})
try:
image = Image.open(io.BytesIO(image_bytes))
return image
except PIL.UnidentifiedImageError as e:
st.error(f"Error: {e} 😞")
return None
st.title('AI Image Generation App 🎨')
with st.expander('About the image 📚'):
st.write('Given a text prompt, it will generate an image 📸')
st.subheader('Image Generation using Hugging Face Diffuser 🔥')
input_prompt = st.text_input(
label='Enter your text input for image Generation 💬', placeholder='text goes here...'
)
style_options = ["Realistic", "Cartoonish", "Abstract"]
style = st.selectbox("Select image style 🎭", style_options)
color_palette_options = ["Bright and Bold", "Pastel", "Monochromatic"]
color_palette = st.selectbox("Select color palette 🎨", color_palette_options)
image_size_options = [256, 512, 1024]
color_palette = st.selectbox("Select color palette 🎨", image_size_options)
image_size = st.slider("Select image size 🔍", min_value=256, max_value=1024)
background_options = ["Solid Color", "Gradient", "Texture"]
background = st.selectbox("Select background style 🌈", background_options)
if input_prompt is not None:
if st.button('Generate Image 🔮'):
image = generate_image(prompt=input_prompt, style=style,
color_palette=color_palette, image_size=image_size, background=background)
if image is not None:
st.image(image)
st.success("Image generated successfully! 🎉")
else:
st.error("Error generating image 😞")
# Create a download button
buf = io.BytesIO()
image.save(buf, format="PNG")
byte_im = buf.getvalue()
st.download_button(
label="Download Image 💾",
data=byte_im,
file_name="generated_image.png",
mime="image/png"
)
# END APP