import gradio as gr import pandas as pd import gspread from oauth2client.service_account import ServiceAccountCredentials from datetime import datetime # --- Config --- USER_CREDENTIALS = {"andrew@lortechnologies.com": "Pass.123"} # --- Google Sheets Setup --- 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("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())) # Time filter 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] # Key metrics 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}" ) # --- Interface Functions --- 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" def report(period): data = get_sheet_data() return generate_report(data, period) # --- Gradio UI --- with gr.Blocks() as demo: 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 Period") report_output = gr.Markdown() report_btn = gr.Button("Generate Report") 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) demo.launch()