File size: 9,492 Bytes
d2449b1
7f8a9be
d2449b1
736b61f
d2449b1
 
 
 
 
 
7f8a9be
 
 
 
d2449b1
 
91db5a0
d2449b1
 
7f8a9be
08fd623
d2449b1
7f8a9be
 
 
 
 
f6dd422
7f8a9be
d2449b1
 
 
7f8a9be
 
 
08fd623
 
 
 
 
 
 
d2449b1
 
91db5a0
7f8a9be
91db5a0
 
 
 
 
d2449b1
91db5a0
 
d2449b1
 
 
 
08fd623
d2449b1
 
 
 
 
 
08fd623
d2449b1
08fd623
d2449b1
 
 
7f8a9be
 
 
08fd623
d2449b1
2c6a8fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f8a9be
 
 
 
 
 
 
 
c081289
2c6a8fa
7f8a9be
 
d2449b1
f0fe416
 
 
d2449b1
7f8a9be
 
 
 
 
08fd623
d2449b1
7f8a9be
91db5a0
 
f0fe416
91db5a0
7f8a9be
91db5a0
08fd623
f0fe416
d2449b1
7f8a9be
0d143ed
 
 
91db5a0
d2449b1
f0fe416
d2449b1
0d143ed
 
08fd623
c081289
f0fe416
0d143ed
7f8a9be
08fd623
0763cc5
 
08fd623
 
 
7f8a9be
08fd623
c081289
 
7f8a9be
 
c081289
 
0763cc5
08fd623
 
39d2a32
736b61f
 
 
d2449b1
7f8a9be
 
 
0d143ed
7f8a9be
 
 
 
 
 
c081289
 
7f8a9be
c081289
7f8a9be
0d143ed
 
7f8a9be
 
 
 
 
0763cc5
d2449b1
596b6ec
0763cc5
7f8a9be
 
f6dd422
7f8a9be
ea9f906
7f8a9be
 
ea9f906
736b61f
7f8a9be
 
736b61f
9080602
7f8a9be
736b61f
7f8a9be
ea9f906
0763cc5
736b61f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6dd422
7f8a9be
ea9f906
7f8a9be
ea9f906
79f1e8f
 
7f8a9be
ea9f906
3413728
ea9f906
9080602
ea9f906
 
 
 
 
 
 
08fd623
736b61f
 
7f8a9be
08fd623
f0fe416
 
 
 
7f8a9be
f0fe416
 
7f8a9be
0763cc5
d2449b1
6773d19
 
 
 
d2449b1
 
7f8a9be
 
 
c081289
d2449b1
7f8a9be
 
 
08fd623
d2449b1
 
 
e4d5002
c081289
d2449b1
 
 
0763cc5
d2449b1
 
 
 
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
301
import os
import json
from collections import Counter
import math  
import gradio as gr
import pandas as pd
import plotly.express as px
import pycountry
from datasets import load_dataset


# =========================
# Config
# =========================
VISITS_URL = os.getenv(
    "VISITS_URL",
    "https://huggingface.co/datasets/19arjun89/ai_recruiting_agent_usage/resolve/main/usage/visits_enriched.jsonl",
)

# Set this as a HF Space SECRET named MAPBOX_TOKEN
MAPBOX_TOKEN = os.getenv("MAPBOX_TOKEN", "").strip()

# Path to your GeoJSON (commit into the Space repo)
GEOJSON_PATH = os.getenv("GEOJSON_PATH", "countries.geojson")

# IMPORTANT: Set this to match the property name inside your GeoJSON features.
# Common values: "properties.ISO_A3" or "properties.ADM0_A3"
GEOJSON_FEATURE_ID_KEY = "properties.ISO3166-1-Alpha-3"

MAX_ROWS = int(os.getenv("MAX_ROWS", "500000"))


# =========================
# Helpers
# =========================
def normalize_country_name(country: str | None) -> str | None:
    if not country or not isinstance(country, str):
        return None
    c = country.strip()
    if not c or c.lower() == "unknown":
        return None
    return c


