huntingcarlisle commited on
Commit
cd6c769
1 Parent(s): a758727

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +151 -50
app.py CHANGED
@@ -1,9 +1,14 @@
1
  import streamlit as st
 
 
2
  import requests
3
  from PIL import Image
4
  from io import BytesIO
5
  # from IPython.display import display
6
  import base64
 
 
 
7
 
8
  # helper decoder
9
  def decode_base64_image(image_string):
@@ -19,66 +24,162 @@ def display_image(image=None,width=500,height=500):
19
  # API Gateway endpoint URL
20
  api_url = 'https://a02q342s5b.execute-api.us-east-2.amazonaws.com/reinvent-demo-inf2-sm-20231114'
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # ===========
24
- # Define Streamlit UI elements
25
- st.title('Stable Diffusion XL with Refiner Image Generation')
26
 
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- prompt = st.text_area("Enter your prompt:",
30
- "Manatee astronaut in space, sci-fi, future, cold color palette, muted colors, detailed, 8k")
31
 
32
- negative_prompt = st.text_area("Enter your negative prompt:",
33
- "anime, cartoon, graphic, text, painting, crayon, graphite, abstract glitch, blurry")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- seed = st.number_input("Random seed", value=555, placeholder="Type a number...", help="set to same value to generate same image, if other inputs are the same, change to generate a different image for same inputs")
36
- # seed = 555
37
 
38
- num_inference_steps = st.slider("Number of Inference Steps",
 
 
 
 
 
 
 
 
 
 
 
39
  min_value=1,
40
  max_value=100,
41
- value=20,
42
- help="more steps might improve quality, with diminishing marginal returns. 30-50 seems best, but your mileage may vary.")
43
-
44
- denoising_start = st.slider("Denoising Start",
45
- min_value=0.0,
46
- max_value=1.0,
47
- value=0.8,
48
- help="when to stop modifying the overall image and start refining the details")
49
-
50
-
51
-
52
- if st.button('Generate Image'):
53
- with st.spinner(f'Generating Image with {num_inference_steps} iterations, beginning to refine around iteration {int(num_inference_steps * denoising_start)}...'):
54
- # ===============
55
- # Example input data
56
- prompt_input = {
57
- "prompt": prompt,
58
- "parameters": {
59
- "num_inference_steps": num_inference_steps,
60
- "seed": seed,
61
- "negative_prompt": negative_prompt
62
- # "denoising_start": denoising_start
63
- }
64
- }
65
-
66
- # Make API request
67
- response = requests.post(api_url, json=prompt_input)
68
-
69
- # Process and display the response
70
- if response.status_code == 200:
71
- result = response.json()
72
- # st.success(f"Prediction result: {result}")
73
- image = display_image(decode_base64_image(result["generated_images"][0]))
74
- st.header("SDXL Base + Refiner")
75
- st.image(image,
76
- caption=f"SDXL Base + Refiner, {num_inference_steps} iterations, beginning to refine around iteration {int(num_inference_steps * denoising_start)}")
77
- else:
78
- st.error(f"Error: {response.text}")
79
 
80
-
 
 
 
 
 
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
 
84
-
 
1
  import streamlit as st
2
+ # Set the page layout to 'wide'
3
+ st.set_page_config(layout="wide")
4
  import requests
5
  from PIL import Image
6
  from io import BytesIO
7
  # from IPython.display import display
8
  import base64
9
+ import time
10
+
11
+
12
 
13
  # helper decoder
14
  def decode_base64_image(image_string):
 
24
  # API Gateway endpoint URL
25
  api_url = 'https://a02q342s5b.execute-api.us-east-2.amazonaws.com/reinvent-demo-inf2-sm-20231114'
26
 
27
+ # Define the CSS to change the text input background color
28
+ input_field_style = """
29
+ <style>
30
+ /* Customize the text input field background and text color */
31
+ .stTextInput input {
32
+ background-color: #fbd8bf; /* 'Rind' color */
33
+ color: #232F3E; /* Dark text color */
34
+ }
35
+ /* You might also want to change the color for textarea if you're using it */
36
+ .stTextArea textarea {
37
+ background-color: #fbd8bf; /* 'Rind' color */
38
+ color: #232F3E; /* Dark text color */
39
+ }
40
+ </style>
41
+ """
42
+
43
+ # Inject custom styles into the Streamlit app
44
+ st.markdown(input_field_style, unsafe_allow_html=True)
45
+
46
+
47
+ # Creating Tabs
48
+ tab1, tab2 = st.tabs(["Image Generation", "Text Generation"])
49
+
50
+ with tab1:
51
+ # Create two columns for layout
52
+ left_column, right_column = st.columns(2)
53
+ # ===========
54
+ with left_column:
55
+ # Define Streamlit UI elements
56
+ st.title('Stable Diffusion XL Image Generation with AWS Inferentia')
57
+
58
+ prompt_one = st.text_area("Enter your prompt:",
59
+ f"Raccoon astronaut in space, sci-fi, future, cold color palette, muted colors, detailed, 8k")
60
+
61
+ # Number of inference steps
62
+ num_inference_steps_one = st.slider("Number of Inference Steps",
63
+ min_value=1,
64
+ max_value=100,
65
+ value=30,
66
+ help="More steps might improve quality, with diminishing marginal returns. 30-50 seems best, but your mileage may vary.")
67
+
68
+ # Create an expandable section for optional parameters
69
+ with st.expander("Optional Parameters"):
70
+ # Random seed input
71
+ seed_one = st.number_input("Random seed",
72
+ value=555,
73
+ help="Set to the same value to generate the same image if other inputs are the same, change to generate a different image for same inputs.")
74
+
75
+ # Negative prompt input
76
+ negative_prompt_one = st.text_area("Enter your negative prompt:",
77
+ "cartoon, graphic, text, painting, crayon, graphite, abstract glitch, blurry")
78
+
79
+
80
+
81
+
82
 
 
 
 
83
 
