Spaces:
Sleeping
Sleeping
Update app.py
Browse files15 unmodified lines1617181920212223242526272814 unmodified lines4344454647484950515253545556575859606162632 unmodified lines66676869707172737475767778798081828310 unmodified lines9495969715 unmodified linesdef extract_amount(input_text):"""Extracts the amount from the input text using a robust regex."""# Improved regex to match amounts preceded by keywords or currency symbolsamount_match = re.search(r'(?:send|loan|pay|transfer)\s*(\d+(?:\.\d+)?)', input_text, re.IGNORECASE)if not amount_match:amount_match = re.search(r'\b(\d+(?:\.\d+)?)\s*(?:AUD|USD|USDT|ETH|BTC)\b', input_text, re.IGNORECASE)if amount_match:return amount_match.group(1)return Nonedef fix_json_output(output):14 unmodified linesleaving 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)2 unmodified linesprompt = "extract: " + input_commandinput_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)# Generate output with increased max_length for complete JSONoutput_ids = model.generate(input_ids,max_length=128, # Increased to allow complete JSON outputnum_beams=2, # Reduced for faster inferenceearly_stopping=True)# Decode the model's outputmodel_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)# Merge the model's output with the extracted amountif amount:result = merge_json_with_amount(model_output, amount)else:10 unmodified lines)if __name__ == "__main__":iface.launch()15 unmodified lines161718192021222324252627282930313233343536373814 unmodified lines53545556575859606162636465666768697071727374757677782 unmodified lines818283848586878889909192939495969710 unmodified lines10810911011115 unmodified linesdef extract_amount(input_text):"""Extracts the amount from the input text using a robust regex.The negative lookahead (?!\S) ensures we stop capturing as soon as a non-space character appears."""# First try: match when the amount follows keywords like send, loan, pay, or transfer.amount_match = re.search(r'(?:send|loan|pay|transfer)\s*(\d+(?:\.\d+)?)(?!\S)',input_text,re.IGNORECASE)if not amount_match:# Fallback: match a number that is immediately followed by a currency symbol/abbreviation.amount_match = re.search(r'\b(\d+(?:\.\d+)?)\s*(?:AUD|USD|USDT|ETH|BTC|EUR)\b',input_text,re.IGNORECASE)if amount_match:return amount_match.group(1).strip()return Nonedef fix_json_output(output):14 unmodified linesleaving all other fields as produced by the model."""try:data = json.loads(model_output)except json.JSONDecodeError:# If JSON parsing fails, attempt to fix common formatting issues.fixed_output = fix_json_output(model_output)try:data = json.loads(fixed_output)except json.JSONDecodeError:# If it still fails, return the model output unmodified.return model_outputif amount:try:# Convert the cleaned string to a floatdata["amount"] = float(amount.strip())except ValueError:# In case conversion fails, keep the original string.data["amount"] = amountreturn json.dumps(data, ensure_ascii=False)def generate_command(input_command):# Extract the amount from the inputamount = extract_amount(input_command)2 unmodified linesprompt = "extract: " + input_commandinput_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)output_ids = model.generate(input_ids,max_length=128, # Increased max_length to allow for complete JSON outputnum_beams=2, # Using beam search for better output qualityearly_stopping=True)model_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)# Merge the model's output with the extracted amount.# The merge function only replaces the "amount" field, leaving all other keys intact.if amount:result = merge_json_with_amount(model_output, amount)else:10 unmodified lines)if __name__ == "__main__":iface.launch()
app.py+32-18