| import json |
| import spaces |
| import requests |
| import numpy as np |
| import gradio as gr |
| from PIL import Image |
| from io import BytesIO |
| from turtle import title |
| from transformers import pipeline |
| import ast |
| pipe = pipeline("zero-shot-image-classification", model="patrickjohncyh/fashion-clip") |
|
|
| file_path = 'config.json' |
|
|
| |
| with open(file_path, 'r') as file: |
| data = json.load(file) |
|
|
| COLOURS_DICT = data['color_mapping'] |
|
|
|
|
|
|
| def shot(input, category): |
| subColour,mainColour,score = get_colour(ast.literal_eval(str(input)),category) |
| return { |
| "colors":{ |
| "main":mainColour, |
| "sub":subColour, |
| "score":round(score*100,2) |
| } |
| } |
|
|
|
|
|
|
| @spaces.GPU |
| def get_colour(image_urls, category): |
| colourLabels = list(COLOURS_DICT.keys()) |
| for i in range(len(colourLabels)): |
| colourLabels[i] = colourLabels[i] + " clothing: " + category |
|
|
| responses = pipe(image_urls, candidate_labels=colourLabels) |
| |
| mainColour = responses[0][0]['label'].split(" clothing:")[0] |
|
|
|
|
| if mainColour not in COLOURS_DICT: |
| return None, None, None |
|
|
| |
| labels = COLOURS_DICT[mainColour] |
| for i in range(len(labels)): |
| labels[i] = labels[i] + " clothing: " + category |
|
|
| |
| responses = pipe(image_urls, candidate_labels=labels) |
| subColour = responses[0][0]['label'].split(" clothing:")[0] |
|
|
| return subColour, mainColour, responses[0][0]['score'] |
|
|
|
|
|
|
|
|
| |
| iface = gr.Interface( |
| fn=shot, |
| inputs=[ |
| gr.Textbox(label="Image URLs (starting with http/https) comma seperated "), |
| gr.Textbox(label="Category") |
| ], |
| outputs=gr.Label(), |
| description="Add an image URL (starting with http/https) or upload a picture, and provide a list of labels separated by commas.", |
| title="Full product flow" |
| ) |
|
|
| |
| iface.launch() |
|
|