Spaces:
Sleeping
Sleeping
Update parsing.py
Browse files- parsing.py +41 -53
parsing.py
CHANGED
|
@@ -5,6 +5,7 @@ import json
|
|
| 5 |
import re
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import requests
|
|
|
|
| 8 |
from datetime import datetime
|
| 9 |
load_dotenv()
|
| 10 |
|
|
@@ -107,61 +108,38 @@ def parse_data_with_llm(extracted_text):
|
|
| 107 |
|
| 108 |
def compute_values(data):
|
| 109 |
"""Perform metal calculations and format diamonds separately."""
|
| 110 |
-
# Fetch current prices
|
| 111 |
gold_price, platinum_price = fetch_metal_prices()
|
| 112 |
|
| 113 |
-
#
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
gbp_pt = price_pt * (1/0.78)
|
| 122 |
-
|
| 123 |
-
# Rest of the existing calculations
|
| 124 |
-
wt_14k = sum(float(x) for x in data.get("Wt. in gram 14K", [])) if isinstance(data.get("Wt. in gram 14K"), list) else float(data.get("Wt. in gram 14K", 0))
|
| 125 |
-
wt_18k = sum(float(x) for x in data.get("Wt. in gram 18K", [])) if isinstance(data.get("Wt. in gram 18K"), list) else float(data.get("Wt. in gram 18K", 0))
|
| 126 |
-
wt_plt = sum(float(x) for x in data.get("Wt. in gram PLT", [])) if isinstance(data.get("Wt. in gram PLT"), list) else float(data.get("Wt. in gram PLT", 0))
|
| 127 |
-
|
| 128 |
-
# Calculate rates
|
| 129 |
-
gold_rate_14k = round((2800 / 31.1035) * 0.585 * 1.1, 2)
|
| 130 |
-
gold_rate_18k = round((2800 / 31.1035) * 0.75 * 1.1, 2)
|
| 131 |
-
plat_rate = round((1040 / 31.1035) * 0.95 * 1.2, 2)
|
| 132 |
-
|
| 133 |
-
# Calculate values with rounding
|
| 134 |
-
gold_value_14k = round(wt_14k * gold_rate_14k, 2)
|
| 135 |
-
gold_value_18k = round(wt_18k * gold_rate_18k, 2)
|
| 136 |
-
plat_value = round(wt_plt * plat_rate, 2)
|
| 137 |
-
|
| 138 |
-
# Calculate labor costs
|
| 139 |
-
labour_14k = round(12 if (wt_14k * 7) < 12 else (wt_14k * 7), 2)
|
| 140 |
-
labour_18k = round(12 if (wt_18k * 7) < 12 else (wt_18k * 7), 2)
|
| 141 |
-
labour_pt = round(15 if (wt_plt * 11) < 15 else (wt_plt * 11), 2)
|
| 142 |
-
|
| 143 |
metal_data = {
|
| 144 |
"Customer Ref": data.get("Customer Ref", ""),
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
"Wt. in gram 14K":
|
| 148 |
-
"Wt. in gram 18K":
|
| 149 |
-
"Wt. in gram PLT":
|
| 150 |
-
"Gold Rate (including loss) 14KT":
|
| 151 |
-
"Gold Rate (including loss) 18KT":
|
| 152 |
-
"Plat rate (including Lab)":
|
| 153 |
-
"Gold Value 14K":
|
| 154 |
-
"Gold Value 18K":
|
| 155 |
-
"Plat Value":
|
| 156 |
-
"Labour 14K":
|
| 157 |
-
"Labour 18K":
|
| 158 |
-
"Labour PT":
|
| 159 |
-
"
|
| 160 |
-
"
|
| 161 |
-
"
|
| 162 |
-
"
|
| 163 |
-
"
|
| 164 |
-
"Price of GBP PT @ 1/0.78": f"£ {gbp_pt:.2f}"
|
| 165 |
}
|
| 166 |
|
| 167 |
# Process diamonds data
|
|
@@ -169,9 +147,19 @@ def compute_values(data):
|
|
| 169 |
for diamond in diamonds:
|
| 170 |
if "Dia Qty" in diamond and "Dia Wt." in diamond:
|
| 171 |
try:
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
except (ValueError, TypeError):
|
| 174 |
diamond["Diamond Weight"] = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
return metal_data, diamonds
|
| 177 |
|
|
@@ -219,4 +207,4 @@ def fetch_metal_prices():
|
|
| 219 |
return gold_price, platinum_price
|
| 220 |
except (requests.exceptions.RequestException, KeyError, ValueError) as e:
|
| 221 |
print(f"Error fetching metal prices: {e}")
|
| 222 |
-
return 0, 0
|
|
|
|
| 5 |
import re
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import requests
|
| 8 |
+
import math
|
| 9 |
from datetime import datetime
|
| 10 |
load_dotenv()
|
| 11 |
|
|
|
|
| 108 |
|
| 109 |
def compute_values(data):
|
| 110 |
"""Perform metal calculations and format diamonds separately."""
|
| 111 |
+
# Fetch current prices at the start
|
| 112 |
gold_price, platinum_price = fetch_metal_prices()
|
| 113 |
|
| 114 |
+
# Use fetched prices with fallback values
|
| 115 |
+
gold_lock = gold_price if gold_price > 0 else 2800
|
| 116 |
+
platinum_lock = platinum_price if platinum_price > 0 else 1040
|
| 117 |
+
|
| 118 |
+
# Create the column names with current prices
|
| 119 |
+
gold_lock_column = f"Gold Lock @ $ {gold_lock:.2f}"
|
| 120 |
+
platinum_lock_column = f"Platinum Lock $ {platinum_lock:.2f}"
|
| 121 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
metal_data = {
|
| 123 |
"Customer Ref": data.get("Customer Ref", ""),
|
| 124 |
+
gold_lock_column: gold_lock, # Use the dynamic column names
|
| 125 |
+
platinum_lock_column: platinum_lock,
|
| 126 |
+
"Wt. in gram 14K": sum(float(x) for x in data.get("Wt. in gram 14K", [])) if isinstance(data.get("Wt. in gram 14K"), list) else float(data.get("Wt. in gram 14K", 0)),
|
| 127 |
+
"Wt. in gram 18K": sum(float(x) for x in data.get("Wt. in gram 18K", [])) if isinstance(data.get("Wt. in gram 18K"), list) else float(data.get("Wt. in gram 18K", 0)),
|
| 128 |
+
"Wt. in gram PLT": sum(float(x) for x in data.get("Wt. in gram PLT", [])) if isinstance(data.get("Wt. in gram PLT"), list) else float(data.get("Wt. in gram PLT", 0)),
|
| 129 |
+
"Gold Rate (including loss) 14KT": round((gold_lock / 31.1035) * 0.585 * 1.1, 2),
|
| 130 |
+
"Gold Rate (including loss) 18KT": round((gold_lock / 31.1035) * 0.75 * 1.1, 2),
|
| 131 |
+
"Plat rate (including Lab)": round((platinum_lock / 31.1035) * 0.95 * 1.2, 2),
|
| 132 |
+
"Gold Value 14K": round(sum(float(x) for x in data.get("Wt. in gram 14K", [])) * round((gold_lock / 31.1035) * 0.585 * 1.1, 2), 2),
|
| 133 |
+
"Gold Value 18K": round(sum(float(x) for x in data.get("Wt. in gram 18K", [])) * round((gold_lock / 31.1035) * 0.75 * 1.1, 2), 2),
|
| 134 |
+
"Plat Value": round(sum(float(x) for x in data.get("Wt. in gram PLT", [])) * round((platinum_lock / 31.1035) * 0.95 * 1.2, 2), 2),
|
| 135 |
+
"Labour 14K": round(12 if (sum(float(x) for x in data.get("Wt. in gram 14K", [])) * 7) < 12 else (sum(float(x) for x in data.get("Wt. in gram 14K", [])) * 7), 2),
|
| 136 |
+
"Labour 18K": round(12 if (sum(float(x) for x in data.get("Wt. in gram 18K", [])) * 7) < 12 else (sum(float(x) for x in data.get("Wt. in gram 18K", [])) * 7), 2),
|
| 137 |
+
"Labour PT": round(15 if (sum(float(x) for x in data.get("Wt. in gram PLT", [])) * 11) < 15 else (sum(float(x) for x in data.get("Wt. in gram PLT", [])) * 11), 2),
|
| 138 |
+
"Finding For Gold 14KT": 0,
|
| 139 |
+
"Finding For Gold 18KT": 0,
|
| 140 |
+
"Finding for Plt.": 0,
|
| 141 |
+
"Rhodium": 0,
|
| 142 |
+
"Stamping": 1
|
|
|
|
| 143 |
}
|
| 144 |
|
| 145 |
# Process diamonds data
|
|
|
|
| 147 |
for diamond in diamonds:
|
| 148 |
if "Dia Qty" in diamond and "Dia Wt." in diamond:
|
| 149 |
try:
|
| 150 |
+
# Convert string values to float and handle potential errors
|
| 151 |
+
qty = float(diamond["Dia Qty"])
|
| 152 |
+
wt = float(diamond["Dia Wt."])
|
| 153 |
+
diamond["Diamond Weight"] = round(qty * wt, 3) # Round to 3 decimal places
|
| 154 |
+
diamond["Diamond Rate"] = 0 # This will be updated later with user input
|
| 155 |
+
diamond["Diamond Value"] = 0 # This will be calculated after rate is provided
|
| 156 |
except (ValueError, TypeError):
|
| 157 |
diamond["Diamond Weight"] = 0
|
| 158 |
+
diamond["Diamond Rate"] = 0
|
| 159 |
+
diamond["Diamond Value"] = 0
|
| 160 |
+
|
| 161 |
+
# Remove all price calculations from here as they'll be done in main.py
|
| 162 |
+
# after diamond rates are applied
|
| 163 |
|
| 164 |
return metal_data, diamonds
|
| 165 |
|
|
|
|
| 207 |
return gold_price, platinum_price
|
| 208 |
except (requests.exceptions.RequestException, KeyError, ValueError) as e:
|
| 209 |
print(f"Error fetching metal prices: {e}")
|
| 210 |
+
return 0, 0
|