RayBe commited on
Commit
56516af
·
verified ·
1 Parent(s): 74e1a78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -5
app.py CHANGED
@@ -19,17 +19,35 @@ if torch.cuda.is_available():
19
  except:
20
  pass
21
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def correct_amount_format(output):
23
- # Attempt to parse as JSON and correct amounts
 
 
24
  try:
 
 
25
  data = json.loads(output)
26
 
27
  def correct_value(value):
28
  if isinstance(value, str):
29
- # Remove commas used as thousand separators
30
- value = re.sub(r',(?=\d{3})', '', value)
31
- # Replace the first comma with a period (decimal)
32
- value = value.replace(',', '.', 1)
 
 
33
  return value
34
 
35
  # Correct each value in the JSON data
 
19
  except:
20
  pass
21
 
22
+ def fix_json_output(output):
23
+ """
24
+ Fixes common JSON formatting issues in the model's output.
25
+ """
26
+ # Remove trailing commas before closing braces/brackets
27
+ output = re.sub(r',\s*([}\]])', r'\1', output)
28
+ # Fix missing or extra quotes around keys
29
+ output = re.sub(r'([{,])\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*:', r'\1"\2":', output)
30
+ # Fix missing or extra quotes around string values
31
+ output = re.sub(r':\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*([,}])', r':"\1"\2', output)
32
+ return output
33
+
34
  def correct_amount_format(output):
35
+ """
36
+ Corrects amount formatting in the JSON output.
37
+ """
38
  try:
39
+ # Fix JSON formatting issues
40
+ output = fix_json_output(output)
41
  data = json.loads(output)
42
 
43
  def correct_value(value):
44
  if isinstance(value, str):
45
+ # Fix amounts with multiple decimal points (e.g., 3140.98.0 → 3140.98)
46
+ if re.match(r'^\d+\.\d+\.\d+$', value):
47
+ value = value.split('.')[0] + '.' + value.split('.')[1]
48
+ # Remove trailing .0 if it's not part of the original amount
49
+ if re.match(r'^\d+\.0$', value):
50
+ value = value.split('.')[0]
51
  return value
52
 
53
  # Correct each value in the JSON data