Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- app.py +109 -0
- pages/Gallery.py +26 -0
- requirements.txt +4 -0
- static/SD3_webui_logo_image.png +0 -0
- static/sai_logo.ico +0 -0
app.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import datetime
|
| 5 |
+
import io
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
output_dir = "outputs"
|
| 9 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 10 |
+
|
| 11 |
+
st.set_page_config(initial_sidebar_state="collapsed", page_icon="static/sai_logo.ico", layout="centered")
|
| 12 |
+
|
| 13 |
+
def generate_image(api_key, prompt, aspect_ratio, mode, model, seed, output_format, strength=None, negative_prompt=None, image_file=None): #, num_outputs=1):
|
| 14 |
+
headers = {
|
| 15 |
+
"authorization": f"Bearer {api_key}",
|
| 16 |
+
"accept": "image/*"
|
| 17 |
+
}
|
| 18 |
+
data = {
|
| 19 |
+
"prompt": prompt,
|
| 20 |
+
"mode": mode,
|
| 21 |
+
"model": model,
|
| 22 |
+
# "num_outputs": num_outputs,
|
| 23 |
+
"output_format": output_format
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
files = {"none": ''} # Default to no file if none provided
|
| 27 |
+
|
| 28 |
+
if mode == "text-to-image":
|
| 29 |
+
files["aspect_ratio"] = (None, aspect_ratio)
|
| 30 |
+
elif mode == "image-to-image":
|
| 31 |
+
if seed != 0:
|
| 32 |
+
data['seed'] = seed
|
| 33 |
+
if negative_prompt:
|
| 34 |
+
data['negative_prompt'] = negative_prompt
|
| 35 |
+
if image_file is not None:
|
| 36 |
+
image_bytes = image_file.getvalue()
|
| 37 |
+
files = {'image': ('image.jpg', image_bytes, 'image/jpeg')}
|
| 38 |
+
if strength is not None:
|
| 39 |
+
files["strength"] = (None, str(strength))
|
| 40 |
+
if seed is not None:
|
| 41 |
+
files["seed"] = (None, str(seed))
|
| 42 |
+
response = requests.post(
|
| 43 |
+
"https://api.stability.ai/v2beta/stable-image/generate/sd3",
|
| 44 |
+
headers=headers,
|
| 45 |
+
files=files,
|
| 46 |
+
data=data
|
| 47 |
+
)
|
| 48 |
+
return response
|
| 49 |
+
|
| 50 |
+
def main():
|
| 51 |
+
st.image("static/SD3_webui_logo_image.png", width=700)
|
| 52 |
+
|
| 53 |
+
if 'api_key' not in st.session_state:
|
| 54 |
+
st.session_state.api_key = ""
|
| 55 |
+
if 'prompt' not in st.session_state:
|
| 56 |
+
st.session_state.prompt = ""
|
| 57 |
+
if 'negative_prompt' not in st.session_state:
|
| 58 |
+
st.session_state.negative_prompt = ""
|
| 59 |
+
if 'seed' not in st.session_state:
|
| 60 |
+
st.session_state.seed = 0
|
| 61 |
+
|
| 62 |
+
api_key = st.text_input("Enter your API key:", type="password", value=st.session_state.api_key)
|
| 63 |
+
models = st.selectbox("Select model:", ["sd3", "sd3-turbo"], index=0)
|
| 64 |
+
mode = st.selectbox("Select generation mode:", ["text-to-image", "image-to-image"], index=0)
|
| 65 |
+
prompt = st.text_area("Enter positive prompt:", value=st.session_state.prompt)
|
| 66 |
+
negative_prompt = st.text_input("Enter negative prompt: (optional)", value=st.session_state.negative_prompt)
|
| 67 |
+
aspect_ratios = st.selectbox("Select the aspect ratio:", ["1:1", "2:3", "3:2", "4:5", "5:4", "16:9", "21:9", "9:16", "9:21"], index=0) if mode == "text-to-image" else None
|
| 68 |
+
seed = st.slider("Set seed: (randomize = 0)", min_value=0, max_value=4294967294, value=st.session_state.seed, step=1)
|
| 69 |
+
strength = st.slider("Denoising strength:", min_value=0.0, max_value=1.0, value=0.5, step=0.01) if mode == "image-to-image" else None
|
| 70 |
+
# num_outputs = st.number_input("Batch Count:", min_value=1, max_value=20, value=1, step=1)
|
| 71 |
+
output_formats = st.selectbox("Select the output format:", ["jpeg", "png"], index=1)
|
| 72 |
+
|
| 73 |
+
st.session_state.api_key = api_key
|
| 74 |
+
st.session_state.prompt = prompt
|
| 75 |
+
st.session_state.negative_prompt = negative_prompt
|
| 76 |
+
st.session_state.seed = seed
|
| 77 |
+
|
| 78 |
+
uploaded_file = st.file_uploader("Upload for image-to-image generation:", type=["png", "jpg", "jpeg"]) if mode == "image-to-image" else None
|
| 79 |
+
|
| 80 |
+
if st.button('Generate Image', use_container_width=True):
|
| 81 |
+
with st.spinner('Generating Image...'):
|
| 82 |
+
result = generate_image(api_key, prompt, aspect_ratios, mode, models, seed, output_formats, strength, negative_prompt, uploaded_file)
|
| 83 |
+
if result.status_code == 200:
|
| 84 |
+
image = Image.open(io.BytesIO(result.content))
|
| 85 |
+
st.image(image, use_column_width=True)
|
| 86 |
+
current_time = datetime.datetime.now().strftime("%Y%m%d_%H.%M.%S").lower()
|
| 87 |
+
file_path = os.path.join(output_dir, f"gen_{models}_{seed}_{current_time}.{output_formats}")
|
| 88 |
+
image.save(file_path)
|
| 89 |
+
st.success(f'Image saved to {file_path}')
|
| 90 |
+
else:
|
| 91 |
+
st.error('Failed to generate image: ' + str(result.json()))
|
| 92 |
+
|
| 93 |
+
with st.sidebar:
|
| 94 |
+
st.link_button("Stability.Ai _ API Documentation", "https://platform.stability.ai/docs/api-reference")
|
| 95 |
+
with st.expander(("Image Generation Costs"), expanded=True):
|
| 96 |
+
st.markdown((
|
| 97 |
+
"""
|
| 98 |
+
- SD3 - 6.5 credits per image or $0.065
|
| 99 |
+
- SD3 Turbo - 4.0 credits per image or $0.04
|
| 100 |
+
|
| 101 |
+
Additional credits can be purchased via the account_page button below.
|
| 102 |
+
|
| 103 |
+
Credits cost $10 per 1,000 credits, which is enough credits for roughly 154 SD3 images or 250 SD3 Turbo images.
|
| 104 |
+
"""
|
| 105 |
+
))
|
| 106 |
+
st.link_button("Account_Page", "https://platform.stability.ai/account/credits")
|
| 107 |
+
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
main()
|
pages/Gallery.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import glob
|
| 5 |
+
|
| 6 |
+
output_dir = "outputs"
|
| 7 |
+
st.set_page_config(initial_sidebar_state="expanded", page_icon="static/sai_logo.ico", layout="centered")
|
| 8 |
+
|
| 9 |
+
with st.sidebar:
|
| 10 |
+
st.link_button("Stability.Ai _ API Documentation", "https://platform.stability.ai/docs/api-reference")
|
| 11 |
+
with st.expander(("Image Generation Costs"), expanded=True):
|
| 12 |
+
st.markdown((
|
| 13 |
+
"""
|
| 14 |
+
- SD3 - 6.5 credits per image or $0.065
|
| 15 |
+
- SD3 Turbo - 4.0 credits per image or $0.04
|
| 16 |
+
|
| 17 |
+
Additional credits can be purchased via the account_page button below.
|
| 18 |
+
|
| 19 |
+
Credits cost $10 per 1,000 credits, which is enough credits for roughly 154 SD3 images or 250 SD3 Turbo images.
|
| 20 |
+
"""
|
| 21 |
+
))
|
| 22 |
+
st.link_button("Account_Page", "https://platform.stability.ai/account/credits")
|
| 23 |
+
files = glob.glob(os.path.join(output_dir, '*'))
|
| 24 |
+
for file in files:
|
| 25 |
+
image = Image.open(file)
|
| 26 |
+
st.image(image, caption=os.path.basename(file), use_column_width=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
requests
|
| 3 |
+
pillow
|
| 4 |
+
datetime
|
static/SD3_webui_logo_image.png
ADDED
|
static/sai_logo.ico
ADDED
|
|