Spaces:
Sleeping
Sleeping
Update app.py
Browse files38 unmodified lines3940414243444546474849505152535455565758596061626364656667686970717238 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:# First, try to parse the model output as-isdata = json.loads(model_output)except json.JSONDecodeError:# If parsing fails, try to fix common JSON issuesfixed_output = fix_json_output(model_output)try:data = json.loads(fixed_output)except json.JSONDecodeError:# If it still fails, start with an empty dictionarydata = {}# Override or add the "amount" field with the extracted valueif 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 inputamount = extract_amount(input_command)38 unmodified lines3940414243444546474849505152535455565758596061626338 unmodified linesdef merge_json_with_amount(model_output, amount):"""Updates only the 'amount' field in the model's JSON output,leaving all other fields as produced by the model."""try:# Attempt to load the model output directly as JSON.data = json.loads(model_output)except json.JSONDecodeError:# If parsing fails, just return the model output unmodified.# (You might choose to log an error here.)return model_output# Replace (or add) the "amount" field using the extracted amount.if amount:data["amount"] = float(amount) if '.' in amount else int(amount)# Dump back to JSON without altering other keys.return json.dumps(data, ensure_ascii=False)def generate_command(input_command):# Extract the amount from the inputamount = extract_amount(input_command)
app.py+10-19