Update app.py
Browse files
app.py
CHANGED
|
@@ -104,30 +104,27 @@ def get_colour(image_urls, category):
|
|
| 104 |
|
| 105 |
@spaces.GPU
|
| 106 |
def get_predicted_attributes(image_urls, category):
|
| 107 |
-
#
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
attributes = list(ATTRIBUTES_DICT.get(category,{}).keys())
|
| 111 |
-
|
| 112 |
# Mapping of possible values per attribute
|
| 113 |
common_result = []
|
| 114 |
for attribute in attributes:
|
| 115 |
-
|
| 116 |
-
values = ATTRIBUTES_DICT.get(category,{}).get(attribute,[])
|
| 117 |
|
| 118 |
if len(values) == 0:
|
| 119 |
continue
|
| 120 |
|
| 121 |
# Adjust labels for the pipeline to be in format: "{attr}: {value}, clothing: {category}"
|
| 122 |
-
|
| 123 |
-
|
| 124 |
|
| 125 |
# Get the predicted values for the attribute
|
| 126 |
-
responses = pipe(image_urls, candidate_labels=
|
| 127 |
result = [response[0]['label'].split(", clothing:")[0] for response in responses]
|
| 128 |
|
| 129 |
# If attribute is details, then get the top 2 most common labels
|
| 130 |
-
if
|
| 131 |
result += [response[1]['label'].split(", clothing:")[0] for response in responses]
|
| 132 |
common_result.append(Counter(result).most_common(2))
|
| 133 |
else:
|
|
@@ -143,12 +140,19 @@ def get_predicted_attributes(image_urls, category):
|
|
| 143 |
for item in common_result:
|
| 144 |
# Split by ': ' to separate the key and value
|
| 145 |
key, value = item.split(': ', 1)
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
return result
|
| 150 |
|
| 151 |
-
|
| 152 |
def get_openAI_tags(image_urls):
|
| 153 |
# Create list containing JSONs of each image URL
|
| 154 |
imageList = []
|
|
|
|
| 104 |
|
| 105 |
@spaces.GPU
|
| 106 |
def get_predicted_attributes(image_urls, category):
|
| 107 |
+
# Assuming ATTRIBUTES_DICT and pipe are defined outside this function
|
| 108 |
+
attributes = list(ATTRIBUTES_DICT.get(category, {}).keys())
|
| 109 |
+
|
|
|
|
|
|
|
| 110 |
# Mapping of possible values per attribute
|
| 111 |
common_result = []
|
| 112 |
for attribute in attributes:
|
| 113 |
+
values = ATTRIBUTES_DICT.get(category, {}).get(attribute, [])
|
|
|
|
| 114 |
|
| 115 |
if len(values) == 0:
|
| 116 |
continue
|
| 117 |
|
| 118 |
# Adjust labels for the pipeline to be in format: "{attr}: {value}, clothing: {category}"
|
| 119 |
+
attribute_formatted = attribute.replace("colartype", "collar").replace("sleevelength", "sleeve length").replace("fabricstyle", "fabric")
|
| 120 |
+
values_formatted = [f"{attribute_formatted}: {value}, clothing: {category}" for value in values]
|
| 121 |
|
| 122 |
# Get the predicted values for the attribute
|
| 123 |
+
responses = pipe(image_urls, candidate_labels=values_formatted)
|
| 124 |
result = [response[0]['label'].split(", clothing:")[0] for response in responses]
|
| 125 |
|
| 126 |
# If attribute is details, then get the top 2 most common labels
|
| 127 |
+
if attribute_formatted == "details":
|
| 128 |
result += [response[1]['label'].split(", clothing:")[0] for response in responses]
|
| 129 |
common_result.append(Counter(result).most_common(2))
|
| 130 |
else:
|
|
|
|
| 140 |
for item in common_result:
|
| 141 |
# Split by ': ' to separate the key and value
|
| 142 |
key, value = item.split(': ', 1)
|
| 143 |
+
|
| 144 |
+
if key == "details":
|
| 145 |
+
details_split = value.split(" , ")
|
| 146 |
+
if len(details_split) == 2:
|
| 147 |
+
result["details_1"] = details_split[0]
|
| 148 |
+
result["details_2"] = details_split[1]
|
| 149 |
+
else:
|
| 150 |
+
result["details_1"] = value # If there's only one detail, assign it to details 1
|
| 151 |
+
else:
|
| 152 |
+
result[key] = value
|
| 153 |
|
| 154 |
return result
|
| 155 |
|
|
|
|
| 156 |
def get_openAI_tags(image_urls):
|
| 157 |
# Create list containing JSONs of each image URL
|
| 158 |
imageList = []
|