""" cold-email-predictor — LeadsBlue Hugging Face Space. Predicts B2B cold email open-rate and reply-rate ranges for a given country / industry / decision-maker seniority, using real benchmark data from the LeadsBlue B2B Cold Email Benchmark dataset. Country and industry rates are measured benchmark data. The seniority adjustment is a transparent modeling estimate (stated in the UI). Data source: country-insights.json (111 countries), industry-insights.json (14 industries) Author: Luther Johnson (ORCID 0009-0008-9836-1280) · LeadsBlue Research License: CC BY 4.0 · Dataset DOI: 10.5281/zenodo.20136256 """ import gradio as gr from leadsblue_common import ( CITATION_FOOTER, softwareapplication_jsonld, load_country_insights, load_industry_insights, parse_pct_range, ) # --------------------------------------------------------------------------- # Load data once at startup (graceful failure) # --------------------------------------------------------------------------- try: COUNTRIES = load_country_insights() INDUSTRIES = load_industry_insights() LOAD_ERROR = None except Exception as exc: # pragma: no cover COUNTRIES, INDUSTRIES, LOAD_ERROR = {}, {}, str(exc) # Dropdown values from the real data COUNTRY_CHOICES = sorted(c.title() for c in COUNTRIES.keys()) INDUSTRY_CHOICES = sorted(INDUSTRIES.keys()) SENIORITY_CHOICES = [ "Owner/Founder", "C-Suite", "VP", "Director", "Manager", "Individual Contributor", ] # Transparent modeling assumption — NOT measured data (disclosed in UI) SENIORITY_FACTOR = { "Owner/Founder": 1.05, "C-Suite": 0.85, "VP": 0.92, "Director": 1.00, "Manager": 1.08, "Individual Contributor": 1.12, } OPEN_CLAMP = (5.0, 60.0) REPLY_CLAMP = (0.5, 15.0) def _clamp(v, lo, hi): return max(lo, min(hi, v)) _EMPTY = [["—", "—"]] def predict(country, industry, seniority): """Core prediction. Returns (markdown_summary, detail_table_rows).""" if LOAD_ERROR: return ("**Data failed to load.** %s" % LOAD_ERROR), _EMPTY if not country or not industry or not seniority: return "Please choose a country, industry, and seniority level.", [] ckey = country.lower() if ckey not in COUNTRIES: return "No benchmark data for %s." % country, [] if industry not in INDUSTRIES: return "No benchmark data for %s." % industry, [] cb = COUNTRIES[ckey].get("benchmarks", {}) perf = INDUSTRIES[industry].get("cold_email_performance", {}) have_country = all( k in cb for k in ("open_rate_low", "open_rate_high", "reply_rate_low", "reply_rate_high") ) have_industry = "open_rate" in perf and "reply_rate" in perf if not have_country and not have_industry: return "Insufficient benchmark data for this combination.", [] # Country numeric ranges if have_country: c_open = (cb["open_rate_low"], cb["open_rate_high"]) c_reply = (cb["reply_rate_low"], cb["reply_rate_high"]) else: c_open = c_reply = None # Industry string ranges if have_industry: i_open = parse_pct_range(perf["open_rate"]) i_reply = parse_pct_range(perf["reply_rate"]) else: i_open = i_reply = None # Blend (average the available sources) def blend(a, b): if a and b: return ((a[0] + b[0]) / 2, (a[1] + b[1]) / 2) return a or b open_lo, open_hi = blend(c_open, i_open) reply_lo, reply_hi = blend(c_reply, i_reply) # Seniority modifier (transparent assumption) f = SENIORITY_FACTOR[seniority] open_lo = _clamp(round(open_lo * f, 1), *OPEN_CLAMP) open_hi = _clamp(round(open_hi * f, 1), *OPEN_CLAMP) reply_lo = _clamp(round(reply_lo * f, 1), *REPLY_CLAMP) reply_hi = _clamp(round(reply_hi * f, 1), *REPLY_CLAMP) # Confidence band if have_country and have_industry: confidence = "High (country + industry benchmark data)" elif have_country or have_industry: confidence = "Medium (single-source benchmark data)" else: confidence = "Low (fallback estimate)" best_days = ", ".join(cb.get("best_days", [])) or "—" best_time = cb.get("best_time_local", "—") subj = perf.get("best_subject_lines", "—") summary = ( "## Predicted performance: %s · %s · %s\n\n" "**Open rate:** %.1f%% – %.1f%% \n" "**Reply rate:** %.1f%% – %.1f%% \n" "**Confidence:** %s\n\n" "**Best send days (country):** %s \n" "**Best send time (country):** %s \n" "**Subject-line angle (industry):** %s\n\n" "> _Country and industry rates are measured benchmark data from the LeadsBlue " "dataset. The seniority adjustment (factor %.2f for %s) is a modeling estimate, " "not measured data._\n\n" "---\n%s" ) % ( country, industry, seniority, open_lo, open_hi, reply_lo, reply_hi, confidence, best_days, best_time, subj, f, seniority, CITATION_FOOTER, ) detail = [ ["Country open rate", "%s–%s%%" % (c_open[0], c_open[1]) if c_open else "n/a"], ["Industry open rate", "%s–%s%%" % (i_open[0], i_open[1]) if i_open else "n/a"], ["Country reply rate", "%s–%s%%" % (c_reply[0], c_reply[1]) if c_reply else "n/a"], ["Industry reply rate", "%s–%s%%" % (i_reply[0], i_reply[1]) if i_reply else "n/a"], ["Seniority factor", "%.2f" % f], ] return summary, detail HEAD = softwareapplication_jsonld( "LeadsBlue Cold Email Predictor", "Predicts B2B cold email open and reply rate ranges by country, industry, and seniority.", ) _blocks_kwargs = {"title": "LeadsBlue Cold Email Predictor"} try: demo = gr.Blocks(head=HEAD, **_blocks_kwargs) except TypeError: demo = gr.Blocks(**_blocks_kwargs) # older/newer gradio: head set at launch() with demo: gr.Markdown( "# LeadsBlue Cold Email Predictor\n" "Estimate B2B cold email open and reply rates by country, industry, and " "decision-maker seniority — based on the LeadsBlue benchmark dataset " "([DOI 10.5281/zenodo.20136256](https://zenodo.org/records/20136256))." ) with gr.Row(): country = gr.Dropdown(COUNTRY_CHOICES, label="Country", value=("United States" if "United States" in COUNTRY_CHOICES else None)) industry = gr.Dropdown(INDUSTRY_CHOICES, label="Industry", value=(INDUSTRY_CHOICES[0] if INDUSTRY_CHOICES else None)) seniority = gr.Dropdown(SENIORITY_CHOICES, label="Decision-maker seniority", value="Director") btn = gr.Button("Predict", variant="primary") out = gr.Markdown() detail = gr.Dataframe(headers=["Signal", "Value"], col_count=(2, "fixed"), value=_EMPTY, label="Underlying benchmark signals", interactive=False) btn.click(predict, [country, industry, seniority], [out, detail]) gr.Markdown("---\n_%s_" % CITATION_FOOTER) if __name__ == "__main__": try: demo.launch(head=HEAD) except TypeError: demo.launch()