Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789102 unmodified lines131415161718192021222324252627282930313233343536373839404142434445464748495051525354552 unmodified lines58596061626364import 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)2 unmodified linesdevice = 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 available# Corrects formatting of amounts, handles commas and precision issuesdef correct_amount_format(output):# Fix the commas in decimal amountsoutput = re.sub(r'(\d+),(\d+)', r'\1.\2', output)# Ensure that numbers with more than 2 decimal places are roundedoutput = re.sub(r'(\d+\.\d{2})\d+', r'\1', output) # Keeps only 2 decimal placesreturn output# Function to generate command and parse amountsdef generate_command(input_command):prompt = "extract: " + input_commandinput_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)output_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)# Apply the post-processing fix to the amountresult = correct_amount_format(result)return 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()1234567892 unmodified lines12131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869702 unmodified lines73747576777879import reimport torchimport gradio as grfrom transformers import T5Tokenizer, T5ForConditionalGeneration# Load the fine-tuned model from the local foldermodel_name = "./t5-finetuned-final"tokenizer = T5Tokenizer.from_pretrained(model_name)model = T5ForConditionalGeneration.from_pretrained(model_name)2 unmodified linesdevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)# Optimize for GPU: half precision and compilation (if supported)if torch.cuda.is_available():model.half() # Use half-precision for faster computationtry:model = torch.compile(model) # Optimize with torch.compile() (PyTorch 2.0+)except Exception:pass # Continue if torch.compile() isn't availabledef fix_amount_in_output(input_command, output_str):"""This function extracts the first decimal number found in the input_commandand then replaces the "amount" field in the model output with that number."""# Extract the first number that has a decimal point (or comma) from the input.match = re.search(r'(\d+(?:[.,]\d+))', input_command)if match:# Normalize to use a period as the decimal separator.correct_amount_str = match.group(1).replace(',', '.')else:# If nothing is found, return the output unchanged.return output_str# Replace the amount value in the output.# This expects the output to contain a pattern like: "amount": some_numberfixed_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_command# Tokenize input and send to the correct device.input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)# Generate output using optimized parameters.output_ids = model.generate(input_ids,max_length=64, # Reduced length for faster generation.num_beams=3, # Fewer beams for faster inference.early_stopping=True)# Decode the generated tokens.result = tokenizer.decode(output_ids[0], skip_special_tokens=True)# Fix the "amount" field in the output using the input value.result_fixed = fix_amount_in_output(input_command, result)return result_fixed# Define 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()
app.py+41-26