Niketha123's picture
Update app.py
f8274f2 verified
Raw
History Blame Contribute Delete
4.26 kB
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"
)