mktgtech commited on
Commit
2ec7242
·
verified ·
1 Parent(s): 33a581d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -14
app.py CHANGED
@@ -4,6 +4,7 @@ import requests
4
  import json
5
  import pandas as pd
6
  import math
 
7
  import gradio as gr
8
  import plotly.express as px
9
  from plotly.subplots import make_subplots
@@ -73,19 +74,36 @@ def get_campaign_match(text, rules):
73
  if not text or not isinstance(text, str):
74
  return None
75
 
76
- text_lower = text.lower()
 
 
77
 
78
  for campaign_name, config in rules.items():
79
  includes = config.get("include", [])
80
  excludes = config.get("exclude", [])
81
 
82
- # 1st: Check Exclusions. If an excluded word is present, skip this campaign entirely
83
- has_exclusion = any(ex.lower() in text_lower for ex in excludes if ex.strip())
 
 
 
 
 
 
 
 
84
  if has_exclusion:
85
  continue
86
 
87
- # 2nd: Check Inclusions. If an included word is present, it's a match
88
- has_inclusion = any(inc.lower() in text_lower for inc in includes if inc.strip())
 
 
 
 
 
 
 
89
  if has_inclusion:
90
  return campaign_name
91
 
@@ -98,7 +116,7 @@ def apply_business_logic(df):
98
  if "lead_quality_score" in df.columns:
99
  df["Quality_Group"] = df["lead_quality_score"].apply(categorize_quality)
100
 
101
- # 2. Campaign Logic (Strict Fallback)
102
  rules = load_campaign_rules()
103
 
104
  def resolve_campaign(row):
@@ -121,7 +139,7 @@ def apply_business_logic(df):
121
  # 2. PRESETS
122
  # =====================================================
