utkarsh134 commited on
Commit
4c1ad4b
·
verified ·
1 Parent(s): 3d687fc

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +458 -151
main.py CHANGED
@@ -1,30 +1,155 @@
1
- from fastapi import FastAPI, File, UploadFile, Form, HTTPException
2
- from io import BytesIO
3
- import pandas as pd
4
- from fastapi.responses import StreamingResponse
5
- from extract import extract_data_from_image
6
- from parsing import process_extracted_text
7
  import csv
 
8
  import json
9
  import math
 
 
 
 
 
 
 
10
  from acc_processor import AccessoryProcessor
 
 
 
11
 
12
  app = FastAPI()
13
 
14
- @app.post("/extract-diamonds/")
15
- async def extract_diamonds(file: UploadFile = File(...)):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  try:
17
  file_content = await file.read()
18
- extracted_text = extract_data_from_image(BytesIO(file_content))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  processed_data = process_extracted_text(extracted_text)
20
  return {"diamonds": processed_data["Diamonds"]}
21
  except Exception as e:
22
  raise HTTPException(status_code=400, detail=str(e))
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(...),
@@ -36,20 +161,30 @@ async def process_image(
36
  diamond_markup_type: str = Form(...),
37
  diamond_markup_value: float = Form(...),
38
  finding_markup_type: str = 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))
47
- processed_data = process_extracted_text(extracted_text)
48
  metal_data = processed_data["Metal Details"]
49
  diamonds = processed_data["Diamonds"]
50
 
51
-
52
  diamond_values_sum = 0
 
 
53
  for idx, diamond in enumerate(diamonds):
54
  rate = float(diamond_rates_dict.get(str(idx), 0))
55
  diamond["Diamond Rate"] = rate
@@ -59,6 +194,18 @@ async def process_image(
59
  diamond_values_sum += diamond_value
60
 
61
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  if finding_markup_type == "percentage":
63
  finding_markup_factor = 1 + (finding_markup_value / 100)
64
  finding_14kt = round(finding_14kt * finding_markup_factor, 2)
@@ -73,115 +220,209 @@ async def process_image(
73
  finding_18kt = round(finding_18kt * finding_markup_value, 2)
74
  finding_plt = round(finding_plt * finding_markup_value, 2)
75
 
76
-
77
  metal_data = processor.calculate_labour(metal_data)
78
 
79
-
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
-
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
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 +
156
- diamond_values_sum +
157
- metal_data["Labour 14K"] +
158
- metal_data["Rhodium"]
159
- )
160
- usd_14k = math.ceil(usd_14k) + 1
161
-
162
- usd_18k = (
163
- metal_data["Gold Value 18K"] +
164
- finding_18kt +
165
- diamond_values_sum +
166
- metal_data["Labour 18K"] +
167
- metal_data["Rhodium"]
168
- )
169
- usd_18k = math.ceil(usd_18k) + 1
170
-
171
- usd_pt = (
172
- metal_data["Plat Value"] +
173
- finding_plt +
174
- diamond_values_sum +
175
- metal_data["Labour PT"] +
176
- metal_data["Rhodium"]
177
- )
178
- usd_pt = math.ceil(usd_pt) + 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
  gbp_14k = usd_14k * 0.78
181
  gbp_18k = usd_18k * 0.78
182
  gbp_pt = usd_pt * 0.78
183
 
184
- duty_14k = gbp_14k * 1.025
185
  duty_18k = gbp_18k * 1.025
186
  duty_pt = gbp_pt * 1.025
187
 
@@ -191,98 +432,132 @@ async def process_image(
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,
197
- "Platinum Markup Type": platinum_markup_type,
198
- "Platinum Markup Value": platinum_markup_value,
199
- "Diamond Markup Type": diamond_markup_type,
200
- "Diamond Markup Value": diamond_markup_value,
201
- "Price of USD 14K": f"$ {usd_14k:.2f}",
202
- "Price of USD 18K": f"$ {usd_18k:.2f}",
203
- "Price of USD PT": f"$ {usd_pt:.2f}",
204
- "Price of GBP 14K @ 1/0.78": f"£ {gbp_14k:.2f}",
205
- "Price of GBP 18K @ 1/0.78": f"£ {gbp_18k:.2f}",
206
- "Price of GBP PT @ 1/0.78": f"£ {gbp_pt:.2f}",
207
- "14K Duty @ 2.5% Including": f"£ {duty_14k:.2f}",
208
- "18K Duty @ 2.5% Including": f"£ {duty_18k:.2f}",
209
- "PT950 Duty @ 2.5% Including": f"£ {duty_pt:.2f}",
210
- "Freight": f"£ {freight:.2f}",
211
- "14K Landing Cost (Duty + Freight)": f"£ {landing_14k:.2f}",
212
- "18K Landing Cost (Duty + Freight)": f"£ {landing_18k:.2f}",
213
- "PT950 Landing Cost (Duty + Freight)": f"£ {landing_pt:.2f}"
214
- })
 
 
215
 
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)
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)
 
 
256
  combined_labour = pd.concat([labour_df, empty_labour], ignore_index=True)
