File size: 2,303 Bytes
da4a4d2
 
 
f2135f8
43a35e2
 
 
f2135f8
 
43a35e2
 
 
 
 
 
da4a4d2
43a35e2
f2135f8
 
 
 
 
43a35e2
 
 
 
 
 
 
f2135f8
 
 
 
 
43a35e2
 
 
f2135f8
 
 
43a35e2
 
 
 
 
 
 
 
 
 
 
 
 
f2135f8
 
43a35e2
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd

from transformers import (
    normalize_open_invoices,
    normalize_payment_notification,
)
from invoice_schema import invoice_pipeline_schema
from validator import ColumnValidator


def process_files(open_invoices_file, payment_notification_file):
    results = {}

    if open_invoices_file is not None:
        # HF uploads are temp files with a .name attribute
        df_open = pd.read_excel(open_invoices_file.name)
        df_open_norm = normalize_open_invoices(
            df_open,
            schema=invoice_pipeline_schema,
            dataset_name="Open Invoices",
        )
        ColumnValidator(invoice_pipeline_schema).validate(
            df_open_norm, dataset_name="Open Invoices (normalized)"
        )
        results["Open Invoices (normalized) head"] = df_open_norm.head().to_markdown()

    if payment_notification_file is not None:
        df_pay = pd.read_excel(payment_notification_file.name)
        df_pay_norm = normalize_payment_notification(
            df_pay,
            schema=invoice_pipeline_schema,
            dataset_name="Payment Notification",
        )
        ColumnValidator(invoice_pipeline_schema).validate(
            df_pay_norm, dataset_name="Payment Notification (normalized)"
        )
        results["Payment Notification (normalized) head"] = (
            df_pay_norm.head().to_markdown()
        )

    if not results:
        return "Upload at least one Excel file to see normalized output."

    parts = []
    for title, md in results.items():
        parts.append(f"## {title}\n\n{md}")
    return "\n\n".join(parts)


with gr.Blocks() as demo:
    gr.Markdown("# Column Matching & Normalization Demo")
    gr.Markdown(
        "Upload your **Open Invoices** and/or **Payment Notification** Excel files.\n"
        "The app will normalize them into the canonical schema and show the first few rows."
    )

    with gr.Row():
        open_file = gr.File(label="Open Invoices (.xlsx)", file_types=[".xlsx"])
        pay_file = gr.File(label="Payment Notification (.xlsx)", file_types=[".xlsx"])

    output = gr.Markdown()

    run_btn = gr.Button("Normalize & Validate")
    run_btn.click(process_files, inputs=[open_file, pay_file], outputs=output)

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