chips commited on
Commit
2dc5702
·
1 Parent(s): 4645b0f

trying a new prompt with schema built in

Browse files
app.py CHANGED
@@ -97,6 +97,7 @@ async def forward_request(
97
  product_taxonomy,
98
  product_data_object, # type: ignore
99
  img_paths=img_paths,
 
100
  )
101
  except:
102
  raise gr.Error("Failed to extract attributes. Something went wrong.")
 
97
  product_taxonomy,
98
  product_data_object, # type: ignore
99
  img_paths=img_paths,
100
+ schema = schema
101
  )
102
  except:
103
  raise gr.Error("Failed to extract attributes. Something went wrong.")
app/core/prompts.py CHANGED
@@ -3,6 +3,8 @@ from typing import Optional
3
 
4
  from pydantic_settings import BaseSettings
5
 
 
 
6
  EXTRACT_INFO_SYSTEM = "You are an expert in structured data extraction. You will be given an image or a set of images of a product and should extract its properties into the given structure."
7
 
8
  EXTRACT_INFO_HUMAN = (
@@ -19,6 +21,181 @@ FOLLOW_SCHEMA_HUMAN = """Convert following attributes to structured schema. Keep
19
 
20
  {json_info}"""
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  class Prompts(BaseSettings):
24
  EXTRACT_INFO_SYSTEM_MESSAGE: str = EXTRACT_INFO_SYSTEM
 
3
 
4
  from pydantic_settings import BaseSettings
5
 
6
+
7
+ '''
8
  EXTRACT_INFO_SYSTEM = "You are an expert in structured data extraction. You will be given an image or a set of images of a product and should extract its properties into the given structure."
9
 
10
  EXTRACT_INFO_HUMAN = (
 
21
 
22
  {json_info}"""
23
 
24
+ '''
25
+
26
+ EXTRACT_INFO_SYSTEM = "You are an expert in structured data extraction. You will be given an image or a set of images of a product and should extract its properties into the given structure."
27
+
28
+ EXTRACT_INFO_STATS = (
29
+ """using the schema below while analysing the product in the images, for each attribute, look through the possible outputs, and assign a percentage to each of them, representing how certain you are of that output being the best fit. :
30
+
31
+ {product_data}
32
+ """
33
+ ).replace(" ", "")
34
+
35
+ EXTRACT_INFO_HUMAN = (
36
+ """I have a schema for extracting attributes from images of products. It's pasted below. For each of the allowed values, in each of the attributes, assign a percentage of certainty that the product fits with that value. I have attached images of the product.
37
+
38
+ schema =
39
+
40
+ "Length": {
41
+
42
+ "description": "Length of dress",
43
+
44
+ "data_type": "string",
45
+
46
+ "allowed_values": [
47
+
48
+ "Maxi", "Knee Length", "Mini", "Midi"
49
+
50
+ ]
51
+
52
+ },
53
+
54
+ "Style": {
55
+
56
+ "description": "Select the most appropriate dress style based on the garment's silhouette, fit, structural features, and overall design. Focus on how the dress is constructed and worn: whether it is fitted or loose, whether it has defining elements such as shirring, boning, buttons, collars, tiers, layering, or wrap ties. Ignore color, pattern, or fabric unless they directly influence the structure (e.g., stretch fabric for Bodycon). Use the visual cues of the neckline, sleeves, waistline, hemline, and closure type to guide your choice. Only select one style that best captures the dominant structural or design identity of the dress. Refer to the following definitions when uncertain: - 'A Line': Fitted at the top and gradually flares toward the hem, forming an 'A' shape.- 'Bodycon': Tight-fitting and figure-hugging, usually made with stretchy fabric.- 'Column': Straight silhouette from top to bottom, with minimal shaping or flare.- 'Shirt Dress': Structured like a shirt with buttons, collar, and sleeves; may include a belt.- 'Wrap Dress': Features a front closure that wraps and ties at the side or back.- 'Slip': Lightweight, spaghetti-strap dress with minimal structure, often bias-cut.- 'Kaftan': Very loose, flowing garment with wide sleeves and minimal shaping.- 'Smock': Loose-fitting with gathered or shirred sections (usually bodice or neckline).- 'Corset': Structured bodice with boning or lacing that shapes the waist.- 'Pinafore': Sleeveless over-dress, often worn layered over another top.- 'Jumper Dress': Layered dress style similar to a pinafore, often more casual or thick-strapped.- 'Blazer Dress': Tailored like a blazer or suit jacket, often double-breasted or lapelled.- 'Tunic': Loose and straight-cut, often worn short or over pants/leggings.- 'Gown': Full-length, formal dress with a structured or dramatic silhouette.- 'Asymmetric': Dress with a non-symmetrical hem, neckline, or sleeve design.- 'Shift': Simple, straight dress with no defined waist, typically above the knee.- 'Drop waist': Waistline sits low on the hips, usually with a loose top and flared skirt.- 'Empire': High waistline just below the bust, flowing skirt from there downward.- 'Modest': Covers most of the body, with high neckline, long sleeves, and longer hemline. Use structural cues over stylistic interpretation. Do not infer intent (e.g., party, formal) unless it’s directly tied to the construction.",
57
+
58
+ "data_type": "string",
59
+
60
+ "allowed_values": [
61
+
62
+ "A Line","Bodycon","Column","Shirt Dress","Wrap Dress","Slip","Kaftan","Smock","Corset","Pinafore","Jumper Dress","Blazer Dress","Tunic","Gown","Asymmetric","Shift","Drop waist","Empire","Modest"
63
+
64
+ ]
65
+
66
+ },
67
+
68
+ "Sleeve_length": {
69
+
70
+ "description": "Length of sleeves on dress",
71
+
72
+ "data_type": "string",
73
+
74
+ "allowed_values": [
75
+
76
+ "Sleeveless","Three quarters Sleeve","Long Sleeve","Short Sleeve","Strapless"
77
+
78
+ ]
79
+
80
+ },
81
+
82
+ "Neckline": {
83
+
84
+ "description": "Identify the neckline style based on the visible shape and structure of the neckline area. Focus on the cut and contour around the collarbone, shoulders, and upper chest. Only choose the neckline that best represents the dominant design — ignore collars, patterns, or styling details unless they significantly alter the neckline shape. Use the following definitions for clarity: - 'V Neck': Neckline dips down in the shape of a 'V', varying from shallow to deep. - 'Sweetheart': A heart-shaped neckline, often curving over the bust and dipping in the center. - 'Round Neck': Circular neckline sitting around the base of the neck, not as high as a crew neck. - 'Halter Neck': Straps go around the neck, leaving shoulders and upper back exposed. - 'Square Neck': Straight horizontal cut across the chest with vertical sides, forming a square. - 'High Neck': Extends up the neck slightly but not folded like a turtle neck. - 'Crew Neck': High, rounded neckline that sits close to the neck (commonly found in T-shirts). - 'Cowl Neck': Draped or folded neckline that hangs in soft folds. - 'Turtle Neck': High neckline that folds over and covers the neck completely. - 'Off the Shoulder': Sits below the shoulders, exposing the shoulders and collarbone. - 'One Shoulder': Covers one shoulder only, leaving the other bare. - 'Bandeau': Straight, strapless neckline that wraps across the bust. - 'Boat Neck': Wide, shallow neckline that runs almost horizontally from shoulder to shoulder. - 'Scoop Neck': U-shaped neckline, typically deeper than a round neck. - Always prioritize structure over styling — for example, a dress with embellishment or a mesh overlay still counts as 'V Neck' if the main shape is a V. If a neckline is borderline between two types, choose the simpler or more dominant structure.",
85
+
86
+ "data_type": "string",
87
+
88
+ "allowed_values": [
89
+
90
+ "V Neck","Sweetheart","Round Neck","Halter Neck","Square Neck","High Neck","Crew Neck","Cowl Neck","Turtle Neck","Off the shoulder","One Shoulder","Bandeau", "Boat Neck", "Scoop Neck"
91
+
92
+ ]
93
+
94
+ },
95
+
96
+ "pattern": {
97
+
98
+ "description": "Pattern of the garment",
99
+
100
+ "data_type": "string",
101
+
102
+ "allowed_values": [
103
+
104
+ "Floral","Stripe","Leopard Print","Spot","Plain","Geometric","Logo","Graphic print","Check","Tartan","Gingham", "Paisley", "other"
105
+
106
+ ]
107
+
108
+ },
109
+
110
+ "fabric": {
111
+
112
+ "description": "Material of the garment",
113
+
114
+ "data_type": "string",
115
+
116
+ "allowed_values": ["Cotton","Denim","Jersey","Linen","Satin","Silk","Sequin","Leather","Velvet","Corduroy","Ponte","Knit","Lace","Suede","Sheer","Chiffon", "Tulle", "Crepe", "Polyester","Viscose"]
117
+
118
+ },
119
+
120
+ "features": {
121
+
122
+ "description": "special features of the garment",
123
+
124
+ "data_type": "list[string]",
125
+
126
+ "allowed_values": [
127
+
128
+ "Pockets", "Lined", "Cut Out", "Backless", "None"
129
+
130
+ ]
131
+
132
+ },
133
+
134
+ "Closure": {
135
+
136
+ "description": "Closure of the garment. How it is closed",
137
+
138
+ "data_type": "list[string]",
139
+
140
+ "allowed_values": [
141
+
142
+ "Button","Zip","Press Stud","Clasp"
143
+
144
+ ]
145
+
146
+ },
147
+
148
+ "Body_Fit": {
149
+
150
+ "description": "How the dress fits the body",
151
+
152
+ "data_type": "string",
153
+
154
+ "allowed_values": [
155
+
156
+ "Petite","Maternity","Regular","Tall","Plus Size"
157
+
158
+ ]
159
+
160
+ },
161
+
162
+ "Occasion": {
163
+
164
+ "description": "What occasions do the dress match",
165
+
166
+ "data_type": "list[string]",
167
+
168
+ "allowed_values": [
169
+
170
+ "Beach","Casual","Cocktail","Day","Bridal","Bridesmaid","Evening","Mother of the Bride","Party","Prom","Wedding Guest","Work","Sportswear"
171
+
172
+ ]
173
+
174
+ },
175
+
176
+ "Season": {
177
+
178
+ "description": "What season do the dress match",
179
+
180
+ "data_type": "list[string]",
181
+
182
+ "allowed_values": [
183
+
184
+ "Spring","Summer","Autumn","Winter"
185
+
186
+ ]
187
+
188
+ },
189
+
190
+ """
191
+ ).replace(" ", "")
192
+
193
+ FOLLOW_SCHEMA_SYSTEM = "You are an expert in structured data extraction. You will be given an dictionary of attributes of a product and should output the its properties into the given structure."
194
+
195
+ FOLLOW_SCHEMA_HUMAN = """Convert following attributes to structured schema. Keep all the keys and number of values. Only replace the values themselves. :
196
+
197
+ {json_info}"""
198
+
199
 
200
  class Prompts(BaseSettings):
201
  EXTRACT_INFO_SYSTEM_MESSAGE: str = EXTRACT_INFO_SYSTEM
app/request_handler/extract_handler.py CHANGED
@@ -26,6 +26,8 @@ async def handle_extract(request: ExtractionRequest):
26
  ai_vendor = "openai"
27
  elif request.ai_model in settings.ANTHROPIC_MODELS:
28
  ai_vendor = "anthropic"
 
 
29
  else:
30
  raise ValueError(
31
  f"Invalid AI model: {request.ai_model}, only support {settings.SUPPORTED_MODELS}"
 
26
  ai_vendor = "openai"
27
  elif request.ai_model in settings.ANTHROPIC_MODELS:
28
  ai_vendor = "anthropic"
29
+ elif request.ai_model in settings.GEMINI_MODELS_MODELS:
30
+ ai_vendor = "gemini"
31
  else:
32
  raise ValueError(
33
  f"Invalid AI model: {request.ai_model}, only support {settings.SUPPORTED_MODELS}"
app/request_handler/follow_handler.py CHANGED
@@ -35,7 +35,7 @@ async def handle_follow(request: FollowSchemaRequest):
35
  break
36
  except ValueError as e:
37
  if attempt == request.max_attempts:
38
- raise HTTPException(
39
  status_code=400,
40
  detail=exception_to_str(e),
41
  headers={"attempt": attempt},
 
35
  break
36
  except ValueError as e:
37
  if attempt == request.max_attempts:
38
+ raise HTTPException(
39
  status_code=400,
40
  detail=exception_to_str(e),
41
  headers={"attempt": attempt},
app/services/base.py CHANGED
@@ -38,6 +38,7 @@ class BaseAttributionService(ABC):
38
  product_data: Dict[str, Union[str, List[str]]],
39
  pil_images: List[Any] = None,
40
  img_paths: List[str] = None,
 
41
  ) -> Dict[str, Any]:
42
  # validate_json_schema(schema)
43
 
 
38
  product_data: Dict[str, Union[str, List[str]]],
39
  pil_images: List[Any] = None,
40
  img_paths: List[str] = None,
41
+ schema: Dict[str, Any] = None,
42
  ) -> Dict[str, Any]:
43
  # validate_json_schema(schema)
44
 
app/services/service_openai.py CHANGED
@@ -70,10 +70,11 @@ class OpenAIService(BaseAttributionService):
70
  product_data: Dict[str, Union[str, List[str]]],
71
  pil_images: List[Any] = None, # do not remove, this is for weave
72
  img_paths: List[str] = None,
 
73
  ) -> Dict[str, Any]:
74
 
75
  print("Prompt: ")
76
- print(prompts.EXTRACT_INFO_HUMAN_MESSAGE.format(product_taxonomy=product_taxonomy, product_data=product_data_to_str(product_data)))
77
 
78
  text_content = [
79
  {
 
70
  product_data: Dict[str, Union[str, List[str]]],
71
  pil_images: List[Any] = None, # do not remove, this is for weave
72
  img_paths: List[str] = None,
73
+ data: Dict[str, Any] = None,
74
  ) -> Dict[str, Any]:
75
 
76
  print("Prompt: ")
77
+ print(prompts.EXTRACT_INFO_HUMAN_MESSAGE.format(product_taxonomy=product_taxonomy, product_data=product_data_to_str(product_data), json_info=data))
78
 
79
  text_content = [
80
  {