File size: 3,056 Bytes
eb016d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
from PIL import Image
from io import BytesIO
# from IPython.display import display
import base64

# helper decoder
def decode_base64_image(image_string):
    base64_image = base64.b64decode(image_string)
    buffer = BytesIO(base64_image)
    return Image.open(buffer)

# display PIL images as grid
def display_image(image=None,width=500,height=500):
    img = image.resize((width, height))
    return img

# API Gateway endpoint URL
api_url = 'https://a02q342s5b.execute-api.us-east-2.amazonaws.com/reinvent-demo-inf2-sm-20231114'


# ===========
# Define Streamlit UI elements
st.title('Stable Diffusion XL with Refiner Image Generation')



prompt = st.text_area("Enter your prompt:", 
                      "Manatee astronaut in space, sci-fi, future, cold color palette, muted colors, detailed, 8k")

negative_prompt = st.text_area("Enter your negative prompt:", 
                      "anime, cartoon, graphic, text, painting, crayon, graphite, abstract glitch, blurry")

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...")
# seed = 555

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.)", 
                                min_value=1, 
                                max_value=100, 
                                value=20)
denoising_start = st.slider("Denoising Start (when to stop modifying the overall image and start refining the details)", 
                            min_value=0.0, 
                            max_value=1.0, 
                            value=0.8)



if st.button('Generate Image'):
    with st.spinner(f'Generating Image with {num_inference_steps} iterations, beginning to refine around iteration {int(num_inference_steps * denoising_start)}...'):
        # ===============
        # Example input data
        prompt_input = {
            "prompt": prompt,
            "parameters": {
                "num_inference_steps": num_inference_steps,
                # "seed": seed,
                "negative_prompt": negative_prompt
                # "denoising_start": denoising_start
            }
        }

        # Make API request
        response = requests.post(api_url, json=prompt_input)

        # Process and display the response
        if response.status_code == 200:
            result = response.json()
            # st.success(f"Prediction result: {result}")
            image = display_image(decode_base64_image(result["generated_images"][0]))
            st.header("SDXL Base + Refiner")
            st.image(image, 
                 caption=f"SDXL Base + Refiner, {num_inference_steps} iterations, beginning to refine around iteration {int(num_inference_steps * denoising_start)}") 
        else:
            st.error(f"Error: {response.text}")