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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb94fb7
 
4039061
 
 
 
bb94fb7
 
4039061
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForTokenClassification
import warnings
warnings.filterwarnings("ignore")

class MultiModelIndianAddressNER:
    def __init__(self):
        # Available models configuration
        self.models_config = {
            "TinyBERT": {
                "name": "shiprocket-ai/open-tinybert-indian-address-ner",
                "description": "Lightweight and fast - 66.4M parameters",
                "base_model": "TinyBERT"
            },
            "ModernBERT": {
                "name": "shiprocket-ai/open-modernbert-indian-address-ner", 
                "description": "Modern architecture - 150M parameters",
                "base_model": "ModernBERT"
            },
            "IndicBERT": {
                "name": "shiprocket-ai/open-indicbert-indian-address-ner",
                "description": "Indic language optimized - 32.9M parameters", 
                "base_model": "IndicBERT"
            }
        }
        
        # Cache for loaded models
        self.loaded_models = {}
        self.loaded_tokenizers = {}
        
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        
        # Entity mappings (same for all models)
        self.id2entity = {
            "0": "O",
            "1": "B-building_name",
            "2": "I-building_name", 
            "3": "B-city",
            "4": "I-city",
            "5": "B-country",
            "6": "I-country",
            "7": "B-floor",
            "8": "I-floor",
            "9": "B-house_details",
            "10": "I-house_details",
            "11": "B-locality",
            "12": "I-locality",
            "13": "B-pincode",
            "14": "I-pincode",
            "15": "B-road",
            "16": "I-road",
            "17": "B-state",
            "18": "I-state",
            "19": "B-sub_locality",
            "20": "I-sub_locality",
            "21": "B-landmarks",
            "22": "I-landmarks"
        }
        
        # Load default model (TinyBERT)
        self.load_model("TinyBERT")
    
    def load_model(self, model_key):
        """Load a specific model if not already loaded"""
        if model_key not in self.loaded_models:
            print(f"Loading {model_key} model...")
            model_name = self.models_config[model_key]["name"]
            
            try:
                tokenizer = AutoTokenizer.from_pretrained(model_name)
                model = AutoModelForTokenClassification.from_pretrained(model_name)
                model.to(self.device)
                model.eval()
                
                self.loaded_tokenizers[model_key] = tokenizer
                self.loaded_models[model_key] = model
                print(f"βœ… {model_key} model loaded successfully!")
                
            except Exception as e:
                print(f"❌ Error loading {model_key}: {str(e)}")
                raise e
        
        return self.loaded_tokenizers[model_key], self.loaded_models[model_key]
    
    def predict(self, address, model_key="TinyBERT"):
        """Extract entities from an Indian address using specified model"""
        if not address.strip():
            return {}, f"Using {model_key} model"
        
        try:
            # Load the selected model
            tokenizer, model = self.load_model(model_key)
            
            # Different approaches based on tokenizer type
            if model_key == "IndicBERT":
                # IndicBERT uses SentencePiece - use token-based approach
                entities = self._predict_token_based(address, tokenizer, model)
            else:
                # TinyBERT and ModernBERT - use offset mapping approach
                entities = self._predict_offset_based(address, tokenizer, model)
            
            model_info = f"Using {model_key} ({self.models_config[model_key]['description']})"
            return entities, model_info
            
        except Exception as e:
            return {}, f"Error with {model_key}: {str(e)}"
    
    def _predict_offset_based(self, address, tokenizer, model):
        """Offset-based prediction for TinyBERT and ModernBERT"""
        inputs = tokenizer(
            address, 
            return_tensors="pt", 
            truncation=True, 
            padding=True, 
            max_length=128,
            return_offsets_mapping=True
        )
        
        # Extract offset mapping before moving to device
        offset_mapping = inputs.pop("offset_mapping")[0]
        inputs = {k: v.to(self.device) for k, v in inputs.items()}
        
        # Predict
        with torch.no_grad():
            outputs = model(**inputs)
            predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
            predicted_ids = torch.argmax(predictions, dim=-1)
            confidence_scores = torch.max(predictions, dim=-1)[0]
        
        # Extract entities using offset mapping
        return self.extract_entities_with_offsets(
            address, 
            predicted_ids[0], 
            confidence_scores[0], 
            offset_mapping
        )
    
    def _predict_token_based(self, address, tokenizer, model):
        """Token-based prediction for IndicBERT (SentencePiece)"""
        inputs = tokenizer(
            address, 
            return_tensors="pt", 
            truncation=True, 
            padding=True, 
            max_length=128
        )
        inputs = {k: v.to(self.device) for k, v in inputs.items()}
        
        # Predict
        with torch.no_grad():
            outputs = model(**inputs)
            predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
            predicted_ids = torch.argmax(predictions, dim=-1)
            confidence_scores = torch.max(predictions, dim=-1)[0]
        
        # Convert to tokens and labels
        tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
        predicted_labels = [self.id2entity.get(str(id.item()), "O") for id in predicted_ids[0]]
        confidences = confidence_scores[0].cpu().numpy()
        
        # Group entities with proper text reconstruction
        return self.group_entities_sentencepiece(tokens, predicted_labels, confidences)
    
    def extract_entities_with_offsets(self, original_text, predicted_ids, confidences, offset_mapping):
        """Extract entities using offset mapping for accurate text reconstruction"""
        entities = {}
        current_entity = None
        
        for i, (pred_id, conf) in enumerate(zip(predicted_ids, confidences)):
            if i >= len(offset_mapping):
                break
                
            start, end = offset_mapping[i]
            
            # Skip special tokens (they have (0,0) mapping)
            if start == end == 0:
                continue
                
            label = self.id2entity.get(str(pred_id.item()), "O")
            
            if label.startswith("B-"):
                # Save previous entity
                if current_entity:
                    entity_type = current_entity["type"]
                    if entity_type not in entities:
                        entities[entity_type] = []
                    entities[entity_type].append({
                        "text": current_entity["text"],
                        "confidence": current_entity["confidence"]
                    })
                
                # Start new entity
                entity_type = label[2:]  # Remove "B-"
                current_entity = {
                    "type": entity_type,
                    "text": original_text[start:end],
                    "confidence": conf.item(),
                    "start": start,
                    "end": end
                }
            
            elif label.startswith("I-") and current_entity:
                # Continue current entity
                entity_type = label[2:]  # Remove "I-"
                if entity_type == current_entity["type"]:
                    # Extend the entity to include this token
                    current_entity["text"] = original_text[current_entity["start"]:end]
                    current_entity["confidence"] = (current_entity["confidence"] + conf.item()) / 2
                    current_entity["end"] = end
            
            elif label == "O" and current_entity:
                # End current entity
                entity_type = current_entity["type"]
                if entity_type not in entities:
                    entities[entity_type] = []
                entities[entity_type].append({
                    "text": current_entity["text"],
                    "confidence": current_entity["confidence"]
                })
                current_entity = None
        
        # Add final entity if exists
        if current_entity:
            entity_type = current_entity["type"]
            if entity_type not in entities:
                entities[entity_type] = []
            entities[entity_type].append({
                "text": current_entity["text"],
                "confidence": current_entity["confidence"]
            })
        
        return entities
    
    def group_entities_sentencepiece(self, tokens, labels, confidences):
        """Group entities for SentencePiece tokenization (IndicBERT) with proper text reconstruction"""
        entities = {}
        current_entity = None
        
        for i, (token, label, conf) in enumerate(zip(tokens, labels, confidences)):
            if token in ["<s>", "</s>", "<pad>", "<unk>"]:
                continue
            
            if label.startswith("B-"):
                # Save previous entity
                if current_entity:
                    entity_type = current_entity["type"]
                    if entity_type not in entities:
                        entities[entity_type] = []
                    
                    # Clean up the text by removing SentencePiece markers and extra spaces
                    clean_text = self._clean_sentencepiece_text(current_entity["text"])
                    entities[entity_type].append({
                        "text": clean_text,
                        "confidence": current_entity["confidence"]
                    })
                
                # Start new entity - handle SentencePiece format
                entity_type = label[2:]  # Remove "B-"
                clean_token = token.replace("▁", " ").strip()
                current_entity = {
                    "type": entity_type,
                    "text": clean_token,
                    "confidence": conf
                }
            
            elif label.startswith("I-") and current_entity:
                # Continue current entity
                entity_type = label[2:]  # Remove "I-"
                if entity_type == current_entity["type"]:
                    # Handle SentencePiece subword continuation
                    if token.startswith("▁"):
                        # New word boundary
                        current_entity["text"] += " " + token.replace("▁", "")
                    else:
                        # Subword continuation
                        current_entity["text"] += token
                    current_entity["confidence"] = (current_entity["confidence"] + conf) / 2
            
            elif label == "O" and current_entity:
                # End current entity
                entity_type = current_entity["type"]
                if entity_type not in entities:
                    entities[entity_type] = []
                
                clean_text = self._clean_sentencepiece_text(current_entity["text"])
                entities[entity_type].append({
                    "text": clean_text,
                    "confidence": current_entity["confidence"]
                })
                current_entity = None
        
        # Add final entity if exists
        if current_entity:
            entity_type = current_entity["type"]
            if entity_type not in entities:
                entities[entity_type] = []
            
            clean_text = self._clean_sentencepiece_text(current_entity["text"])
            entities[entity_type].append({
                "text": clean_text,
                "confidence": current_entity["confidence"]
            })
        
        return entities
    
    def _clean_sentencepiece_text(self, text):
        """Clean SentencePiece text by removing markers and fixing spacing"""
        # Remove SentencePiece markers
        clean_text = text.replace("▁", " ")
        # Remove extra spaces and clean up
        clean_text = " ".join(clean_text.split())
        # Remove trailing commas and spaces
        clean_text = clean_text.strip().rstrip(",").strip()
        return clean_text

