MackinationsAi commited on
Commit
a5205b4
·
verified ·
1 Parent(s): de161c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -29
app.py CHANGED
@@ -1,16 +1,13 @@
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/*"
@@ -19,11 +16,10 @@ def generate_image(api_key, prompt, aspect_ratio, mode, model, seed, output_form
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)
@@ -31,10 +27,10 @@ def generate_image(api_key, prompt, aspect_ratio, mode, model, seed, output_form
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:
@@ -47,6 +43,7 @@ def generate_image(api_key, prompt, aspect_ratio, mode, model, seed, output_form
47
  )
48
  return response
49
 
 
50
  def main():
51
  st.image("static/SD3_webui_logo_image.png", use_column_width=True)
52
 
@@ -56,8 +53,6 @@ def main():
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)
@@ -65,45 +60,44 @@ def main():
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()
 
1
  import streamlit as st
2
  import requests
3
  from PIL import Image
 
4
  import io
5
  import os
6
+ import time
7
 
8
+ st.set_page_config(initial_sidebar_state="collapsed", layout="centered")
 
9
 
10
+ def generate_image(api_key, prompt, aspect_ratio, mode, model, seed, output_format, strength=None, negative_prompt=None, image_file=None):
 
 
11
  headers = {
12
  "authorization": f"Bearer {api_key}",
13
  "accept": "image/*"
 
16
  "prompt": prompt,
17
  "mode": mode,
18
  "model": model,
 
19
  "output_format": output_format
20
  }
21
 
22
+ files = {"none": ''}
23
 
24
  if mode == "text-to-image":
25
  files["aspect_ratio"] = (None, aspect_ratio)
 
27
  if seed != 0:
28
  data['seed'] = seed
29
  if negative_prompt:
30
+ data['negative_prompt'] = negative_prompt
31
  if image_file is not None:
32
+ image_bytes = image_file.getvalue()
33
+ files = {'image': ('image.jpg', image_bytes, 'image/jpeg')}
34
  if strength is not None:
35
  files["strength"] = (None, str(strength))
36
  if seed is not None:
 
43
  )
44
  return response
45
 
46
+ # Streamlit Ui
47
  def main():
48
  st.image("static/SD3_webui_logo_image.png", use_column_width=True)
49
 
 
53
  st.session_state.prompt = ""
54
  if 'negative_prompt' not in st.session_state:
55
  st.session_state.negative_prompt = ""
 
 
56
 
57
  api_key = st.text_input("Enter your API key:", type="password", value=st.session_state.api_key)
58
  models = st.selectbox("Select model:", ["sd3", "sd3-turbo"], index=0)
 
60
  prompt = st.text_area("Enter positive prompt:", value=st.session_state.prompt)
61
  negative_prompt = st.text_input("Enter negative prompt: (optional)", value=st.session_state.negative_prompt)
62
  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
63
+ seed = st.number_input("Set seed: (randomize = 0)", min_value=0, max_value=4294967294, value=0, step=1)
64
  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
 
65
  output_formats = st.selectbox("Select the output format:", ["jpeg", "png"], index=1)
66
 
67
  st.session_state.api_key = api_key
68
  st.session_state.prompt = prompt
69
  st.session_state.negative_prompt = negative_prompt
 
70
 
71
  uploaded_file = st.file_uploader("Upload for image-to-image generation:", type=["png", "jpg", "jpeg"]) if mode == "image-to-image" else None
72
 
73
+ if st.button(':blue[Generate Image]', use_container_width=True):
74
  with st.spinner('Generating Image...'):
75
+ result = generate_image(api_key, prompt, aspect_ratios, mode, models, seed, output_formats, strength, negative_prompt, uploaded_file, download)
76
  if result.status_code == 200:
77
  image = Image.open(io.BytesIO(result.content))
78
  st.image(image, use_column_width=True)
79
+ current_time = time.strftime("%H.%M.%S").lower()
80
+ file_path = os.path.join(f"gen_{models}_{seed}_{current_time}.{output_formats}")
81
  image.save(file_path)
82
  st.success(f'Image saved to {file_path}')
83
+ # Download generated image for Huggingface Spaces add-on
84
+ with open(file_path, "rb") as file:
85
+ download = st.download_button(label=":green[Download Image]", data=file, file_name=file_path, mime=f"image/{output_formats}", use_container_width=True)
86
  else:
87
  st.error('Failed to generate image: ' + str(result.json()))
88
+
89
  with st.sidebar:
90
+ st.link_button("Stability.Ai _ API Documentation", "https://platform.stability.ai/docs/api-reference", use_container_width=True)
91
+ with st.expander("Image Generation Costs", expanded=True):
92
+ st.markdown("""
 
93
  - SD3 - 6.5 credits per image or $0.065
94
  - SD3 Turbo - 4.0 credits per image or $0.04
95
 
96
  Additional credits can be purchased via the account_page button below.
97
 
98
+ Credits cost $10 per 1,000 credits, which is enough credits for roughly 154 SD3 images or 250 SD3 Turbo images.
99
+ """)
100
+ st.link_button("Account_Page", "https://platform.stability.ai/account/credits", use_container_width=True)
 
101
 
102
  if __name__ == "__main__":
103
  main()