257
  combined_prices = pd.concat([price_df, empty_prices], ignore_index=True)
258
-
259
- final_df = pd.concat([
260
- combined_metal,
261
- diamonds_df,
262
- combined_findings,
263
- combined_labour,
264
- combined_prices
265
- ], axis=1)
 
 
 
266
 
267
  for col in columns_config["price_columns"]:
268
- if col in final_df.columns:
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
 
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,
282
- index=False,
283
- encoding='utf-8-sig',
284
  quoting=csv.QUOTE_MINIMAL,
285
- escapechar='\\'
286
  )
287
  output.seek(0)
288
 
@@ -291,8 +566,40 @@ async def process_image(
291
  media_type="text/csv; charset=utf-8",
292
  headers={
293
  "Content-Disposition": "attachment; filename=output.csv",
294
- "Content-Type": "text/csv; charset=utf-8"
295
- }
296
  )
297
  except Exception as e:
298
- raise HTTPException(status_code=400, detail=str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import csv
2
+ import hashlib
3
  import json
4
  import math
5
+ from io import BytesIO
6
+
7
+ import pandas as pd
8
+ from fastapi import (FastAPI, File, Form, HTTPException,
9
+ UploadFile)
10
+ from fastapi.responses import StreamingResponse
11
+
12
  from acc_processor import AccessoryProcessor
13
+ from cache import textract_cache
14
+ from extract import extract_data_from_image
15
+ from parsing import map_accessory_type, process_extracted_text
16
 
17
  app = FastAPI()
18
 
19
+
20
+ def get_file_hash(file_content: bytes) -> str:
21
+ """Generate a unique hash for a file to use as cache key"""
22
+ return hashlib.md5(file_content).hexdigest()
23
+
24
+
25
+ async def get_extracted_text(file: UploadFile = None, extraction_id: str = None) -> str:
26
+ """Get extracted text from cache or Textract based on file or extraction ID"""
27
+ if extraction_id:
28
+ # Try to get from cache using extraction ID
29
+ cached_text = textract_cache.get(extraction_id)
30
+ if cached_text is not None:
31
+ return cached_text
32
+ raise HTTPException(
33
+ status_code=404,
34
+ detail=f"Extraction ID {extraction_id} not found or expired",
35
+ )
36
+
37
+ if not file:
38
+ raise HTTPException(
39
+ status_code=400, detail="Either file or extraction_id must be provided"
40
+ )
41
+
42
+ file_content = await file.read()
43
+ file_hash = get_file_hash(file_content)
44
+
45
+ # Try to get from cache using file hash
46
+ cached_text = textract_cache.get(file_hash)
47
+ if cached_text is not None:
48
+ # Ensure file.file is repositioned for any subsequent reads
49
+ await file.seek(0)
50
+ return cached_text
51
+
52
+ # Not in cache, extract using Textract
53
+ extracted_text = extract_data_from_image(BytesIO(file_content))
54
+
55
+ # Store in cache (no need to return the ID here)
56
+ textract_cache.set(file_hash, extracted_text)
57
+
58
+ # Ensure file.file is repositioned for any subsequent reads
59
+ await file.seek(0)
60
+
61
+ return extracted_text
62
+
63
+
64
+ @app.post("/extract-raw-data/")
65
+ async def extract_raw_data(file: UploadFile = File(...)):
66
+ """
67
+ Extract raw text data from an image and cache it for later use.
68
+ Returns an extraction_id that can be used in other endpoints.
69
+ """
70
  try:
71
  file_content = await file.read()
72
+ file_hash = get_file_hash(file_content)
73
+
74
+ # Try to get from cache first
75
+ cached_text = textract_cache.get(file_hash)
76
+
77
+ if cached_text is None:
78
+ # Not in cache, extract using Textract
79
+ extracted_text = extract_data_from_image(BytesIO(file_content))
80
+ else:
81
+ extracted_text = cached_text
82
+
83
+ # Store in cache and get extraction ID
84
+ extraction_id = textract_cache.set(file_hash, extracted_text)
85
+
86
+ # Return a short sample of the extracted text for verification
87
+ text_sample = (
88
+ extracted_text[:200] + "..."
89
+ if len(extracted_text) > 200
90
+ else extracted_text
91
+ )
92
+
93
+ return {
94
+ "extraction_id": extraction_id,
95
+ "text_sample": text_sample,
96
+ "message": "Extraction successful. Use this extraction_id for subsequent processing calls.",
97
+ }
98
+ except Exception as e:
99
+ raise HTTPException(status_code=500, detail=f"Extraction failed: {str(e)}")
100
+
101
+
102
+ @app.post("/extract-diamonds/")
103
+ async def extract_diamonds(
104
+ file: UploadFile = File(None), extraction_id: str = Form(None)
105
+ ):
106
+ """Extract diamonds data using either a file or an extraction_id"""
107
+ try:
108
+ if not file and not extraction_id:
109
+ raise HTTPException(
110
+ status_code=400, detail="Either file or extraction_id must be provided"
111
+ )
112
+
113
+ extracted_text = await get_extracted_text(file, extraction_id)
114
  processed_data = process_extracted_text(extracted_text)
115
  return {"diamonds": processed_data["Diamonds"]}
116
  except Exception as e:
117
  raise HTTPException(status_code=400, detail=str(e))
118
 
119
+
120
+ @app.post("/detect-accessory-type/")
121
+ async def detect_accessory_type(
122
+ file: UploadFile = File(None), extraction_id: str = Form(None)
123
+ ):
124
+ """Detect accessory type using either a file or an extraction_id"""
125
+ try:
126
+ if not file and not extraction_id:
127
+ raise HTTPException(
128
+ status_code=400, detail="Either file or extraction_id must be provided"
129
+ )
130
+
131
+ extracted_text = await get_extracted_text(file, extraction_id)
132
+ processed_data = process_extracted_text(extracted_text)
133
+
134
+ detected_type = processed_data.get("Accessory Type", "pendant")
135
+
136
+ return {
137
+ "detected_accessory_type": detected_type,
138
+ "raw_text_sample": (
139
+ extracted_text[:500] + "..."
140
+ if len(extracted_text) > 500
141
+ else extracted_text
142
+ ),
143
+ }
144
+ except Exception as e:
145
+ raise HTTPException(status_code=400, detail=str(e))
146
+
147
+
148
  @app.post("/process-image/")
149
  async def process_image(
150
+ file: UploadFile = File(None),
151
+ extraction_id: str = Form(None),
152
+ accessory_type: str = Form(None),
153
  finding_14kt: float = Form(...),
154
  finding_18kt: float = Form(...),
155
  finding_plt: float = Form(...),
 
161
  diamond_markup_type: str = Form(...),
162
  diamond_markup_value: float = Form(...),
163
  finding_markup_type: str = Form(...),
164
+ finding_markup_value: float = Form(...),
165
  ):
166
+ """Process image data using either a file or an extraction_id"""
167
  try:
168
+ if not file and not extraction_id:
169
+ raise HTTPException(
170
+ status_code=400, detail="Either file or extraction_id must be provided"
171
+ )
172
+
173
+ extracted_text = await get_extracted_text(file, extraction_id)
174
+ processed_data = process_extracted_text(extracted_text)
175
+
176
+ detected_accessory_type = processed_data.get("Accessory Type", "pendant")
177
+ accessory_type = accessory_type or detected_accessory_type
178
+
179
  processor = AccessoryProcessor(accessory_type)
180
+
181
  diamond_rates_dict = json.loads(diamond_rates)
 
 
 
182
  metal_data = processed_data["Metal Details"]
183
  diamonds = processed_data["Diamonds"]
184
 
 
185
  diamond_values_sum = 0
186
+ settings_sum = 0
187
+
188
  for idx, diamond in enumerate(diamonds):
189
  rate = float(diamond_rates_dict.get(str(idx), 0))
190
  diamond["Diamond Rate"] = rate
 
194
  diamond_values_sum += diamond_value
195
 
196
 
197
+ if accessory_type in ["bracelet", "necklace"]:
198
+ dia_qty = float(diamond.get("Dia Qty", 0))
199
+ diamond["Setting"] = round(dia_qty * 0.25, 2)
200
+ else:
201
+ diamond["Setting"] = 0
202
+
203
+ settings_sum = (
204
+ sum(diamond["Setting"] for diamond in diamonds)
205
+ if accessory_type in ["bracelet", "necklace"]
206
+ else 0
207
+ )
208
+
209
  if finding_markup_type == "percentage":
210
  finding_markup_factor = 1 + (finding_markup_value / 100)
211
  finding_14kt = round(finding_14kt * finding_markup_factor, 2)
 
220
  finding_18kt = round(finding_18kt * finding_markup_value, 2)
221
  finding_plt = round(finding_plt * finding_markup_value, 2)
222
 
223
+
224
  metal_data = processor.calculate_labour(metal_data)
225
 
226
+
227
  finding_key_map = {
228
  "pendant": {
229
  "14kt": "Finding For Gold 14KT",
230
  "18kt": "Finding For Gold 18KT",
231
+ "plt": "Finding for Plt.",
232
  },
233
  "ringband": {
234
  "14kt": "CFP for Gold 14KT",
235
  "18kt": "CFP for Gold 18KT",
236
+ "plt": "CFP for Plt.",
237
  },
238
  "earring": {
239
  "14kt": "Finding & CFP For Gold 14KT",
240
  "18kt": "Finding For Gold 18KT",
241
+ "plt": "Finding for Plt.",
242
+ },
243
+ "bracelet": {
244
+ "14kt": "CFP For Gold 14KT",
245
+ "18kt": "CFP For Gold 18KT",
246
+ "plt": "CFP For Gold PLT",
247
+ },
248
+ "necklace": {
249
+ "14kt": "CFP For Gold 14KT",
250
+ "18kt": "CFP For Gold 18KT",
251
+ "plt": "CFP For Gold PLT",
252
+ },
253
  }[accessory_type]
254
 
255
+ if accessory_type in ["bracelet", "necklace"]:
256
+ metal_data.update(
257
+ {
258
+ "CFP For Gold 14KT": finding_14kt,
259
+ "CFP For Gold 18KT": finding_18kt,
260
+ "CFP For Gold PLT": finding_plt,
261
+ "Finding Markup Type": finding_markup_type,
262
+ "Finding Markup Value": finding_markup_value,
263
+ "Setting": settings_sum,
264
+ }
265
+ )
266
+ elif accessory_type == "ringband":
267
+ metal_data.update(
268
+ {
269
+ "CFP for Gold 14KT": finding_14kt,
270
+ "CFP for Gold 18KT": finding_18kt,
271
+ "CFP for Plt.": finding_plt,
272
+ "Finding Markup Type": finding_markup_type,
273
+ "Finding Markup Value": finding_markup_value,
274
+ }
275
+ )
276
+ elif accessory_type == "earring":
277
+ metal_data.update(
278
+ {
279
+ "Finding & CFP For Gold 14KT": finding_14kt,
280
+ "Finding For Gold 18KT": finding_18kt,
281
+ "Finding for Plt.": finding_plt,
282
+ "Finding Markup Type": finding_markup_type,
283
+ "Finding Markup Value": finding_markup_value,
284
+ }
285
+ )
286
+ else: # pendant case
287
+ metal_data.update(
288
+ {
289
+ "Finding For Gold 14KT": finding_14kt,
290
+ "Finding For Gold 18KT": finding_18kt,
291
+ "Finding for Plt.": finding_plt,
292
+ "Finding Markup Type": finding_markup_type,
293
+ "Finding Markup Value": finding_markup_value,
294
+ }
295
+ )
296
+
297
+
298
+ metal_data.update(
299
+ {
300
+ "Gold Lock": round(
301
+ metal_data.get("Wt. in gram 14K", 0) * 0.15
302
+ + metal_data.get("Wt. in gram 18K", 0) * 0.15,
303
+ 2,
304
+ ),
305
+ "Platinum Lock": round(metal_data.get("Wt. in gram PLT", 0) * 0.25, 2),
306
+ }
307
+ )
308
+
309
+
310
  if gold_markup_type == "percentage":
311
  gold_markup_factor = 1 + (gold_markup_value / 100)
312
+ metal_data["Gold Value 14K"] = round(
313
+ metal_data["Gold Value 14K"] * gold_markup_factor, 2
314
+ )
315
+ metal_data["Gold Value 18K"] = round(
316
+ metal_data["Gold Value 18K"] * gold_markup_factor, 2
317
+ )
318
  elif gold_markup_type == "fixed":
319
+ metal_data["Gold Value 14K"] = round(
320
+ metal_data["Gold Value 14K"] + gold_markup_value, 2
321
+ )
322
+ metal_data["Gold Value 18K"] = round(
323
+ metal_data["Gold Value 18K"] + gold_markup_value, 2
324
+ )
325
  elif gold_markup_type == "times":
326
+ metal_data["Gold Value 14K"] = round(
327
+ metal_data["Gold Value 14K"] * gold_markup_value, 2
328
+ )
329
+ metal_data["Gold Value 18K"] = round(
330
+ metal_data["Gold Value 18K"] * gold_markup_value, 2
331
+ )
332
 
333
  if platinum_markup_type == "percentage":
334
  platinum_markup_factor = 1 + (platinum_markup_value / 100)
335
+ metal_data["Plat Value"] = round(
336
+ metal_data["Plat Value"] * platinum_markup_factor, 2
337
+ )
338
  elif platinum_markup_type == "fixed":
339
+ metal_data["Plat Value"] = round(
340
+ metal_data["Plat Value"] + platinum_markup_value, 2
341
+ )
342
  elif platinum_markup_type == "times":
343
+ metal_data["Plat Value"] = round(
344
+ metal_data["Plat Value"] * platinum_markup_value, 2
345
+ )
346
 
347
  if diamond_markup_type == "percentage":
348
  diamond_markup_factor = 1 + (diamond_markup_value / 100)
349
  for diamond in diamonds:
350
+ diamond["Diamond Value"] = round(
351
+ diamond["Diamond Value"] * diamond_markup_factor, 2
352
+ )
353
  elif diamond_markup_type == "fixed":
354
  for diamond in diamonds:
355
+ diamond["Diamond Value"] = round(
356
+ diamond["Diamond Value"] + diamond_markup_value, 2
357
+ )
358
  elif diamond_markup_type == "times":
359
  for diamond in diamonds:
360
+ diamond["Diamond Value"] = round(
361
+ diamond["Diamond Value"] * diamond_markup_value, 2
362
+ )
363
 
364
  diamond_values_sum = sum(diamond["Diamond Value"] for diamond in diamonds)
365
+
366
+ if accessory_type in ["bracelet", "necklace"]:
367
+ usd_14k = (
368
+ metal_data["Gold Value 14K"]
369
+ + finding_14kt
370
+ + diamond_values_sum
371
+ + settings_sum
372
+ + metal_data["Rhodium"]
373
+ )
374
+ usd_14k = math.ceil(usd_14k) + 1
375
+
376
+ usd_18k = (
377
+ metal_data["Gold Value 18K"]
378
+ + finding_18kt
379
+ + diamond_values_sum
380
+ + settings_sum
381
+ + metal_data["Rhodium"]
382
+ )
383
+ usd_18k = math.ceil(usd_18k) + 1
384
+
385
+ usd_pt = (
386
+ metal_data["Plat Value"]
387
+ + finding_plt
388
+ + diamond_values_sum
389
+ + settings_sum
390
+ + metal_data["Rhodium"]
391
+ )
392
+ usd_pt = math.ceil(usd_pt) + 1
393
+ else:
394
+ usd_14k = (
395
+ metal_data["Gold Value 14K"]
396
+ + finding_14kt
397
+ + diamond_values_sum
398
+ + metal_data["Labour 14K"]
399
+ + metal_data["Rhodium"]
400
+ )
401
+ usd_14k = math.ceil(usd_14k) + 1
402
+
403
+ usd_18k = (
404
+ metal_data["Gold Value 18K"]
405
+ + finding_18kt
406
+ + diamond_values_sum
407
+ + metal_data["Labour 18K"]
408
+ + metal_data["Rhodium"]
409
+ )
410
+ usd_18k = math.ceil(usd_18k) + 1
411
+
412
+ usd_pt = (
413
+ metal_data["Plat Value"]
414
+ + finding_plt
415
+ + diamond_values_sum
416
+ + metal_data["Labour PT"]
417
+ + metal_data["Rhodium"]
418
+ )
419
+ usd_pt = math.ceil(usd_pt) + 1
420
 
421
  gbp_14k = usd_14k * 0.78
422
  gbp_18k = usd_18k * 0.78
423
  gbp_pt = usd_pt * 0.78
424
 
425
+ duty_14k = gbp_14k * 1.025
426
  duty_18k = gbp_18k * 1.025
427
  duty_pt = gbp_pt * 1.025
428
 
 
432
  landing_18k = duty_18k + freight
433
  landing_pt = duty_pt + freight
434
 
435
+ metal_data.update(
436
+ {
437
+ "Gold Markup Type": gold_markup_type,
438
+ "Gold Markup Value": gold_markup_value,
439
+ "Platinum Markup Type": platinum_markup_type,
440
+ "Platinum Markup Value": platinum_markup_value,
441
+ "Diamond Markup Type": diamond_markup_type,
442
+ "Diamond Markup Value": diamond_markup_value,
443
+ "Price of USD 14K": f"$ {usd_14k:.2f}",
444
+ "Price of USD 18K": f"$ {usd_18k:.2f}",
445
+ "Price of USD PT": f"$ {usd_pt:.2f}",
446
+ "Price of GBP 14K @ 1/0.78": f"£ {gbp_14k:.2f}",
447
+ "Price of GBP 18K @ 1/0.78": f"£ {gbp_18k:.2f}",
448
+ "Price of GBP PT @ 1/0.78": f"£ {gbp_pt:.2f}",
449
+ "14K Duty @ 2.5% Including": f"£ {duty_14k:.2f}",
450
+ "18K Duty @ 2.5% Including": f"£ {duty_18k:.2f}",
451
+ "PT950 Duty @ 2.5% Including": f"£ {duty_pt:.2f}",
452
+ "Freight": f"£ {freight:.2f}",
453
+ "14K Landing Cost (Duty + Freight)": f"£ {landing_14k:.2f}",
454
+ "18K Landing Cost (Duty + Freight)": f"£ {landing_18k:.2f}",
455
+ "PT950 Landing Cost (Duty + Freight)": f"£ {landing_pt:.2f}",
456
+ }
457
+ )
458
 
459
  if not diamonds:
460
  final_df = pd.DataFrame([metal_data])
461
  else:
462
  columns_config = processor.get_column_config()
463
+
464
+ gold_lock_col = next(
465
+ col for col in metal_data.keys() if "Gold Lock @" in col
466
+ )
467
+ platinum_lock_col = next(
468
+ col for col in metal_data.keys() if "Platinum Lock" in col
469
+ )
470
+
471
  metal_columns = [
472
+ col(metal_data) if callable(col) else col
473
  for col in columns_config["metal_columns"]
474
  ]
475
+
476
+ metal_data_basic = {
477
+ k: v for k, v in metal_data.items() if k in metal_columns
478
+ }
479
+
480
  findings_data = {}
481
  for col in columns_config["finding_columns"]:
482
+ findings_data[col] = metal_data.get(col, 0) # Use 0 as default value
483
 
484
  findings_df = pd.DataFrame([findings_data])
485
+
486
+ labour_data = {
487
+ k: v
488
+ for k, v in metal_data.items()
489
+ if k in columns_config["labour_columns"]
490
+ }
491
+
492
+ price_data = {
493
+ k: v
494
+ for k, v in metal_data.items()
495
+ if k in columns_config["price_columns"]
496
+ }
497
+
498
  metal_df = pd.DataFrame([metal_data_basic])
499
  labour_df = pd.DataFrame([labour_data])
500
  price_df = pd.DataFrame([price_data])
501
  diamonds_df = pd.DataFrame(diamonds)
502
+
503
+ empty_metal = pd.DataFrame(
504
+ [{k: "" for k in metal_columns}] * (len(diamonds_df) - 1)
505
+ )
506
+ empty_findings = pd.DataFrame(
507
+ [{k: "" for k in columns_config["finding_columns"]}]
508
+ * (len(diamonds_df) - 1)
509
+ )
510
+ empty_labour = pd.DataFrame(
511
+ [{k: "" for k in columns_config["labour_columns"]}]
512
+ * (len(diamonds_df) - 1)
513
+ )
514
+ empty_prices = pd.DataFrame(
515
+ [{k: "" for k in columns_config["price_columns"]}]
516
+ * (len(diamonds_df) - 1)
517
+ )
518
+
519
  combined_metal = pd.concat([metal_df, empty_metal], ignore_index=True)
520
+ combined_findings = pd.concat(
521
+ [findings_df, empty_findings], ignore_index=True
522
+ )
523
  combined_labour = pd.concat([labour_df, empty_labour], ignore_index=True)
524
  combined_prices = pd.concat([price_df, empty_prices], ignore_index=True)
525
+
526
+ final_df = pd.concat(
527
+ [
528
+ combined_metal,
529
+ diamonds_df,
530
+ combined_findings,
531
+ combined_labour,
532
+ combined_prices,
533
+ ],
534
+ axis=1,
535
+ )
536
 
537
  for col in columns_config["price_columns"]:
538
+ if col in final_df.columns: # Check if column exists
539
  final_df[col] = final_df[col].astype(str)
540
+ final_df[col] = final_df[col].apply(
541
+ lambda x: x.strip('"').strip("'") if x else ""
542
+ )
543
  if "GBP" in col:
544
+ final_df[col] = final_df[col].apply(
545
+ lambda x: x.replace("£", "£") if x else ""
546
+ )
547
 
548
  print("Finding columns config:", columns_config["finding_columns"])
549
+ print(
550
+ "Metal data finding keys:", [k for k in metal_data.keys() if "Finding" in k]
551
+ )
552
  print("Findings data:", findings_data)
553
 
554
  output = BytesIO()
555
  final_df.to_csv(
556
+ output,
557
+ index=False,
558
+ encoding="utf-8-sig",
559
  quoting=csv.QUOTE_MINIMAL,
560
+ escapechar="\\",
561
  )
562
  output.seek(0)
563
 
 
566
  media_type="text/csv; charset=utf-8",
567
  headers={
568
  "Content-Disposition": "attachment; filename=output.csv",
569
+ "Content-Type": "text/csv; charset=utf-8",
570
+ },
571
  )