# Initialize the multi-model system
print("Initializing Multi-Model Indian Address NER...")
ner_system = MultiModelIndianAddressNER()
print("System ready!")

def process_address(address_text, selected_model):
    """Process address and return formatted results with selected model"""
    if not address_text.strip():
        return "Please enter an address to analyze."
    
    try:
        # Extract entities using selected model
        entities, model_info = ner_system.predict(address_text, selected_model)
        
        if not entities:
            return f"❌ No entities found in the provided address.\n\n**{model_info}**"
        
        # Format results
        result = f"πŸ“ **Input Address:** {address_text}\n\n"
        result += f"πŸ€– **{model_info}**\n\n"
        result += "🏷️ **Extracted Entities:**\n\n"
        
        # Sort entities by type for better presentation
        entity_order = [
            'building_name', 'floor', 'house_details', 'road', 
            'sub_locality', 'locality', 'landmarks', 'city', 
            'state', 'country', 'pincode'
        ]
        
        displayed_entities = set()
        
        # Display entities in order
        for entity_type in entity_order:
            if entity_type in entities and entity_type not in displayed_entities:
                result += f"**{entity_type.replace('_', ' ').title()}:**\n"
                for entity in entities[entity_type]:
                    confidence = entity['confidence']
                    text = entity['text']
                    confidence_icon = "🟒" if confidence > 0.8 else "🟑" if confidence > 0.6 else "πŸ”΄"
                    result += f"  {confidence_icon} {text} (confidence: {confidence:.3f})\n"
                result += "\n"
                displayed_entities.add(entity_type)
        
        # Display any remaining entities
        for entity_type, entity_list in entities.items():
            if entity_type not in displayed_entities:
                result += f"**{entity_type.replace('_', ' ').title()}:**\n"
                for entity in entity_list:
                    confidence = entity['confidence']
                    text = entity['text']
                    confidence_icon = "🟒" if confidence > 0.8 else "🟑" if confidence > 0.6 else "πŸ”΄"
                    result += f"  {confidence_icon} {text} (confidence: {confidence:.3f})\n"
                result += "\n"
        
        result += "\n**Legend:**\n"
        result += "🟒 High confidence (>0.8)\n"
        result += "🟑 Medium confidence (0.6-0.8)\n"
        result += "πŸ”΄ Low confidence (<0.6)\n"
        
        return result
        
    except Exception as e:
        return f"❌ Error processing address: {str(e)}"

