Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,28 +37,22 @@ def fix_json_output(output):
|
|
| 37 |
output = re.sub(r':\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*([,}])', r':"\1"\2', output)
|
| 38 |
return output
|
| 39 |
|
| 40 |
-
def
|
| 41 |
"""
|
| 42 |
-
|
| 43 |
"""
|
| 44 |
try:
|
| 45 |
# Fix JSON formatting issues
|
| 46 |
-
|
| 47 |
-
data = json.loads(
|
| 48 |
|
| 49 |
-
# Replace the amount field
|
| 50 |
-
if
|
| 51 |
data["amount"] = float(amount) if '.' in amount else int(amount)
|
| 52 |
|
| 53 |
-
# Ensure all required fields are present
|
| 54 |
-
required_fields = ["action", "amount", "currency", "recipient"]
|
| 55 |
-
for field in required_fields:
|
| 56 |
-
if field not in data:
|
| 57 |
-
data[field] = "unknown" # Fallback value
|
| 58 |
-
|
| 59 |
return json.dumps(data, ensure_ascii=False)
|
| 60 |
except json.JSONDecodeError:
|
| 61 |
-
#
|
| 62 |
return json.dumps({
|
| 63 |
"action": "unknown",
|
| 64 |
"amount": float(amount) if '.' in amount else int(amount),
|
|
@@ -82,11 +76,14 @@ def generate_command(input_command):
|
|
| 82 |
early_stopping=True
|
| 83 |
)
|
| 84 |
|
| 85 |
-
|
|
|
|
| 86 |
|
| 87 |
-
#
|
| 88 |
if amount:
|
| 89 |
-
result =
|
|
|
|
|
|
|
| 90 |
|
| 91 |
return result
|
| 92 |
|
|
|
|
| 37 |
output = re.sub(r':\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*([,}])', r':"\1"\2', output)
|
| 38 |
return output
|
| 39 |
|
| 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 |
# Fix JSON formatting issues
|
| 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 the model's output is invalid, return a default structure with the correct amount
|
| 56 |
return json.dumps({
|
| 57 |
"action": "unknown",
|
| 58 |
"amount": float(amount) if '.' in amount else int(amount),
|
|
|
|
| 76 |
early_stopping=True
|
| 77 |
)
|
| 78 |
|
| 79 |
+
# Decode the model's output
|
| 80 |
+
model_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 81 |
|
| 82 |
+
# Merge the model's output with the extracted amount
|
| 83 |
if amount:
|
| 84 |
+
result = merge_json_with_amount(model_output, amount)
|
| 85 |
+
else:
|
| 86 |
+
result = model_output # Use the model's output as-is if no amount is found
|
| 87 |
|
| 88 |
return result
|
| 89 |
|