def iso2_to_iso3(country_code: str | None) -> str | None:
    """Convert ISO-2 -> ISO-3 for map matching."""
    if not country_code or not isinstance(country_code, str):
        return None
    c2 = country_code.strip().upper()
    if len(c2) != 2:
        return None
    try:
        rec = pycountry.countries.get(alpha_2=c2)
        return rec.alpha_3 if rec else None
    except Exception:
        return None


def load_rows_streaming():
    ds = load_dataset(
        "json",
        data_files=VISITS_URL,
        split="train",
        streaming=True,
    )
    for i, row in enumerate(ds):
        yield row
        if i + 1 >= MAX_ROWS:
            break


def load_geojson(path: str) -> dict:
    with open(path, "r", encoding="utf-8") as f:
        return json.load(f)


def patch_geojson_iso_codes(countries_geojson: dict) -> dict:
    """
    Some GeoJSON files have ISO codes as '-99'. Patch them using the 'name' field.
    Updates:
      properties['ISO3166-1-Alpha-2']
      properties['ISO3166-1-Alpha-3']
    """
    fixed = 0
    for feat in countries_geojson.get("features", []):
        props = feat.get("properties", {}) or {}
        iso3 = str(props.get("ISO3166-1-Alpha-3", "") or "").strip()
        iso2 = str(props.get("ISO3166-1-Alpha-2", "") or "").strip()
        name = str(props.get("name", "") or "").strip()

        needs_fix = (iso3 == "-99" or iso2 == "-99" or not iso3 or not iso2)
        if not needs_fix or not name:
            continue

        try:
            rec = pycountry.countries.search_fuzzy(name)[0]
            props["ISO3166-1-Alpha-3"] = rec.alpha_3
            props["ISO3166-1-Alpha-2"] = rec.alpha_2
            fixed += 1
        except Exception:
            # leave as-is if we can't resolve
            pass

    print(f"DEBUG patched GeoJSON features: {fixed}")
    return countries_geojson


