Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970712 unmodified lines7475767778798081import reimport torchimport gradio as grfrom transformers import T5Tokenizer, T5ForConditionalGeneration# Load the fine-tuned model and tokenizer from the local foldermodel_name = "./t5-finetuned-final"tokenizer = T5Tokenizer.from_pretrained(model_name)model = T5ForConditionalGeneration.from_pretrained(model_name)# Move model to GPU if available; otherwise, it will run on CPUdevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)# If using GPU, enable half precision and try torch.compile (PyTorch 2.0+)if torch.cuda.is_available():model.half() # Use half-precision for faster computation on GPUtry:model = torch.compile(model)except Exception:pass # Continue if torch.compile is unavailabledef fix_amount_in_output(input_command, output_str):"""Extracts the first decimal number from the input and replaces the "amount" valuein the output with that exact value."""# Look for a number with optional decimal separator in the input command.match = re.search(r'(\d+(?:[.,]\d+))', input_command)if match:# Normalize any commas to a period.correct_amount_str = match.group(1).replace(',', '.')else:return output_str# Replace the "amount" value in the output with the extracted amount.fixed_output = re.sub(r'("amount"\s*:\s*)(\d+(?:\.\d+)?)',r'\1' + correct_amount_str,output_str)return fixed_outputdef generate_command(input_command):prompt = "extract: " + input_commandinput_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)# Use greedy decoding (num_beams=1) on CPU for speed; otherwise, use beam search on GPU.if device.type == "cpu":output_ids = model.generate(input_ids,max_length=64,num_beams=1, # Greedy decoding for faster output on CPUearly_stopping=True)else:output_ids = model.generate(input_ids,max_length=64,num_beams=3, # Beam search for potentially higher quality on GPUearly_stopping=True)result = tokenizer.decode(output_ids[0], skip_special_tokens=True)result_fixed = fix_amount_in_output(input_command, result)return result_fixed# Create a Gradio interface.iface = gr.Interface(fn=generate_command,inputs=gr.Textbox(lines=2, placeholder="Enter a command..."),2 unmodified linesdescription="Enter a command, and the fine-tuned T5 model will extract relevant details in JSON format.",)if __name__ == "__main__":iface.launch()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273742 unmodified lines777879808182import jsonimport torchimport reimport gradio as grfrom transformers import T5Tokenizer, T5ForConditionalGeneration# Load the fine-tuned modelmodel_name = "./t5-finetuned-final"tokenizer = T5Tokenizer.from_pretrained(model_name)model = T5ForConditionalGeneration.from_pretrained(model_name)# Move model to GPU if availabledevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)# Enable optimizations for GPUif torch.cuda.is_available():model.half() # Use half-precision for faster computationtry:model = torch.compile(model) # PyTorch 2.0+ optimizationexcept:pass # Ignore if torch.compile is not availabledef sanitize_amount(output):"""Sanitizes the amount field to ensure it is correctly formatted."""# Fix malformed amounts like "46307.0" -> "4630.07" or "4630327.0" -> "463032.07"def fix_malformed_amount(match):full_match = match.group(0)integer_part = match.group(1)decimal_part = match.group(2)return f"{integer_part}.{decimal_part}" # Reconstruct the correct format# Match numbers with misplaced decimal pointsoutput = re.sub(r'(\d+)(\d{2})\.0', fix_malformed_amount, output)return outputdef generate_command(input_command):"""Generates the command and ensures the exact amount is displayed without changes."""prompt = "extract: " + input_commandinput_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)# Generate output from the modeloutput_ids = model.generate(input_ids,max_length=64, # Reduced for speednum_beams=3, # Lowered from 5 to 3 for faster outputearly_stopping=True)result = tokenizer.decode(output_ids[0], skip_special_tokens=True)# Sanitize the output to fix malformed amountssanitized_result = sanitize_amount(result)try:# Attempt to parse the sanitized result as JSONdata = json.loads(sanitized_result)# Convert numeric amounts to strings to preserve exact formattingif isinstance(data.get("amount"), (int, float)):data["amount"] = str(data["amount"])return json.dumps(data, ensure_ascii=False) # Return as JSON stringexcept json.JSONDecodeError:# If not valid JSON, return the raw sanitized outputreturn sanitized_result# Create a Gradio interfaceiface = gr.Interface(fn=generate_command,inputs=gr.Textbox(lines=2, placeholder="Enter a command..."),2 unmodified linesdescription="Enter a command, and the fine-tuned T5 model will extract relevant details in JSON format.",)# Launch the appif __name__ == "__main__":iface.launch()
app.py+50-49