Views
No views yet
1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3
4# Load model and tokenizer
5model_name = "shiprocket-ai/open-llama-1b-address-completion"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7
8# Load the merged model (no need for PEFT since weights are already merged)
9model = AutoModelForCausalLM.from_pretrained(
10 model_name,
11 torch_dtype=torch.float16,
12 device_map="auto"
13)
14
15def extract_address_components(address, max_new_tokens=150):
16 """Extract address components using the model"""
17
18 # Format prompt for Llama 3.2-1B-Instruct
19 prompt = f"""<|begin_of_text|><|start_header_id|>user<|end_header_id|>
20
21Extract address components from: {address}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
22
23"""
24
25 # Tokenize
26 inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
27
28 # FIX: Move inputs to the same device as the model
29 device = next(model.parameters()).device
30 inputs = {k: v.to(device) for k, v in inputs.items()}
31
32 # Generate
33 with torch.no_grad():
34 outputs = model.generate(
35 **inputs,
36 max_new_tokens=max_new_tokens,
37 temperature=0.1,
38 top_p=0.9,
39 do_sample=True,
40 pad_token_id=tokenizer.eos_token_id,
41 repetition_penalty=1.05
42 )
43
44 # Decode only the new tokens
45 input_length = inputs['input_ids'].shape[1]
46 generated_tokens = outputs[0][input_length:]
47 response = tokenizer.decode(generated_tokens, skip_special_tokens=True)
48
49 return response.strip()
50
51# Example usage
52test_addresses = [
53 "C-704, Gayatri Shivam, Thakur Complex, Kandivali East, 400101",
54 "Villa 141, Geown Oasis, V Kallahalli, Off Sarjapur, Bengaluru, Karnataka, 562125",
55 "E401 Supertech Icon Indrapam 201301 UP"
56]
57
58print("🏠 ADDRESS EXTRACTION EXAMPLES")
59print("=" * 50)
60
61for i, address in enumerate(test_addresses, 1):
62 print(f"\n📍 Example {i}: {address}")
63 result = extract_address_components(address)
64 print(f"🤖 Extracted: {result}")<|begin_of_text|><|start_header_id|>user<|end_header_id|>
Extract address components from: [address_text]<|eot_id|><|start_header_id|>assistant<|end_header_id|>
<|begin_of_text|><|start_header_id|>user<|end_header_id|>
Complete this partial address: [partial_address]<|eot_id|><|start_header_id|>assistant<|end_header_id|>
config.json: Model configuration and hyperparameterspytorch_model.bin / model.safetensors: Model weightstokenizer.json: Tokenizer configurationtokenizer_config.json: Tokenizer settingsspecial_tokens_map.json: Special tokens mappinggeneration_config.json: Generation parameters1@misc{llama-1b-address-completion,
2 title={Llama 3.2-1B Address Completion Model},
3 year={2025},
4 publisher={Hugging Face},
5 url={https://huggingface.co/shiprocket-ai/open-llama-1b-address-completion}
6}