# =========================
# Main report builder
# =========================
def build_report():
    if not MAPBOX_TOKEN:
        # We can still run, but Mapbox will not render nicely without token.
        # We'll still build a figure (it may appear blank/limited).
        pass

    countries_geojson = patch_geojson_iso_codes(load_geojson(GEOJSON_PATH))

    # Counters for clean reconciliation
    scanned = 0
    skipped_session_start = 0
    missing_country = 0
    invalid_country_code = 0

    # Table (country name) and map (iso3)
    country_counts = Counter()
    iso3_counts = Counter()
    iso3_to_name = {}

    for row in load_rows_streaming():
        scanned += 1

        event_type = str(row.get("event", "") or "").strip().lower()
        if event_type == "session_start":
            skipped_session_start += 1
            continue

        country = normalize_country_name(row.get("final_country"))
        if not country:
            missing_country += 1
            continue

        # Count it for the table FIRST (all usage events with a valid country name)
        country_counts[country] += 1

        iso3 = iso2_to_iso3(row.get("final_country_code"))
        if not iso3:
            invalid_country_code += 1
            continue
        
        # Count it for the map only (requires ISO3)
        iso3_counts[iso3] += 1
        iso3_to_name.setdefault(iso3, country)


    # Build table dataframe
    table_df = (
        pd.DataFrame([{"country": k, "usage events": v} for k, v in country_counts.items()])
        .sort_values("usage events", ascending=False)
        .reset_index(drop=True)
    )

    # Build map dataframe
    map_df = (
        pd.DataFrame(
            [
                {"iso3": iso3, "country": iso3_to_name.get(iso3, iso3), "usage events": cnt}
                for iso3, cnt in iso3_counts.items()
            ]
        )
        .sort_values("usage events", ascending=False)
        .reset_index(drop=True)
    )

    # Log scale for nicer color spread (keeps small countries visible)
    map_df["usage_log"] = map_df["usage events"].clip(lower=1).apply(lambda x: math.log10(x))


    # Reconciliation
    rows_mappable = int(map_df["usage events"].sum())  # note: this is TOTAL events, not rows
    mappable_rows_count = int(sum(iso3_counts.values()))  # count of rows after filters (events counted)
    table_rows_counted = int(sum(country_counts.values()))
    accounted = skipped_session_start + missing_country + invalid_country_code + mappable_rows_count

    # If you want “Rows mappable” to mean “rows that made it to map”, use mappable_rows_count
    # If you want “Total usage events” (same thing here), use table_df sum.

    # Map figure
    if map_df.empty:
        fig = px.scatter(title="No mappable data found")
        fig.update_layout(height=740, margin=dict(l=0, r=0, t=40, b=0))
        summary = (
            f"Rows scanned: {scanned:,}\n"
            f"- Rows counted in table: {table_rows_counted:,}\n"
            f"- Rows mapped: {mappable_rows_count:,}\n"
            f"- Session starts skipped: {skipped_session_start:,}\n"
            f"- Missing country: {missing_country:,}\n"
            f"- Invalid country code: {invalid_country_code:,}\n\n"
            f"Accounted rows: {accounted:,} / {scanned:,}\n"
            f"Countries (table): {len(table_df):,}\n"
            f"Total usage events: {int(table_df['usage events'].sum()) if len(table_df) else 0:,}"
        )
        return fig, table_df.head(50), summary

    # Mapbox choropleth using GeoJSON
    px.set_mapbox_access_token(MAPBOX_TOKEN)
    map_df["iso3"] = map_df["iso3"].astype(str).str.upper()
    fig = px.choropleth_mapbox(
        map_df,
        geojson=countries_geojson,
        featureidkey=GEOJSON_FEATURE_ID_KEY,
        locations="iso3",
        color="usage_log",
        hover_name="country",
        hover_data={"usage events": True, "iso3": True},
        labels={"usage_log": "Usage intensity (log10)", "usage events": "Usage events"},
        mapbox_style="open-street-map",
        opacity=0.75,
        zoom=1.5,
        center={"lat": 15, "lon": 0},
    )

    fig.update_traces(
    # Use a clean hover card
    hovertemplate=(
        "<b>%{customdata[0]}</b><br>"
        "Usage events: %{customdata[1]:,}<br>"
        "<extra></extra>"
    ),
    # customdata lets us show real counts even though color is log-scaled
    customdata=map_df[["country", "usage events"]].to_numpy(),
    )

    fig.update_traces(
    marker_line_width=0.8,
    marker_line_color="rgba(255,255,255,0.85)",  # nice on light basemaps
    )


    # Full-bleed layout
    fig.update_layout(
        height=740,
        margin=dict(l=0, r=0, t=0, b=0),
    )

    # Dashboard title
    fig.add_annotation(
        text="Usage Events by Country",
        x=0.01,
        y=0.95,
        xref="paper",
        yref="paper",
        xanchor="left",
        yanchor="top",
        showarrow=False,
        font=dict(size=20),
    )

    fig.update_layout(coloraxis_showscale=False)

    # Summary text (clean math)
    summary = (
        f"Rows scanned: {scanned:,}\n"
        f"- Session starts skipped: {skipped_session_start:,}\n"
        f"- Missing country: {missing_country:,}\n"
        f"- Invalid country code: {invalid_country_code:,}\n"
        f"- Rows mapped: {mappable_rows_count:,}\n\n"
        f"Accounted rows: {accounted:,} / {scanned:,}\n"
        f"Countries (table): {len(table_df):,}\n"
        f"Countries (map): {map_df['iso3'].nunique():,}\n"
        f"Total usage events: {int(table_df['usage events'].sum()) if len(table_df) else 0:,}"
    )
    
    table_out = table_df.head(50).copy()
    table_out.insert(0, "refreshed_at_utc", pd.Timestamp.utcnow().strftime("%Y-%m-%d %H:%M:%S"))
    return fig, table_out, summary


# =========================
# UI
# =========================
with gr.Blocks(title="AI Recruiting Agent — Usage Map") as demo:
    gr.Markdown(
        "# AI Recruiting Agent — Usage by Country (Mapbox)\n"
        "This Space reads **only** `visits_enriched.jsonl`, excludes `event=session_start`, "
        "and plots **usage events** by country.\n\n"
    )

    run = gr.Button("Generate map")
    summary = gr.Markdown()
    plot = gr.Plot()
    table = gr.Dataframe(label="Top countries", interactive=False)

    run.click(
        fn=build_report,
        inputs=[],
        outputs=[plot, table, summary],
    )

demo.launch()