utkarsh134 commited on
Commit
775ba9c
·
verified ·
1 Parent(s): fb1b75a

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +65 -57
main.py CHANGED
@@ -7,6 +7,7 @@ from parsing import process_extracted_text
7
  import csv
8
  import json
9
  import math
 
10
 
11
  app = FastAPI()
12
 
@@ -23,6 +24,7 @@ async def extract_diamonds(file: UploadFile = File(...)):
23
  @app.post("/process-image/")
24
  async def process_image(
25
  file: UploadFile = File(...),
 
26
  finding_14kt: float = Form(...),
27
  finding_18kt: float = Form(...),
28
  finding_plt: float = Form(...),
@@ -37,6 +39,8 @@ async def process_image(
37
  finding_markup_value: float = Form(...)
38
  ):
39
  try:
 
 
40
  diamond_rates_dict = json.loads(diamond_rates)
41
  file_content = await file.read()
42
  extracted_text = extract_data_from_image(BytesIO(file_content))
@@ -69,54 +73,83 @@ async def process_image(
69
  finding_18kt = round(finding_18kt * finding_markup_value, 2)
70
  finding_plt = round(finding_plt * finding_markup_value, 2)
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  metal_data.update({
73
- "Finding For Gold 14KT": finding_14kt,
74
- "Finding For Gold 18KT": finding_18kt,
75
- "Finding for Plt.": finding_plt,
 
 
 
76
  "Finding Markup Type": finding_markup_type,
77
  "Finding Markup Value": finding_markup_value,
 
78
  })
79
 
 
 
 
 
 
 
 
 
80
  # Apply markup to gold values - only percentage or fixed
81
  if gold_markup_type == "percentage":
82
  gold_markup_factor = 1 + (gold_markup_value / 100)
83
  metal_data["Gold Value 14K"] = round(metal_data["Gold Value 14K"] * gold_markup_factor, 2)
84
  metal_data["Gold Value 18K"] = round(metal_data["Gold Value 18K"] * gold_markup_factor, 2)
85
  elif gold_markup_type == "fixed":
86
- # Ensure the markup value is properly applied
87
  metal_data["Gold Value 14K"] = round(metal_data["Gold Value 14K"] + gold_markup_value, 2)
88
  metal_data["Gold Value 18K"] = round(metal_data["Gold Value 18K"] + gold_markup_value, 2)
89
  elif gold_markup_type == "times":
90
  metal_data["Gold Value 14K"] = round(metal_data["Gold Value 14K"] * gold_markup_value, 2)
91
  metal_data["Gold Value 18K"] = round(metal_data["Gold Value 18K"] * gold_markup_value, 2)
92
 
93
- # Apply markup to platinum values - only percentage or fixed
94
  if platinum_markup_type == "percentage":
95
  platinum_markup_factor = 1 + (platinum_markup_value / 100)
96
  metal_data["Plat Value"] = round(metal_data["Plat Value"] * platinum_markup_factor, 2)
97
  elif platinum_markup_type == "fixed":
98
- # Ensure the markup value is properly applied
99
  metal_data["Plat Value"] = round(metal_data["Plat Value"] + platinum_markup_value, 2)
100
  elif platinum_markup_type == "times":
101
  metal_data["Plat Value"] = round(metal_data["Plat Value"] * platinum_markup_value, 2)
102
 
103
- # Apply markup to diamond values - only percentage or fixed
104
  if diamond_markup_type == "percentage":
105
  diamond_markup_factor = 1 + (diamond_markup_value / 100)
106
  for diamond in diamonds:
107
  diamond["Diamond Value"] = round(diamond["Diamond Value"] * diamond_markup_factor, 2)
108
  elif diamond_markup_type == "fixed":
109
- # Ensure the markup value is properly applied for each diamond
110
  for diamond in diamonds:
111
  diamond["Diamond Value"] = round(diamond["Diamond Value"] + diamond_markup_value, 2)
112
  elif diamond_markup_type == "times":
113
  for diamond in diamonds:
114
  diamond["Diamond Value"] = round(diamond["Diamond Value"] * diamond_markup_value, 2)
115
 
116
- # Recalculate diamond_values_sum after applying markup
117
  diamond_values_sum = sum(diamond["Diamond Value"] for diamond in diamonds)
118
 
119
- # Calculate prices with the marked-up values
120
  usd_14k = (
121
  metal_data["Gold Value 14K"] +
122
  finding_14kt +
@@ -158,7 +191,6 @@ async def process_image(
158
  landing_18k = duty_18k + freight
159
  landing_pt = duty_pt + freight
160
 
161
- # Add markup information to metal_data for reference
162
  metal_data.update({
163
  "Gold Markup Type": gold_markup_type,
164
  "Gold Markup Value": gold_markup_value,
@@ -184,69 +216,40 @@ async def process_image(
184
  if not diamonds:
185
  final_df = pd.DataFrame([metal_data])
186
  else:
187
- # Find the gold lock and platinum lock columns dynamically
188
- gold_lock_col = next((col for col in metal_data.keys() if "Gold Lock @" in col), None)
189
- platinum_lock_col = next((col for col in metal_data.keys() if "Platinum Lock" in col), None)
190
-
191
- # Make sure these columns are included in metal_columns
192
- metal_columns = [
193
- "Customer Ref",
194
- gold_lock_col, # Include the gold lock column
195
- platinum_lock_col, # Include the platinum lock column
196
- "Wt. in gram 14K",
197
- "Wt. in gram 18K",
198
- "Wt. in gram PLT",
199
- "Gold Rate (including loss) 14KT",
200
- "Gold Rate (including loss) 18KT",
201
- "Plat rate (including Lab)",
202
- "Gold Value 14K",
203
- "Gold Value 18K",
204
- "Plat Value"
205
- ]
206
 
207
- finding_columns = [
208
- "Finding For Gold 14KT", "Finding For Gold 18KT", "Finding for Plt.",
209
- "Rhodium", "Stamping"
210
- ]
211
 
212
- labour_columns = [
213
- "Labour 14K", "Labour 18K", "Labour PT"
 
214
  ]
215
 
216
- price_columns = [
217
- "Price of USD 14K", "Price of USD 18K", "Price of USD PT",
218
- "Price of GBP 14K @ 1/0.78", "Price of GBP 18K @ 1/0.78", "Price of GBP PT @ 1/0.78",
219
- "14K Duty @ 2.5% Including", "18K Duty @ 2.5% Including", "PT950 Duty @ 2.5% Including",
220
- "Freight",
221
- "14K Landing Cost (Duty + Freight)", "18K Landing Cost (Duty + Freight)",
222
- "PT950 Landing Cost (Duty + Freight)"
223
- ]
224
-
225
- # Filter out any None values from columns
226
- metal_columns = [col for col in metal_columns if col is not None]
227
-
228
  metal_data_basic = {k: v for k, v in metal_data.items()
229
  if k in metal_columns}
230
 
231
- findings_data = {k: v for k, v in metal_data.items()
232
- if k in finding_columns}
 
 
 
233
 
234
  labour_data = {k: v for k, v in metal_data.items()
235
- if k in labour_columns}
236
 
237
  price_data = {k: v for k, v in metal_data.items()
238
- if k in price_columns}
239
 
240
  metal_df = pd.DataFrame([metal_data_basic])
241
- findings_df = pd.DataFrame([findings_data])
242
  labour_df = pd.DataFrame([labour_data])
243
  price_df = pd.DataFrame([price_data])
244
  diamonds_df = pd.DataFrame(diamonds)
245
 
246
  empty_metal = pd.DataFrame([{k: '' for k in metal_columns}] * (len(diamonds_df) - 1))
247
- empty_findings = pd.DataFrame([{k: '' for k in finding_columns}] * (len(diamonds_df) - 1))
248
- empty_labour = pd.DataFrame([{k: '' for k in labour_columns}] * (len(diamonds_df) - 1))
249
- empty_prices = pd.DataFrame([{k: '' for k in price_columns}] * (len(diamonds_df) - 1))
250
 
251
  combined_metal = pd.concat([metal_df, empty_metal], ignore_index=True)
252
  combined_findings = pd.concat([findings_df, empty_findings], ignore_index=True)
@@ -261,13 +264,18 @@ async def process_image(
261
  combined_prices
262
  ], axis=1)
263
 
264
- for col in price_columns:
265
  if col in final_df.columns: # Check if column exists
266
  final_df[col] = final_df[col].astype(str)
267
  final_df[col] = final_df[col].apply(lambda x: x.strip('"').strip("'") if x else '')
268
  if "GBP" in col:
269
  final_df[col] = final_df[col].apply(lambda x: x.replace('£', '£') if x else '')
270
 
 
 
 
 
 
271
  output = BytesIO()
272
  final_df.to_csv(
273
  output,
 
7
  import csv
8
  import json
9
  import math
10
+ from acc_processor import AccessoryProcessor
11
 
12
  app = FastAPI()
13
 
 
24
  @app.post("/process-image/")
25
  async def process_image(
26
  file: UploadFile = File(...),
27
+ accessory_type: str = Form(...),
28
  finding_14kt: float = Form(...),
29
  finding_18kt: float = Form(...),
30
  finding_plt: float = Form(...),
 
39
  finding_markup_value: float = Form(...)
40
  ):
41
  try:
42
+ processor = AccessoryProcessor(accessory_type)
43
+
44
  diamond_rates_dict = json.loads(diamond_rates)
45
  file_content = await file.read()
46
  extracted_text = extract_data_from_image(BytesIO(file_content))
 
73
  finding_18kt = round(finding_18kt * finding_markup_value, 2)
74
  finding_plt = round(finding_plt * finding_markup_value, 2)
75
 
76
+ # Calculate labour based on accessory type
77
+ metal_data = processor.calculate_labour(metal_data)
78
+
79
+ # Update finding names based on accessory type
80
+ finding_key_map = {
81
+ "pendant": {
82
+ "14kt": "Finding For Gold 14KT",
83
+ "18kt": "Finding For Gold 18KT",
84
+ "plt": "Finding for Plt."
85
+ },
86
+ "ringband": {
87
+ "14kt": "CFP for Gold 14KT",
88
+ "18kt": "CFP for Gold 18KT",
89
+ "plt": "CFP for Plt."
90
+ },
91
+ "earring": {
92
+ "14kt": "Finding & CFP For Gold 14KT",
93
+ "18kt": "Finding For Gold 18KT",
94
+ "plt": "Finding for Plt."
95
+ }
96
+ }[accessory_type]
97
+
98
  metal_data.update({
99
+ finding_key_map["14kt"]: finding_14kt,
100
+ finding_key_map["18kt"]: finding_18kt,
101
+ finding_key_map["plt"]: finding_plt,
102
+ "Finding & CFP For Gold 14KT": finding_14kt if accessory_type == "earring" else None,
103
+ "Finding For Gold 18KT": finding_18kt if accessory_type == "earring" else None,
104
+ "Finding for Plt.": finding_plt if accessory_type == "earring" else None,
105
  "Finding Markup Type": finding_markup_type,
106
  "Finding Markup Value": finding_markup_value,
107
+ "Setting": 0,
108
  })
109
 
110
+
111
+ # Calculate Gold Lock and Platinum Lock values
112
+ metal_data.update({
113
+ "Gold Lock": round(metal_data.get("Wt. in gram 14K", 0) * 0.15 +
114
+ metal_data.get("Wt. in gram 18K", 0) * 0.15, 2),
115
+ "Platinum Lock": round(metal_data.get("Wt. in gram PLT", 0) * 0.25, 2)
116
+ })
117
+
118
  # Apply markup to gold values - only percentage or fixed
119
  if gold_markup_type == "percentage":
120
  gold_markup_factor = 1 + (gold_markup_value / 100)
121
  metal_data["Gold Value 14K"] = round(metal_data["Gold Value 14K"] * gold_markup_factor, 2)
122
  metal_data["Gold Value 18K"] = round(metal_data["Gold Value 18K"] * gold_markup_factor, 2)
123
  elif gold_markup_type == "fixed":
 
124
  metal_data["Gold Value 14K"] = round(metal_data["Gold Value 14K"] + gold_markup_value, 2)
125
  metal_data["Gold Value 18K"] = round(metal_data["Gold Value 18K"] + gold_markup_value, 2)
126
  elif gold_markup_type == "times":
127
  metal_data["Gold Value 14K"] = round(metal_data["Gold Value 14K"] * gold_markup_value, 2)
128
  metal_data["Gold Value 18K"] = round(metal_data["Gold Value 18K"] * gold_markup_value, 2)
129
 
 
130
  if platinum_markup_type == "percentage":
131
  platinum_markup_factor = 1 + (platinum_markup_value / 100)
132
  metal_data["Plat Value"] = round(metal_data["Plat Value"] * platinum_markup_factor, 2)
133
  elif platinum_markup_type == "fixed":
134
+
135
  metal_data["Plat Value"] = round(metal_data["Plat Value"] + platinum_markup_value, 2)
136
  elif platinum_markup_type == "times":
137
  metal_data["Plat Value"] = round(metal_data["Plat Value"] * platinum_markup_value, 2)
138
 
139
+
140
  if diamond_markup_type == "percentage":
141
  diamond_markup_factor = 1 + (diamond_markup_value / 100)
142
  for diamond in diamonds:
143
  diamond["Diamond Value"] = round(diamond["Diamond Value"] * diamond_markup_factor, 2)
144
  elif diamond_markup_type == "fixed":
 
145
  for diamond in diamonds:
146
  diamond["Diamond Value"] = round(diamond["Diamond Value"] + diamond_markup_value, 2)
147
  elif diamond_markup_type == "times":
148
  for diamond in diamonds:
149
  diamond["Diamond Value"] = round(diamond["Diamond Value"] * diamond_markup_value, 2)
150
 
 
151
  diamond_values_sum = sum(diamond["Diamond Value"] for diamond in diamonds)
152
 
 
153
  usd_14k = (
154
  metal_data["Gold Value 14K"] +
155
  finding_14kt +
 
191
  landing_18k = duty_18k + freight
192
  landing_pt = duty_pt + freight
193
 
 
194
  metal_data.update({
195
  "Gold Markup Type": gold_markup_type,
196
  "Gold Markup Value": gold_markup_value,
 
216
  if not diamonds:
217
  final_df = pd.DataFrame([metal_data])
218
  else:
219
+ columns_config = processor.get_column_config()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220
 
221
+ gold_lock_col = next(col for col in metal_data.keys() if "Gold Lock @" in col)
222
+ platinum_lock_col = next(col for col in metal_data.keys() if "Platinum Lock" in col)
 
 
223
 
224
+ metal_columns = [
225
+ col(metal_data) if callable(col) else col
226
+ for col in columns_config["metal_columns"]
227
  ]
228
 
 
 
 
 
 
 
 
 
 
 
 
 
229
  metal_data_basic = {k: v for k, v in metal_data.items()
230
  if k in metal_columns}
231
 
232
+ findings_data = {}
233
+ for col in columns_config["finding_columns"]:
234
+ findings_data[col] = metal_data.get(col, 0) # Use 0 as default value
235
+
236
+ findings_df = pd.DataFrame([findings_data])
237
 
238
  labour_data = {k: v for k, v in metal_data.items()
239
+ if k in columns_config["labour_columns"]}
240
 
241
  price_data = {k: v for k, v in metal_data.items()
242
+ if k in columns_config["price_columns"]}
243
 
244
  metal_df = pd.DataFrame([metal_data_basic])
 
245
  labour_df = pd.DataFrame([labour_data])
246
  price_df = pd.DataFrame([price_data])
247
  diamonds_df = pd.DataFrame(diamonds)
248
 
249
  empty_metal = pd.DataFrame([{k: '' for k in metal_columns}] * (len(diamonds_df) - 1))
250
+ empty_findings = pd.DataFrame([{k: '' for k in columns_config["finding_columns"]}] * (len(diamonds_df) - 1))
251
+ empty_labour = pd.DataFrame([{k: '' for k in columns_config["labour_columns"]}] * (len(diamonds_df) - 1))
252
+ empty_prices = pd.DataFrame([{k: '' for k in columns_config["price_columns"]}] * (len(diamonds_df) - 1))
253
 
254
  combined_metal = pd.concat([metal_df, empty_metal], ignore_index=True)
255
  combined_findings = pd.concat([findings_df, empty_findings], ignore_index=True)
 
264
  combined_prices
265
  ], axis=1)
266
 
267
+ for col in columns_config["price_columns"]:
268
  if col in final_df.columns: # Check if column exists
269
  final_df[col] = final_df[col].astype(str)
270
  final_df[col] = final_df[col].apply(lambda x: x.strip('"').strip("'") if x else '')
271
  if "GBP" in col:
272
  final_df[col] = final_df[col].apply(lambda x: x.replace('£', '£') if x else '')
273
 
274
+ # Add this before creating the final DataFrame
275
+ print("Finding columns config:", columns_config["finding_columns"])
276
+ print("Metal data finding keys:", [k for k in metal_data.keys() if "Finding" in k])
277
+ print("Findings data:", findings_data)
278
+
279
  output = BytesIO()
280
  final_df.to_csv(
281
  output,