"""Gradio UI for the text Preprocessor / Normalizer (big-screen / demo layout).
Run: python app.py -> http://127.0.0.1:7860
python app.py --host 0.0.0.0 -> reachable from other computers
Place this file next to normalizer.py (inside the Preprocessor folder).
"""
import argparse
import ast
import html
import socket
from pathlib import Path
import gradio as gr
from normalizer import Normalizer
normalizer = Normalizer()
HERE = Path(__file__).parent
def normalize(text: str) -> str:
if not text or not text.strip():
return ""
# Process line by line so multi-line input keeps its structure.
return "\n".join(normalizer.process(line) if line.strip() else line
for line in text.splitlines())
def load_example_groups(path: Path = HERE / "simple_test.py") -> dict[str, list[list[str]]]:
"""Collect every `n.process("...")` input from simple_test.py, grouped by the
test function it lives in (currency, units, ...). Stays in sync with the tests."""
if not path.exists():
return {}
tree = ast.parse(path.read_text(encoding="utf-8"))
groups, seen = {}, set()
for func in tree.body:
if not (isinstance(func, ast.FunctionDef) and func.name.startswith("test_")):
continue
title = (func.name.removeprefix("test_").removesuffix("_normalization")
.replace("_", " ").strip().title())
title = {"Postal Nationalid": "Postal Code & National ID"}.get(title, title)
items = []
for node in ast.walk(func):
if (isinstance(node, ast.Call)
and isinstance(node.func, ast.Attribute)
and node.func.attr == "process"
and node.args
and isinstance(node.args[0], ast.Constant)
and isinstance(node.args[0].value, str)
and node.args[0].value not in seen):
seen.add(node.args[0].value)
items.append((node.lineno, node.args[0].value))
if items:
groups[title] = [[s] for _, s in sorted(items)]
return groups
# ---- example gallery (custom HTML so we control the design) -----------------
PALETTE = ["#6366f1", "#0ea5e9", "#10b981", "#f59e0b", "#ef4444", "#8b5cf6", "#ec4899", "#14b8a6"]
def build_gallery(groups: dict[str, list[list[str]]]) -> str:
colors = {name: PALETTE[i % len(PALETTE)] for i, name in enumerate(groups)}
total = sum(len(v) for v in groups.values())
chips = [f'']
for name, rows in groups.items():
chips.append(f'')
cards = []
for name, rows in groups.items():
for (text,) in rows:
result = normalize(text)
cards.append(
f'