GenAssist-Demo / app.py
mina1004h's picture
Update app.py
d4ada0b verified
raw
history blame
4.26 kB
import os
import gradio as gr
import torch
import pandas as pd
from PIL import Image
def load_example(prompt, img0):
img0 = Image.open(img0)
width, height = img0.size
single_width = width // 2
single_height = height // 2
splitted_images = []
for i in range(4):
x = i % 2 * single_width
y = i // 2 * single_height
single_filename = f'img{i+1}.jpg'
splitted_images.append(single_filename)
img0.crop((x, y, x + single_width, y + single_height)).save(single_filename, quality=100)
if prompt == "A young chef is cooking the dinner for his parents":
table1 = pd.DataFrame({
'Comparison': ["The four images are all related to cooking/food preparation. They all have a natural lighting source, and the emotions depicted in all of them are happy. The differences among these images include medium (vector art, stock photo), focus on subject (a boy, a father, a mother), availability of ingredients, kitchen size, utensils being used, whether family members are wearing aprons, whether the family members are cooking individually or collectively, etc."],
'Image1': ["In this cartoon illustration, a man with a chef's hat is cooking in a kitchen. Two children are helping him. There are no parents present. The kitchen is small and has a stove, pots, pans, and utensils. The man is using a pan to cook. The children are not wearing aprons. The overall mood is happy. The kitchen has a bright natural lighting."],
'Image2': ["In the photo, a young boy is wearing a white chef's hat and preparing vegetables on the kitchen counter. He has a serious look on his face but seems happy. He is holding a spoon, fork, and knife. The stove is not turned on. The image shows a headshot of him in natural lighting."],
'Image3': ["In this vector art image with natural lighting, a man, a woman, and a child are happily preparing sausages together. The chef is male and holding cooking utensils. There are ingredients on the counter behind them. The family members are wearing aprons but there are no kitchen appliances being used. The lighting is bright and there are no decorations or artwork on the walls."],
'Image4': ["This is a vector art of a happy family of three preparing food in the kitchen under natural lighting. The father, a male chef, is holding cooking utensils while cooking meat and vegetables on the stove. Ingredients are visible on the counter and in the background. The spacious kitchen has pots, pans, bowls, spoons, spatulas being used. The mother and children are also cooking together without aprons or chef hats. All family members are focused and joyful while cooking together."]
})
return splitted_images + [table1]
# Add more conditions as needed
return splitted_images + [None]
with gr.Blocks() as demo:
with gr.Row():
prompt = gr.Textbox(label="Text Prompt")
with gr.Row():
img0 = gr.Image(label="Input Image", type="filepath")
with gr.Row():
img1 = gr.Image(label="Image 1", type="filepath")
img2 = gr.Image(label="Image 2", type="filepath")
img3 = gr.Image(label="Image 3", type="filepath")
img4 = gr.Image(label="Image 4", type="filepath")
gr.Markdown("""<h1 style="text-align: center;">Summary Caption</h1>""")
table1 = gr.Dataframe(
headers=["Comparison", "Image 1", "Image 2", "Image 3", "Image 4"],
datatype=["str", "str", "str", "str", "str"],
interactive=True
)
gr.Examples(
examples=[
["A young chef is cooking the dinner for his parents", "demo1.jpg"],
["Young man and woman walking on a sunny beach, the sun is visible in the frame, on a white background", "demo2.jpg"],
["Black female, hair stylist, designing her room", "demo3.jpg"],
["Use plastic that is durable rather than the one that breaks soon.", "demo4.jpg"],
["An interpreter in a booth with headphones and microphones, reading notes, listening and speaking", "demo5.jpg"]
],
inputs=[prompt, img0],
outputs=[img1, img2, img3, img4, table1],
fn=load_example
)
if __name__ == "__main__":
demo.launch()