def compare_models(address_text):
    """Compare results from all models"""
    if not address_text.strip():
        return "Please enter an address to compare models."
    
    result = f"πŸ“ **Address:** {address_text}\n\n"
    result += "πŸ”„ **Model Comparison:**\n\n"
    
    for model_key in ner_system.models_config.keys():
        try:
            entities, model_info = ner_system.predict(address_text, model_key)
            result += f"### {model_key}\n"
            result += f"*{ner_system.models_config[model_key]['description']}*\n\n"
            
            if entities:
                entity_count = sum(len(entity_list) for entity_list in entities.values())
                result += f"**Found {entity_count} entities:**\n"
                
                for entity_type, entity_list in sorted(entities.items()):
                    for entity in entity_list:
                        confidence = entity['confidence']
                        text = entity['text']
                        confidence_icon = "🟒" if confidence > 0.8 else "🟑" if confidence > 0.6 else "πŸ”΄"
                        result += f"  {confidence_icon} {entity_type}: {text} ({confidence:.3f})\n"
            else:
                result += "❌ No entities found\n"
            
            result += "\n---\n\n"
            
        except Exception as e:
            result += f"### {model_key}\n❌ Error: {str(e)}\n\n---\n\n"
    
    return result

# Sample addresses for examples
sample_addresses = [
    "Shop No 123, Sunshine Apartments, Andheri West, Mumbai, 400058",
    "DLF Cyber City, Sector 25, Gurgaon, Haryana",
    "Flat 201, MG Road, Bangalore, Karnataka, 560001",
    "Phoenix Mall, Kurla West, Mumbai",
    "House No 456, Green Park Extension, New Delhi, 110016",
    "Office 302, Tech Park, Electronic City, Bangalore, Karnataka, 560100"
]

