| |
| 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 |
|
|
| |
| 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." |
|
|
| |
| 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') |
|
|
| |
| 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 |
|
|
| |
| def most_frequent_values(df): |
| |
| most_frequent = {} |
|
|
| |
| for column in df.columns: |
| |
| scores = {} |
|
|
| |
| for row in df[column]: |
| |
| for entry in row: |
| answer = entry['answer'] |
| score = entry['score'] |
| |
| if answer in scores: |
| scores[answer] += score |
| else: |
| scores[answer] = score |
|
|
| |
| most_common = max(scores, key=scores.get) |
| |
| most_frequent[column] = most_common |
|
|
| |
| most_frequent_df = pd.DataFrame(most_frequent, index=[0]) |
|
|
| return most_frequent_df |
|
|
|
|
| |
| def list_to_dataframe(attribute_list): |
| |
| row_indices = ['color', 'style', 'vibe', 'product', |
| 'personalizable', 'material', 'event', 'font'] |
| |
| rows = [attribute_list[i:i + len(row_indices)] for i in range(0, len(attribute_list), 8)] |
| |
| 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}" |
|
|
| |
| def infer(prompt): |
|
|
| seed = random.randint(0, MAX_SEED) |
|
|
| generator = torch.Generator().manual_seed(seed) |
| |
| 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 |
|
|
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| 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 |
| |
| if torch.cuda.is_available(): |
| power_device = "GPU" |
| else: |
| power_device = "CPU" |
|
|
| |
| |
|
|
| 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') |
| |
| |
|
|
| |
| 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() |
|
|