Spaces:
Running
Running
Commit
·
eb016d8
1
Parent(s):
88ebdc5
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
10 |
+
base64_image = base64.b64decode(image_string)
|
11 |
+
buffer = BytesIO(base64_image)
|
12 |
+
return Image.open(buffer)
|
13 |
+
|
14 |
+
# display PIL images as grid
|
15 |
+
def display_image(image=None,width=500,height=500):
|
16 |
+
img = image.resize((width, height))
|
17 |
+
return img
|
18 |
+
|
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 (set to same value to generate same image, if other inputs are the same, change to generate a different image for same inputs)", value=None, placeholder="Type a number...")
|
36 |
+
# seed = 555
|
37 |
+
|
38 |
+
num_inference_steps = st.slider("Number of Inference Steps (more steps might improve quality, with diminishing marginal returns. 30-50 seems best, but your mileage may vary.)",
|
39 |
+
min_value=1,
|
40 |
+
max_value=100,
|
41 |
+
value=20)
|
42 |
+
denoising_start = st.slider("Denoising Start (when to stop modifying the overall image and start refining the details)",
|
43 |
+
min_value=0.0,
|
44 |
+
max_value=1.0,
|
45 |
+
value=0.8)
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
+
if st.button('Generate Image'):
|
50 |
+
with st.spinner(f'Generating Image with {num_inference_steps} iterations, beginning to refine around iteration {int(num_inference_steps * denoising_start)}...'):
|
51 |
+
# ===============
|
52 |
+
# Example input data
|
53 |
+
prompt_input = {
|
54 |
+
"prompt": prompt,
|
55 |
+
"parameters": {
|
56 |
+
"num_inference_steps": num_inference_steps,
|
57 |
+
# "seed": seed,
|
58 |
+
"negative_prompt": negative_prompt
|
59 |
+
# "denoising_start": denoising_start
|
60 |
+
}
|
61 |
+
}
|
62 |
+
|
63 |
+
# Make API request
|
64 |
+
response = requests.post(api_url, json=prompt_input)
|
65 |
+
|
66 |
+
# Process and display the response
|
67 |
+
if response.status_code == 200:
|
68 |
+
result = response.json()
|
69 |
+
# st.success(f"Prediction result: {result}")
|
70 |
+
image = display_image(decode_base64_image(result["generated_images"][0]))
|
71 |
+
st.header("SDXL Base + Refiner")
|
72 |
+
st.image(image,
|
73 |
+
caption=f"SDXL Base + Refiner, {num_inference_steps} iterations, beginning to refine around iteration {int(num_inference_steps * denoising_start)}")
|
74 |
+
else:
|
75 |
+
st.error(f"Error: {response.text}")
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
|