123
  DASHBOARD_PRESETS = {
124
- "Key Campaigns Bifurcation (Top Campaigns)": ("SPECIAL_KEY_BIFURCATION", None, None),
125
  "All Campaigns Performance": ("SPECIAL_ALL_PERFORMANCE", None, None),
126
  "Visits by Campaign": ("Campaign", "total_visits", "sum"),
127
  "Leads by Campaign": ("Campaign", "company_name", "count"),
@@ -330,17 +348,17 @@ def build_dashboard(df, preset, top_n):
330
  if df is None or df.empty: return px.bar(title="No Data")
331
  if "Campaign" not in df.columns: df = apply_business_logic(df)
332
 
333
- if preset == "Key Campaigns Bifurcation (Top Campaigns)":
334
- rules = load_campaign_rules()
335
- target = list(rules.keys())[:3] if rules else []
336
  filtered = df[df["Campaign"].isin(target)].copy()
337
  if filtered.empty: return px.bar(title="No Data for Key Campaigns")
338
-
339
  agg = filtered.groupby("Campaign").agg(
340
  Leads_Count=("company_name", "count"),
341
  Total_Visits=("total_visits", "sum")
342
  ).reset_index()
343
-
344
  fig = go.Figure()
345
  fig.add_trace(go.Bar(x=agg["Campaign"], y=agg["Leads_Count"], name="No. of Leads", marker_color="#00C49F"))
346
  fig.add_trace(go.Bar(x=agg["Campaign"], y=agg["Total_Visits"], name="Total Visits", marker_color="#FFBB28"))
@@ -407,7 +425,7 @@ def get_trend_filter_options(df, preset):
407
  options = sorted(df[segment].astype(str).unique().tolist())
408
 
409
  if segment == "Campaign":
410
- defaults = options[:3] # Pick top 3 dynamic campaigns automatically
411
  else:
412
  defaults = options[:5]
413
 
@@ -450,7 +468,7 @@ with gr.Blocks(title="Leadfeeder Campaign Pro") as demo:
450
 
451
  gr.Markdown("### 📊 Charts")
452
  with gr.Row():
453
- preset = gr.Dropdown(choices=list(DASHBOARD_PRESETS.keys()), label="Chart View", value="Key Campaigns Bifurcation (Top Campaigns)")
454
  top_n = gr.Slider(5, 50, value=10, label="Top N Items")
455
  chart = gr.Plot()
456
  gr.Button("Build View").click(build_dashboard, [df_state, preset, top_n], chart)
 
4
  import json
5
  import pandas as pd
6
  import math
7
+ import re
8
  import gradio as gr
9
  import plotly.express as px
10
  from plotly.subplots import make_subplots
 
74
  if not text or not isinstance(text, str):
75
  return None
76
 
77
+ # Convert URL delimiters to spaces and pad the string for EXACT word matching
78
+ # Example: "/blog/ai-readiness" -> " blog ai readiness "
79
+ search_text = " " + re.sub(r'[\-_/.,?=&+#]', ' ', text.lower()) + " "
80
 
81
  for campaign_name, config in rules.items():
82
  includes = config.get("include", [])
83
  excludes = config.get("exclude", [])
84
 
85
+ # 1st: Check Exclusions (Exact Word Match)
86
+ has_exclusion = False
87
+ for ex in excludes:
88
+ if not ex.strip(): continue
89
+ # Format keyword same way (e.g. "asp.net" -> "asp net")
90
+ kw = re.sub(r'[\-_/.,?=&+#]', ' ', ex.lower().strip())
91
+ if f" {kw} " in search_text:
92
+ has_exclusion = True
93
+ break
94
+
95
  if has_exclusion:
96
  continue
97
 
98
+ # 2nd: Check Inclusions (Exact Word Match)
99
+ has_inclusion = False
100
+ for inc in includes:
101
+ if not inc.strip(): continue
102
+ kw = re.sub(r'[\-_/.,?=&+#]', ' ', inc.lower().strip())
103
+ if f" {kw} " in search_text:
104
+ has_inclusion = True
105
+ break
106
+
107
  if has_inclusion:
108
  return campaign_name
109
 
 
116
  if "lead_quality_score" in df.columns:
117
  df["Quality_Group"] = df["lead_quality_score"].apply(categorize_quality)
118
 
119
+ # 2. Campaign Logic (Strict - URLs only)
120
  rules = load_campaign_rules()
121
 
122
  def resolve_campaign(row):
 
139
  # 2. PRESETS
140
  # =====================================================
141
  DASHBOARD_PRESETS = {
142
+ "Key Campaigns Bifurcation (MS Tech, Fintech, Adtech)": ("SPECIAL_KEY_BIFURCATION", None, None),
143
  "All Campaigns Performance": ("SPECIAL_ALL_PERFORMANCE", None, None),
144
  "Visits by Campaign": ("Campaign", "total_visits", "sum"),
145
  "Leads by Campaign": ("Campaign", "company_name", "count"),
 
348
  if df is None or df.empty: return px.bar(title="No Data")
349
  if "Campaign" not in df.columns: df = apply_business_logic(df)
350
 
351
+ # RESTORED EXACT ORIGINAL CHART LOGIC
352
+ if preset == "Key Campaigns Bifurcation (MS Tech, Fintech, Adtech)":
353
+ target = ["MS Tech", "Fintech", "Adtech"]
354
  filtered = df[df["Campaign"].isin(target)].copy()
355
  if filtered.empty: return px.bar(title="No Data for Key Campaigns")
356
+
357
  agg = filtered.groupby("Campaign").agg(
358
  Leads_Count=("company_name", "count"),
359
  Total_Visits=("total_visits", "sum")
360
  ).reset_index()
361
+
362
  fig = go.Figure()
363
  fig.add_trace(go.Bar(x=agg["Campaign"], y=agg["Leads_Count"], name="No. of Leads", marker_color="#00C49F"))
364
  fig.add_trace(go.Bar(x=agg["Campaign"], y=agg["Total_Visits"], name="Total Visits", marker_color="#FFBB28"))
 
425
  options = sorted(df[segment].astype(str).unique().tolist())
426
 
427
  if segment == "Campaign":
428
+ defaults = options[:3]
429
  else:
430
  defaults = options[:5]
431
 
 
468
 
469
  gr.Markdown("### 📊 Charts")
470
  with gr.Row():
471
+ preset = gr.Dropdown(choices=list(DASHBOARD_PRESETS.keys()), label="Chart View", value="Key Campaigns Bifurcation (MS Tech, Fintech, Adtech)")
472
  top_n = gr.Slider(5, 50, value=10, label="Top N Items")
473
  chart = gr.Plot()
474
  gr.Button("Build View").click(build_dashboard, [df_state, preset, top_n], chart)