RayBe commited on
Commit
5a4f8f7
·
verified ·
1 Parent(s): 7206150

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -37,28 +37,22 @@ def fix_json_output(output):
37
  output = re.sub(r':\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*([,}])', r':"\1"\2', output)
38
  return output
39
 
40
- def replace_amount_in_json(output, amount):
41
  """
42
- Replaces the amount in the model's JSON output with the extracted amount.
43
  """
44
  try:
45
  # Fix JSON formatting issues
46
- output = fix_json_output(output)
47
- data = json.loads(output)
48
 
49
- # Replace the amount field if it exists
50
- if "amount" in data:
51
  data["amount"] = float(amount) if '.' in amount else int(amount)
52
 
53
- # Ensure all required fields are present
54
- required_fields = ["action", "amount", "currency", "recipient"]
55
- for field in required_fields:
56
- if field not in data:
57
- data[field] = "unknown" # Fallback value
58
-
59
  return json.dumps(data, ensure_ascii=False)
60
  except json.JSONDecodeError:
61
- # Fallback for invalid JSON: return a default structure
62
  return json.dumps({
63
  "action": "unknown",
64
  "amount": float(amount) if '.' in amount else int(amount),
@@ -82,11 +76,14 @@ def generate_command(input_command):
82
  early_stopping=True
83
  )
84
 
85
- result = tokenizer.decode(output_ids[0], skip_special_tokens=True)
 
86
 
87
- # Replace the model's amount with the extracted amount
88
  if amount:
89
- result = replace_amount_in_json(result, amount)
 
 
90
 
91
  return result
92
 
 
37
  output = re.sub(r':\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*([,}])', r':"\1"\2', output)
38
  return output
39
 
40
+ def merge_json_with_amount(model_output, amount):
41
  """
42
+ Merges the model's JSON output with the extracted amount.
43
  """
44
  try:
45
  # Fix JSON formatting issues
46
+ model_output = fix_json_output(model_output)
47
+ data = json.loads(model_output)
48
 
49
+ # Replace the amount field with the extracted amount
50
+ if amount:
51
  data["amount"] = float(amount) if '.' in amount else int(amount)
52
 
 
 
 
 
 
 
53
  return json.dumps(data, ensure_ascii=False)
54
  except json.JSONDecodeError:
55
+ # If the model's output is invalid, return a default structure with the correct amount
56
  return json.dumps({
57
  "action": "unknown",
58
  "amount": float(amount) if '.' in amount else int(amount),
 
76
  early_stopping=True
77
  )
78
 
79
+ # Decode the model's output
80
+ model_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
81
 
82
+ # Merge the model's output with the extracted amount
83
  if amount:
84
+ result = merge_json_with_amount(model_output, amount)
85
+ else:
86
+ result = model_output # Use the model's output as-is if no amount is found
87
 
88
  return result
89