File size: 5,749 Bytes
edc5316
 
 
b794a09
 
 
 
 
 
 
edc5316
 
 
 
b794a09
 
 
edc5316
 
 
 
 
 
 
 
 
 
 
b794a09
edc5316
 
 
 
 
 
b794a09
 
edc5316
b794a09
 
 
 
edc5316
b794a09
 
 
edc5316
 
b794a09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc5316
b794a09
edc5316
b794a09
edc5316
 
b794a09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc5316
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import streamlit as st

# Import backend functions
from backend import (
    validate_incoterms_xml,
    suggest_correct_incoterm,
    validate_po_supplier_xml,
    suggest_correct_supplier,
    apply_combined_corrections,
)


def main():
    """
    Main function to set up the Streamlit web application.
    Validates both Incoterms location and Purchase Order Supplier ID,
    and provides AI-based suggestions for corrections.
    """
    # Configure Streamlit page layout and initial settings
    st.set_page_config(
        page_title="TM XML Validator",
        layout="centered",
        initial_sidebar_state="auto",
    )

    # Display the title and description of the app
    st.title("TM XML Validator")
    st.write(
        "Upload your XML file to validate Incoterms and Purchase Order Supplier ID."
    )

    # File upload widget for XML files
    uploaded_file = st.file_uploader("Upload an XML file", type="xml")

    if uploaded_file:
        try:
            st.subheader("Validation Results")

            # Perform Incoterms validation
            incoterm_status, original_location, incoterm_suggestion = (
                handle_incoterms_validation(uploaded_file)
            )

            # Perform Supplier ID validation
            supplier_status, original_supplier_id, supplier_suggestion = (
                handle_supplier_validation(uploaded_file)
            )

            # Handle corrections if either validation fails
            if incoterm_status == "invalid" or supplier_status == "invalid":
                st.subheader("Suggested Corrections")

                # Checkbox for Incoterm correction suggestion
                apply_incoterm_correction = False
                if incoterm_suggestion:
                    apply_incoterm_correction = st.checkbox(
                        f"Accept suggested Incoterm correction: {incoterm_suggestion}"
                    )

                # Checkbox for Supplier correction suggestion
                apply_supplier_correction = False
                if supplier_suggestion:
                    apply_supplier_correction = st.checkbox(
                        f"Accept suggested Supplier ID correction: {supplier_suggestion}"
                    )

                # Apply corrections and provide corrected XML for download
                corrected_xml = apply_combined_corrections(
                    uploaded_file,
                    incoterm_correction=(
                        incoterm_suggestion if apply_incoterm_correction else None
                    ),
                    supplier_correction=(
                        supplier_suggestion if apply_supplier_correction else None
                    ),
                )

                st.download_button(
                    label="Apply Selected Corrections and Download Corrected XML",
                    data=corrected_xml,
                    file_name=f"corrected_combined_{uploaded_file.name}",
                    mime="application/xml",
                )
        except Exception as e:
            st.error(f"An error occurred: {str(e)}")


def handle_incoterms_validation(file):
    """
    Validates Incoterms in the uploaded XML file and provides correction suggestions if needed.

    Args:
        file: Uploaded XML file.

    Returns:
        tuple: (validation status, original location, suggested correction).
    """
    try:
        # Validate Incoterms location
        status, original_location = validate_incoterms_xml(file)

        if status == "valid":
            st.success("Incoterms Validation Passed: The location is correctly mapped.")
            st.markdown(f"**Location Name:** {original_location}")
            return "valid", original_location, None
        elif status == "invalid":
            st.error("Incoterms Validation Failed: Incorrect location found.")
            st.markdown(f"**Incorrect Location Name:** {original_location}")

            # Provide suggested correction
            suggestion_sentence, ai_suggestion = suggest_correct_incoterm(
                original_location
            )
            st.warning(f"Suggested Correction for Location: {suggestion_sentence}")
            return "invalid", original_location, ai_suggestion

    except Exception as e:
        st.error(f"An error occurred during Incoterms validation: {str(e)}")
        return "error", None, None


def handle_supplier_validation(file):
    """
    Validates Supplier ID in the uploaded XML file and provides correction suggestions if needed.

    Args:
        file: Uploaded XML file.

    Returns:
        tuple: (validation status, original supplier ID, suggested correction).
    """
    try:
        # Validate Supplier ID
        status, supplier_id, tag_path = validate_po_supplier_xml(file)

        if status == "valid":
            st.success(
                "Supplier Validation Passed: The supplier information is correct."
            )
            st.markdown(f"**Supplier ID:** {supplier_id}")
            return "valid", supplier_id, None
        elif status == "invalid":
            st.error(
                "Supplier Validation Failed: Incorrect supplier information found."
            )
            st.markdown(f"**Incorrect Supplier ID:** {supplier_id}")

            # Provide suggested correction
            suggestion_sentence, ai_suggestion = suggest_correct_supplier(supplier_id)
            st.warning(f"Suggested Correction for Supplier ID: {suggestion_sentence}")
            return "invalid", supplier_id, ai_suggestion

    except Exception as e:
        st.error(f"An error occurred during Supplier validation: {str(e)}")
        return "error", None, None


# Entry point of the app
if __name__ == "__main__":
    main()