Spaces:
Running
Running
Update app.py
Browse files
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 |
-
#
|
8 |
-
|
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 |
-
# ===
|
57 |
all_reps = sorted(df['Rep Name'].dropna().unique())
|
58 |
|
59 |
-
# ===
|
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 |
-
# ===
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
with gr.Blocks() as app:
|
143 |
-
gr.Markdown("##
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
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()
|