Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import warnings | |
| import os | |
| from huggingface_hub import hf_hub_download | |
| warnings.filterwarnings('ignore') | |
| backlog_df = None | |
| def initialize_data(): | |
| global backlog_df | |
| try: | |
| print("Checking for data files...") | |
| repo_id = os.environ.get("SPACE_ID", "Niketha123/open-order-quantity") | |
| print(f"Repository: {repo_id}") | |
| try: | |
| print("Downloading BACKLOG.xlsx from Hugging Face...") | |
| file_path = hf_hub_download( | |
| repo_id=repo_id, | |
| filename="BACKLOG.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}") | |
| file_path = "BACKLOG.xlsx" | |
| print(f"Loading data from: {file_path}") | |
| backlog_df = pd.read_excel(file_path) | |
| print(f"β Successfully loaded {len(backlog_df):,} rows") | |
| print(f"Columns: {list(backlog_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_open_quantity_orders(): | |
| if backlog_df is None: | |
| return pd.DataFrame({"Message": ["β οΈ Data not loaded. Check logs for details."]}) | |
| try: | |
| open_orders = [] | |
| print(f"Processing {len(backlog_df):,} rows...") | |
| for idx, row in backlog_df.iterrows(): | |
| open_qty = float(row.get('OPEN_QUANTITY_OF_SO', 0)) | |
| unit_price = float(row.get('UNIT_PRICE', 0)) | |
| if open_qty > 0: | |
| open_value = open_qty * unit_price | |
| open_orders.append({ | |
| 'Sales Order': row.get('SALES_ORDER_NO', 'N/A'), | |
| 'Material ID': row.get('SAP_MATERIAL_NO', 'N/A'), | |
| 'Open Quantity': f"{open_qty:,.0f}", | |
| 'Open Value': f"${open_value:,.2f}", | |
| 'Supplier': row.get('SUPPLIER_NAME', 'N/A'), | |
| 'Sort_Value': open_value | |
| }) | |
| print(f"β Found {len(open_orders):,} open orders") | |
| df = pd.DataFrame(open_orders) | |
| if len(df) > 0: | |
| df = df.sort_values('Sort_Value', ascending=False) | |
| df = df.drop(columns=['Sort_Value']) | |
| df = df.reset_index(drop=True) | |
| return df if len(df) > 0 else pd.DataFrame({"Message": ["No open quantity 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 Open Order Quantity Dashboard...") | |
| print("=" * 60) | |
| data_loaded = initialize_data() | |
| print(f"Data loaded status: {data_loaded}") | |
| with gr.Blocks(title="Open Order Quantity", theme=gr.themes.Soft()) as dashboard: | |
| gr.Markdown("# π¦ Open Order Quantity") | |
| if not data_loaded: | |
| gr.Markdown(""" | |
| β οΈ **Error loading data files.** | |
| The data file may be in Git LFS. Check the Container logs for details. | |
| """) | |
| open_qty_table = gr.Dataframe( | |
| value=get_open_quantity_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_open_quantity_orders, outputs=open_qty_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="π Open Order Quantity - Enter Your Credentials" | |
| ) |