import gradio as gr import pandas as pd import gspread from oauth2client.service_account import ServiceAccountCredentials from datetime import datetime # -- Login Setup -- USER_CREDENTIALS = {"andrew@lortechnologies.com": "Pass.123"} # -- Load Google Sheets Data -- def get_sheet_data(): scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"] creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope) client = gspread.authorize(creds) sheet = client.open_by_title("userAccess (6)").worksheet("Field Sales") data = pd.DataFrame(sheet.get_all_records()) return data # -- Reporting Logic -- def generate_report(data, period="Daily"): data["Date"] = pd.to_datetime(data.get("Date", datetime.today())) now = pd.Timestamp.today() if period == "Weekly": data = data[data["Date"] >= now - pd.Timedelta(days=7)] elif period == "Monthly": data = data[data["Date"].dt.month == now.month] elif period == "Yearly": data = data[data["Date"].dt.year == now.year] total_visits = len(data) current = len(data[data["Current/Prospect Custor"] == "Current"]) new = len(data[data["Current/Prospect Custor"] == "Prospect"]) second_hand = len(data[data["Customer Type"].str.contains("Second", na=False)]) telesales = len(data[data["Source"] == "TeleSales"]) oem_visits = len(data[data["Source"] == "OEM Visit"]) orders = len(data[data["Order Received"] == "Yes"]) order_value = data["Order Value"].sum() return ( f"### {period} Report\n" f"- Total Visits: {total_visits}\n" f"- Current Dealerships: {current}\n" f"- New Dealerships: {new}\n" f"- % Current: {current / total_visits * 100:.1f}% | % New: {new / total_visits * 100:.1f}%\n" f"- Second-hand Dealerships: {second_hand}\n" f"- TeleSales Calls: {telesales}\n" f"- OEM Visits: {oem_visits}\n" f"- Orders Received: {orders}\n" f"- Total Order Value: ${order_value:,.2f}" ) # -- Login Handler -- def login(email, password): if USER_CREDENTIALS.get(email) == password: return gr.update(visible=True), gr.update(visible=False), "" else: return gr.update(visible=False), gr.update(visible=True), "❌ Invalid credentials" # -- UI Logic -- def report(period): data = get_sheet_data() return generate_report(data, period) # -- Gradio App -- with gr.Blocks() as app: with gr.Row(visible=True) as login_row: email = gr.Textbox(label="Email") password = gr.Textbox(label="Password", type="password") login_btn = gr.Button("Login") login_error = gr.Textbox(label="", visible=False) with gr.Row(visible=False) as dashboard: period_dropdown = gr.Dropdown(["Daily", "Weekly", "Monthly", "Yearly"], value="Daily", label="Select Report Period") report_btn = gr.Button("Generate Report") report_output = gr.Markdown() login_btn.click(fn=login, inputs=[email, password], outputs=[dashboard, login_error, login_error]) report_btn.click(fn=report, inputs=[period_dropdown], outputs=report_output) app.launch()