utkarsh134 commited on
Commit
f8e2c68
·
verified ·
1 Parent(s): 6ec1187

Update parsing.py

Browse files
Files changed (1) hide show
  1. parsing.py +10 -17
parsing.py CHANGED
@@ -22,7 +22,6 @@ def get_llm():
22
 
23
  def extract_json_from_text(text):
24
  """Extract JSON from text that might contain explanation text or code blocks."""
25
- # Look for text between curly braces
26
  json_pattern = r'\{.*\}'
27
  match = re.search(json_pattern, text, re.DOTALL)
28
 
@@ -33,13 +32,11 @@ def extract_json_from_text(text):
33
  except json.JSONDecodeError:
34
  pass
35
 
36
- # Try to find JSON in markdown code blocks
37
  code_block_pattern = r'```(?:json)?\s*([\s\S]*?)\s*```'
38
  matches = re.findall(code_block_pattern, text, re.DOTALL)
39
 
40
  for potential_json in matches:
41
  try:
42
- # Remove any triple backticks that might be included
43
  cleaned = potential_json.strip().replace('```', '')
44
  if cleaned.startswith('{') and cleaned.endswith('}'):
45
  return json.loads(cleaned)
@@ -66,6 +63,7 @@ def parse_data_with_llm(extracted_text):
66
  - **Wt. in gram 14K**: Extract all "14K WG" weight values of parts and return as a list.
67
  - **Wt. in gram 18K**: Extract all "18K WG" weight values of parts and return as a list.
68
  - **Wt. in gram PLT**: Extract all "PLT" weight values of parts and return as a list.
 
69
  - **Diamonds**: Extract each diamond entry as an object with "Dia Qlty"(is the name under the shape column under diamond details), "MM Size", "Dia Qty", and "Dia Wt.".
70
 
71
  **Output STRICTLY as JSON:**
@@ -75,6 +73,7 @@ def parse_data_with_llm(extracted_text):
75
  "Wt. in gram 14K": ...,
76
  "Wt. in gram 18K": ...,
77
  "Wt. in gram PLT": ...,
 
78
  "Diamonds": [
79
  {{
80
  "Dia Qlty": "...",
@@ -92,7 +91,6 @@ def parse_data_with_llm(extracted_text):
92
  prompt = prompt_template.format(extracted_data=extracted_text)
93
  response = llm.invoke(prompt)
94
 
95
- # Extract valid JSON from response
96
  response_text = response.content.strip() if hasattr(response, 'content') else str(response).strip()
97
  json_data = extract_json_from_text(response_text)
98
 
@@ -108,20 +106,20 @@ def parse_data_with_llm(extracted_text):
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)),
@@ -138,28 +136,24 @@ def compute_values(data):
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
146
  diamonds = data.get("Diamonds", [])
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
 
@@ -196,7 +190,6 @@ def fetch_metal_prices():
196
  response.raise_for_status()
197
  data = response.json()
198
 
199
- # Extract goldPM and platinumPM from the most recent result (index 0)
200
  results = data["data"]["GetLondonFixByYearV3"]["results"]
201
  if results and len(results) > 0:
202
  gold_price = float(results[0]["goldPM"])
 
22
 
23
  def extract_json_from_text(text):
24
  """Extract JSON from text that might contain explanation text or code blocks."""
 
25
  json_pattern = r'\{.*\}'
26
  match = re.search(json_pattern, text, re.DOTALL)
27
 
 
32
  except json.JSONDecodeError:
33
  pass
34
 
 
35
  code_block_pattern = r'```(?:json)?\s*([\s\S]*?)\s*```'
36
  matches = re.findall(code_block_pattern, text, re.DOTALL)
37
 
38
  for potential_json in matches:
39
  try:
 
40
  cleaned = potential_json.strip().replace('```', '')
41
  if cleaned.startswith('{') and cleaned.endswith('}'):
42
  return json.loads(cleaned)
 
63
  - **Wt. in gram 14K**: Extract all "14K WG" weight values of parts and return as a list.
64
  - **Wt. in gram 18K**: Extract all "18K WG" weight values of parts and return as a list.
65
  - **Wt. in gram PLT**: Extract all "PLT" weight values of parts and return as a list.
66
+ - **Rhodium**: Look for any mentions of "Rhodium" or "RH" and extract the associated numeric value. If not found, return 0.
67
  - **Diamonds**: Extract each diamond entry as an object with "Dia Qlty"(is the name under the shape column under diamond details), "MM Size", "Dia Qty", and "Dia Wt.".
68
 
69
  **Output STRICTLY as JSON:**
 
73
  "Wt. in gram 14K": ...,
74
  "Wt. in gram 18K": ...,
75
  "Wt. in gram PLT": ...,
76
+ "Rhodium": ...,
77
  "Diamonds": [
78
  {{
79
  "Dia Qlty": "...",
 
91
  prompt = prompt_template.format(extracted_data=extracted_text)
92
  response = llm.invoke(prompt)
93
 
 
94
  response_text = response.content.strip() if hasattr(response, 'content') else str(response).strip()
95
  json_data = extract_json_from_text(response_text)
96
 
 
106
 
107
  def compute_values(data):
108
  """Perform metal calculations and format diamonds separately."""
 
109
  gold_price, platinum_price = fetch_metal_prices()
110
 
 
111
  gold_lock = gold_price if gold_price > 0 else 2800
112
  platinum_lock = platinum_price if platinum_price > 0 else 1040
113
 
 
114
  gold_lock_column = f"Gold Lock @ $ {gold_lock:.2f}"
115
  platinum_lock_column = f"Platinum Lock $ {platinum_lock:.2f}"
116
 
117
+ # Extract rhodium value from LLM data
118
+ rhodium_value = float(data.get("Rhodium", 0))
119
+
120
  metal_data = {
121
  "Customer Ref": data.get("Customer Ref", ""),
122
+ gold_lock_column: gold_lock,
123
  platinum_lock_column: platinum_lock,
124
  "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)),
125
  "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)),
 
136
  "Finding For Gold 14KT": 0,
137
  "Finding For Gold 18KT": 0,
138
  "Finding for Plt.": 0,
139
+ "Rhodium": rhodium_value, # Use the extracted rhodium value
140
  "Stamping": 1
141
  }
142
 
 
143
  diamonds = data.get("Diamonds", [])
144
  for diamond in diamonds:
145
  if "Dia Qty" in diamond and "Dia Wt." in diamond:
146
  try:
 
147
  qty = float(diamond["Dia Qty"])
148
  wt = float(diamond["Dia Wt."])
149
+ diamond["Diamond Weight"] = round(qty * wt, 3)
150
+ diamond["Diamond Rate"] = 0
151
+ diamond["Diamond Value"] = 0
152
  except (ValueError, TypeError):
153
  diamond["Diamond Weight"] = 0
154
  diamond["Diamond Rate"] = 0
155
  diamond["Diamond Value"] = 0
156
 
 
 
157
 
158
  return metal_data, diamonds
159
 
 
190
  response.raise_for_status()
191
  data = response.json()
192
 
 
193
  results = data["data"]["GetLondonFixByYearV3"]["results"]
194
  if results and len(results) > 0:
195
  gold_price = float(results[0]["goldPM"])