Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -86,25 +86,55 @@ def fix_scientific_notation_in_json(json_str):
|
|
| 86 |
float_val = float(scientific_num)
|
| 87 |
|
| 88 |
if float_val == 0:
|
| 89 |
-
return "0"
|
| 90 |
elif 0 < abs(float_val) < 1e-10:
|
| 91 |
-
return f"{float_val:.20f}".rstrip('0').rstrip('.')
|
| 92 |
elif 0 < abs(float_val) < 1e-6:
|
| 93 |
-
return f"{float_val:.15f}".rstrip('0').rstrip('.')
|
| 94 |
elif abs(float_val) < 1:
|
| 95 |
-
return f"{float_val:.10f}".rstrip('0').rstrip('.')
|
| 96 |
else:
|
| 97 |
-
return f"{float_val:.8f}".rstrip('0').rstrip('.')
|
| 98 |
-
except:
|
|
|
|
| 99 |
return match.group(0) # Return original if conversion fails
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
return
|
| 108 |
|
| 109 |
def convert_scientific_decimals(obj):
|
| 110 |
"""
|
|
@@ -358,13 +388,17 @@ Output JSON:
|
|
| 358 |
"""
|
| 359 |
response = model.generate_content(prompt)
|
| 360 |
llm_output = response.text
|
|
|
|
|
|
|
| 361 |
json_start = llm_output.find("{")
|
| 362 |
json_end = llm_output.rfind("}") + 1
|
| 363 |
json_str = llm_output[json_start:json_end]
|
| 364 |
|
|
|
|
|
|
|
| 365 |
# CRITICAL: Fix scientific notation in the JSON string BEFORE parsing
|
| 366 |
json_str = fix_scientific_notation_in_json(json_str)
|
| 367 |
-
logger.info(f"Fixed JSON string: {json_str
|
| 368 |
|
| 369 |
# --- START OF MODIFICATIONS ---
|
| 370 |
# 2. Use custom decimal parser instead of parse_float=Decimal
|
|
|
|
| 86 |
float_val = float(scientific_num)
|
| 87 |
|
| 88 |
if float_val == 0:
|
| 89 |
+
return "0.0"
|
| 90 |
elif 0 < abs(float_val) < 1e-10:
|
| 91 |
+
return f"{float_val:.20f}".rstrip('0').rstrip('.') or "0"
|
| 92 |
elif 0 < abs(float_val) < 1e-6:
|
| 93 |
+
return f"{float_val:.15f}".rstrip('0').rstrip('.') or "0"
|
| 94 |
elif abs(float_val) < 1:
|
| 95 |
+
return f"{float_val:.10f}".rstrip('0').rstrip('.') or "0"
|
| 96 |
else:
|
| 97 |
+
return f"{float_val:.8f}".rstrip('0').rstrip('.') or "0"
|
| 98 |
+
except Exception as e:
|
| 99 |
+
print(f"Error converting {match.group(0)}: {e}")
|
| 100 |
return match.group(0) # Return original if conversion fails
|
| 101 |
|
| 102 |
+
# More comprehensive patterns to catch different scientific notation formats
|
| 103 |
+
patterns = [
|
| 104 |
+
r'-?\d+\.?\d*[eE][+-]?\d+', # Standard: 1.5e-7, 9E-7
|
| 105 |
+
r'-?\d+[eE][+-]?\d+', # Without decimal: 9e-7
|
| 106 |
+
r'-?\d+\.\d+[eE][+-]?\d+', # With decimal: 1.5e-7
|
| 107 |
+
]
|
| 108 |
+
|
| 109 |
+
original_json = json_str
|
| 110 |
+
for pattern in patterns:
|
| 111 |
+
json_str = re.sub(pattern, replace_scientific, json_str)
|
| 112 |
+
|
| 113 |
+
# Also handle cases where scientific notation might be in quoted strings
|
| 114 |
+
# This catches cases like "value": "1.5e-7" (quoted scientific notation)
|
| 115 |
+
def replace_quoted_scientific(match):
|
| 116 |
+
full_match = match.group(0) # "1.5e-7"
|
| 117 |
+
number_part = match.group(1) # 1.5e-7
|
| 118 |
+
try:
|
| 119 |
+
float_val = float(number_part)
|
| 120 |
+
if 0 < abs(float_val) < 1e-6:
|
| 121 |
+
converted = f"{float_val:.15f}".rstrip('0').rstrip('.') or "0"
|
| 122 |
+
else:
|
| 123 |
+
converted = f"{float_val:.10f}".rstrip('0').rstrip('.') or "0"
|
| 124 |
+
return f'"{converted}"'
|
| 125 |
+
except:
|
| 126 |
+
return full_match
|
| 127 |
+
|
| 128 |
+
# Pattern for quoted scientific notation
|
| 129 |
+
quoted_pattern = r'"(-?\d+\.?\d*[eE][+-]?\d+)"'
|
| 130 |
+
json_str = re.sub(quoted_pattern, replace_quoted_scientific, json_str)
|
| 131 |
|
| 132 |
+
if original_json != json_str:
|
| 133 |
+
print(f"JSON transformation occurred")
|
| 134 |
+
print(f"Original: {original_json[:200]}...")
|
| 135 |
+
print(f"Fixed: {json_str[:200]}...")
|
| 136 |
|
| 137 |
+
return json_str
|
| 138 |
|
| 139 |
def convert_scientific_decimals(obj):
|
| 140 |
"""
|
|
|
|
| 388 |
"""
|
| 389 |
response = model.generate_content(prompt)
|
| 390 |
llm_output = response.text
|
| 391 |
+
logger.info(f"Raw Gemini response: {llm_output}") # Debug: see what Gemini actually returns
|
| 392 |
+
|
| 393 |
json_start = llm_output.find("{")
|
| 394 |
json_end = llm_output.rfind("}") + 1
|
| 395 |
json_str = llm_output[json_start:json_end]
|
| 396 |
|
| 397 |
+
logger.info(f"Extracted JSON before fix: {json_str}") # Debug: see extracted JSON
|
| 398 |
+
|
| 399 |
# CRITICAL: Fix scientific notation in the JSON string BEFORE parsing
|
| 400 |
json_str = fix_scientific_notation_in_json(json_str)
|
| 401 |
+
logger.info(f"Fixed JSON string: {json_str}") # Debug: see if regex worked
|
| 402 |
|
| 403 |
# --- START OF MODIFICATIONS ---
|
| 404 |
# 2. Use custom decimal parser instead of parse_float=Decimal
|