Sean Laurent
commited on
Commit
·
212910b
1
Parent(s):
5cbaaf6
Added S3 integration
Browse files
app.py
CHANGED
|
@@ -4,9 +4,21 @@ from io import BytesIO
|
|
| 4 |
from PIL import Image
|
| 5 |
import gradio as gr
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
model_key = os.environ.get("model_key")
|
| 9 |
api_key = os.environ.get("api_key")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
model_inputs = {
|
| 12 |
"endpoint": "txt2img",
|
|
@@ -25,6 +37,9 @@ model_inputs = {
|
|
| 25 |
}
|
| 26 |
}
|
| 27 |
|
|
|
|
|
|
|
|
|
|
| 28 |
def stable_diffusion_txt2img(prompt, api_key, model_key, model_inputs):
|
| 29 |
# Update the model_inputs with the provided prompt
|
| 30 |
model_inputs["params"]["prompt"] = prompt
|
|
@@ -37,6 +52,16 @@ def stable_diffusion_txt2img(prompt, api_key, model_key, model_inputs):
|
|
| 37 |
image_encoded = image_byte_string[0].encode("utf-8")
|
| 38 |
image_bytes = BytesIO(base64.b64decode(image_encoded))
|
| 39 |
image = Image.open(image_bytes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return image
|
| 41 |
|
| 42 |
# Gradio Interface
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import gradio as gr
|
| 6 |
import os
|
| 7 |
+
import boto3
|
| 8 |
|
| 9 |
model_key = os.environ.get("model_key")
|
| 10 |
api_key = os.environ.get("api_key")
|
| 11 |
+
aws_access_key_id = os.environ.get("aws_access_key_id")
|
| 12 |
+
aws_secret_access_key = os.environ.get("aws_secret_access_key")
|
| 13 |
+
|
| 14 |
+
# Create a session using AWS credentials
|
| 15 |
+
session = boto3.Session(aws_access_key_id, aws_secret_access_key)
|
| 16 |
+
|
| 17 |
+
# Create an S3 resource object using the session
|
| 18 |
+
s3 = session.resource('s3')
|
| 19 |
+
|
| 20 |
+
# Select your bucket
|
| 21 |
+
bucket = s3.Bucket('bwlmonet')
|
| 22 |
|
| 23 |
model_inputs = {
|
| 24 |
"endpoint": "txt2img",
|
|
|
|
| 37 |
}
|
| 38 |
}
|
| 39 |
|
| 40 |
+
for obj in bucket.objects.all():
|
| 41 |
+
print(obj.key)
|
| 42 |
+
|
| 43 |
def stable_diffusion_txt2img(prompt, api_key, model_key, model_inputs):
|
| 44 |
# Update the model_inputs with the provided prompt
|
| 45 |
model_inputs["params"]["prompt"] = prompt
|
|
|
|
| 52 |
image_encoded = image_byte_string[0].encode("utf-8")
|
| 53 |
image_bytes = BytesIO(base64.b64decode(image_encoded))
|
| 54 |
image = Image.open(image_bytes)
|
| 55 |
+
|
| 56 |
+
# Save image to S3
|
| 57 |
+
key = f"{prompt}.png"
|
| 58 |
+
image.save(key)
|
| 59 |
+
with open(key, "rb") as data:
|
| 60 |
+
bucket.put_object(Key=key, Body=data)
|
| 61 |
+
|
| 62 |
+
for obj in bucket.objects.all():
|
| 63 |
+
print(obj.key)
|
| 64 |
+
|
| 65 |
return image
|
| 66 |
|
| 67 |
# Gradio Interface
|