Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -40,25 +40,32 @@ def fix_json_output(output):
|
|
| 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 |
-
#
|
| 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
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
def generate_command(input_command):
|
| 64 |
# Extract the amount from the input
|
|
|
|
| 40 |
def merge_json_with_amount(model_output, amount):
|
| 41 |
"""
|
| 42 |
Merges the model's JSON output with the extracted amount.
|
| 43 |
+
It preserves the values for keys (like "action", "currency", "recipient")
|
| 44 |
+
that the model provided and only updates the "amount" field.
|
| 45 |
"""
|
| 46 |
try:
|
| 47 |
+
# First, try to parse the model output as-is
|
|
|
|
| 48 |
data = json.loads(model_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
except json.JSONDecodeError:
|
| 50 |
+
# If parsing fails, try to fix common JSON issues
|
| 51 |
+
fixed_output = fix_json_output(model_output)
|
| 52 |
+
try:
|
| 53 |
+
data = json.loads(fixed_output)
|
| 54 |
+
except json.JSONDecodeError:
|
| 55 |
+
# If it still fails, start with an empty dictionary
|
| 56 |
+
data = {}
|
| 57 |
+
|
| 58 |
+
# Override or add the "amount" field with the extracted value
|
| 59 |
+
if amount:
|
| 60 |
+
data["amount"] = float(amount) if '.' in amount else int(amount)
|
| 61 |
+
|
| 62 |
+
# Ensure that the keys for action, currency, and recipient exist.
|
| 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
|