Spaces:
Sleeping
Sleeping
Update app.py
Browse files13 unmodified lines14151617181920212223242526272829303132333435363738394041424344454647484950515253545556572 unmodified lines6061626364656613 unmodified linesmodel.to(device)def extract_amount(input_text):"""Extracts the first number (with optional decimals) from the input text.For example, in:"Should I send 2659.53464 EUR to my wife today?"it returns the string "2659.53464"."""match = re.search(r'\b(\d+(?:\.\d+)?)\b', input_text)if match:return match.group(1)return Nonedef merge_json_with_amount(model_output, amount):"""Parses the model's JSON output and overrides the "amount" keywith the manually extracted value."""try:data = json.loads(model_output)except Exception:data = {}if amount:try:data["amount"] = float(amount)except Exception:data["amount"] = amountreturn json.dumps(data, ensure_ascii=False)def generate_command(input_command):# Manually extract the amount from the input.amount = extract_amount(input_command)# Generate the JSON output from the model.prompt = "extract: " + input_commandinput_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)output_ids = model.generate(input_ids, max_length=128, num_beams=2, early_stopping=True)model_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)# Merge the manually extracted amount into the model output.result = merge_json_with_amount(model_output, amount)return result2 unmodified linesinputs=gr.Textbox(lines=2, placeholder="Enter a command..."),outputs=gr.Textbox(label="Extracted JSON Output"),title="T5 Command Extractor",description="The model provides action, currency, and recipient. The amount is manually extracted from the input.")if __name__ == "__main__":13 unmodified lines141516171819202122232425262728293031323334353637383940414243444546472 unmodified lines5051525354555613 unmodified linesmodel.to(device)def extract_amount(input_text):"""Extracts the first numeric value (with decimals) from the input text."""match = re.search(r'\b(\d+(?:\.\d+)?)\b', input_text)return match.group(1) if match else Nonedef merge_json_with_amount(model_output, amount):"""Ensures the extracted amount is inserted into the model's output JSON."""try:data = json.loads(model_output) # Try parsing the model outputexcept json.JSONDecodeError:return model_output # Return raw model output if it's not valid JSONif amount:try:data["amount"] = float(amount) # Convert to floatexcept ValueError:data["amount"] = amount # Keep as string if conversion failsreturn json.dumps(data, ensure_ascii=False)def generate_command(input_command):"""Processes the input, extracts the amount manually, and runs the model."""amount = extract_amount(input_command) # Extract amountinput_ids = tokenizer("extract: " + input_command, return_tensors="pt").input_ids.to(device)output_ids = model.generate(input_ids, max_length=128, num_beams=1, early_stopping=True) # Faster decodingmodel_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)result = merge_json_with_amount(model_output, amount)return result2 unmodified linesinputs=gr.Textbox(lines=2, placeholder="Enter a command..."),outputs=gr.Textbox(label="Extracted JSON Output"),title="T5 Command Extractor",description="The model extracts action, currency, and recipient. The amount is manually extracted.")if __name__ == "__main__":
app.py+18-28