Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -26,7 +26,15 @@ async def process_image(
|
|
| 26 |
finding_14kt: float = Form(...),
|
| 27 |
finding_18kt: float = Form(...),
|
| 28 |
finding_plt: float = Form(...),
|
| 29 |
-
diamond_rates: str = Form(...)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
):
|
| 31 |
try:
|
| 32 |
diamond_rates_dict = json.loads(diamond_rates)
|
|
@@ -36,25 +44,67 @@ async def process_image(
|
|
| 36 |
metal_data = processed_data["Metal Details"]
|
| 37 |
diamonds = processed_data["Diamonds"]
|
| 38 |
|
| 39 |
-
#
|
| 40 |
diamond_values_sum = 0
|
| 41 |
for idx, diamond in enumerate(diamonds):
|
| 42 |
rate = float(diamond_rates_dict.get(str(idx), 0))
|
| 43 |
diamond["Diamond Rate"] = rate
|
| 44 |
-
# Calculate Diamond Value using Diamond Weight and Rate
|
| 45 |
diamond_weight = float(diamond.get("Diamond Weight", 0))
|
| 46 |
diamond_value = round(diamond_weight * rate, 2)
|
| 47 |
diamond["Diamond Value"] = diamond_value
|
| 48 |
diamond_values_sum += diamond_value
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
metal_data.update({
|
| 51 |
"Finding For Gold 14KT": finding_14kt,
|
| 52 |
"Finding For Gold 18KT": finding_18kt,
|
| 53 |
-
"Finding for Plt.": finding_plt
|
|
|
|
|
|
|
| 54 |
})
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
usd_14k = (
|
| 59 |
metal_data["Gold Value 14K"] +
|
| 60 |
finding_14kt +
|
|
@@ -62,7 +112,7 @@ async def process_image(
|
|
| 62 |
metal_data["Labour 14K"] +
|
| 63 |
metal_data["Rhodium"]
|
| 64 |
)
|
| 65 |
-
usd_14k = math.ceil(usd_14k) + 1
|
| 66 |
|
| 67 |
usd_18k = (
|
| 68 |
metal_data["Gold Value 18K"] +
|
|
@@ -71,7 +121,7 @@ async def process_image(
|
|
| 71 |
metal_data["Labour 18K"] +
|
| 72 |
metal_data["Rhodium"]
|
| 73 |
)
|
| 74 |
-
usd_18k = math.ceil(usd_18k) + 1
|
| 75 |
|
| 76 |
usd_pt = (
|
| 77 |
metal_data["Plat Value"] +
|
|
@@ -80,28 +130,30 @@ async def process_image(
|
|
| 80 |
metal_data["Labour PT"] +
|
| 81 |
metal_data["Rhodium"]
|
| 82 |
)
|
| 83 |
-
usd_pt = math.ceil(usd_pt) + 1
|
| 84 |
|
| 85 |
-
# GBP conversions
|
| 86 |
gbp_14k = usd_14k * 0.78
|
| 87 |
gbp_18k = usd_18k * 0.78
|
| 88 |
gbp_pt = usd_pt * 0.78
|
| 89 |
|
| 90 |
-
|
| 91 |
-
duty_14k = gbp_14k * 1.025 # Adding 2.5% to GBP amount
|
| 92 |
duty_18k = gbp_18k * 1.025
|
| 93 |
duty_pt = gbp_pt * 1.025
|
| 94 |
|
| 95 |
-
# Freight constant
|
| 96 |
freight = 75
|
| 97 |
|
| 98 |
-
# Landing costs
|
| 99 |
landing_14k = duty_14k + freight
|
| 100 |
landing_18k = duty_18k + freight
|
| 101 |
landing_pt = duty_pt + freight
|
| 102 |
|
| 103 |
-
#
|
| 104 |
metal_data.update({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
"Price of USD 14K": f"$ {usd_14k:.2f}",
|
| 106 |
"Price of USD 18K": f"$ {usd_18k:.2f}",
|
| 107 |
"Price of USD PT": f"$ {usd_pt:.2f}",
|
|
@@ -111,7 +163,7 @@ async def process_image(
|
|
| 111 |
"14K Duty @ 2.5% Including": f"£ {duty_14k:.2f}",
|
| 112 |
"18K Duty @ 2.5% Including": f"£ {duty_18k:.2f}",
|
| 113 |
"PT950 Duty @ 2.5% Including": f"£ {duty_pt:.2f}",
|
| 114 |
-
"Freight": freight,
|
| 115 |
"14K Landing Cost (Duty + Freight)": f"£ {landing_14k:.2f}",
|
| 116 |
"18K Landing Cost (Duty + Freight)": f"£ {landing_18k:.2f}",
|
| 117 |
"PT950 Landing Cost (Duty + Freight)": f"£ {landing_pt:.2f}"
|
|
@@ -120,13 +172,15 @@ async def process_image(
|
|
| 120 |
if not diamonds:
|
| 121 |
final_df = pd.DataFrame([metal_data])
|
| 122 |
else:
|
| 123 |
-
|
| 124 |
-
|
|
|
|
| 125 |
|
|
|
|
| 126 |
metal_columns = [
|
| 127 |
"Customer Ref",
|
| 128 |
-
|
| 129 |
-
|
| 130 |
"Wt. in gram 14K",
|
| 131 |
"Wt. in gram 18K",
|
| 132 |
"Wt. in gram PLT",
|
|
@@ -140,7 +194,7 @@ async def process_image(
|
|
| 140 |
|
| 141 |
finding_columns = [
|
| 142 |
"Finding For Gold 14KT", "Finding For Gold 18KT", "Finding for Plt.",
|
| 143 |
-
"Rhodium", "Stamping"
|
| 144 |
]
|
| 145 |
|
| 146 |
labour_columns = [
|
|
@@ -156,6 +210,9 @@ async def process_image(
|
|
| 156 |
"PT950 Landing Cost (Duty + Freight)"
|
| 157 |
]
|
| 158 |
|
|
|
|
|
|
|
|
|
|
| 159 |
metal_data_basic = {k: v for k, v in metal_data.items()
|
| 160 |
if k in metal_columns}
|
| 161 |
|
|
@@ -185,24 +242,21 @@ async def process_image(
|
|
| 185 |
combined_prices = pd.concat([price_df, empty_prices], ignore_index=True)
|
| 186 |
|
| 187 |
final_df = pd.concat([
|
| 188 |
-
combined_metal,
|
| 189 |
-
diamonds_df,
|
| 190 |
-
combined_findings,
|
| 191 |
-
combined_labour,
|
| 192 |
-
combined_prices
|
| 193 |
], axis=1)
|
| 194 |
|
| 195 |
-
# Format currency values and fix pound sign encoding
|
| 196 |
for col in price_columns:
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
final_df[col] = final_df[col].apply(lambda x: x.replace('£', '£') if x else '')
|
| 203 |
|
| 204 |
output = BytesIO()
|
| 205 |
-
# Write CSV with UTF-8 encoding and BOM
|
| 206 |
final_df.to_csv(
|
| 207 |
output,
|
| 208 |
index=False,
|
|
@@ -221,4 +275,4 @@ async def process_image(
|
|
| 221 |
}
|
| 222 |
)
|
| 223 |
except Exception as e:
|
| 224 |
-
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
| 26 |
finding_14kt: float = Form(...),
|
| 27 |
finding_18kt: float = Form(...),
|
| 28 |
finding_plt: float = Form(...),
|
| 29 |
+
diamond_rates: str = Form(...),
|
| 30 |
+
gold_markup_type: str = Form(...),
|
| 31 |
+
gold_markup_value: float = Form(...),
|
| 32 |
+
platinum_markup_type: str = Form(...),
|
| 33 |
+
platinum_markup_value: float = Form(...),
|
| 34 |
+
diamond_markup_type: str = Form(...),
|
| 35 |
+
diamond_markup_value: float = Form(...),
|
| 36 |
+
finding_markup_type: str = Form(...),
|
| 37 |
+
finding_markup_value: float = Form(...)
|
| 38 |
):
|
| 39 |
try:
|
| 40 |
diamond_rates_dict = json.loads(diamond_rates)
|
|
|
|
| 44 |
metal_data = processed_data["Metal Details"]
|
| 45 |
diamonds = processed_data["Diamonds"]
|
| 46 |
|
| 47 |
+
# Calculate initial diamond values
|
| 48 |
diamond_values_sum = 0
|
| 49 |
for idx, diamond in enumerate(diamonds):
|
| 50 |
rate = float(diamond_rates_dict.get(str(idx), 0))
|
| 51 |
diamond["Diamond Rate"] = rate
|
|
|
|
| 52 |
diamond_weight = float(diamond.get("Diamond Weight", 0))
|
| 53 |
diamond_value = round(diamond_weight * rate, 2)
|
| 54 |
diamond["Diamond Value"] = diamond_value
|
| 55 |
diamond_values_sum += diamond_value
|
| 56 |
|
| 57 |
+
# Apply markup to finding values - before updating metal_data
|
| 58 |
+
if finding_markup_type == "percentage":
|
| 59 |
+
finding_markup_factor = 1 + (finding_markup_value / 100)
|
| 60 |
+
finding_14kt = round(finding_14kt * finding_markup_factor, 2)
|
| 61 |
+
finding_18kt = round(finding_18kt * finding_markup_factor, 2)
|
| 62 |
+
finding_plt = round(finding_plt * finding_markup_factor, 2)
|
| 63 |
+
elif finding_markup_type == "fixed":
|
| 64 |
+
finding_14kt = round(finding_14kt + finding_markup_value, 2)
|
| 65 |
+
finding_18kt = round(finding_18kt + finding_markup_value, 2)
|
| 66 |
+
finding_plt = round(finding_plt + finding_markup_value, 2)
|
| 67 |
+
|
| 68 |
metal_data.update({
|
| 69 |
"Finding For Gold 14KT": finding_14kt,
|
| 70 |
"Finding For Gold 18KT": finding_18kt,
|
| 71 |
+
"Finding for Plt.": finding_plt,
|
| 72 |
+
"Finding Markup Type": finding_markup_type,
|
| 73 |
+
"Finding Markup Value": finding_markup_value,
|
| 74 |
})
|
| 75 |
|
| 76 |
+
# Apply markup to gold values - only percentage or fixed
|
| 77 |
+
if gold_markup_type == "percentage":
|
| 78 |
+
gold_markup_factor = 1 + (gold_markup_value / 100)
|
| 79 |
+
metal_data["Gold Value 14K"] = round(metal_data["Gold Value 14K"] * gold_markup_factor, 2)
|
| 80 |
+
metal_data["Gold Value 18K"] = round(metal_data["Gold Value 18K"] * gold_markup_factor, 2)
|
| 81 |
+
elif gold_markup_type == "fixed":
|
| 82 |
+
# Ensure the markup value is properly applied
|
| 83 |
+
metal_data["Gold Value 14K"] = round(metal_data["Gold Value 14K"] + gold_markup_value, 2)
|
| 84 |
+
metal_data["Gold Value 18K"] = round(metal_data["Gold Value 18K"] + gold_markup_value, 2)
|
| 85 |
+
|
| 86 |
+
# Apply markup to platinum values - only percentage or fixed
|
| 87 |
+
if platinum_markup_type == "percentage":
|
| 88 |
+
platinum_markup_factor = 1 + (platinum_markup_value / 100)
|
| 89 |
+
metal_data["Plat Value"] = round(metal_data["Plat Value"] * platinum_markup_factor, 2)
|
| 90 |
+
elif platinum_markup_type == "fixed":
|
| 91 |
+
# Ensure the markup value is properly applied
|
| 92 |
+
metal_data["Plat Value"] = round(metal_data["Plat Value"] + platinum_markup_value, 2)
|
| 93 |
+
|
| 94 |
+
# Apply markup to diamond values - only percentage or fixed
|
| 95 |
+
if diamond_markup_type == "percentage":
|
| 96 |
+
diamond_markup_factor = 1 + (diamond_markup_value / 100)
|
| 97 |
+
for diamond in diamonds:
|
| 98 |
+
diamond["Diamond Value"] = round(diamond["Diamond Value"] * diamond_markup_factor, 2)
|
| 99 |
+
elif diamond_markup_type == "fixed":
|
| 100 |
+
# Ensure the markup value is properly applied for each diamond
|
| 101 |
+
for diamond in diamonds:
|
| 102 |
+
diamond["Diamond Value"] = round(diamond["Diamond Value"] + diamond_markup_value, 2)
|
| 103 |
+
|
| 104 |
+
# Recalculate diamond_values_sum after applying markup
|
| 105 |
+
diamond_values_sum = sum(diamond["Diamond Value"] for diamond in diamonds)
|
| 106 |
+
|
| 107 |
+
# Calculate prices with the marked-up values
|
| 108 |
usd_14k = (
|
| 109 |
metal_data["Gold Value 14K"] +
|
| 110 |
finding_14kt +
|
|
|
|
| 112 |
metal_data["Labour 14K"] +
|
| 113 |
metal_data["Rhodium"]
|
| 114 |
)
|
| 115 |
+
usd_14k = math.ceil(usd_14k) + 1
|
| 116 |
|
| 117 |
usd_18k = (
|
| 118 |
metal_data["Gold Value 18K"] +
|
|
|
|
| 121 |
metal_data["Labour 18K"] +
|
| 122 |
metal_data["Rhodium"]
|
| 123 |
)
|
| 124 |
+
usd_18k = math.ceil(usd_18k) + 1
|
| 125 |
|
| 126 |
usd_pt = (
|
| 127 |
metal_data["Plat Value"] +
|
|
|
|
| 130 |
metal_data["Labour PT"] +
|
| 131 |
metal_data["Rhodium"]
|
| 132 |
)
|
| 133 |
+
usd_pt = math.ceil(usd_pt) + 1
|
| 134 |
|
|
|
|
| 135 |
gbp_14k = usd_14k * 0.78
|
| 136 |
gbp_18k = usd_18k * 0.78
|
| 137 |
gbp_pt = usd_pt * 0.78
|
| 138 |
|
| 139 |
+
duty_14k = gbp_14k * 1.025
|
|
|
|
| 140 |
duty_18k = gbp_18k * 1.025
|
| 141 |
duty_pt = gbp_pt * 1.025
|
| 142 |
|
|
|
|
| 143 |
freight = 75
|
| 144 |
|
|
|
|
| 145 |
landing_14k = duty_14k + freight
|
| 146 |
landing_18k = duty_18k + freight
|
| 147 |
landing_pt = duty_pt + freight
|
| 148 |
|
| 149 |
+
# Add markup information to metal_data for reference
|
| 150 |
metal_data.update({
|
| 151 |
+
"Gold Markup Type": gold_markup_type,
|
| 152 |
+
"Gold Markup Value": gold_markup_value,
|
| 153 |
+
"Platinum Markup Type": platinum_markup_type,
|
| 154 |
+
"Platinum Markup Value": platinum_markup_value,
|
| 155 |
+
"Diamond Markup Type": diamond_markup_type,
|
| 156 |
+
"Diamond Markup Value": diamond_markup_value,
|
| 157 |
"Price of USD 14K": f"$ {usd_14k:.2f}",
|
| 158 |
"Price of USD 18K": f"$ {usd_18k:.2f}",
|
| 159 |
"Price of USD PT": f"$ {usd_pt:.2f}",
|
|
|
|
| 163 |
"14K Duty @ 2.5% Including": f"£ {duty_14k:.2f}",
|
| 164 |
"18K Duty @ 2.5% Including": f"£ {duty_18k:.2f}",
|
| 165 |
"PT950 Duty @ 2.5% Including": f"£ {duty_pt:.2f}",
|
| 166 |
+
"Freight": f"£ {freight:.2f}",
|
| 167 |
"14K Landing Cost (Duty + Freight)": f"£ {landing_14k:.2f}",
|
| 168 |
"18K Landing Cost (Duty + Freight)": f"£ {landing_18k:.2f}",
|
| 169 |
"PT950 Landing Cost (Duty + Freight)": f"£ {landing_pt:.2f}"
|
|
|
|
| 172 |
if not diamonds:
|
| 173 |
final_df = pd.DataFrame([metal_data])
|
| 174 |
else:
|
| 175 |
+
# Find the gold lock and platinum lock columns dynamically
|
| 176 |
+
gold_lock_col = next((col for col in metal_data.keys() if "Gold Lock @" in col), None)
|
| 177 |
+
platinum_lock_col = next((col for col in metal_data.keys() if "Platinum Lock" in col), None)
|
| 178 |
|
| 179 |
+
# Make sure these columns are included in metal_columns
|
| 180 |
metal_columns = [
|
| 181 |
"Customer Ref",
|
| 182 |
+
gold_lock_col, # Include the gold lock column
|
| 183 |
+
platinum_lock_col, # Include the platinum lock column
|
| 184 |
"Wt. in gram 14K",
|
| 185 |
"Wt. in gram 18K",
|
| 186 |
"Wt. in gram PLT",
|
|
|
|
| 194 |
|
| 195 |
finding_columns = [
|
| 196 |
"Finding For Gold 14KT", "Finding For Gold 18KT", "Finding for Plt.",
|
| 197 |
+
"Rhodium", "Stamping"
|
| 198 |
]
|
| 199 |
|
| 200 |
labour_columns = [
|
|
|
|
| 210 |
"PT950 Landing Cost (Duty + Freight)"
|
| 211 |
]
|
| 212 |
|
| 213 |
+
# Filter out any None values from columns
|
| 214 |
+
metal_columns = [col for col in metal_columns if col is not None]
|
| 215 |
+
|
| 216 |
metal_data_basic = {k: v for k, v in metal_data.items()
|
| 217 |
if k in metal_columns}
|
| 218 |
|
|
|
|
| 242 |
combined_prices = pd.concat([price_df, empty_prices], ignore_index=True)
|
| 243 |
|
| 244 |
final_df = pd.concat([
|
| 245 |
+
combined_metal,
|
| 246 |
+
diamonds_df,
|
| 247 |
+
combined_findings,
|
| 248 |
+
combined_labour,
|
| 249 |
+
combined_prices
|
| 250 |
], axis=1)
|
| 251 |
|
|
|
|
| 252 |
for col in price_columns:
|
| 253 |
+
if col in final_df.columns: # Check if column exists
|
| 254 |
+
final_df[col] = final_df[col].astype(str)
|
| 255 |
+
final_df[col] = final_df[col].apply(lambda x: x.strip('"').strip("'") if x else '')
|
| 256 |
+
if "GBP" in col:
|
| 257 |
+
final_df[col] = final_df[col].apply(lambda x: x.replace('£', '£') if x else '')
|
|
|
|
| 258 |
|
| 259 |
output = BytesIO()
|
|
|
|
| 260 |
final_df.to_csv(
|
| 261 |
output,
|
| 262 |
index=False,
|
|
|
|
| 275 |
}
|
| 276 |
)
|
| 277 |
except Exception as e:
|
| 278 |
+
raise HTTPException(status_code=400, detail=str(e))
|