dylanhogg's picture
Add more addresses
462b9e0
raw
history blame
4.32 kB
import torch
from transformers import pipeline
import gradio as gr
import json
# Initialize model pipeline
model_id = "dylanhogg/gnaf-structured-address-v0.1-75a1791-20250921-063650"
pipe = pipeline(
"text-generation",
model=model_id,
dtype=torch.bfloat16,
device_map="auto",
)
def parse_address(user_address: str) -> tuple[str, str]:
"""Parse address and return both raw response and JSON"""
user_content = f"Translate a text address into structured json.\n{user_address}"
messages = [
{"role": "system", "content": "Translate a text address into structured json."},
{"role": "user", "content": user_content},
]
outputs = pipe(
messages,
max_new_tokens=256,
do_sample=False,
)
response = outputs[0]["generated_text"]
last_content = response[-1]["content"]
# Try to extract JSON from the response
try:
# Find first '{' and last '}' to extract JSON
start = last_content.find("{")
end = last_content.rfind("}") + 1
if start != -1 and end > start:
json_str = last_content[start:end]
parsed = json.loads(json_str)
formatted_json = json.dumps(parsed, indent=2)
else:
formatted_json = "No JSON found in response"
except Exception as e:
formatted_json = f"Error parsing JSON: {str(e)}\n\nRaw output:\n{last_content}"
return formatted_json, last_content
# Create Gradio interface
with gr.Blocks(title="Address Parser") as demo:
gr.Markdown("# 🏠 Structured Address Parser")
gr.Markdown("This model converts text addresses into structured JSON format.")
with gr.Row():
with gr.Column():
input_text = gr.Textbox(
label="Input Address", placeholder="Enter an address...", value="48a Pirrama Rd Pyrmont NSW 2009"
)
submit_btn = gr.Button("Parse Address", variant="primary")
with gr.Column():
json_output = gr.Textbox(label="Structured JSON", interactive=False, lines=10)
raw_output = gr.Textbox(label="Raw Model Output", interactive=False, lines=5)
# Examples
gr.Examples(
examples=[
"48a Pirrama Rd Pyrmont NSW 2009",
"Floor 3, 152-156 Clarence St, Sydney NSW 2000",
"Aptt 16, 400 Bondi Rd, Bondi NSW 2026",
"Unit 18/14-18 Flood St, Bondi, NSW 2026",
"Lvl 15/333 George St Sydney NSW 2000",
"Check out: 44 Ulm St, Maroubra NSW 2035",
"44 Ulm St, Maroubra NSW 2035 is where it's at!",
"Have you been to level 3 123 George St, Sydney NSW 2000?",
"Suite 4, 27-31 King William St, Adelaide SA 5000",
"Unit 2B 15 O'Connell St, Parramatta NSW 2150",
"Lvl 9, 100 Creek St, Brisbane QLD 4000",
"Shop 5/42 Jetty Rd, Glenelg SA 5045",
"Lot 12, 89 Wattle Grove Rd, Mulgoa NSW 2745",
"Meet me at 20 Smith St, Collingwood VIC 3066 later today!",
"We just moved into 7/145 Marine Parade, Coolangatta QLD 4225.",
"Drop the package at level 2, 88 Pitt St Sydney NSW 2000 please.",
"I think the café is near 11-13 Lygon St, Carlton VIC 3053.",
"Could you send the invoice to our office at 120 Northbourne Ave Canberra ACT 2601?",
"PO Box 42, Dubbo NSW 2830",
"Head to 9/250 St Kilda Rd, Melbourne VIC 3006 for the meeting.",
"Unit 5B, 18-22 Manning St, South Brisbane QLD 4101",
"Corner of King St and Brown St, Newtown NSW 2042",
"Flat 3, 77 Princes Hwy, Dandenong VIC 3175",
"We're staying at 22 Beach Rd, Batemans Bay NSW 2536 this weekend.",
"Drop it off at 6/89 Murray St, Perth WA 6000.",
"Warehouse 12, 45 Industrial Dr, Mayfield NSW 2304",
"Visit us on level 4, 11 Elizabeth St Hobart TAS 7000.",
"The Airbnb is at 3 Hilltop Ave, Burleigh Heads QLD 4220.",
],
inputs=input_text,
)
# Handle events
submit_btn.click(fn=parse_address, inputs=input_text, outputs=[json_output, raw_output])
input_text.submit(fn=parse_address, inputs=input_text, outputs=[json_output, raw_output])
# Launch the app
if __name__ == "__main__":
demo.launch()