Spaces:
Sleeping
Sleeping
File size: 4,473 Bytes
ac621a7 5b01907 50a3a35 ac621a7 9cc7eb2 ac621a7 323c22b ac621a7 323c22b ac621a7 323c22b ac621a7 323c22b ac621a7 7019506 ac621a7 c66c6a2 205d937 ac621a7 205d937 c66c6a2 205d937 ac621a7 205d937 ac621a7 9cc7eb2 ac621a7 66c6d87 | 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | import os
import io
from PIL import Image
import gradio as gr
import aws_utils
AWS_BUCKET = os.getenv("AWS_BUCKET")
os.environ["AWS_ACCESS_KEY_ID"] = os.getenv("AWS_ACCESS_KEY_ID")
os.environ["AWS_SECRET_ACCESS_KEY"] = os.getenv("AWS_SECRET_ACCESS_KEY")
os.environ["S3_BUCKET_NAME"] = os.getenv("AWS_BUCKET")
def load_text_data(characters: list, current_index: int):
curr_char = characters[current_index]
return (
characters,
current_index,
curr_char["name"],
curr_char["age"],
curr_char["gender"],
curr_char["description"],
)
def load_data(comic_id: str, character_data: list, current_index: int):
if current_index < len(character_data) - 1:
current_index += 1
else:
return [], *load_text_data(character_data, current_index)
images = []
name = character_data[current_index]["name"]
for idx in range(1, 5):
url = f"s3://{AWS_BUCKET}/{comic_id}/characters/compositions/{name}/{idx}.jpg"
data = aws_utils.fetch_from_s3(url)
images.append(Image.open(io.BytesIO(data)))
return images, *load_text_data(character_data, current_index)
def load_data_once(comic_id: str, current_index: int):
# Logic to load and return character images based on comic_id
# You can replace this with actual image paths or generation logic
print(f"Getting characters for comic id: {comic_id}")
characters = []
data = eval(
aws_utils.fetch_from_s3(
source=f"s3://{AWS_BUCKET}/{comic_id}/characters/characters.json"
).decode("utf-8")
)
for _, profile in data.items():
characters.append(profile)
images = []
# Loading the 0th frame of 0th scene in 0th episode.
name = characters[current_index]["name"]
for idx in range(1, 5):
url = f"s3://{AWS_BUCKET}/{comic_id}/characters/compositions/{name}/{idx}.jpg"
data = aws_utils.fetch_from_s3(url)
images.append(Image.open(io.BytesIO(data)))
return images, *load_text_data(characters, current_index)
def save_image(
selected_image,
comic_id: str,
character_data: list,
current_index: int,
):
# Implement your AWS S3 save logic here
print(f"Saving image: {selected_image}")
name = character_data[current_index]["name"]
with Image.open(selected_image[0]) as img:
# Convert and save as JPG
img_bytes = io.BytesIO()
img.convert("RGB").save(img_bytes, "JPEG")
img_bytes.seek(0)
aws_utils.save_to_s3(
AWS_BUCKET,
f"{comic_id}/characters/images",
img_bytes,
f"{name}.jpg",
)
print("Image saved successfully!")
gr.Info("Saved Image successfully!")
with gr.Blocks() as demo:
# selected_image = gr.State(None)
selected_image = gr.State()
current_index = gr.State(0)
character_data = gr.State([])
with gr.Row():
comic_id = gr.Textbox(label="Enter Comic ID:", placeholder="Enter Comic ID")
load_button = gr.Button("Load Data")
images = gr.Gallery(
label="Select an Image", elem_id="image_select", columns=4, height=300
)
# Display information about current Character
with gr.Row():
name = gr.Textbox(label="Name", interactive=False)
age = gr.Textbox(label="Age", interactive=False)
gender = gr.Textbox(label="Gender", interactive=False)
description = gr.Textbox(label="description", interactive=False)
# buttons to interact with the data
with gr.Row():
save_button = gr.Button("Save Image")
next_button = gr.Button("Next Image")
load_button.click(
load_data_once,
inputs=[comic_id, current_index],
outputs=[images, character_data, current_index, name, age, gender, description],
)
# When an image is clicked
def get_select_index(evt: gr.SelectData, images):
return images[evt.index]
images.select(get_select_index, images, selected_image)
save_button.click(
save_image,
inputs=[
selected_image,
comic_id,
character_data,
current_index,
],
outputs=[],
)
next_button.click(
load_data,
inputs=[comic_id, character_data, current_index],
outputs=[images, character_data, current_index, name, age, gender, description],
)
demo.launch(auth=("admin", "Qrt@12*34#immersfy"), share=True, ssr_mode=False)
|