IAMTFRMZA commited on
Commit
bc2d37c
Β·
verified Β·
1 Parent(s): d975ba4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -49
app.py CHANGED
@@ -4,31 +4,13 @@ from oauth2client.service_account import ServiceAccountCredentials
4
  import gradio as gr
5
  import plotly.express as px
6
 
7
- # ------------------ Authentication ------------------
8
- VALID_USERS = {
9
- "[email protected]": "Pass.123",
10
- "[email protected]": "Pass.123",
11
  "[email protected]": "Pass.123",
 
 
12
  }
13
 
14
- def login():
15
- st.title("πŸ” Login Required")
16
- email = st.text_input("Email")
17
- password = st.text_input("Password", type="password")
18
- if st.button("Login"):
19
- if VALID_USERS.get(email) == password:
20
- st.session_state.authenticated = True
21
- st.rerun()
22
- else:
23
- st.error("❌ Incorrect email or password.")
24
-
25
- if "authenticated" not in st.session_state:
26
- st.session_state.authenticated = False
27
-
28
- if not st.session_state.authenticated:
29
- login()
30
- st.stop()
31
-
32
  # === Google Sheets Auth ===
33
  scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
34
  creds = ServiceAccountCredentials.from_json_keyfile_name("tough-star.json", scope)
@@ -53,10 +35,10 @@ df = df.sort_values(by=['Rep Name', 'Timestamp'])
53
  df['Time Diff (min)'] = df.groupby(['Rep Name', 'Date'])['Timestamp'].diff().dt.total_seconds().div(60).fillna(0)
54
  df['Visit Order'] = df.groupby(['Rep Name', 'Date']).cumcount() + 1
55
 
56
- # === Helper: All unique reps in dataset ===
57
  all_reps = sorted(df['Rep Name'].dropna().unique())
58
 
59
- # === Tab 1: Summary ===
60
  def generate_summary(date_str):
61
  day_df = df[df['Date'] == date_str]
62
  active = day_df.groupby('Rep Name').size().reset_index(name='Total Visits')
@@ -65,7 +47,7 @@ def generate_summary(date_str):
65
  inactive_df = pd.DataFrame({'Inactive Reps': inactive_list})
66
  return active, inactive_df
67
 
68
- # === Tab 2: KAMs ===
69
  def get_reps(date_str):
70
  reps = df[df['Date'] == date_str]['Rep Name'].dropna().unique()
71
  return gr.update(choices=sorted(reps))
@@ -112,7 +94,6 @@ def show_map(date_str, rep):
112
 
113
  fig.update_layout(mapbox_style="open-street-map", title=f"πŸ“ {rep}'s Route on {date_str}")
114
 
115
- # Final table
116
  table = subset[[
117
  'Visit Order', 'Dealership Name', 'Time', 'Time Diff (min)',
118
  'Type of call', 'Sales or service'
@@ -124,7 +105,6 @@ def show_map(date_str, rep):
124
  'Sales or service': 'πŸ’Ό Category'
125
  })
126
 
127
- # Add summary row
128
  total_time = round(table['⏱️ Time Spent'].sum(), 2)
