Spaces:
Sleeping
Sleeping
Commit
·
7d9848b
1
Parent(s):
30b12a2
Update main.py
Browse files
main.py
CHANGED
@@ -27,6 +27,18 @@ def generate_image_from_text(prompt):
|
|
27 |
else:
|
28 |
r.raise_for_status()
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def upscale_image_stable_diffusion(image_bytes):
|
31 |
# Set up environment variables
|
32 |
os.environ['STABILITY_HOST'] = 'grpc.stability.ai:443'
|
@@ -81,8 +93,11 @@ def main():
|
|
81 |
st.success("Generating image from text prompt...")
|
82 |
image_bytes = generate_image_from_text(prompt)
|
83 |
|
|
|
|
|
|
|
84 |
st.success("Upscaling image with stable-diffusion-x4-latent-upscaler...")
|
85 |
-
upscaled_image_bytes = upscale_image_stable_diffusion(
|
86 |
|
87 |
st.success("Further upscaling image with GFPGAN...")
|
88 |
img = further_upscale_image(upscaled_image_bytes)
|
|
|
27 |
else:
|
28 |
r.raise_for_status()
|
29 |
|
30 |
+
def resize_image(image_bytes, max_size=(1024, 1024)):
|
31 |
+
# Open the image from bytes
|
32 |
+
img = Image.open(BytesIO(image_bytes))
|
33 |
+
|
34 |
+
# Resize the image
|
35 |
+
img.thumbnail(max_size)
|
36 |
+
|
37 |
+
# Save it back to bytes
|
38 |
+
buffer = BytesIO()
|
39 |
+
img.save(buffer, format="PNG")
|
40 |
+
return buffer.getvalue()
|
41 |
+
|
42 |
def upscale_image_stable_diffusion(image_bytes):
|
43 |
# Set up environment variables
|
44 |
os.environ['STABILITY_HOST'] = 'grpc.stability.ai:443'
|
|
|
93 |
st.success("Generating image from text prompt...")
|
94 |
image_bytes = generate_image_from_text(prompt)
|
95 |
|
96 |
+
st.success("Resizing image...")
|
97 |
+
resized_image_bytes = resize_image(image_bytes)
|
98 |
+
|
99 |
st.success("Upscaling image with stable-diffusion-x4-latent-upscaler...")
|
100 |
+
upscaled_image_bytes = upscale_image_stable_diffusion(resized_image_bytes)
|
101 |
|
102 |
st.success("Further upscaling image with GFPGAN...")
|
103 |
img = further_upscale_image(upscaled_image_bytes)
|