572
  except Exception as e:
573
+ raise HTTPException(status_code=400, detail=str(e))
574
+
575
+ @app.post("/test-accessory-mapping/")
576
+ async def test_accessory_mapping(description: str = Form(...)):
577
+ """Test endpoint to see how the LLM maps different accessory descriptions."""
578
+ try:
579
+ mapped_type = map_accessory_type(description)
580
+
581
+ return {"original_description": description, "mapped_type": mapped_type}
582
+ except Exception as e:
583
+ raise HTTPException(status_code=400, detail=str(e))
584
+
585
+
586
+ @app.post("/admin/clear-cache/")
587
+ async def clear_cache(key: str = None):
588
+ """Admin endpoint to clear the cache"""
589
+ textract_cache.clear(key)
590
+ return {"status": "success", "message": "Cache cleared"}
591
+
592
+
593
+ @app.get("/check-extraction/")
594
+ async def check_extraction(extraction_id: str):
595
+ """Check if an extraction ID is valid and not expired"""
596
+ is_valid = textract_cache.is_valid_id(extraction_id)
597
+ return {
598
+ "extraction_id": extraction_id,
599
+ "valid": is_valid,
600
+ "message": (
601
+ "Extraction ID is valid and can be used"
602
+ if is_valid
603
+ else "Extraction ID not found or expired"
604
+ ),
605
+ }