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()