84
 
85
+ if st.button('Generate Image'):
86
+ with st.spinner(f'Generating Image with {num_inference_steps_one} iterations'):
87
+ with right_column:
88
+ start_time = time.time()
89
+ # ===============
90
+ # Example input data
91
+ prompt_input_one = {
92
+ "prompt": prompt_one,
93
+ "parameters": {
94
+ "num_inference_steps": num_inference_steps_one,
95
+ "seed": seed_one,
96
+ "negative_prompt": negative_prompt_one
97
+ }
98
+ }
99
 
100
+ # Make API request
101
+ response_one = requests.post(api_url, json=prompt_input_one)
102
 
103
+ # Process and display the response
104
+ if response_one.status_code == 200:
105
+ result_one = response_one.json()
106
+ # st.success(f"Prediction result: {result}")
107
+ image_one = display_image(decode_base64_image(result_one["generated_images"][0]))
108
+ st.image(image_one,
109
+ caption=f"{prompt_one}")
110
+ end_time = time.time()
111
+ total_time = round(end_time - start_time, 2)
112
+ st.text(f"Prompt: {prompt_one}")
113
+ st.text(f"Number of Iterations: {num_inference_steps_one}")
114
+ st.text(f"Random Seed: {seed_one}")
115
+ st.text(f'Total time taken: {total_time} seconds')
116
+ # Calculate and display the time per iteration in milliseconds
117
+ time_per_iteration_ms = (total_time / num_inference_steps_one)
118
+ st.text(f'Time per iteration: {time_per_iteration_ms:.2f} seconds')
119
+ else:
120
+ st.error(f"Error: {response_one.text}")
121
 
 
 
122
 
123
+ with tab2:
124
+ # ===========
125
+ # Define Streamlit UI elements
126
+ st.title('Stable Diffusion XL Image Generation')
127
+
128
+
129
+
130
+ prompt = st.text_area("Enter your prompt:",
131
+ "Raccoons astronaut in space, sci-fi, future, cold color palette, muted colors, detailed, 8k")
132
+
133
+ # Number of inference steps
134
+ num_inference_steps = st.slider("Number of Inference Steps",
135
  min_value=1,
136
  max_value=100,
137
+ value=40,
138
+ help="More steps might improve quality, with diminishing marginal returns. 30-50 seems best, but your mileage may vary.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
+ # Create an expandable section for optional parameters
141
+ with st.expander("Optional Parameters"):
142
+ # Random seed input
143
+ seed = st.number_input("Random seed",
144
+ value=42,
145
+ help="Set to the same value to generate the same image if other inputs are the same, change to generate a different image for same inputs.")
146
 
147
+ # Negative prompt input
148
+ negative_prompt = st.text_area("Enter your negative prompt:",
149
+ "anime, cartoon, graphic, text, painting, crayon, graphite, abstract glitch, blurry")
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+ if st.button('Generate Image:'):
158
+ with st.spinner(f'Generating Image with {num_inference_steps} iterations'):
159
+ # ===============
160
+ # Example input data
161
+ prompt_input = {
162
+ "prompt": prompt,
163
+ "parameters": {
164
+ "num_inference_steps": num_inference_steps,
165
+ "seed": seed,
166
+ "negative_prompt": negative_prompt
167
+ }
168
+ }
169
+
170
+ # Make API request
171
+ response = requests.post(api_url, json=prompt_input)
172
+
173
+ # Process and display the response
174
+ if response.status_code == 200:
175
+ result = response.json()
176
+ # st.success(f"Prediction result: {result}")
177
+ image2 = display_image(decode_base64_image(result["generated_images"][0]))
178
+ st.header("SDXL Base")
179
+ st.image(image2,
180
+ caption=f"SDXL Base, {num_inference_steps} iterations")
181
+ else:
182
+ st.error(f"Error: {response.text}")
183
 
184
 
185
+