Spaces:
Sleeping
Sleeping
Update app.py
Browse files39 unmodified lines4041424344454647484950515253545556575859606162636439 unmodified linesdef merge_json_with_amount(model_output, amount):"""Merges the model's JSON output with the extracted amount."""try:# Fix JSON formatting issuesmodel_output = fix_json_output(model_output)data = json.loads(model_output)# Replace the amount field with the extracted amountif 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 amountreturn json.dumps({"action": "unknown","amount": float(amount) if '.' in amount else int(amount),"currency": "unknown","recipient": "unknown"}, ensure_ascii=False)def generate_command(input_command):# Extract the amount from the input39 unmodified lines404142434445464748495051525354555657585960616263646566676869707139 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 input
app.py+22-15