Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -108,56 +108,93 @@ def shot(input, category, level):
|
|
| 108 |
|
| 109 |
# return subColour, mainColour, responses[0][0]['score']
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
@spaces.GPU
|
| 112 |
def get_colour(image_urls, category):
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
# Split labels into two batches
|
| 119 |
-
mid_index = len(colourLabels) // 2
|
| 120 |
-
first_batch = colourLabels[:mid_index]
|
| 121 |
-
second_batch = colourLabels[mid_index:]
|
| 122 |
-
|
| 123 |
-
# Process the first batch
|
| 124 |
-
responses_first_batch = pipe(image_urls, candidate_labels=first_batch)
|
| 125 |
-
# Get the top 3 from the first batch
|
| 126 |
-
top3_first_batch = sorted(responses_first_batch[0], key=lambda x: x['score'], reverse=True)[:3]
|
| 127 |
-
|
| 128 |
-
# Process the second batch
|
| 129 |
-
responses_second_batch = pipe(image_urls, candidate_labels=second_batch)
|
| 130 |
-
# Get the top 3 from the second batch
|
| 131 |
-
top3_second_batch = sorted(responses_second_batch[0], key=lambda x: x['score'], reverse=True)[:3]
|
| 132 |
-
|
| 133 |
-
# Combine the top 3 from each batch
|
| 134 |
-
combined_top6 = top3_first_batch + top3_second_batch
|
| 135 |
-
# Get the final top 3 from the combined list
|
| 136 |
-
final_top3 = sorted(combined_top6, key=lambda x: x['score'], reverse=True)[:3]
|
| 137 |
-
|
| 138 |
-
mainColour = final_top3[0]['label'].split(" clothing:")[0]
|
| 139 |
-
|
| 140 |
-
if mainColour not in COLOURS_DICT:
|
| 141 |
return None, None, None
|
| 142 |
|
| 143 |
-
|
| 144 |
-
labels = [f"{label} clothing: {category}" for label in COLOURS_DICT[mainColour]]
|
| 145 |
-
print("Labels for pipe:", labels) # Debug: Confirm labels are correct
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
|
|
|
| 149 |
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
-
# Function for get_predicted_attributes
|
| 155 |
def get_most_common_label(responses):
|
| 156 |
feature_scores = defaultdict(float)
|
| 157 |
for response in responses:
|
| 158 |
label, score = response[0]['label'].split(", clothing:")[0], response[0]['score']
|
| 159 |
feature_scores[label] += score
|
| 160 |
-
|
|
|
|
|
|
|
| 161 |
|
| 162 |
@spaces.GPU
|
| 163 |
def get_predicted_attributes(image_urls, category):
|
|
|
|
| 108 |
|
| 109 |
# return subColour, mainColour, responses[0][0]['score']
|
| 110 |
|
| 111 |
+
# @spaces.GPU
|
| 112 |
+
# def get_colour(image_urls, category):
|
| 113 |
+
# # Prepare color labels
|
| 114 |
+
# colourLabels = [f"{color} clothing: {category}" for color in COLOURS_DICT.keys()]
|
| 115 |
+
# print("Colour Labels:", colourLabels) # Debug: Print colour labels
|
| 116 |
+
# print("Image URLs:", image_urls) # Debug: Print image URLs
|
| 117 |
+
|
| 118 |
+
# # Split labels into two batches
|
| 119 |
+
# mid_index = len(colourLabels) // 2
|
| 120 |
+
# first_batch = colourLabels[:mid_index]
|
| 121 |
+
# second_batch = colourLabels[mid_index:]
|
| 122 |
+
|
| 123 |
+
# # Process the first batch
|
| 124 |
+
# responses_first_batch = pipe(image_urls, candidate_labels=first_batch)
|
| 125 |
+
# # Get the top 3 from the first batch
|
| 126 |
+
# top3_first_batch = sorted(responses_first_batch[0], key=lambda x: x['score'], reverse=True)[:3]
|
| 127 |
+
|
| 128 |
+
# # Process the second batch
|
| 129 |
+
# responses_second_batch = pipe(image_urls, candidate_labels=second_batch)
|
| 130 |
+
# # Get the top 3 from the second batch
|
| 131 |
+
# top3_second_batch = sorted(responses_second_batch[0], key=lambda x: x['score'], reverse=True)[:3]
|
| 132 |
+
|
| 133 |
+
# # Combine the top 3 from each batch
|
| 134 |
+
# combined_top6 = top3_first_batch + top3_second_batch
|
| 135 |
+
# # Get the final top 3 from the combined list
|
| 136 |
+
# final_top3 = sorted(combined_top6, key=lambda x: x['score'], reverse=True)[:3]
|
| 137 |
+
|
| 138 |
+
# mainColour = final_top3[0]['label'].split(" clothing:")[0]
|
| 139 |
+
|
| 140 |
+
# if mainColour not in COLOURS_DICT:
|
| 141 |
+
# return None, None, None
|
| 142 |
+
|
| 143 |
+
# # Get sub-colors for the main color
|
| 144 |
+
# labels = [f"{label} clothing: {category}" for label in COLOURS_DICT[mainColour]]
|
| 145 |
+
# print("Labels for pipe:", labels) # Debug: Confirm labels are correct
|
| 146 |
+
|
| 147 |
+
# responses = pipe(image_urls, candidate_labels=labels)
|
| 148 |
+
# subColour = responses[0][0]['label'].split(" clothing:")[0]
|
| 149 |
+
|
| 150 |
+
# return subColour, mainColour, responses[0][0]['score']
|
| 151 |
+
|
| 152 |
+
|
| 153 |
@spaces.GPU
|
| 154 |
def get_colour(image_urls, category):
|
| 155 |
+
colour_labels = [f"{colour}, clothing: {category}" for colour in COLOURS_DICT.keys()]
|
| 156 |
+
responses = pipe(image_urls, candidate_labels=colour_labels, device=device)
|
| 157 |
+
|
| 158 |
+
main_colour, main_score = get_most_common_label(responses)
|
| 159 |
+
if main_colour not in COLOURS_DICT:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
return None, None, None
|
| 161 |
|
| 162 |
+
score = [main_score]
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
labels = COLOURS_DICT[main_colour]
|
| 165 |
+
if main_colour == "multicolor":
|
| 166 |
+
labels = [label for key, values in COLOURS_DICT.items() if key != main_colour for label in values]
|
| 167 |
|
| 168 |
+
labels = [f"{label}, clothing: {category}" for label in labels]
|
| 169 |
+
responses = pipe(image_urls, candidate_labels=labels, device=device)
|
| 170 |
+
|
| 171 |
+
most_common, sub_score = get_most_common_label(responses)
|
| 172 |
+
sub_colours = [most_common]
|
| 173 |
+
score.append(sub_score)
|
| 174 |
+
|
| 175 |
+
if main_colour == "multicolor":
|
| 176 |
+
sub_key = next(key for key, values in COLOURS_DICT.items() if most_common in values)
|
| 177 |
+
labels = [label for key, values in COLOURS_DICT.items() if key not in {main_colour, sub_key} for label in values]
|
| 178 |
+
labels = [f"{label}, clothing: {category}" for label in labels]
|
| 179 |
+
responses = pipe(image_urls, candidate_labels=labels, device=device)
|
| 180 |
+
|
| 181 |
+
most_common, tertiary_score = get_most_common_label(responses)
|
| 182 |
+
sub_colours.append(most_common)
|
| 183 |
+
score.append(tertiary_score)
|
| 184 |
+
|
| 185 |
+
return sub_colours, main_colour, score
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
+
# Function for get_predicted_attributes and get_colour
|
| 190 |
def get_most_common_label(responses):
|
| 191 |
feature_scores = defaultdict(float)
|
| 192 |
for response in responses:
|
| 193 |
label, score = response[0]['label'].split(", clothing:")[0], response[0]['score']
|
| 194 |
feature_scores[label] += score
|
| 195 |
+
|
| 196 |
+
max_label = max(feature_scores, key=feature_scores.get)
|
| 197 |
+
return max_label, feature_scores[max_label] / len(responses)
|
| 198 |
|
| 199 |
@spaces.GPU
|
| 200 |
def get_predicted_attributes(image_urls, category):
|