sajalmadan0909 commited on
Commit
28372d0
·
verified ·
1 Parent(s): bb73c24

Upload 3 files

Browse files
Files changed (3) hide show
  1. llama_app.py +390 -0
  2. llama_readme.md +78 -0
  3. llama_requirements.txt +6 -0
llama_app.py ADDED
@@ -0,0 +1,390 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+ import warnings
5
+ warnings.filterwarnings("ignore")
6
+
7
+ class LlamaAddressCompletion:
8
+ def __init__(self):
9
+ self.model_name = "shiprocket-ai/open-llama-1b-address-completion"
10
+ self.model = None
11
+ self.tokenizer = None
12
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
+ self.load_model()
14
+
15
+ def load_model(self):
16
+ """Load the Llama model and tokenizer"""
17
+ try:
18
+ print("Loading Llama 3.2-1B Address Completion model...")
19
+ self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
20
+
21
+ # Load model with appropriate settings for the space
22
+ self.model = AutoModelForCausalLM.from_pretrained(
23
+ self.model_name,
24
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
25
+ device_map="auto" if torch.cuda.is_available() else None,
26
+ trust_remote_code=True
27
+ )
28
+
29
+ if not torch.cuda.is_available():
30
+ self.model = self.model.to(self.device)
31
+
32
+ self.model.eval()
33
+ print("✅ Model loaded successfully!")
34
+
35
+ except Exception as e:
36
+ print(f"❌ Error loading model: {str(e)}")
37
+ raise e
38
+
39
+ def extract_address_components(self, address, max_new_tokens=150):
40
+ """Extract address components using the model"""
41
+ if not address.strip():
42
+ return "Please provide an address to extract components from."
43
+
44
+ try:
45
+ # Format prompt for Llama 3.2-1B-Instruct
46
+ prompt = f"""<|begin_of_text|><|start_header_id|>user<|end_header_id|>
47
+
48
+ Extract address components from: {address}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
49
+
50
+ """
51
+
52
+ # Tokenize
53
+ inputs = self.tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
54
+
55
+ # Move inputs to the same device as the model
56
+ device = next(self.model.parameters()).device
57
+ inputs = {k: v.to(device) for k, v in inputs.items()}
58
+
59
+ # Generate
60
+ with torch.no_grad():
61
+ outputs = self.model.generate(
62
+ **inputs,
63
+ max_new_tokens=max_new_tokens,
64
+ temperature=0.1,
65
+ top_p=0.9,
66
+ do_sample=True,
67
+ pad_token_id=self.tokenizer.eos_token_id,
68
+ repetition_penalty=1.05
69
+ )
70
+
71
+ # Decode only the new tokens
72
+ input_length = inputs['input_ids'].shape[1]
73
+ generated_tokens = outputs[0][input_length:]
74
+ response = self.tokenizer.decode(generated_tokens, skip_special_tokens=True)
75
+
76
+ return response.strip()
77
+
78
+ except Exception as e:
79
+ return f"Error processing address: {str(e)}"
80
+
81
+ def complete_partial_address(self, partial_address, max_new_tokens=100):
82
+ """Complete a partial address"""
83
+ if not partial_address.strip():
84
+ return "Please provide a partial address to complete."
85
+
86
+ try:
87
+ # Format prompt for address completion
88
+ prompt = f"""<|begin_of_text|><|start_header_id|>user<|end_header_id|>
89
+
90
+ Complete this partial address: {partial_address}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
91
+
92
+ """
93
+
94
+ # Tokenize
95
+ inputs = self.tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
96
+
97
+ # Move inputs to the same device as the model
98
+ device = next(self.model.parameters()).device
99
+ inputs = {k: v.to(device) for k, v in inputs.items()}
100
+
101
+ # Generate
102
+ with torch.no_grad():
103
+ outputs = self.model.generate(
104
+ **inputs,
105
+ max_new_tokens=max_new_tokens,
106
+ temperature=0.2,
107
+ top_p=0.9,
108
+ do_sample=True,
109
+ pad_token_id=self.tokenizer.eos_token_id,
110
+ repetition_penalty=1.05
111
+ )
112
+
113
+ # Decode only the new tokens
114
+ input_length = inputs['input_ids'].shape[1]
115
+ generated_tokens = outputs[0][input_length:]
116
+ response = self.tokenizer.decode(generated_tokens, skip_special_tokens=True)
117
+
118
+ return response.strip()
119
+
120
+ except Exception as e:
121
+ return f"Error completing address: {str(e)}"
122
+
123
+ def standardize_address(self, address, max_new_tokens=150):
124
+ """Standardize an address format"""
125
+ if not address.strip():
126
+ return "Please provide an address to standardize."
127
+
128
+ try:
129
+ # Format prompt for address standardization
130
+ prompt = f"""<|begin_of_text|><|start_header_id|>user<|end_header_id|>
131
+
132
+ Standardize this address into proper format: {address}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
133
+
134
+ """
135
+
136
+ # Tokenize
137
+ inputs = self.tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
138
+
139
+ # Move inputs to the same device as the model
140
+ device = next(self.model.parameters()).device
141
+ inputs = {k: v.to(device) for k, v in inputs.items()}
142
+
143
+ # Generate
144
+ with torch.no_grad():
145
+ outputs = self.model.generate(
146
+ **inputs,
147
+ max_new_tokens=max_new_tokens,
148
+ temperature=0.1,
149
+ top_p=0.9,
150
+ do_sample=True,
151
+ pad_token_id=self.tokenizer.eos_token_id,
152
+ repetition_penalty=1.05
153
+ )
154
+
155
+ # Decode only the new tokens
156
+ input_length = inputs['input_ids'].shape[1]
157
+ generated_tokens = outputs[0][input_length:]
158
+ response = self.tokenizer.decode(generated_tokens, skip_special_tokens=True)
159
+
160
+ return response.strip()
161
+
162
+ except Exception as e:
163
+ return f"Error standardizing address: {str(e)}"
164
+
165
+ # Initialize the model
166
+ print("Initializing Llama Address Completion system...")
167
+ try:
168
+ llama_system = LlamaAddressCompletion()
169
+ print("System ready!")
170
+ except Exception as e:
171
+ print(f"Failed to initialize system: {e}")
172
+ llama_system = None
173
+
174
+ def extract_components_interface(address_text):
175
+ """Interface function for component extraction"""
176
+ if llama_system is None:
177
+ return "❌ Model not loaded. Please check the logs."
178
+
179
+ result = llama_system.extract_address_components(address_text)
180
+ return f"**Input:** {address_text}\n\n**Extracted Components:**\n{result}"
181
+
182
+ def complete_address_interface(partial_address):
183
+ """Interface function for address completion"""
184
+ if llama_system is None:
185
+ return "❌ Model not loaded. Please check the logs."
186
+
187
+ result = llama_system.complete_partial_address(partial_address)
188
+ return f"**Partial Address:** {partial_address}\n\n**Completed Address:**\n{result}"
189
+
190
+ def standardize_address_interface(address_text):
191
+ """Interface function for address standardization"""
192
+ if llama_system is None:
193
+ return "❌ Model not loaded. Please check the logs."
194
+
195
+ result = llama_system.standardize_address(address_text)
196
+ return f"**Original:** {address_text}\n\n**Standardized:**\n{result}"
197
+
198
+ # Sample data
199
+ sample_addresses = [
200
+ "C-704, Gayatri Shivam, Thakur Complex, Kandivali East, 400101",
201
+ "Villa 141, Geown Oasis, V Kallahalli, Off Sarjapur, Bengaluru, Karnataka, 562125",
202
+ "E401 Supertech Icon Indrapam 201301 UP",
203
+ "Shop No 123, Sunshine Apartments, Andheri West, Mumbai, 400058",
204
+ "Flat 201, MG Road, Bangalore, Karnataka, 560001"
205
+ ]
206
+
207
+ partial_addresses = [
208
+ "C-704, Gayatri Shivam, Thakur Complex",
209
+ "Villa 141, Geown Oasis, V Kallahalli",
210
+ "E401 Supertech Icon",
211
+ "Shop No 123, Sunshine Apartments",
212
+ "Flat 201, MG Road, Bangalore"
213
+ ]
214
+
215
+ informal_addresses = [
216
+ "c704 gayatri shivam thakur complex kandivali e 400101",
217
+ "villa141 geown oasis vkallahalli off sarjapur blr kar 562125",
218
+ "e401 supertech icon indrapam up 201301",
219
+ "shop123 sunshine apts andheri w mumbai 400058"
220
+ ]
221
+
222
+ # Create Gradio interface
223
+ with gr.Blocks(title="Llama Address Intelligence", theme=gr.themes.Soft()) as demo:
224
+ gr.Markdown("""
225
+ # 🦙 Llama 3.2-1B Address Intelligence
226
+
227
+ Powered by a fine-tuned Llama 3.2-1B model specialized for Indian address processing. This lightweight model can extract components, complete partial addresses, and standardize informal address formats.
228
+
229
+ **Model:** [shiprocket-ai/open-llama-1b-address-completion](https://huggingface.co/shiprocket-ai/open-llama-1b-address-completion)
230
+ """)
231
+
232
+ with gr.Tab("📋 Extract Components"):
233
+ gr.Markdown("Extract structured components from complete addresses")
234
+ with gr.Row():
235
+ with gr.Column(scale=1):
236
+ extract_input = gr.Textbox(
237
+ label="Enter Address",
238
+ placeholder="e.g., C-704, Gayatri Shivam, Thakur Complex, Kandivali East, 400101",
239
+ lines=3
240
+ )
241
+ extract_btn = gr.Button("🔍 Extract Components", variant="primary")
242
+
243
+ gr.Markdown("### Sample Addresses:")
244
+ extract_samples = []
245
+ for addr in sample_addresses:
246
+ btn = gr.Button(addr, size="sm")
247
+ btn.click(fn=lambda x=addr: x, outputs=extract_input)
248
+ extract_samples.append(btn)
249
+
250
+ with gr.Column(scale=1):
251
+ extract_output = gr.Markdown(
252
+ value="Enter an address and click 'Extract Components' to see structured breakdown."
253
+ )
254
+
255
+ extract_btn.click(
256
+ fn=extract_components_interface,
257
+ inputs=extract_input,
258
+ outputs=extract_output
259
+ )
260
+
261
+ extract_input.submit(
262
+ fn=extract_components_interface,
263
+ inputs=extract_input,
264
+ outputs=extract_output
265
+ )
266
+
267
+ with gr.Tab("✨ Complete Address"):
268
+ gr.Markdown("Complete partial or incomplete addresses using AI")
269
+ with gr.Row():
270
+ with gr.Column(scale=1):
271
+ complete_input = gr.Textbox(
272
+ label="Enter Partial Address",
273
+ placeholder="e.g., C-704, Gayatri Shivam, Thakur Complex",
274
+ lines=3
275
+ )
276
+ complete_btn = gr.Button("🚀 Complete Address", variant="primary")
277
+
278
+ gr.Markdown("### Sample Partial Addresses:")
279
+ complete_samples = []
280
+ for addr in partial_addresses:
281
+ btn = gr.Button(addr, size="sm")
282
+ btn.click(fn=lambda x=addr: x, outputs=complete_input)
283
+ complete_samples.append(btn)
284
+
285
+ with gr.Column(scale=1):
286
+ complete_output = gr.Markdown(
287
+ value="Enter a partial address and click 'Complete Address' to see the AI completion."
288
+ )
289
+
290
+ complete_btn.click(
291
+ fn=complete_address_interface,
292
+ inputs=complete_input,
293
+ outputs=complete_output
294
+ )
295
+
296
+ complete_input.submit(
297
+ fn=complete_address_interface,
298
+ inputs=complete_input,
299
+ outputs=complete_output
300
+ )
301
+
302
+ with gr.Tab("📐 Standardize Format"):
303
+ gr.Markdown("Convert informal or messy addresses into proper standardized format")
304
+ with gr.Row():
305
+ with gr.Column(scale=1):
306
+ standardize_input = gr.Textbox(
307
+ label="Enter Informal Address",
308
+ placeholder="e.g., c704 gayatri shivam thakur complex kandivali e 400101",
309
+ lines=3
310
+ )
311
+ standardize_btn = gr.Button("📏 Standardize Format", variant="primary")
312
+
313
+ gr.Markdown("### Sample Informal Addresses:")
314
+ standardize_samples = []
315
+ for addr in informal_addresses:
316
+ btn = gr.Button(addr, size="sm")
317
+ btn.click(fn=lambda x=addr: x, outputs=standardize_input)
318
+ standardize_samples.append(btn)
319
+
320
+ with gr.Column(scale=1):
321
+ standardize_output = gr.Markdown(
322
+ value="Enter an informal address and click 'Standardize Format' to see the cleaned version."
323
+ )
324
+
325
+ standardize_btn.click(
326
+ fn=standardize_address_interface,
327
+ inputs=standardize_input,
328
+ outputs=standardize_output
329
+ )
330
+
331
+ standardize_input.submit(
332
+ fn=standardize_address_interface,
333
+ inputs=standardize_input,
334
+ outputs=standardize_output
335
+ )
336
+
337
+ with gr.Tab("ℹ️ Model Information"):
338
+ gr.Markdown("""
339
+ ## 🦙 About Llama 3.2-1B Address Completion
340
+
341
+ ### Model Specifications
342
+ - **Base Model**: meta-llama/Llama-3.2-1B-Instruct
343
+ - **Parameters**: 1.24B parameters
344
+ - **Model Size**: ~2.47GB
345
+ - **Architecture**: Causal Language Model (Autoregressive)
346
+ - **Max Context**: 131,072 tokens
347
+ - **Precision**: FP16 for GPU, FP32 for CPU
348
+
349
+ ### Key Features
350
+ - **Lightweight**: Only 1B parameters for fast inference
351
+ - **Specialized**: Fine-tuned specifically for Indian addresses
352
+ - **Versatile**: Handles extraction, completion, and standardization
353
+ - **Efficient**: Optimized for real-time applications
354
+ - **Context-Aware**: Understands relationships between address components
355
+
356
+ ### Supported Address Components
357
+ - **Building Names**: Apartments, complexes, towers, malls
358
+ - **Localities**: Areas, neighborhoods, sectors
359
+ - **Pincodes**: 6-digit Indian postal codes
360
+ - **Cities**: Major and minor Indian cities
361
+ - **States**: All Indian states and union territories
362
+ - **Sub-localities**: Sectors, phases, blocks
363
+ - **Road Names**: Streets, lanes, main roads
364
+ - **Landmarks**: Notable reference points
365
+
366
+ ### Use Cases
367
+ - **E-commerce**: Auto-complete checkout addresses
368
+ - **Forms**: Intelligent address suggestions
369
+ - **Data Cleaning**: Standardize legacy address databases
370
+ - **Mobile Apps**: On-device address processing
371
+ - **APIs**: Real-time address validation services
372
+
373
+ ### Performance Tips
374
+ - Use lower temperatures (0.1-0.3) for factual outputs
375
+ - Keep prompts under 512 tokens for optimal speed
376
+ - Process in batches for high-throughput scenarios
377
+ - Works best with Llama chat format prompts
378
+ """)
379
+
380
+ gr.Markdown("""
381
+ ---
382
+ **Powered by:** [Llama 3.2-1B Address Completion](https://huggingface.co/shiprocket-ai/open-llama-1b-address-completion) |
383
+ **License:** Apache 2.0 |
384
+ **Developed by:** Shiprocket AI Team
385
+
386
+ This model demonstrates the power of lightweight LLMs for specialized address intelligence tasks.
387
+ """)
388
+
389
+ if __name__ == "__main__":
390
+ demo.launch()
llama_readme.md ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Llama Address Intelligence
3
+ emoji: 🦙
4
+ colorFrom: purple
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 4.44.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ ---
12
+
13
+ # Llama 3.2-1B Address Intelligence Demo
14
+
15
+ This Space demonstrates the capabilities of [shiprocket-ai/open-llama-1b-address-completion](https://huggingface.co/shiprocket-ai/open-llama-1b-address-completion), a fine-tuned Llama 3.2-1B model specialized for Indian address processing.
16
+
17
+ ## What it does
18
+
19
+ This application showcases three main capabilities:
20
+
21
+ 1. **Component Extraction**: Parse addresses into structured components (building, locality, pincode, etc.)
22
+ 2. **Address Completion**: Complete partial or incomplete addresses using AI
23
+ 3. **Format Standardization**: Convert informal addresses to proper standardized format
24
+
25
+ ## Features
26
+
27
+ - **Lightweight**: Only 1.24B parameters for fast inference
28
+ - **Specialized**: Fine-tuned specifically for Indian address patterns
29
+ - **Versatile**: Handles multiple address intelligence tasks
30
+ - **Interactive**: Three separate tabs for different use cases
31
+ - **Real-time**: Optimized for quick responses
32
+
33
+ ## How to use
34
+
35
+ ### Component Extraction
36
+ 1. Go to the "Extract Components" tab
37
+ 2. Enter a complete address
38
+ 3. Click "Extract Components" to see structured breakdown
39
+
40
+ ### Address Completion
41
+ 1. Go to the "Complete Address" tab
42
+ 2. Enter a partial address
43
+ 3. Click "Complete Address" to see AI completion
44
+
45
+ ### Format Standardization
46
+ 1. Go to the "Standardize Format" tab
47
+ 2. Enter an informal or messy address
48
+ 3. Click "Standardize Format" to see cleaned version
49
+
50
+ ## Example addresses
51
+
52
+ - **Complete**: C-704, Gayatri Shivam, Thakur Complex, Kandivali East, 400101
53
+ - **Partial**: C-704, Gayatri Shivam, Thakur Complex
54
+ - **Informal**: c704 gayatri shivam thakur complex kandivali e 400101
55
+
56
+ ## Model Information
57
+
58
+ - **Base Model**: meta-llama/Llama-3.2-1B-Instruct
59
+ - **Parameters**: 1.24B
60
+ - **Model Size**: ~2.47GB
61
+ - **Max Context**: 131K tokens
62
+ - **License**: Apache 2.0
63
+
64
+ ## Supported Components
65
+
66
+ The model can handle:
67
+ - Building names, localities, pincodes
68
+ - Cities, states, sub-localities
69
+ - Road names, landmarks
70
+ - Various Indian address formats
71
+
72
+ ## Performance
73
+
74
+ Optimized for:
75
+ - Real-time applications
76
+ - Mobile/edge deployment
77
+ - High-throughput processing
78
+ - Low memory usage
llama_requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch>=2.0.0
2
+ transformers>=4.36.0
3
+ gradio>=4.44.0
4
+ accelerate>=0.25.0
5
+ numpy>=1.21.0
6
+ tokenizers>=0.15.0