File size: 12,259 Bytes
fd74205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
71
72
73
74
75
76
77
78
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
"""Streamlit sandbox demo for the Redrob Candidate Intelligence Engine.



The sandbox runs the COMPLETE pipeline on a small input that the user

uploads. It demonstrates:



  * The Role Blueprint and how the JD is parsed

  * Hybrid retrieval (BM25 + sentence-transformers + RRF)

  * Feature engineering

  * LightGBM LambdaRank (or deterministic fallback) ranking

  * Per-candidate reasoning

  * Export of the top-N as a CSV



For the OFFICIAL reproduce_command see `scripts/run_ranking.py` in the

repository. That command processes the full 100k candidate pool within

the 5-minute / 16GB sandbox budget.



This is the Stage-1 sandbox required by the hackathon spec

(submission_spec.docx Section 10.5). Deploy to Streamlit Cloud or

HuggingFace Spaces to get a working sandbox URL.

"""
from __future__ import annotations

import json
import sys
import time
from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parent.parent
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

import pandas as pd
import streamlit as st

from redrob.blueprint import build_blueprint
from redrob.rank.predict import rank_shortlist
from redrob.reasoning.template import generate_reasoning_dataframe
from redrob.submit.write import write_submission


st.set_page_config(page_title="Redrob Candidate Intelligence", layout="wide")

st.title("Redrob Candidate Intelligence Engine")
st.caption("Senior AI Engineer ranking — sandbox demo (Stage 1 submission)")

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _load_uploaded_jsonl(uploaded_file) -> list:
    """Read an uploaded jsonl or json file as a list of dicts."""
    raw = uploaded_file.getvalue().decode("utf-8", errors="ignore")
    rows = []
    for line in raw.splitlines():
        line = line.strip()
        if not line:
            continue
        try:
            rows.append(json.loads(line))
        except json.JSONDecodeError:
            try:
                rows.append(json.loads(raw))
                break
            except Exception:
                pass
    return rows


def _rows_to_candidate_objects(rows: list) -> list:
    """Convert raw dicts to the dataclass shape used by the pipeline.



    The pipeline's `iter_canonical` parses one of two formats:

      - the bundled `sample_candidates.json` (list of records)

      - the streamed `candidates.jsonl` (one record per line, with the

        record itself containing a `profile` key).

    The bundled sample is already in the right shape. The streamed

    jsonl wraps the profile under `profile`. We support both.

    """
    from redrob.data import CanonicalCandidate  # type: ignore
    out = []
    for r in rows:
        if "profile" in r and isinstance(r["profile"], dict):
            # Streamed jsonl format
            cand = CanonicalCandidate(
                candidate_id=r.get("candidate_id") or r.get("id", ""),
                name=(r.get("profile") or {}).get("name", ""),
                headline=(r.get("profile") or {}).get("headline", ""),
                summary=(r.get("profile") or {}).get("summary", ""),
                location=(r.get("profile") or {}).get("location", ""),
                country=(r.get("profile") or {}).get("country", ""),
                yoe=int((r.get("profile") or {}).get("years_experience", 0) or 0),
                current_title=(r.get("profile") or {}).get("current_title", ""),
                current_company=(r.get("profile") or {}).get("current_company", ""),
                current_company_size=(r.get("profile") or {}).get("current_company_size", ""),
                current_industry=(r.get("profile") or {}).get("current_industry", ""),
                career=(r.get("profile") or {}).get("career", []) or [],
                edu=(r.get("profile") or {}).get("education", []) or [],
                skills=(r.get("profile") or {}).get("skills", []) or [],
                certs=(r.get("profile") or {}).get("certifications", []) or [],
                languages=(r.get("profile") or {}).get("languages", []) or [],
                signals=r.get("signals") or {},
            )
        else:
            # Bundled sample format
            cand = CanonicalCandidate(
                candidate_id=r.get("candidate_id", ""),
                name=r.get("name", ""),
                headline=r.get("headline", ""),
                summary=r.get("summary", ""),
                location=r.get("location", ""),
                country=r.get("country", ""),
                yoe=int(r.get("yoe", 0) or 0),
                current_title=r.get("current_title", ""),
                current_company=r.get("current_company", ""),
                current_company_size=r.get("current_company_size", ""),
                current_industry=r.get("current_industry", ""),
                career=r.get("career", []) or [],
                edu=r.get("edu", []) or [],
                skills=r.get("skills", []) or [],
                certs=r.get("certs", []) or [],
                languages=r.get("languages", []) or [],
                signals=r.get("signals", {}) or {},
            )
        out.append(cand)
    return out


def _candidates_to_dataframe(cands) -> pd.DataFrame:
    rows = []
    for cc in cands:
        rows.append({
            "candidate_id": cc.candidate_id,
            "name": cc.name,
            "headline": cc.headline,
            "summary": cc.summary,
            "location": cc.location,
            "country": cc.country,
            "yoe": cc.yoe,
            "current_title": cc.current_title,
            "current_company": cc.current_company,
            "current_company_size": cc.current_company_size,
            "current_industry": cc.current_industry,
            "career": cc.career,
            "edu": cc.edu,
            "skills": cc.skills,
            "certs": cc.certs,
            "languages": cc.languages,
            "signals": cc.signals,
            "text_corpus": cc.text_corpus,
            "career_total_months": cc.career_total_months,
            "career_overlap_months": cc.career_overlap_months,
            "career_industries": cc.career_industries,
            "career_titles": cc.career_titles,
            "career_companies": cc.career_companies,
            "skill_names": cc.skill_names,
            "skill_durations_max": cc.skill_durations_max,
            "has_ai_skill": cc.has_ai_skill,
        })
    return pd.DataFrame(rows)