# Create Gradio interface
with gr.Blocks(title="Multi-Model Indian Address NER", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # 🏠 Multi-Model Indian Address Named Entity Recognition
    
    Compare different transformer models for extracting components from Indian addresses. Choose between TinyBERT (fast), ModernBERT (modern), and IndicBERT (Indic-optimized).
    
    **Supported entities:** Building Name, Floor, House Details, Road, Sub-locality, Locality, Landmarks, City, State, Country, Pincode
    """)
    
    with gr.Tab("Single Model Analysis"):
        with gr.Row():
            with gr.Column(scale=1):
                model_dropdown = gr.Dropdown(
                    choices=list(ner_system.models_config.keys()),
                    value="TinyBERT",
                    label="Select Model",
                    info="Choose which model to use for entity extraction"
                )
                
                address_input = gr.Textbox(
                    label="Enter Indian Address",
                    placeholder="e.g., Shop No 123, Sunshine Apartments, Andheri West, Mumbai, 400058",
                    lines=3,
                    max_lines=5
                )
                
                submit_btn = gr.Button("πŸ” Extract Entities", variant="primary")
                
                gr.Markdown("### πŸ“ Sample Addresses (click to use):")
                sample_buttons = []
                for addr in sample_addresses:
                    btn = gr.Button(addr, size="sm")
                    btn.click(fn=lambda x=addr: x, outputs=address_input)
                    sample_buttons.append(btn)
            
            with gr.Column(scale=1):
                output_text = gr.Markdown(
                    label="Extracted Entities",
                    value="Select a model, enter an address, and click 'Extract Entities' to see the results."
                )
        
        # Event handlers for single model
        submit_btn.click(
            fn=process_address,
            inputs=[address_input, model_dropdown],
            outputs=output_text
        )
        
        address_input.submit(
            fn=process_address,
            inputs=[address_input, model_dropdown],
            outputs=output_text
        )
    
    with gr.Tab("Model Comparison"):
        with gr.Row():
            with gr.Column(scale=1):
                address_compare = gr.Textbox(
                    label="Enter Indian Address for Comparison",
                    placeholder="e.g., Shop No 123, Sunshine Apartments, Andheri West, Mumbai, 400058",
                    lines=3,
                    max_lines=5
                )
                
                compare_btn = gr.Button("πŸ”„ Compare All Models", variant="secondary")
                
                gr.Markdown("### πŸ“ Sample Addresses (click to use):")
                sample_buttons_compare = []
                for addr in sample_addresses:
                    btn = gr.Button(addr, size="sm")
                    btn.click(fn=lambda x=addr: x, outputs=address_compare)
                    sample_buttons_compare.append(btn)
            
            with gr.Column(scale=1):
                comparison_output = gr.Markdown(
                    label="Model Comparison Results",
                    value="Enter an address and click 'Compare All Models' to see how different models perform."
                )
        
        # Event handlers for comparison
        compare_btn.click(
            fn=compare_models,
            inputs=address_compare,
            outputs=comparison_output
        )
        
        address_compare.submit(
            fn=compare_models,
            inputs=address_compare,
            outputs=comparison_output
        )
    
    with gr.Tab("Model Information"):
        gr.Markdown("""
        ## πŸ“Š Available Models
        
        ### TinyBERT
        - **Base Model**: huawei-noah/TinyBERT_General_6L_768D
        - **Model Size**: ~66.4M parameters
        - **Advantages**: Fastest inference, lowest memory usage, mobile-friendly
        - **Best for**: Real-time applications, edge deployment
        
        ### ModernBERT  
        - **Base Model**: answerdotai/ModernBERT-base
        - **Model Size**: ~150M parameters
        - **Advantages**: Latest architectural improvements, balanced performance
        - **Best for**: High-accuracy requirements with reasonable speed
        
        ### IndicBERT
        - **Base Model**: ai4bharat/indic-bert
        - **Model Size**: ~32.9M parameters  
        - **Advantages**: Optimized for Indian languages and contexts
        - **Best for**: Mixed language addresses, regional Indian contexts
        
        ## 🎯 Entity Types Supported
        
        All models can extract the following entities:
        - **Building Name**: Apartment/building names
        - **Floor**: Floor numbers and details
        - **House Details**: House/flat numbers
        - **Road**: Street and road names
        - **Sub-locality**: Sector, block details
        - **Locality**: Area, neighborhood names  
        - **Landmarks**: Notable nearby locations
        - **City**: City names
        - **State**: State names
        - **Country**: Country names
        - **Pincode**: Postal codes
        """)
    
    gr.Markdown("""
    ---
    **Models:** 
    - [TinyBERT](https://huggingface.co/shiprocket-ai/open-tinybert-indian-address-ner) | 
    [ModernBERT](https://huggingface.co/shiprocket-ai/open-modernbert-indian-address-ner) | 
    [IndicBERT](https://huggingface.co/shiprocket-ai/open-indicbert-indian-address-ner)
    
    **About:** These models are specifically trained on Indian address patterns and can handle various formats and styles common in Indian addresses.
    """)

if __name__ == "__main__":
    demo.launch()