ChintanSatva commited on
Commit
78317c1
·
verified ·
1 Parent(s): c547593

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -1
app.py CHANGED
@@ -36,6 +36,46 @@ def custom_encoder(obj: Any) -> Any:
36
  # For any other type, fall back to the default encoder
37
  return jsonable_encoder(obj)
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  # --- END OF MODIFICATIONS ---
40
 
41
 
@@ -220,11 +260,14 @@ Output JSON:
220
  # This converts numbers like 0.0000009 into a high-precision Decimal
221
  # object inside Python, instead of a standard float.
222
  structured_data = json.loads(json_str, parse_float=Decimal)
 
 
 
223
  # --- END OF MODIFICATIONS ---
224
 
225
  structured_data_cache[text_hash] = structured_data
226
  logger.info(f"Gemini processing for {filename}, took {time.time() - start_time:.2f} seconds, {log_memory_usage()}")
227
- # This will now print Decimal('0.0000009') in your console, which is correct
228
  logger.info(f"Structured Data: {structured_data}")
229
  return structured_data
230
  except Exception as e:
 
36
  # For any other type, fall back to the default encoder
37
  return jsonable_encoder(obj)
38
 
39
+ def convert_scientific_decimals(obj):
40
+ """
41
+ Recursively convert any Decimal objects in scientific notation to decimal notation.
42
+ This handles cases where json.loads(parse_float=Decimal) creates Decimal('9E-7')
43
+ instead of the desired Decimal('0.0000009').
44
+ """
45
+ if isinstance(obj, dict):
46
+ return {k: convert_scientific_decimals(v) for k, v in obj.items()}
47
+ elif isinstance(obj, list):
48
+ return [convert_scientific_decimals(item) for item in obj]
49
+ elif isinstance(obj, Decimal):
50
+ # Check if the decimal is in scientific notation by converting to string
51
+ decimal_str = str(obj)
52
+ if 'E' in decimal_str or 'e' in decimal_str:
53
+ # Convert to float first to get the actual numeric value, then back to Decimal
54
+ # This forces the decimal representation
55
+ float_val = float(obj)
56
+ # Use high precision string formatting to avoid float precision issues
57
+ if abs(float_val) < 1e-10: # Very small numbers
58
+ formatted = f"{float_val:.15f}"
59
+ elif abs(float_val) < 1e-6: # Small numbers
60
+ formatted = f"{float_val:.12f}"
61
+ elif abs(float_val) < 1: # Numbers less than 1
62
+ formatted = f"{float_val:.10f}"
63
+ else: # Larger numbers
64
+ formatted = f"{float_val:.8f}"
65
+
66
+ # Remove trailing zeros and decimal point if not needed
67
+ formatted = formatted.rstrip('0').rstrip('.')
68
+ if '.' not in formatted and float_val != int(float_val):
69
+ # If we removed all decimal places but it's not actually an integer
70
+ # This handles edge cases
71
+ formatted = f"{float_val:.1f}".rstrip('0').rstrip('.')
72
+
73
+ return Decimal(formatted)
74
+ else:
75
+ return obj
76
+ else:
77
+ return obj
78
+
79
  # --- END OF MODIFICATIONS ---
80
 
81
 
 
260
  # This converts numbers like 0.0000009 into a high-precision Decimal
261
  # object inside Python, instead of a standard float.
262
  structured_data = json.loads(json_str, parse_float=Decimal)
263
+
264
+ # CRITICAL: Convert any scientific notation Decimals to decimal format
265
+ structured_data = convert_scientific_decimals(structured_data)
266
  # --- END OF MODIFICATIONS ---
267
 
268
  structured_data_cache[text_hash] = structured_data
269
  logger.info(f"Gemini processing for {filename}, took {time.time() - start_time:.2f} seconds, {log_memory_usage()}")
270
+ # This will now print proper decimal format like Decimal('0.0000009') in your console
271
  logger.info(f"Structured Data: {structured_data}")
272
  return structured_data
273
  except Exception as e: