File size: 6,394 Bytes
bf81783 b30c4ec 06abebe b30c4ec 6b9578c b30c4ec 06abebe b30c4ec bf81783 7fdf6bb 06abebe de2f27e 1c274c4 de2f27e fb3055d de2f27e 1c274c4 06abebe 1c274c4 b30c4ec 88749a3 1c274c4 b30c4ec bcf9eab 4c29c26 ac97792 4c29c26 bcf9eab b30c4ec bcf9eab 4c29c26 bcf9eab 4c29c26 be6f932 4c29c26 bcf9eab de2f27e 06abebe de2f27e 06abebe de2f27e 06abebe de2f27e 06abebe de2f27e 06abebe 8928700 de2f27e | 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | #imports
import gradio as gr
import os
from PIL import Image
import io
from transformers import ViltProcessor, ViltForQuestionAnswering
import torch
import pandas as pd
import numpy as np
import random
from diffusers import DiffusionPipeline
from transformers import pipeline
#function to turn dataframe into prompt
def format_prompt_from_dataframe(df):
"""
Create a prompt-style phrase from the DataFrame output.
Parameters:
df (pd.DataFrame): A DataFrame where each column represents the answers to the questions.
Returns:
str: A prompt-style phrase describing the product and image.
"""
if df.empty or df.shape[0] == 0:
return "No data available to create a prompt."
# Assuming the DataFrame has one row and columns match the question list
row = df.iloc[0]
color = row.get('color', 'Unknown')
style = row.get('style', 'Unknown')
vibe = row.get('vibe', 'Unknown')
product = row.get('product', 'Unknown')
personalizable = row.get('personalizable', 'Unknown')
material = row.get('material', 'Unknown')
event = row.get('event', 'Unknown')
font = row.get('font', 'Unknown')
# Construct the prompt-style phrase
phrase = (
f"A {product} made from {material} for {event}. "
f"The color is {color} and the style is {style} with a font {font}. "
f"The vibe is {vibe} and it is {'personalizable' if personalizable.lower() == 'yes' else 'not personalizable'}."
)
return phrase
#function to get most frequent values from dataframe
def most_frequent_values(df):
# Create an empty dictionary to store the most frequent values
most_frequent = {}
# Iterate over each column in the DataFrame
for column in df.columns:
# Create a dictionary to store the scores for each unique answer
scores = {}
# Iterate over each row in the column
for row in df[column]:
# Iterate over each entry in the list of dictionaries
for entry in row:
answer = entry['answer']
score = entry['score']
# Accumulate the score for each answer
if answer in scores:
scores[answer] += score
else:
scores[answer] = score
# Find the answer with the highest total score
most_common = max(scores, key=scores.get)
# Add it to the dictionary
most_frequent[column] = most_common
# Convert the dictionary to a DataFrame
most_frequent_df = pd.DataFrame(most_frequent, index=[0])
return most_frequent_df
#funtion to turn list outputed into a dataframe
def list_to_dataframe(attribute_list):
# Row indices
row_indices = ['color', 'style', 'vibe', 'product',
'personalizable', 'material', 'event', 'font']
# Split the list into rows of 8 elements each
rows = [attribute_list[i:i + len(row_indices)] for i in range(0, len(attribute_list), 8)]
# Create the DataFrame
df = pd.DataFrame(rows, columns=row_indices)
return df
vqa_pipeline = pipeline("visual-question-answering",model='dandelin/vilt-b32-finetuned-vqa')
def answer_question(gallery):
try:
answer_list = []
question_list = ['What color is in the image?',
'What style is the image?',
'What vibe is the image?',
'What kind of product is the image?',
'Is the product in the image personalizable?',
'What material is the product in the image made of?',
'What event is the product in the image made for?',
'What font is in the image?']
for i,c in gallery:
for q in question_list:
predicted_answer = vqa_pipeline(i, q, top_k=3)
answer_list.append(predicted_answer)
df = list_to_dataframe(answer_list)
frequent= most_frequent_values(df)
prompt = format_prompt_from_dataframe(frequent)
return prompt
except Exception as e:
return f"An error occurred: {e}"
#function for returning image with prompt
def infer(prompt):
seed = random.randint(0, MAX_SEED)
generator = torch.Generator().manual_seed(seed)
#prompt = 'ceramic mug with heart'
image = pipe(
prompt = 'create a mockup photo for an etsy listing. the product is '+prompt,
guidance_scale = 0,
num_inference_steps = 2,
width = 512,
height = 512,
generator = generator
).images[0]
return image
#def infer(prompt):
# image = pipeline(
# prompt = 'create a mockup photo for an etsy listing. the product is '+prompt
# ).images[0]
#
# return image
device = "cuda" if torch.cuda.is_available() else "cpu"
if torch.cuda.is_available():
torch.cuda.max_memory_allocated(device=device)
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
pipe.enable_xformers_memory_efficient_attention()
pipe = pipe.to(device)
else:
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
pipe = pipe.to(device)
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 1024
#now try it with gradio interface and upload gallery
if torch.cuda.is_available():
power_device = "GPU"
else:
power_device = "CPU"
#pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
#pipeline.to("cuda")
with gr.Blocks() as demo:
title = gr.Markdown("""
<div align="center">
<strong style = 'font-size: 24px;''>Finding Common Trends</strong>
</div>""")
instructions = gr.Markdown("""
<div align="center">
<strong style = 'font-size: 16px;''>Upload a Few Product Images to Find Commonalites Between Them</strong>
</div>""")
gallery = gr.Gallery(label="Gallery",type='pil')
#question = gr.Textbox(label="question")
#name buttons to display
image_button = gr.Button("predict trends")
answer = gr.Textbox(label='Predicted answer')
text_button = gr.Button("show image")
image_button.click(fn=answer_question, inputs=[gallery], outputs=[answer])
result = gr.Image(label="Result", show_label=False)
text_button.click(fn=infer,inputs =[answer],outputs=[result])
demo.launch()
|