Spaces:
Running
Running
Update app.py
Browse files4 unmodified lines567891011121 unmodified line14151617181920212223242526272829303152 unmodified lines848586878889909192939495969798991001011021031041051061071084 unmodified linesimport tempfileimport os# --- CORE LOGIC FUNCTIONS ---def clean_assignment(val):val = str(val).upper()match = re.search(r'TILL(\d+)', val)1 unmodified linedef process_reconciliation(file_main, file_mapping, file_mid, start_match_id):try:# Load files - skipping 4 rows for the specific MID header structuremain_df = pd.read_csv(file_main.name)mapping_df = pd.read_csv(file_mapping.name)mid_df = pd.read_csv(file_mid.name, skiprows=4)# Clean Headersmain_df.columns = main_df.columns.str.strip()mapping_df.columns = mapping_df.columns.str.strip()mid_df.columns = mid_df.columns.str.strip()# Build Lookupsmid_map = dict(zip(mid_df['Outlet Number'].astype(str), mid_df['Customer Reference No'].astype(str).str.split('.').str[0].str.zfill(4)))till_map = {clean_assignment(str(a)): str(b).zfill(4) for a, b in zip(mapping_df['Assignment'], mapping_df['Branch number'])}# Branch Identification52 unmodified linesexcept Exception as e:raise gr.Error(f"System Error: {str(e)}")# --- UI STYLING & INTERFACE ---custom_css = """.header-banner { background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%); padding: 2.5rem; border-radius: 12px; text-align: center; margin-bottom: 2rem; border: 1px solid #334155; }.header-banner h1 { color: #f8fafc !important; font-size: 2.25rem !important; } .header-banner p { color: #94a3b8 !important; }"""with gr.Blocks(css=custom_css) as demo:gr.HTML('<div class="header-banner"><h1>🧮 CCCASH Auto Reconciliation Tool</h1><p>Secure Ledger Matching & Exception Detection</p></div>')with gr.Row():file_main = gr.File(label="SAP Main Ledger (CSV)")file_map = gr.File(label="TILL Mapping (CSV)")file_mid = gr.File(label="MID Mapping (CSV)")start_id = gr.Number(value=1000, label="Starting Match ID")btn = gr.Button("Execute Match Engine", variant="primary")with gr.Row():out1 = gr.File(label="Verified Output")out2 = gr.File(label="Exception List")btn.click(process_reconciliation, [file_main, file_map, file_mid, start_id], [out1, out2])# Security AuthHF_USER = os.environ.get("APP_USERNAME", "cityplumbing")HF_PASS = os.environ.get("APP_PASSWORD", "cashandbanking")demo.launch(auth=(HF_USER, HF_PASS))4 unmodified lines56789101 unmodified line12131415161718192021222324252627282930313233343552 unmodified lines8889909192939495969798991001011021031041051061071084 unmodified linesimport tempfileimport osdef clean_assignment(val):val = str(val).upper()match = re.search(r'TILL(\d+)', val)1 unmodified linedef process_reconciliation(file_main, file_mapping, file_mid, start_match_id):try:# Load filesmain_df = pd.read_csv(file_main.name)mapping_df = pd.read_csv(file_mapping.name)# Using header=4 to skip the first 4 rows that contain descriptive textmid_df = pd.read_csv(file_mid.name, header=4)# Strip all whitespace from headers to prevent KeyErrorsmain_df.columns = main_df.columns.str.strip()mapping_df.columns = mapping_df.columns.str.strip()mid_df.columns = mid_df.columns.str.strip()# Identify the correct Branch column name dynamically if 'Customer Reference No' varies# We use a case-insensitive match for the column namebranch_col = [col for col in mid_df.columns if 'Customer' in col][0]outlet_col = [col for col in mid_df.columns if 'Outlet' in col][0]# Build Lookupsmid_map = dict(zip(mid_df[outlet_col].astype(str), mid_df[branch_col].astype(str).str.split('.').str[0].str.zfill(4)))till_map = {clean_assignment(str(a)): str(b).zfill(4) for a, b in zip(mapping_df['Assignment'], mapping_df['Branch number'])}# Branch Identification52 unmodified linesexcept Exception as e:raise gr.Error(f"System Error: {str(e)}")# UI Stylingcustom_css = """.header-banner { background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%); padding: 2.5rem; border-radius: 12px; text-align: center; margin-bottom: 2rem; border: 1px solid #334155; }.header-banner h1 { color: #f8fafc !important; font-size: 2.25rem !important; } .header-banner p { color: #94a3b8 !important; }"""with gr.Blocks(css=custom_css) as demo:gr.HTML('<div class="header-banner"><h1>🧮 CCCASH Auto Reconciliation Tool</h1></div>')with gr.Row():file_main = gr.File(label="SAP Main Ledger (CSV)")file_map = gr.File(label="TILL Mapping (CSV)")file_mid = gr.File(label="MID Mapping (CSV)")start_id = gr.Number(value=1000, label="Starting Match ID")btn = gr.Button("Execute Match Engine", variant="primary")out1, out2 = gr.File(label="Matched CSV"), gr.File(label="Unmatched CSV")btn.click(process_reconciliation, [file_main, file_map, file_mid, start_id], [out1, out2])HF_USER = os.environ.get("APP_USERNAME", "cityplumbing")HF_PASS = os.environ.get("APP_PASSWORD", "cashandbanking")demo.launch(auth=(HF_USER, HF_PASS))
app.py+13-13