user-agent commited on
Commit
210730f
·
verified ·
1 Parent(s): bede05f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -5
app.py CHANGED
@@ -10,24 +10,31 @@ from transformers import pipeline
10
  import ast
11
  pipe = pipeline("zero-shot-image-classification", model="patrickjohncyh/fashion-clip")
12
 
13
- file_path = 'color_config.json'
 
14
 
15
- # Open and read the JSON file
16
- with open(file_path, 'r') as file:
17
- data = json.load(file)
18
 
19
- COLOURS_DICT = data['color_mapping']
 
 
20
 
 
 
21
 
22
 
23
  def shot(input, category):
24
  subColour,mainColour,score = get_colour(ast.literal_eval(str(input)),category)
 
25
  return {
26
  "colors":{
27
  "main":mainColour,
28
  "sub":subColour,
29
  "score":round(score*100,2)
30
  }
 
31
  }
32
 
33
 
@@ -57,6 +64,43 @@ def get_colour(image_urls, category):
57
 
58
  return subColour, mainColour, responses[0][0]['score']
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
 
62
 
 
10
  import ast
11
  pipe = pipeline("zero-shot-image-classification", model="patrickjohncyh/fashion-clip")
12
 
13
+ color_file_path = 'color_config.json'
14
+ attributes_file_path = 'attributes_config.json'
15
 
16
+ # Open and read the COLOR JSON file
17
+ with open(color_file_path, 'r') as file:
18
+ color_data = json.load(file)
19
 
20
+ # Open and read the ATTRIBUTES JSON file
21
+ with open(attributes_file_path, 'r') as file:
22
+ attributes_data = json.load(file)
23
 
24
+ COLOURS_DICT = color_data['color_mapping']
25
+ ATTRIBUTES_DICT = attributes_data['attribute_mapping']
26
 
27
 
28
  def shot(input, category):
29
  subColour,mainColour,score = get_colour(ast.literal_eval(str(input)),category)
30
+ common_result = get_predicted_attributes(ast.literal_eval(str(input)),category)
31
  return {
32
  "colors":{
33
  "main":mainColour,
34
  "sub":subColour,
35
  "score":round(score*100,2)
36
  }
37
+ "attributes":common_result
38
  }
39
 
40
 
 
64
 
65
  return subColour, mainColour, responses[0][0]['score']
66
 
67
+ @spaces.GPU
68
+ def get_predicted_attributes(image_urls, category):
69
+ # Get the predicted attributes for the image
70
+ # attributes = get_category_attributes(category)
71
+
72
+ attributes = list(ATTRIBUTES_DICT.get(category,{}).keys())
73
+
74
+ # Mapping of possible values per attribute
75
+ common_result = []
76
+ for attribute in attributes:
77
+ # values = get_attribute_values(attribute, category)
78
+ values = list(ATTRIBUTES_DICT.get(category,{}).get(attribute,{}).keys())
79
+
80
+ if len(values) == 0:
81
+ continue
82
+
83
+ # Adjust labels for the pipeline to be in format: "{attr}: {value}, clothing: {category}"
84
+ attribute = attribute.replace("colartype", "collar").replace("sleevelength", "sleeve length").replace("fabricstyle", "fabric")
85
+ values = [f"{attribute}: {value}, clothing: {category}" for value in values]
86
+
87
+ # Get the predicted values for the attribute
88
+ responses = pipe(image_urls, candidate_labels=values, device=device)
89
+ result = [response[0]['label'].split(", clothing:")[0] for response in responses]
90
+
91
+ # If attribute is details, then get the top 2 most common labels
92
+ if attribute == "details":
93
+ result += [response[1]['label'].split(", clothing:")[0] for response in responses]
94
+ common_result.append(Counter(result).most_common(2))
95
+ else:
96
+ common_result.append(Counter(result).most_common(1))
97
+
98
+ # Clean up the results into one long string
99
+ for i, result in enumerate(common_result):
100
+ common_result[i] = ", ".join([f"{x[0]}" for x in result])
101
+
102
+ return common_result
103
+
104
 
105
 
106