# ---------------------------------------------------------------------------
# Sidebar: inputs
# ---------------------------------------------------------------------------
st.sidebar.header("Inputs")
uploaded = st.sidebar.file_uploader(
    "Upload candidates.jsonl or sample_candidates.json (max ~5,000 records)",
    type=["jsonl", "json"],
    help="Get the file from the hackathon bundle. The sandbox runs the "
         "complete pipeline on a small subset for demonstration.",
)
sample_size = st.sidebar.slider("Sample size for sandbox run", 50, 2000, 200, step=50)
use_dense = st.sidebar.checkbox(
    "Use sentence-transformers (slower, ~1 min extra)",
    value=False,
)
run_btn = st.sidebar.button("Run ranking pipeline", type="primary")

st.sidebar.markdown("---")
st.sidebar.caption(
    "For the full 100k run, use `python scripts/run_ranking.py --no_dense --no_lgbm` "
    "from the repo root (see README)."
)

# ---------------------------------------------------------------------------
# Blueprint panel
# ---------------------------------------------------------------------------
bp = build_blueprint()
with st.expander("Role Blueprint (Senior AI Engineer, Redrob)", expanded=True):
    c1, c2, c3 = st.columns(3)
    with c1:
        st.markdown("**Core competencies**")
        st.write(", ".join(bp["core_competencies"][:20]) + "…")
        st.markdown(f"*{len(bp['core_competencies'])} total*")
    with c2:
        st.markdown("**Target titles**")
        st.write(", ".join(bp["target_titles"][:10]) + "…")
        st.markdown("**Experience band (yrs)**")
        st.write(
            f"min {bp['experience_band']['min']} · ideal "
            f"{bp['experience_band']['ideal_low']}–{bp['experience_band']['ideal_high']}"
        )
    with c3:
        st.markdown("**Notice period**")
        st.write(f"preferred ≤ {bp['notice_period_ok_days']} days")
        st.markdown("**Locations**")
        st.write(", ".join(bp["preferred_locations"]))

# ---------------------------------------------------------------------------
# Pipeline run
# ---------------------------------------------------------------------------
if run_btn:
    if uploaded is None:
        st.error(
            "Please upload a candidates file. The sandbox needs your "
            "data — there's no bundled sample in the public repo."
        )
        st.stop()

    t0 = time.time()
    raw_rows = _load_uploaded_jsonl(uploaded)
    if not raw_rows:
        st.error("Could not parse the uploaded file as JSON/JSONL.")
        st.stop()

    # Truncate to sample size
    raw_rows = raw_rows[:sample_size]
    cands = _rows_to_candidate_objects(raw_rows)
    df = _candidates_to_dataframe(cands)
    st.success(f"Loaded {len(df):,} candidates in {time.time()-t0:.1f}s")

    if not use_dense:
        # Disable dense retrieval for the sandbox (faster)
        from redrob import retrieval
        retrieval.dense.dense_search = lambda *a, **kw: {}

    t0 = time.time()
    # Provide placeholder retrieval scores
    df["sparse_score"] = 0.0
    df["dense_score"] = 0.0
    df["rrf_score"] = 0.0
    df["structured_hit"] = 0.0

    ranked = rank_shortlist(df, use_lgbm=False)
    ranked = ranked.head(min(100, len(ranked)))
    elapsed = time.time() - t0
    st.success(f"Ranked in {elapsed:.1f}s — top-{len(ranked)} ready")

    # Reasoning
    reasonings = generate_reasoning_dataframe(ranked)
    ranked = ranked.copy()
    ranked["reasoning"] = reasonings

    # Display
    st.subheader("Top 25 (preview)")
    display_cols = [
        "candidate_id", "current_title", "yoe",
        "title_fit_blend", "recruitability", "honeypot_penalty",
        "final_score", "reasoning",
    ]
    display_cols = [c for c in display_cols if c in ranked.columns]
    st.dataframe(ranked.head(25)[display_cols], use_container_width=True, hide_index=True)

    st.subheader(f"Top-{len(ranked)} full table")
    full_cols = ["candidate_id", "current_title", "yoe", "title_fit_blend",
                 "recruitability", "honeypot_penalty", "final_score", "reasoning"]
    full_cols = [c for c in full_cols if c in ranked.columns]
    st.dataframe(ranked[full_cols], use_container_width=True, hide_index=True)

    # Charts
    c1, c2 = st.columns(2)
    with c1:
        if "current_title" in ranked.columns:
            st.markdown("**Top-100 title distribution**")
            st.bar_chart(ranked["current_title"].value_counts().head(10))
    with c2:
        if "title_fit_blend" in ranked.columns:
            st.markdown("**Top-100 title fit histogram**")
            st.bar_chart(pd.cut(ranked["title_fit_blend"], bins=10).value_counts().sort_index())

    # Export
    out_csv = PROJECT_ROOT / "submission_sandbox.csv"
    write_submission(ranked, out_path=out_csv, validate=False)
    with open(out_csv, "rb") as f:
        st.download_button(
            "Download submission CSV",
            data=f.read(),
            file_name="submission.csv",
            mime="text/csv",
        )

    st.info(
        "The sandbox ran the COMPLETE pipeline (BM25 retrieval, 5-axis "
        "PageRank, 74 features, deterministic composite, reasoning) "
        "on your uploaded sample. For the full 100k pool, run "
        "`python scripts/run_ranking.py --no_dense --no_lgbm` from "
        "the repo root (see README)."
    )
else:
    st.info("Upload a candidates file and press 'Run ranking pipeline' in the sidebar to begin.")