Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -156,7 +156,6 @@ def enrich_leads_with_visits(df, start_date, end_date, max_rows=None, progress=g
|
|
| 156 |
print(f"🕵️ Deep enriching {total} rows ({start_date} to {end_date})...")
|
| 157 |
|
| 158 |
for index, row in target_df.iterrows():
|
| 159 |
-
# Only enrich if data is missing
|
| 160 |
if row.get("landing_page_path") and row.get("exit_page_path"):
|
| 161 |
continue
|
| 162 |
|
|
@@ -319,11 +318,11 @@ def build_trend(df, preset, grain, filter_values):
|
|
| 319 |
return px.line(ts, x="period", y=val, markers=True, template="plotly_white", title=preset)
|
| 320 |
|
| 321 |
# =====================================================
|
| 322 |
-
# ADVANCED ANALYTICS (WITH
|
| 323 |
# =====================================================
|
| 324 |
def build_advanced_plots(df, search_lead_id, search_company, start_date, end_date):
|
| 325 |
if df is None or df.empty:
|
| 326 |
-
return None, None
|
| 327 |
|
| 328 |
# --- 1. FILTER ---
|
| 329 |
filtered_df = df.copy()
|
|
@@ -339,31 +338,23 @@ def build_advanced_plots(df, search_lead_id, search_company, start_date, end_dat
|
|
| 339 |
title_suffix = f" (Company: {search_company})"
|
| 340 |
|
| 341 |
if filtered_df.empty:
|
| 342 |
-
return go.Figure(layout=dict(title="No
|
|
|
|
|
|
|
| 343 |
|
| 344 |
# --- 2. AUTO-ENRICHMENT ---
|
| 345 |
-
# Check if the filtered rows are missing landing/exit data.
|
| 346 |
-
# If so, fetch it immediately!
|
| 347 |
missing_data = filtered_df[filtered_df["landing_page_path"].isnull()]
|
| 348 |
-
|
| 349 |
if not missing_data.empty:
|
| 350 |
-
# We pass the subset to the enrich function
|
| 351 |
print(f"🔄 Auto-enriching {len(missing_data)} rows for visualization...")
|
| 352 |
-
|
| 353 |
-
# We need to update the original filtered_df with the new data
|
| 354 |
enriched_subset = enrich_leads_with_visits(missing_data, start_date, end_date)
|
| 355 |
-
|
| 356 |
-
# Update the rows in filtered_df using the enriched data
|
| 357 |
filtered_df.update(enriched_subset)
|
| 358 |
|
| 359 |
# --- 3. SANKEY DIAGRAM ---
|
| 360 |
flow_df = filtered_df.dropna(subset=["landing_page_path", "exit_page_path"])
|
| 361 |
-
|
| 362 |
if flow_df.empty:
|
| 363 |
-
sankey_fig = go.Figure(layout=dict(title="No visit data available
|
| 364 |
else:
|
| 365 |
flow_counts = flow_df.groupby(["landing_page_path", "exit_page_path"]).size().reset_index(name="count")
|
| 366 |
-
|
| 367 |
if not search_lead_id and not search_company:
|
| 368 |
flow_counts = flow_counts.sort_values("count", ascending=False).head(20)
|
| 369 |
|
|
@@ -402,7 +393,33 @@ def build_advanced_plots(df, search_lead_id, search_company, start_date, end_dat
|
|
| 402 |
title=f"Activity Heatmap{title_suffix}"
|
| 403 |
)
|
| 404 |
|
| 405 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
|
| 407 |
# =====================================================
|
| 408 |
# UI LAYOUT
|
|
@@ -462,7 +479,7 @@ with gr.Blocks(title="Leadfeeder Pro Analytics") as demo:
|
|
| 462 |
plot
|
| 463 |
)
|
| 464 |
|
| 465 |
-
# --- TAB 4: ADVANCED ANALYTICS ---
|
| 466 |
with gr.Tab("🔬 Advanced Analytics"):
|
| 467 |
gr.Markdown("Visualize user behavior flows. Leave search blank to see aggregate Top 20 flows.")
|
| 468 |
|
|
@@ -470,14 +487,23 @@ with gr.Blocks(title="Leadfeeder Pro Analytics") as demo:
|
|
| 470 |
search_lead = gr.Textbox(label="Search by Lead ID (Exact)")
|
| 471 |
search_comp = gr.Textbox(label="Search by Company Name (Partial)")
|
| 472 |
|
| 473 |
-
btn_adv = gr.Button("Generate
|
| 474 |
|
|
|
|
| 475 |
with gr.Row():
|
| 476 |
sankey_plot = gr.Plot(label="User Flow (Sankey)")
|
| 477 |
heatmap_plot = gr.Plot(label="Activity Heatmap")
|
| 478 |
|
| 479 |
-
#
|
| 480 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
|
| 482 |
# --- TAB 5: DEBUGGER ---
|
| 483 |
with gr.Tab("🛠️ Debugger"):
|
|
|
|
| 156 |
print(f"🕵️ Deep enriching {total} rows ({start_date} to {end_date})...")
|
| 157 |
|
| 158 |
for index, row in target_df.iterrows():
|
|
|
|
| 159 |
if row.get("landing_page_path") and row.get("exit_page_path"):
|
| 160 |
continue
|
| 161 |
|
|
|
|
| 318 |
return px.line(ts, x="period", y=val, markers=True, template="plotly_white", title=preset)
|
| 319 |
|
| 320 |
# =====================================================
|
| 321 |
+
# ADVANCED ANALYTICS (WITH MATRIX, SANKEY, HEATMAP)
|
| 322 |
# =====================================================
|
| 323 |
def build_advanced_plots(df, search_lead_id, search_company, start_date, end_date):
|
| 324 |
if df is None or df.empty:
|
| 325 |
+
return None, None, None
|
| 326 |
|
| 327 |
# --- 1. FILTER ---
|
| 328 |
filtered_df = df.copy()
|
|
|
|
| 338 |
title_suffix = f" (Company: {search_company})"
|
| 339 |
|
| 340 |
if filtered_df.empty:
|
| 341 |
+
return (go.Figure(layout=dict(title="No data")),
|
| 342 |
+
go.Figure(layout=dict(title="No data")),
|
| 343 |
+
go.Figure(layout=dict(title="No data")))
|
| 344 |
|
| 345 |
# --- 2. AUTO-ENRICHMENT ---
|
|
|
|
|
|
|
| 346 |
missing_data = filtered_df[filtered_df["landing_page_path"].isnull()]
|
|
|
|
| 347 |
if not missing_data.empty:
|
|
|
|
| 348 |
print(f"🔄 Auto-enriching {len(missing_data)} rows for visualization...")
|
|
|
|
|
|
|
| 349 |
enriched_subset = enrich_leads_with_visits(missing_data, start_date, end_date)
|
|
|
|
|
|
|
| 350 |
filtered_df.update(enriched_subset)
|
| 351 |
|
| 352 |
# --- 3. SANKEY DIAGRAM ---
|
| 353 |
flow_df = filtered_df.dropna(subset=["landing_page_path", "exit_page_path"])
|
|
|
|
| 354 |
if flow_df.empty:
|
| 355 |
+
sankey_fig = go.Figure(layout=dict(title="No visit data available"))
|
| 356 |
else:
|
| 357 |
flow_counts = flow_df.groupby(["landing_page_path", "exit_page_path"]).size().reset_index(name="count")
|
|
|
|
| 358 |
if not search_lead_id and not search_company:
|
| 359 |
flow_counts = flow_counts.sort_values("count", ascending=False).head(20)
|
| 360 |
|
|
|
|
| 393 |
title=f"Activity Heatmap{title_suffix}"
|
| 394 |
)
|
| 395 |
|
| 396 |
+
# --- 5. INDUSTRY VALUE MATRIX (NEW) ---
|
| 397 |
+
# Only meaningful on aggregate data, so we use the MAIN df, not filtered_df
|
| 398 |
+
# unless filtered by date.
|
| 399 |
+
roi_df = df.groupby("primary_industry").agg({
|
| 400 |
+
"total_visits": "sum",
|
| 401 |
+
"lead_quality_score": "mean",
|
| 402 |
+
"company_name": "count" # Bubble size
|
| 403 |
+
}).reset_index()
|
| 404 |
+
|
| 405 |
+
# Filter small industries to reduce noise
|
| 406 |
+
roi_df = roi_df[roi_df["company_name"] > 2]
|
| 407 |
+
|
| 408 |
+
matrix_fig = px.scatter(
|
| 409 |
+
roi_df,
|
| 410 |
+
x="total_visits",
|
| 411 |
+
y="lead_quality_score",
|
| 412 |
+
size="company_name",
|
| 413 |
+
color="primary_industry",
|
| 414 |
+
hover_name="primary_industry",
|
| 415 |
+
title="Industry Value Matrix (Volume vs Quality)",
|
| 416 |
+
labels={"total_visits": "Volume (Total Visits)", "lead_quality_score": "Avg Quality Score"}
|
| 417 |
+
)
|
| 418 |
+
if not roi_df.empty:
|
| 419 |
+
matrix_fig.add_hline(y=roi_df["lead_quality_score"].mean(), line_dash="dot", annotation_text="Avg Quality")
|
| 420 |
+
matrix_fig.add_vline(x=roi_df["total_visits"].mean(), line_dash="dot", annotation_text="Avg Volume")
|
| 421 |
+
|
| 422 |
+
return sankey_fig, heatmap_fig, matrix_fig
|
| 423 |
|
| 424 |
# =====================================================
|
| 425 |
# UI LAYOUT
|
|
|
|
| 479 |
plot
|
| 480 |
)
|
| 481 |
|
| 482 |
+
# --- TAB 4: ADVANCED ANALYTICS (NEW MATRIX ADDED) ---
|
| 483 |
with gr.Tab("🔬 Advanced Analytics"):
|
| 484 |
gr.Markdown("Visualize user behavior flows. Leave search blank to see aggregate Top 20 flows.")
|
| 485 |
|
|
|
|
| 487 |
search_lead = gr.Textbox(label="Search by Lead ID (Exact)")
|
| 488 |
search_comp = gr.Textbox(label="Search by Company Name (Partial)")
|
| 489 |
|
| 490 |
+
btn_adv = gr.Button("Generate Advanced Charts", variant="primary")
|
| 491 |
|
| 492 |
+
# Row 1: Flow and Heatmap
|
| 493 |
with gr.Row():
|
| 494 |
sankey_plot = gr.Plot(label="User Flow (Sankey)")
|
| 495 |
heatmap_plot = gr.Plot(label="Activity Heatmap")
|
| 496 |
|
| 497 |
+
# Row 2: Value Matrix
|
| 498 |
+
with gr.Row():
|
| 499 |
+
matrix_plot = gr.Plot(label="Industry Value Matrix")
|
| 500 |
+
|
| 501 |
+
# Pass all inputs and outputs
|
| 502 |
+
btn_adv.click(
|
| 503 |
+
build_advanced_plots,
|
| 504 |
+
[df_state, search_lead, search_comp, start, end],
|
| 505 |
+
[sankey_plot, heatmap_plot, matrix_plot]
|
| 506 |
+
)
|
| 507 |
|
| 508 |
# --- TAB 5: DEBUGGER ---
|
| 509 |
with gr.Tab("🛠️ Debugger"):
|