Spaces:
Running
Running
import os | |
import gradio as gr | |
import os | |
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] | |
if prompt == "Young man and woman walking on a sunny beach, the sun is visible in the frame, on a white background": | |
table1 = pd.DataFrame({'Comparison': ["All four images depict a young couple walking on a sunny beach. They differ in the type of medium, lighting, shot types, and colors used. The first three images mainly use blue and white, while the fourth image has a black and white theme. The couple is holding hands in image 2, while in image 1 and 3, they are not. The images have different intended uses, ranging from a website or album cover to a poster for promoting beach trips."], 'Image1': ["This is a digital painting of a man and woman walking on a sunny beach. The vector art shows a white sand beach with blue water, and natural lighting with shadows. The man is shirtless while the woman wears a hat. They walk on dry sand, towards the camera. The background is empty, and there are no waves or ocean visible."], 'Image2': ["This is a cartoon poster of a couple holding hands while walking on a beach at sunset. The sunlight and natural lighting create an orange spotlight. The landscape shot showcases the blue sky and choppy water. They both wear hats, and the background is not white. This cheerful image can be used in a poster to promote a trip to the beach."], 'Image3': ["This photo shows a young couple walking on a sunny beach. The shot is centered and exhibits natural sunlight, shadows, and lighting. The surroundings are clear but not as to whether the background is white or not. The couple are likely in their 20s to 30s, wearing hats, and walking far out into the water, seemingly happy. No other people, umbrellas, or belongings are seen on the empty beach."], 'Image4': ["This is an album cover showing a young couple walking on a sunny beach. They walk hand in hand on the dry sand, away from small waves in the background. The image is black and white and shows natural, backlight, and soft lighting. The couple doesn't wear any accessories, and no one else seems to be around."]}) | |
return [*splitted_images, table1] | |
if prompt == "Black female, hair stylist, designing her room": | |
table1 = pd.DataFrame({'Comparison': ["All images feature a black woman with long hair in a positive and happy mood. Image 1 shows a hair stylist in an empty hair salon, while Image 2 features a woman getting her hair styled in a mirror with black and white lighting. Image 3 shows a woman in a beauty salon wearing an orange dress, and Image 4 is a digital illustration of a woman in her room with a plant, wearing a hoodie, and looking into a mirror hanging on a wall."], 'Image1': ["In this headshot photo taken with natural lighting, we see an African American woman standing in a hair salon. She wears a black shirt and has dreadlocks. There are hair products nearby but the salon is empty. The woman faces the camera, and the atmosphere seems calm and relaxing with blue and white colors dominating the background. This image could likely be found in a hair salon."], 'Image2': ["In this airbrush painting, a happy black woman with long, dark brown hair is sitting in front of a wall-mounted mirror. She is styling her hair with a curling iron while combing or brushing it. There is no hair wrap or towel on her head. Only soft and dim natural lighting is present in the room. The woman is alone and facing the mirror. The image is centered on her headshot, and the main colors used are black and white. This image would likely be used in a magazine."], 'Image3': ["This photo features a happy woman sitting on a chair with a backrest in front of a mirror in a well-lit room. She is wearing an orange dress and has long, dark brown hair. There are no decorations on the mirror, and the woman is not wearing any jewelry. The main colors used in the image are orange, black, and white. It seems like the image could be used in a beauty salon since the woman is a hairstylist."], 'Image4': ["This is a digital painting of a happy black woman sitting in her room with natural lighting. She is wearing a hoodie and looking into a mirror hanging on the wall. Her black hair color can be seen. The mirror has nothing on it. In the background, there is a window. The main colors used in the image are pink, blue, green and orange. The woman has a serious look on her face, and it is early morning in the room. There is a plant in the room with her. This image may likely be used on a website."]}) | |
return [*splitted_images, table1] | |
if prompt == "Use plastic that is durable rather than the one that breaks soon.": | |
table1 = pd.DataFrame({'Comparison': ["All images contain plastic objects and are brightly colored. Differences: Image 1 contains various containers and utensils on a blue background in a close-up shot, while Image 2 portrays a pile of cups and straws in a dark room. Image 3 is a chart of plastic utensils with blue and green colors on a wooden table, and Image 4 features four plastic bowls and utensils on a kitchen counter in non-logical placement. Emotions in the images range from negative to happy."], 'Image1': ["In this still life stock photo, 4 clear and blue plastic containers and utensils are shown on a blue background. The containers are small and transparent, without any lids. The utensils are large and made of the same smooth material as the containers. There is no writing or branding visible on the containers or utensils. The image is negative in emotion and likely used in restaurants or cafeterias."], 'Image2': ["This still life image showcases a pile of plastic cups and straws. The brightly colored blue and orange cups stand out against a black background. The cups and straws are in focus, arranged without any pattern. The setting is a dark room. Though the cups and straws are clean, the image is negative in emotion as it highlights the abundance of single-use plastic. The small-sized cups and straws are recyclable. This image would likely be used on a website for a plastics company."], 'Image3': ["A haphazard arrangement of blue and green plastic utensils and plates lie on a smooth wooden table. The plates appear empty and there are no other objects on the table. The plastic utensils are small and disposable. The setting is likely indoors, possibly at a restaurant. No patterns or decorations are visible."], 'Image4': ["A bright still life image of four plastic bowls and two forks on a kitchen counter. The bowls are green, blue, yellow, and orange, and all the same size. The forks are on the table beside the bowls, a few centimeters apart. There are no other utensils, decorations, or food on the table. This happy image could be used in a restaurant or cafeteria."]}) | |
return [*splitted_images, table1] | |
if prompt == "An interpreter in a boot with headphones and microphones, reading notes, listening and speaking": | |
table1 = pd.DataFrame({'Comparison': ["All the four images share a common theme of a man involved in some form of reading exercise while wearing headphones and a microphone, recorded in an indoor setting. The subjects of the images predominantly include a man, headphones, and a reading material. The artistic styles differ, as the images encompass illustrations, vector art, and a photo. The man's attire varies across the images, sometimes dressed casually in a hoodie and jeans, and other times more formally in a blue shirt and pants. The reading materials also vary, including a book and a newspaper, and the surrounding environments change, such as a simple chair setting, a room, a plane, and an unidentified indoor setting."], 'Image1': ["This is a pen and ink cartoon-style illustration for a storybook. It depicts a young man wearing a hoodie, jeans, and headphones, sitting indoors on a chair with a backrest. He is reading a book closely in front of a microphone, but there's no visible title on the book. The colors in the image are green, blue, black and white. His expression is neutral and he is not performing multiple tasks. The image depicts a positive mood."], 'Image2': ["The image is a centered, landscape digital sketch art. It depicts an indoor scene of a man sitting alone on a folding chair, engrossed in reading a newspaper. He is dressed in a hoodie and jeans, and wears a pair of headphones. There are no notable expressions on his face. The setting is clean and minimal with no other objects or furniture around. No one else is in the picture. The dominant colors are white, black, and gray."], 'Image3': ["This is a centered-shot photo of a young man in a blue shirt and pants sitting alone in an airplane cockpit, presumably mid-flight. The man, wearing headphones and a microphone, is seen writing on lined paper with a ballpoint pen, indicating communication."], 'Image4': ["This is a vector art cartoon character portrait. The image presents an indoor setting where a man is sitting in a chair. He is spotted wearing a hoodie and a set of headphones, presumably for communication, given the presence of a microphone near him. The man is deeply engrossed in reading a newspaper. The color palette is primarily orange, black and white, hinting at a serene and positive atmosphere. As per the context, the man appears to be alone in the room and comfortable in his solitude."]}) | |
return [*splitted_images, table1] | |
with gr.Blocks() as demo: | |
with gr.Row(): | |
prompt = gr.Textbox(label="Text Prompt") | |
with gr.Row(): | |
img0 = gr.Image(label="Generated Candidates", type="filepath") | |
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><center>Summary Caption</center></h1>""") | |
table1 = gr.DataFrame( | |
headers = ["Comparison", "Image 1", "Image 2", "Image 3", "Image 4"], | |
datatype = ["str", "str", "str", "str", "str"], | |
visible = True, | |
row_count = (1, 'dynamic'), | |
wrap=True, | |
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 boot with headphones and microphones, reading notes, listening and speaking", "demo5.jpg"]], | |
inputs=[prompt, img0], | |
outputs=[img1, img2, img3, img4, table1], | |
fn=load_example, | |
cache_examples=True, | |
) | |
if __name__ == "__main__": | |
demo.launch() |