import gradio as gr import pandas as pd import warnings import os from huggingface_hub import hf_hub_download warnings.filterwarnings('ignore') billing_df = None def initialize_data(): global billing_df try: print("Checking for data files...") # For LFS files, we need to use hf_hub_download # Get the repo info from environment variables repo_id = os.environ.get("SPACE_ID", "Niketha123/orders-fulfilled") print(f"Repository: {repo_id}") try: # Download the file from HF hub (handles LFS automatically) print("Downloading BILLING.xlsx from Hugging Face...") file_path = hf_hub_download( repo_id=repo_id, filename="BILLING.xlsx", repo_type="space" ) print(f"✅ File downloaded to: {file_path}") except Exception as e: print(f"Could not download from hub, trying local path: {e}") # Fall back to local path file_path = "BILLING.xlsx" # Load the Excel file print(f"Loading data from: {file_path}") billing_df = pd.read_excel(file_path) print(f"✅ Successfully loaded {len(billing_df):,} rows") print(f"Columns: {list(billing_df.columns)[:10]}") return True except Exception as e: print(f"❌ Error loading data: {str(e)}") import traceback traceback.print_exc() return False def get_fulfilled_orders(): if billing_df is None: return pd.DataFrame({"Message": ["⚠️ Data not loaded. Check logs for details."]}) try: fulfilled_orders = [] print(f"Processing {len(billing_df):,} rows...") for idx, row in billing_df.iterrows(): billing_qty = float(row.get('BILLING_QUANTITY', 0)) extended_resale = float(row.get('EXTENDED_RESALE_USD', 0)) if billing_qty > 0: fulfilled_orders.append({ 'Sales Order': row.get('SALES_ORDER_NO', 'N/A'), 'End Customer': row.get('END_CUSTOMER_NAME', 'N/A'), 'Order Value': f"${extended_resale:,.2f}", 'Sort_Value': extended_resale # Numeric value for sorting }) print(f"✅ Found {len(fulfilled_orders):,} fulfilled orders") df = pd.DataFrame(fulfilled_orders) if len(df) > 0: # Sort by numeric value in descending order (highest first) df = df.sort_values('Sort_Value', ascending=False) # Drop the sorting column before displaying df = df.drop(columns=['Sort_Value']) # Reset index for clean display df = df.reset_index(drop=True) return df if len(df) > 0 else pd.DataFrame({"Message": ["No fulfilled orders found"]}) except Exception as e: print(f"❌ Error processing orders: {str(e)}") import traceback traceback.print_exc() return pd.DataFrame({"Error": [str(e)]}) def create_dashboard(): print("=" * 60) print("Initializing Orders Fulfilled Dashboard...") print("=" * 60) data_loaded = initialize_data() print(f"Data loaded status: {data_loaded}") with gr.Blocks(title="Orders Fulfilled", theme=gr.themes.Soft()) as dashboard: gr.Markdown("# ✅ Orders Fulfilled") if not data_loaded: gr.Markdown(""" ⚠️ **Error loading data files.** The data file may be in Git LFS. Check the Container logs for details. """) fulfilled_table = gr.Dataframe( value=get_fulfilled_orders() if data_loaded else pd.DataFrame({"Message": ["Data not loaded"]}), wrap=True ) refresh_btn = gr.Button("🔄 Refresh", variant="secondary") refresh_btn.click(fn=get_fulfilled_orders, outputs=fulfilled_table) return dashboard if __name__ == "__main__": dashboard = create_dashboard() dashboard.launch( server_name="0.0.0.0", server_port=7860, ssr_mode=False, auth=[ ("kinnari", "alert2025"), ("neha", "alert2025"), ("niketha", "alert2025"), ("sahith", "alert2025"), ("shriya", "alert2025"), ], auth_message="📊 Orders Fulfilled - Enter Your Credentials" )