Spaces:
Sleeping
Sleeping
Update app.py
Browse files39 unmodified lines404142434445464546474848495051525349555657585960615051525354555657585960616263646566676869707139 unmodified linesdef merge_json_with_amount(model_output, amount): """ Merges the model's JSON output with the extracted amount. It preserves the values for keys (like "action", "currency", "recipient") that the model provided and only updates the "amount" field. """ try: # Fix JSON formatting issues model_output = fix_json_output(model_output) # First, try to parse the model output as-is data = json.loads(model_output) # Replace the amount field with the extracted amount if amount: data["amount"] = float(amount) if '.' in amount else int(amount) return json.dumps(data, ensure_ascii=False) except json.JSONDecodeError: # If the model's output is invalid, return a default structure with the correct amount return json.dumps({ "action": "unknown", "amount": float(amount) if '.' in amount else int(amount), "currency": "unknown", "recipient": "unknown" }, ensure_ascii=False) # If parsing fails, try to fix common JSON issues fixed_output = fix_json_output(model_output) try: data = json.loads(fixed_output) except json.JSONDecodeError: # If it still fails, start with an empty dictionary data = {}
# Override or add the "amount" field with the extracted value if amount: data["amount"] = float(amount) if '.' in amount else int(amount)
# Ensure that the keys for action, currency, and recipient exist. # Only fill in "unknown" if they are missing or empty. for key in ["action", "currency", "recipient"]: if key not in data or not data[key]: data[key] = "unknown"
return json.dumps(data, ensure_ascii=False)
def generate_command(input_command): # Extract the amount from the inputapp.py+22-15