Add inference time
Browse files
Makefile
CHANGED
|
@@ -12,6 +12,9 @@ clean:
|
|
| 12 |
run:
|
| 13 |
source .venv/bin/activate ; python app.py
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
black-check:
|
| 16 |
source .venv/bin/activate ; black . --check --verbose --line-length 120
|
| 17 |
|
|
|
|
| 12 |
run:
|
| 13 |
source .venv/bin/activate ; python app.py
|
| 14 |
|
| 15 |
+
debug:
|
| 16 |
+
source .venv/bin/activate ; gradio app.py
|
| 17 |
+
|
| 18 |
black-check:
|
| 19 |
source .venv/bin/activate ; black . --check --verbose --line-length 120
|
| 20 |
|
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import torch
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import gradio as gr
|
| 4 |
import json
|
|
|
|
| 5 |
|
| 6 |
model_id = "dylanhogg/gnaf-structured-address-v0.1-75a1791-20250921-063650"
|
| 7 |
max_new_tokens = 256
|
|
@@ -32,26 +33,29 @@ pipe = pipeline(
|
|
| 32 |
|
| 33 |
print(f"Model {pipe.model=}")
|
| 34 |
print(f"Model config {pipe.model.config=}")
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
-
def parse_address(user_address: str) -> tuple[str, str]:
|
| 38 |
-
"""Parse address and return both raw response and
|
| 39 |
user_content = f"Translate a text address into structured json.\n{user_address}"
|
| 40 |
messages = [
|
| 41 |
{"role": "system", "content": "Translate a text address into structured json."},
|
| 42 |
{"role": "user", "content": user_content},
|
| 43 |
]
|
| 44 |
|
|
|
|
| 45 |
outputs = pipe(
|
| 46 |
messages,
|
| 47 |
max_new_tokens=max_new_tokens,
|
| 48 |
do_sample=do_sample,
|
| 49 |
)
|
|
|
|
| 50 |
|
| 51 |
response = outputs[0]["generated_text"]
|
| 52 |
last_content = response[-1]["content"]
|
| 53 |
|
| 54 |
-
#
|
| 55 |
try:
|
| 56 |
# Find first '{' and last '}' to extract JSON
|
| 57 |
start = last_content.find("{")
|
|
@@ -65,23 +69,29 @@ def parse_address(user_address: str) -> tuple[str, str]:
|
|
| 65 |
except Exception as e:
|
| 66 |
formatted_json = f"Error parsing JSON: {str(e)}\n\nRaw output:\n{last_content}"
|
| 67 |
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
with gr.Blocks(title="Address Parser") as demo:
|
| 72 |
gr.Markdown("# π Structured Address Parser")
|
| 73 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
with gr.Row():
|
| 76 |
with gr.Column():
|
| 77 |
input_text = gr.Textbox(
|
| 78 |
-
label="Input Address", placeholder="Enter an address...", value="48a Pirrama Rd Pyrmont NSW 2009"
|
| 79 |
)
|
| 80 |
submit_btn = gr.Button("Parse Address", variant="primary")
|
| 81 |
|
| 82 |
with gr.Column():
|
| 83 |
-
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
gr.Examples(
|
| 87 |
examples=[
|
|
@@ -117,8 +127,8 @@ with gr.Blocks(title="Address Parser") as demo:
|
|
| 117 |
inputs=input_text,
|
| 118 |
)
|
| 119 |
|
| 120 |
-
submit_btn.click(fn=parse_address, inputs=input_text, outputs=[json_output, raw_output])
|
| 121 |
-
input_text.submit(fn=parse_address, inputs=input_text, outputs=[json_output, raw_output])
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
print("Launching app...")
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import gradio as gr
|
| 4 |
import json
|
| 5 |
+
import time
|
| 6 |
|
| 7 |
model_id = "dylanhogg/gnaf-structured-address-v0.1-75a1791-20250921-063650"
|
| 8 |
max_new_tokens = 256
|
|
|
|
| 33 |
|
| 34 |
print(f"Model {pipe.model=}")
|
| 35 |
print(f"Model config {pipe.model.config=}")
|
| 36 |
+
print("Ready to parse addresses!")
|
| 37 |
|
| 38 |
|
| 39 |
+
def parse_address(user_address: str) -> tuple[str, str, str]:
|
| 40 |
+
"""Parse address and return both raw response, JSON, and timing info"""
|
| 41 |
user_content = f"Translate a text address into structured json.\n{user_address}"
|
| 42 |
messages = [
|
| 43 |
{"role": "system", "content": "Translate a text address into structured json."},
|
| 44 |
{"role": "user", "content": user_content},
|
| 45 |
]
|
| 46 |
|
| 47 |
+
inference_start = time.time()
|
| 48 |
outputs = pipe(
|
| 49 |
messages,
|
| 50 |
max_new_tokens=max_new_tokens,
|
| 51 |
do_sample=do_sample,
|
| 52 |
)
|
| 53 |
+
inference_time = time.time() - inference_start
|
| 54 |
|
| 55 |
response = outputs[0]["generated_text"]
|
| 56 |
last_content = response[-1]["content"]
|
| 57 |
|
| 58 |
+
# Parse JSON (no timing needed)
|
| 59 |
try:
|
| 60 |
# Find first '{' and last '}' to extract JSON
|
| 61 |
start = last_content.find("{")
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
formatted_json = f"Error parsing JSON: {str(e)}\n\nRaw output:\n{last_content}"
|
| 71 |
|
| 72 |
+
timing_info = f"{inference_time:.3f}s (on {device} device)"
|
| 73 |
+
return formatted_json, last_content, timing_info
|
| 74 |
|
| 75 |
|
| 76 |
with gr.Blocks(title="Address Parser") as demo:
|
| 77 |
gr.Markdown("# π Structured Address Parser")
|
| 78 |
+
gr.Markdown("Extracts structured address information from text.")
|
| 79 |
+
gr.Markdown(
|
| 80 |
+
"Structured output is JSON format with Australian [G-NAF](https://docs.geoscape.com.au/projects/gnaf_desc/en/stable/overview.html) (Geocoded National Address File) fields."
|
| 81 |
+
)
|
| 82 |
+
gr.Markdown(f"Model: [{model_id}](https://huggingface.co/{model_id})")
|
| 83 |
|
| 84 |
with gr.Row():
|
| 85 |
with gr.Column():
|
| 86 |
input_text = gr.Textbox(
|
| 87 |
+
label="π Input Address", placeholder="Enter an address...", value="48a Pirrama Rd Pyrmont NSW 2009"
|
| 88 |
)
|
| 89 |
submit_btn = gr.Button("Parse Address", variant="primary")
|
| 90 |
|
| 91 |
with gr.Column():
|
| 92 |
+
took_output = gr.Textbox(label="β±οΈ Inference Time", interactive=False, max_lines=1)
|
| 93 |
+
json_output = gr.Textbox(label="π Structured JSON", interactive=False, lines=10)
|
| 94 |
+
raw_output = gr.Textbox(label="π Raw Model Output", interactive=False, lines=3)
|
| 95 |
|
| 96 |
gr.Examples(
|
| 97 |
examples=[
|
|
|
|
| 127 |
inputs=input_text,
|
| 128 |
)
|
| 129 |
|
| 130 |
+
submit_btn.click(fn=parse_address, inputs=input_text, outputs=[json_output, raw_output, took_output])
|
| 131 |
+
input_text.submit(fn=parse_address, inputs=input_text, outputs=[json_output, raw_output, took_output])
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
| 134 |
print("Launching app...")
|