Inference code for trsy
#1
by SandeepBA - opened
- trsy_inference.py +91 -0
trsy_inference.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Imports
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
import torch
|
| 4 |
+
from accelerate import Accelerator
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
# --- Author and Date ---
|
| 9 |
+
# Author: Sandeep Belgavi
|
| 10 |
+
# Date: July 24, 2025
|
| 11 |
+
|
| 12 |
+
# Initialize Accelerator for distributed training and mixed precision operations.
|
| 13 |
+
accelerator = Accelerator()
|
| 14 |
+
|
| 15 |
+
# --- Configuration ---
|
| 16 |
+
# Set the path to the directory where your fine-tuned model checkpoint was saved.
|
| 17 |
+
model_path = Path("/home/sandeep/trsy/checkpoint")
|
| 18 |
+
|
| 19 |
+
# Ensure the specified model directory exists.
|
| 20 |
+
if not model_path.exists():
|
| 21 |
+
print(f"Error: Model directory not found at '{model_path}'. Please ensure the path is correct and accessible.")
|
| 22 |
+
exit()
|
| 23 |
+
|
| 24 |
+
# --- Model Loading ---
|
| 25 |
+
print(f"Loading model and tokenizer from: {model_path}")
|
| 26 |
+
try:
|
| 27 |
+
start_time = datetime.now()
|
| 28 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 29 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
|
| 30 |
+
end_time = datetime.now()
|
| 31 |
+
print(f"Model and tokenizer loaded successfully in {end_time - start_time}.")
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Error loading model from checkpoint: {e}")
|
| 34 |
+
exit()
|
| 35 |
+
|
| 36 |
+
# Prepare the model for inference using Accelerator.
|
| 37 |
+
model = accelerator.prepare(model)
|
| 38 |
+
|
| 39 |
+
# Set the model to evaluation mode.
|
| 40 |
+
model.eval()
|
| 41 |
+
|
| 42 |
+
# --- Inference Function ---
|
| 43 |
+
def predict_poi(address: str) -> str:
|
| 44 |
+
"""
|
| 45 |
+
Predicts the Point of Interest (POI) name for a given address string using the fine-tuned model.
|
| 46 |
+
Args:
|
| 47 |
+
address (str): The input address string for which to predict the POI.
|
| 48 |
+
Example: "Opposite SBI, BTM Layout, Bangalore"
|
| 49 |
+
Returns:
|
| 50 |
+
str: The predicted POI name as a string.
|
| 51 |
+
Example: "SBI"
|
| 52 |
+
"""
|
| 53 |
+
inputs = tokenizer(address, return_tensors="pt", padding=True, truncation=True)
|
| 54 |
+
inputs = {k: v.to(accelerator.device) for k, v in inputs.items()}
|
| 55 |
+
|
| 56 |
+
with torch.no_grad():
|
| 57 |
+
output = model.generate(
|
| 58 |
+
**inputs,
|
| 59 |
+
max_length=32,
|
| 60 |
+
num_beams=4,
|
| 61 |
+
early_stopping=True
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 65 |
+
|
| 66 |
+
# --- Inference Examples ---
|
| 67 |
+
print("\n--- Starting Inference Examples ---")
|
| 68 |
+
|
| 69 |
+
test_address_1 = "Opposite SBI, BTM Layout, Bangalore"
|
| 70 |
+
predicted_poi_1 = predict_poi(test_address_1)
|
| 71 |
+
print(f"Input Address: '{test_address_1}'")
|
| 72 |
+
print(f"Predicted POI: '{predicted_poi_1}'")
|
| 73 |
+
print("-" * 20)
|
| 74 |
+
|
| 75 |
+
test_address_2 = "Near HSR Layout bus stand, HSR Layout, Bengaluru"
|
| 76 |
+
predicted_poi_2 = predict_poi(test_address_2)
|
| 77 |
+
print(f"Input Address: '{test_address_2}'")
|
| 78 |
+
print(f"Predicted POI: '{predicted_poi_2}'")
|
| 79 |
+
print("-" * 20)
|
| 80 |
+
|
| 81 |
+
test_address_3 = "Next to The Forum Mall, Koramangala, Bangalore"
|
| 82 |
+
predicted_poi_3 = predict_poi(test_address_3)
|
| 83 |
+
print(f"Input Address: '{test_address_3}'")
|
| 84 |
+
print(f"Predicted POI: '{predicted_poi_3}'")
|
| 85 |
+
print("-" * 20)
|
| 86 |
+
|
| 87 |
+
test_address_4 = "Indiranagar 100 Feet Road"
|
| 88 |
+
predicted_poi_4 = predict_poi(test_address_4)
|
| 89 |
+
print(f"Input Address: '{test_address_4}'")
|
| 90 |
+
print(f"Predicted POI: '{predicted_poi_4}'")
|
| 91 |
+
print("-" * 20)
|