Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -39,34 +39,25 @@ def fix_json_output(output):
|
|
| 39 |
|
| 40 |
def merge_json_with_amount(model_output, amount):
|
| 41 |
"""
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
that the model provided and only updates the "amount" field.
|
| 45 |
"""
|
| 46 |
try:
|
| 47 |
-
#
|
| 48 |
data = json.loads(model_output)
|
| 49 |
except json.JSONDecodeError:
|
| 50 |
-
# If parsing fails,
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
data = json.loads(fixed_output)
|
| 54 |
-
except json.JSONDecodeError:
|
| 55 |
-
# If it still fails, start with an empty dictionary
|
| 56 |
-
data = {}
|
| 57 |
|
| 58 |
-
#
|
| 59 |
if amount:
|
| 60 |
data["amount"] = float(amount) if '.' in amount else int(amount)
|
| 61 |
-
|
| 62 |
-
#
|
| 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)
|