129
  summary_row = pd.DataFrame([{
130
  'Visit Order': '',
@@ -138,28 +118,47 @@ def show_map(date_str, rep):
138
 
139
  return table, fig
140
 
141
- # === Gradio UI ===
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  with gr.Blocks() as app:
143
- gr.Markdown("## πŸ—‚οΈ Carfind Rep Tracker")
144
-
145
- with gr.Tab("πŸ“Š Summary"):
146
- date_summary = gr.Dropdown(label="Select Date", choices=sorted(df['Date'].unique(), reverse=True))
147
- active_table = gr.Dataframe(label="βœ… Active Reps (with total visits)")
148
- inactive_table = gr.Dataframe(label="⚠️ Inactive Reps")
149
- date_summary.change(fn=generate_summary, inputs=date_summary, outputs=[active_table, inactive_table])
150
-
151
- with gr.Tab("πŸ‘€ KAM's"):
152
- with gr.Row():
153
- with gr.Column(scale=1):
154
- date_picker = gr.Dropdown(label="Select Date", choices=sorted(df['Date'].unique(), reverse=True))
155
- rep_picker = gr.Dropdown(label="Select Rep")
156
- btn = gr.Button("Show Route")
157
- with gr.Column(scale=2):
158
- table = gr.Dataframe(label="Call Table")
159
-
160
- map_plot = gr.Plot(label="Map")
161
-
162
- date_picker.change(fn=get_reps, inputs=date_picker, outputs=rep_picker)
163
- btn.click(fn=show_map, inputs=[date_picker, rep_picker], outputs=[table, map_plot])
164
 
165
  app.launch()
 
4
  import gradio as gr
5
  import plotly.express as px
6
 
7
+ # === Allowed Users ===
8
+ allowed_users = {
 
 
9
  "[email protected]": "Pass.123",
10
+ "[email protected]": "Pass.123",
11
+ "[email protected]": "Pass.123"
12
  }
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  # === Google Sheets Auth ===
15
  scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
16
  creds = ServiceAccountCredentials.from_json_keyfile_name("tough-star.json", scope)
 
35
  df['Time Diff (min)'] = df.groupby(['Rep Name', 'Date'])['Timestamp'].diff().dt.total_seconds().div(60).fillna(0)
36
  df['Visit Order'] = df.groupby(['Rep Name', 'Date']).cumcount() + 1
37
 
38
+ # === All reps ===
39
  all_reps = sorted(df['Rep Name'].dropna().unique())
40
 
41
+ # === Summary logic ===
42
  def generate_summary(date_str):
43
  day_df = df[df['Date'] == date_str]
44
  active = day_df.groupby('Rep Name').size().reset_index(name='Total Visits')
 
47
  inactive_df = pd.DataFrame({'Inactive Reps': inactive_list})
48
  return active, inactive_df
49
 
50
+ # === KAM logic ===
51
  def get_reps(date_str):
52
  reps = df[df['Date'] == date_str]['Rep Name'].dropna().unique()
53
  return gr.update(choices=sorted(reps))
 
94
 
95
  fig.update_layout(mapbox_style="open-street-map", title=f"πŸ“ {rep}'s Route on {date_str}")
96
 
 
97
  table = subset[[
98
  'Visit Order', 'Dealership Name', 'Time', 'Time Diff (min)',
99
  'Type of call', 'Sales or service'
 
105
  'Sales or service': 'πŸ’Ό Category'
106
  })
107
 
 
108
  total_time = round(table['⏱️ Time Spent'].sum(), 2)
109
  summary_row = pd.DataFrame([{
110
  'Visit Order': '',
 
118
 
119
  return table, fig
120
 
121
+ # === Gradio App ===
122
+ def build_dashboard():
123
+ with gr.Blocks() as dashboard:
124
+ gr.Markdown("## πŸ—‚οΈ Carfind Rep Tracker")
125
+
126
+ with gr.Tab("πŸ“Š Summary"):
127
+ date_summary = gr.Dropdown(label="Select Date", choices=sorted(df['Date'].unique(), reverse=True))
128
+ active_table = gr.Dataframe(label="βœ… Active Reps (with total visits)")
129
+ inactive_table = gr.Dataframe(label="⚠️ Inactive Reps")
130
+ date_summary.change(fn=generate_summary, inputs=date_summary, outputs=[active_table, inactive_table])
131
+
132
+ with gr.Tab("πŸ‘€ KAM's"):
133
+ with gr.Row():
134
+ with gr.Column(scale=1):
135
+ date_picker = gr.Dropdown(label="Select Date", choices=sorted(df['Date'].unique(), reverse=True))
136
+ rep_picker = gr.Dropdown(label="Select Rep")
137
+ btn = gr.Button("Show Route")
138
+ with gr.Column(scale=2):
139
+ table = gr.Dataframe(label="Call Table")
140
+
141
+ map_plot = gr.Plot(label="Map")
142
+ date_picker.change(fn=get_reps, inputs=date_picker, outputs=rep_picker)
143
+ btn.click(fn=show_map, inputs=[date_picker, rep_picker], outputs=[table, map_plot])
144
+ return dashboard
145
+
146
+ def authenticate(email, password):
147
+ if email in allowed_users and allowed_users[email] == password:
148
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
149
+ else:
150
+ return "Invalid login", None, None
151
+
152
  with gr.Blocks() as app:
153
+ gr.Markdown("## πŸ” Login to Access Carfind Rep Tracker")
154
+ with gr.Row():
155
+ email_input = gr.Textbox(label="Email")
156
+ pass_input = gr.Textbox(label="Password", type="password")
157
+ login_button = gr.Button("Login")
158
+ error_box = gr.Markdown("", visible=False)
159
+ dashboard = build_dashboard()
160
+ dashboard.visible = False
161
+
162
+ login_button.click(fn=authenticate, inputs=[email_input, pass_input], outputs=[error_box, error_box, dashboard])
 
 
 
 
 
 
 
 
 
 
 
163
 
164
  app.launch()