Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,118 +3,109 @@ import gspread
|
|
| 3 |
import gradio as gr
|
| 4 |
from oauth2client.service_account import ServiceAccountCredentials
|
| 5 |
from datetime import datetime
|
| 6 |
-
import
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
VALID_USERS = {
|
| 10 |
-
"[email protected]": "Pass.123",
|
| 11 |
-
"[email protected]": "Pass.123",
|
| 12 |
-
"[email protected]": "Pass.123"
|
| 13 |
-
}
|
| 14 |
-
|
| 15 |
-
# ------------------ GOOGLE SHEET SETUP ------------------
|
| 16 |
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
| 17 |
creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
|
| 18 |
client = gspread.authorize(creds)
|
| 19 |
sheet_file = client.open("userAccess")
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
def load_tab(sheet_name
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
if combined:
|
| 71 |
-
return pd.concat(combined, ignore_index=True)
|
| 72 |
-
else:
|
| 73 |
-
return pd.DataFrame([["No orders on this date"]], columns=["Message"])
|
| 74 |
-
|
| 75 |
-
# Define other helper functions similarly using load_tab...
|
| 76 |
-
|
| 77 |
-
# ------------------ GRADIO APP ------------------
|
| 78 |
with gr.Blocks() as app:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
app.launch()
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from oauth2client.service_account import ServiceAccountCredentials
|
| 5 |
from datetime import datetime
|
| 6 |
+
from geopy.distance import geodesic
|
| 7 |
+
import folium
|
| 8 |
+
from io import BytesIO
|
| 9 |
|
| 10 |
+
# Google Sheets Auth
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
| 12 |
creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
|
| 13 |
client = gspread.authorize(creds)
|
| 14 |
sheet_file = client.open("userAccess")
|
| 15 |
|
| 16 |
+
# Load Data
|
| 17 |
+
def load_tab(sheet_name):
|
| 18 |
+
try:
|
| 19 |
+
df = pd.DataFrame(sheet_file.worksheet(sheet_name).get_all_records())
|
| 20 |
+
return df
|
| 21 |
+
except:
|
| 22 |
+
return pd.DataFrame(["β οΈ Could not load sheet."], columns=["Error"])
|
| 23 |
+
|
| 24 |
+
# GPS calculations
|
| 25 |
+
def calculate_gps_data(df):
|
| 26 |
+
df = df.sort_values(['Date', 'Time']).reset_index(drop=True)
|
| 27 |
+
df[['Latitude', 'Longitude']] = df['Location'].str.split(', ', expand=True).astype(float)
|
| 28 |
+
|
| 29 |
+
df['Kms Travelled'] = 0.0
|
| 30 |
+
df['Duration Between Calls (min)'] = 0.0
|
| 31 |
+
|
| 32 |
+
for i in range(1, len(df)):
|
| 33 |
+
prev_coords = (df.at[i-1, 'Latitude'], df.at[i-1, 'Longitude'])
|
| 34 |
+
current_coords = (df.at[i, 'Latitude'], df.at[i, 'Longitude'])
|
| 35 |
+
df.at[i, 'Kms Travelled'] = geodesic(prev_coords, current_coords).km
|
| 36 |
+
|
| 37 |
+
prev_time = pd.to_datetime(df.at[i-1, 'Date'] + ' ' + df.at[i-1, 'Time'])
|
| 38 |
+
current_time = pd.to_datetime(df.at[i, 'Date'] + ' ' + df.at[i, 'Time'])
|
| 39 |
+
df.at[i, 'Duration Between Calls (min)'] = (current_time - prev_time).total_seconds() / 60.0
|
| 40 |
+
|
| 41 |
+
return df
|
| 42 |
+
|
| 43 |
+
# Load and process Field Sales data
|
| 44 |
+
field_sales_df = calculate_gps_data(load_tab("Field Sales"))
|
| 45 |
+
|
| 46 |
+
# Map generation
|
| 47 |
+
def generate_map(df):
|
| 48 |
+
if df.empty or df[['Latitude', 'Longitude']].isna().all().all():
|
| 49 |
+
return None
|
| 50 |
+
|
| 51 |
+
coords = df[['Latitude', 'Longitude']].dropna().values
|
| 52 |
+
map_center = coords[0]
|
| 53 |
+
m = folium.Map(location=map_center, zoom_start=12)
|
| 54 |
+
|
| 55 |
+
for idx, coord in enumerate(coords):
|
| 56 |
+
folium.Marker(location=coord, popup=f"Visit {idx+1}").add_to(m)
|
| 57 |
+
|
| 58 |
+
folium.PolyLine(coords, color='blue').add_to(m)
|
| 59 |
+
|
| 60 |
+
buf = BytesIO()
|
| 61 |
+
m.save(buf, close_file=False)
|
| 62 |
+
return buf.getvalue().decode()
|
| 63 |
+
|
| 64 |
+
# Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
with gr.Blocks() as app:
|
| 66 |
+
gr.Markdown("## π CarMat Dashboard")
|
| 67 |
+
|
| 68 |
+
unique_dates = sorted(field_sales_df['Date'].unique(), reverse=True)
|
| 69 |
+
|
| 70 |
+
# Field Sales Tab
|
| 71 |
+
with gr.Tab("πΊοΈ Field Sales"):
|
| 72 |
+
date_selector = gr.Dropdown(label="Select Date", choices=unique_dates)
|
| 73 |
+
data_output = gr.DataFrame()
|
| 74 |
+
map_html = gr.HTML()
|
| 75 |
+
|
| 76 |
+
def update_field_sales(date):
|
| 77 |
+
day_df = field_sales_df[field_sales_df['Date'] == date]
|
| 78 |
+
map_render = generate_map(day_df)
|
| 79 |
+
return day_df, map_render
|
| 80 |
+
|
| 81 |
+
date_selector.change(fn=update_field_sales, inputs=date_selector, outputs=[data_output, map_html])
|
| 82 |
+
|
| 83 |
+
# Summary Tab
|
| 84 |
+
with gr.Tab("π Summary"):
|
| 85 |
+
date_summary = gr.Dropdown(label="Select Date", choices=unique_dates)
|
| 86 |
+
summary_visits = gr.DataFrame()
|
| 87 |
+
|
| 88 |
+
def update_summary(date):
|
| 89 |
+
day_df = field_sales_df[field_sales_df['Date'] == date]
|
| 90 |
+
visits = day_df.groupby("Rep").size().reset_index(name="Total Visits")
|
| 91 |
+
return visits
|
| 92 |
+
|
| 93 |
+
date_summary.change(fn=update_summary, inputs=date_summary, outputs=summary_visits)
|
| 94 |
+
|
| 95 |
+
# Orders Tab
|
| 96 |
+
with gr.Tab("π¦ Orders"):
|
| 97 |
+
order_date = gr.Dropdown(label="Select Date", choices=unique_dates)
|
| 98 |
+
orders_output = gr.DataFrame()
|
| 99 |
+
|
| 100 |
+
def orders_summary(date):
|
| 101 |
+
day_df = field_sales_df[field_sales_df['Date'] == date]
|
| 102 |
+
orders_df = day_df[day_df["Order Received"] == "Yes"]
|
| 103 |
+
summary = orders_df.groupby("Rep").agg({
|
| 104 |
+
"Order Value": "sum",
|
| 105 |
+
"Order Received": "count"
|
| 106 |
+
}).rename(columns={"Order Received": "Orders Count"}).reset_index()
|
| 107 |
+
return summary
|
| 108 |
+
|
| 109 |
+
order_date.change(fn=orders_summary, inputs=order_date, outputs=orders_output)
|
| 110 |
|
| 111 |
app.launch()
|