Spaces:
Sleeping
Sleeping
Update parsing.py
Browse files- parsing.py +48 -31
parsing.py
CHANGED
|
@@ -5,7 +5,7 @@ import json
|
|
| 5 |
import re
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import requests
|
| 8 |
-
import
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
def get_llm():
|
|
@@ -115,23 +115,35 @@ def compute_values(data):
|
|
| 115 |
price_18k = (18 / 24) * gold_price
|
| 116 |
price_pt = platinum_price
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
# Rest of the existing calculations
|
| 119 |
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))
|
| 120 |
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))
|
| 121 |
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))
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
| 130 |
|
| 131 |
metal_data = {
|
| 132 |
"Customer Ref": data.get("Customer Ref", ""),
|
| 133 |
-
"Gold Lock @ $ 2800":
|
| 134 |
-
"Platinum Lock $ 1040":
|
| 135 |
"Wt. in gram 14K": wt_14k,
|
| 136 |
"Wt. in gram 18K": wt_18k,
|
| 137 |
"Wt. in gram PLT": wt_plt,
|
|
@@ -141,9 +153,15 @@ def compute_values(data):
|
|
| 141 |
"Gold Value 14K": gold_value_14k,
|
| 142 |
"Gold Value 18K": gold_value_18k,
|
| 143 |
"Plat Value": plat_value,
|
| 144 |
-
"
|
| 145 |
-
"
|
| 146 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
}
|
| 148 |
|
| 149 |
# Process diamonds data
|
|
@@ -164,26 +182,23 @@ def process_extracted_text(extracted_text):
|
|
| 164 |
return {"Metal Details": metal_data, "Diamonds": diamonds}
|
| 165 |
|
| 166 |
def fetch_metal_prices():
|
| 167 |
-
"""Fetch current gold and platinum prices from Kitco API."""
|
| 168 |
url = "https://kdb-gw.prod.kitco.com/"
|
| 169 |
-
|
| 170 |
-
|
| 171 |
payload = {
|
| 172 |
-
"query": """fragment
|
| 173 |
-
ID
|
| 174 |
-
}
|
| 175 |
-
fragment MetalQuoteFragment on Quote{
|
| 176 |
-
ID ask bid change changePercentage close high low mid open originalTime timestamp unit
|
| 177 |
}
|
| 178 |
-
query
|
| 179 |
-
|
| 180 |
-
|
|
|
|
| 181 |
}""",
|
| 182 |
"variables": {
|
| 183 |
-
"
|
| 184 |
"currency": "USD"
|
| 185 |
},
|
| 186 |
-
"operationName": "
|
| 187 |
}
|
| 188 |
|
| 189 |
headers = {"Content-Type": "application/json"}
|
|
@@ -193,13 +208,15 @@ def fetch_metal_prices():
|
|
| 193 |
response.raise_for_status()
|
| 194 |
data = response.json()
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
return gold_price, platinum_price
|
| 200 |
except (requests.exceptions.RequestException, KeyError, ValueError) as e:
|
| 201 |
print(f"Error fetching metal prices: {e}")
|
| 202 |
-
return 0, 0
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
| 5 |
import re
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import requests
|
| 8 |
+
from datetime import datetime
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
def get_llm():
|
|
|
|
| 115 |
price_18k = (18 / 24) * gold_price
|
| 116 |
price_pt = platinum_price
|
| 117 |
|
| 118 |
+
# Calculate GBP prices (USD price * (1/0.78))
|
| 119 |
+
gbp_14k = price_14k * (1/0.78)
|
| 120 |
+
gbp_18k = price_18k * (1/0.78)
|
| 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 |
+
"Gold Lock @ $ 2800": 2800,
|
| 146 |
+
"Platinum Lock $ 1040": 1040,
|
| 147 |
"Wt. in gram 14K": wt_14k,
|
| 148 |
"Wt. in gram 18K": wt_18k,
|
| 149 |
"Wt. in gram PLT": wt_plt,
|
|
|
|
| 153 |
"Gold Value 14K": gold_value_14k,
|
| 154 |
"Gold Value 18K": gold_value_18k,
|
| 155 |
"Plat Value": plat_value,
|
| 156 |
+
"Labour 14K": labour_14k,
|
| 157 |
+
"Labour 18K": labour_18k,
|
| 158 |
+
"Labour PT": labour_pt,
|
| 159 |
+
"Price of USD 14K": f"$ {price_14k:.2f}",
|
| 160 |
+
"Price of USD 18K": f"$ {price_18k:.2f}",
|
| 161 |
+
"Price of USD PT": f"$ {price_pt:.2f}",
|
| 162 |
+
"Price of GBP 14K @ 1/0.78": f"£ {gbp_14k:.2f}",
|
| 163 |
+
"Price of GBP 18K @ 1/0.78": f"£ {gbp_18k:.2f}",
|
| 164 |
+
"Price of GBP PT @ 1/0.78": f"£ {gbp_pt:.2f}"
|
| 165 |
}
|
| 166 |
|
| 167 |
# Process diamonds data
|
|
|
|
| 182 |
return {"Metal Details": metal_data, "Diamonds": diamonds}
|
| 183 |
|
| 184 |
def fetch_metal_prices():
|
| 185 |
+
"""Fetch current gold and platinum prices from Kitco London Fix API."""
|
| 186 |
url = "https://kdb-gw.prod.kitco.com/"
|
| 187 |
+
current_year = str(datetime.now().year)
|
|
|
|
| 188 |
payload = {
|
| 189 |
+
"query": """fragment LondonQuoteFragment on LondonQuote{
|
| 190 |
+
ID goldAM goldPM timestamp silver platinumAM platinumPM palladiumAM palladiumPM
|
|
|
|
|
|
|
|
|
|
| 191 |
}
|
| 192 |
+
query LondonFixByYear($year:String!,$currency:String!){
|
| 193 |
+
GetLondonFixByYearV3(year:$year,currency:$currency){
|
| 194 |
+
ID currency results{...LondonQuoteFragment}
|
| 195 |
+
}
|
| 196 |
}""",
|
| 197 |
"variables": {
|
| 198 |
+
"year": current_year,
|
| 199 |
"currency": "USD"
|
| 200 |
},
|
| 201 |
+
"operationName": "LondonFixByYear"
|
| 202 |
}
|
| 203 |
|
| 204 |
headers = {"Content-Type": "application/json"}
|
|
|
|
| 208 |
response.raise_for_status()
|
| 209 |
data = response.json()
|
| 210 |
|
| 211 |
+
# Extract goldPM and platinumPM from the most recent result (index 0)
|
| 212 |
+
results = data["data"]["GetLondonFixByYearV3"]["results"]
|
| 213 |
+
if results and len(results) > 0:
|
| 214 |
+
gold_price = float(results[0]["goldPM"])
|
| 215 |
+
platinum_price = float(results[0]["platinumPM"])
|
| 216 |
+
else:
|
| 217 |
+
raise KeyError("No results found in the response")
|
| 218 |
|
| 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
|
|
|
|
|
|
|
|
|