RayBe commited on
Commit
727d394
·
verified ·
1 Parent(s): edfdb0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -19
app.py CHANGED
@@ -39,34 +39,25 @@ def fix_json_output(output):
39
 
40
  def merge_json_with_amount(model_output, amount):
41
  """
42
- Merges the model's JSON output with the extracted amount.
43
- It preserves the values for keys (like "action", "currency", "recipient")
44
- that the model provided and only updates the "amount" field.
45
  """
46
  try:
47
- # First, try to parse the model output as-is
48
  data = json.loads(model_output)
49
  except json.JSONDecodeError:
50
- # If parsing fails, try to fix common JSON issues
51
- fixed_output = fix_json_output(model_output)
52
- try:
53
- data = json.loads(fixed_output)
54
- except json.JSONDecodeError:
55
- # If it still fails, start with an empty dictionary
56
- data = {}
57
 
58
- # Override or add the "amount" field with the extracted value
59
  if amount:
60
  data["amount"] = float(amount) if '.' in amount else int(amount)
61
-
62
- # Ensure that the keys for action, currency, and recipient exist.
63
- # Only fill in "unknown" if they are missing or empty.
64
- for key in ["action", "currency", "recipient"]:
65
- if key not in data or not data[key]:
66
- data[key] = "unknown"
67
-
68
  return json.dumps(data, ensure_ascii=False)
69
 
 
70
  def generate_command(input_command):
71
  # Extract the amount from the input
72
  amount = extract_amount(input_command)
 
39
 
40
  def merge_json_with_amount(model_output, amount):
41
  """
42
+ Updates only the 'amount' field in the model's JSON output,
43
+ leaving all other fields as produced by the model.
 
44
  """
45
  try:
46
+ # Attempt to load the model output directly as JSON.
47
  data = json.loads(model_output)
48
  except json.JSONDecodeError:
49
+ # If parsing fails, just return the model output unmodified.
50
+ # (You might choose to log an error here.)
51
+ return model_output
 
 
 
 
52
 
53
+ # Replace (or add) the "amount" field using the extracted amount.
54
  if amount:
55
  data["amount"] = float(amount) if '.' in amount else int(amount)
56
+
57
+ # Dump back to JSON without altering other keys.
 
 
 
 
 
58
  return json.dumps(data, ensure_ascii=False)
59
 
60
+
61
  def generate_command(input_command):
62
  # Extract the amount from the input
63
  amount = extract_amount(input_command)