import gradio as gr from rapidfuzz.fuzz import token_set_ratio from rapidfuzz.distance import JaroWinkler import re # Preprocess and normalize Indian address def preprocess_address(address): address = str(address).lower() # Split alphanumeric combinations like 45vastrapur → 45 vastrapur address = re.sub(r"(\d+)([a-zA-Z]+)", r"\1 \2", address) address = re.sub(r"([a-zA-Z]+)(\d+)", r"\1 \2", address) # Replace common Indian address terms replacements = { r"\brd\b": "road", r"\bst\b": "street", r"\bopp\b": "opposite", r"\bnear\b": "", r"\bg\.k\.\b": "greater kailash", r"\bno\b\.?": "", r"\bsec\b": "sector", r"[\.,\-]": " ", # punctuation → space } for pattern, repl in replacements.items(): address = re.sub(pattern, repl, address) address = re.sub(r"\s+", " ", address).strip() return address # Main comparison logic def match_addresses(input_address, reference_address): if not input_address or not reference_address: return "Please enter both addresses.", "", "", "", "", "" clean_input = preprocess_address(input_address) clean_reference = preprocess_address(reference_address) jaro = round(JaroWinkler.normalized_similarity(clean_input, clean_reference) * 100, 2) token = token_set_ratio(clean_input, clean_reference) final = round((jaro + token) / 2, 2) return f"{final}%", clean_input, clean_reference, f"{jaro}%", f"{token}%", "✔ Match Calculated" # Gradio Interface demo = gr.Interface( fn=match_addresses, inputs=[ gr.Textbox(label="Input Address", placeholder="e.g., 45 Vastrapur Vastrapur"), gr.Textbox(label="Reference Address", placeholder="e.g., 45vastrapur Vastrapur") ], outputs=[ gr.Textbox(label="Final Match Score (%)"), gr.Textbox(label="Preprocessed Input"), gr.Textbox(label="Preprocessed Reference"), gr.Textbox(label="Jaro-Winkler Score (%)"), gr.Textbox(label="Token Set Ratio (%)"), gr.Textbox(label="Status") ], title="Indian Address Matcher", description="Compares two Indian addresses using preprocessing + Jaro-Winkler + Token Set Ratio. Handles numeric-locality combinations like '45vastrapur'." ) demo.launch()