import gradio as gr
import pandas as pd
import folium
import numpy as np
import os
import re
import json
BASE = os.path.dirname(os.path.abspath(__file__)) if "__file__" in dir() else os.getcwd()
STAY_POINTS = os.path.join(BASE, "data", "stay_points_inference_sample.csv")
POI_PATH = os.path.join(BASE, "data", "poi_inference_sample.csv")
DEMO_PATH = os.path.join(BASE, "data", "demographics_inference_sample.csv")
COT_PATH = os.path.join(BASE, "data", "inference_results_sample.json")
SEX_MAP = {1:"Male", 2:"Female", -8:"Unknown", -7:"Prefer not to answer"}
EDU_MAP = {1:"Less than HS", 2:"HS Graduate/GED", 3:"Some College/Associate",
4:"Bachelor's Degree", 5:"Graduate/Professional Degree",
-1:"N/A", -7:"Prefer not to answer", -8:"Unknown"}
INC_MAP = {1:"<$10,000", 2:"$10,000–$14,999", 3:"$15,000–$24,999",
4:"$25,000–$34,999", 5:"$35,000–$49,999", 6:"$50,000–$74,999",
7:"$75,000–$99,999", 8:"$100,000–$124,999", 9:"$125,000–$149,999",
10:"$150,000–$199,999", 11:"$200,000+",
-7:"Prefer not to answer", -8:"Unknown", -9:"Not ascertained"}
RACE_MAP = {1:"White", 2:"Black or African American", 3:"Asian",
4:"American Indian or Alaska Native",
5:"Native Hawaiian or Other Pacific Islander",
6:"Multiple races", 97:"Other",
-7:"Prefer not to answer", -8:"Unknown"}
ACT_MAP = {0:"Transportation", 1:"Home", 2:"Work", 3:"School", 4:"ChildCare",
5:"BuyGoods", 6:"Services", 7:"EatOut", 8:"Errands", 9:"Recreation",
10:"Exercise", 11:"Visit", 12:"HealthCare", 13:"Religious",
14:"SomethingElse", 15:"DropOff"}
print("Loading data...")
sp = pd.read_csv(STAY_POINTS)
poi = pd.read_csv(POI_PATH)
demo = pd.read_csv(DEMO_PATH)
sp = sp.merge(poi, on="poi_id", how="left")
sp["start_datetime"] = pd.to_datetime(sp["start_datetime"], utc=True)
sp["end_datetime"] = pd.to_datetime(sp["end_datetime"], utc=True)
sp["duration_min"] = ((sp["end_datetime"] - sp["start_datetime"]).dt.total_seconds() / 60).round(1)
def parse_act_types(x):
try:
codes = list(map(int, str(x).strip("[]").split()))
return ", ".join(ACT_MAP.get(c, str(c)) for c in codes)
except:
return str(x)
sp["act_label"] = sp["act_types"].apply(parse_act_types)
# Load CoT JSON (optional)
cot_by_agent = {}
if os.path.exists(COT_PATH):
with open(COT_PATH, "r") as f:
cot_raw = json.load(f)
records = cot_raw if isinstance(cot_raw, list) else cot_raw.get("inference_results", [])
for result in records:
cot_by_agent[int(result["agent_id"])] = result
print(f"Loaded CoT for {len(cot_by_agent)} agents.")
sample_agents = sorted(sp["agent_id"].unique().tolist())
print(f"Ready. {len(sample_agents)} agents loaded.")
def get_cot(agent_id):
result = cot_by_agent.get(int(agent_id), {})
s1 = result.get("step1_response", "")
s2 = result.get("step2_response", "")
s3 = result.get("step3_response", "")
p1 = result.get("step1_prompt", "")
p2 = result.get("step2_prompt", "")
p3 = result.get("step3_prompt", "")
return s1, s2, s3, p1, p2, p3
# ── Mobility text builders ────────────────────────────────────────────────────
def build_mobility_summary(agent_sp):
top5 = (agent_sp.groupby("name")["duration_min"]
.agg(visits="count", avg_dur="mean")
.sort_values("visits", ascending=False)
.head(5))
obs_start = agent_sp["start_datetime"].min().strftime("%Y-%m-%d")
obs_end = agent_sp["end_datetime"].max().strftime("%Y-%m-%d")
days = (agent_sp["end_datetime"].max() - agent_sp["start_datetime"].min()).days
act_counts = agent_sp["act_label"].value_counts().head(3)
top_acts = ", ".join(f"{a} ({n})" for a, n in act_counts.items())
agent_sp2 = agent_sp.copy()
agent_sp2["hour"] = agent_sp2["start_datetime"].dt.hour
def tod(h):
if 5 <= h < 12: return "Morning"
if 12 <= h < 17: return "Afternoon"
if 17 <= h < 21: return "Evening"
return "Night"
agent_sp2["tod"] = agent_sp2["hour"].apply(tod)
peak_tod = agent_sp2["tod"].value_counts().idxmax()
agent_sp2["is_weekend"] = agent_sp2["start_datetime"].dt.dayofweek >= 5
wd_pct = int((~agent_sp2["is_weekend"]).mean() * 100)
lines = [
f"Period: {obs_start} ~ {obs_end} ({days} days)",
f"Stay points: {len(agent_sp)} | Unique locations: {agent_sp['name'].nunique()}",
f"Weekday/Weekend: {wd_pct}% / {100-wd_pct}% | Peak time: {peak_tod}",
f"Top activities: {top_acts}",
"",
"Top Locations:",
]
for i, (name, row) in enumerate(top5.iterrows(), 1):
lines.append(f" {i}. {name} — {int(row['visits'])} visits, avg {int(row['avg_dur'])} min")
return "\n".join(lines)
def build_weekly_checkin(agent_sp, max_days=None):
agent_sp2 = agent_sp.copy()
agent_sp2["date"] = agent_sp2["start_datetime"].dt.date
all_dates = sorted(agent_sp2["date"].unique())
dates_to_show = all_dates[:max_days] if max_days else all_dates
total_days = len(all_dates)
lines = ["WEEKLY CHECK-IN SUMMARY", "======================="]
for date in dates_to_show:
grp = agent_sp2[agent_sp2["date"] == date]
dow = grp["start_datetime"].iloc[0].strftime("%A")
label = "Weekend" if grp["start_datetime"].iloc[0].dayofweek >= 5 else "Weekday"
lines.append(f"\n--- {dow}, {date} ({label}) ---")
lines.append(f"Total activities: {len(grp)}")
for _, row in grp.iterrows():
lines.append(
f"- {row['start_datetime'].strftime('%H:%M')}-"
f"{row['end_datetime'].strftime('%H:%M')} "
f"({int(row['duration_min'])} mins): "
f"{row['name']} - {row['act_label']}"
)
if max_days and total_days > max_days:
lines.append(f"\n... ({total_days - max_days} more days)")
return "\n".join(lines)
# ── HTML reasoning chain ──────────────────────────────────────────────────────
CHAIN_CSS = """
"""
def _loading(msg):
return (f'
'
f''
f'{msg}
')
# ── Parsers ───────────────────────────────────────────────────────────────────
def _parse_s1(text):
locations, dur_map, tod, wk, dist = [], {}, {}, {}, None
for line in text.splitlines():
s = line.strip()
# Locations: "- Name: N visits/times/time/times each"
m = re.match(r'-\s+(.+?):\s+(\d+)\s+(?:visit|time)', s, re.IGNORECASE)
if m:
locations.append((m.group(1).strip(), int(m.group(2))))
continue
# Duration — 4 formats
m2 = re.match(r'-?\s*(.+?):\s+(?:Average duration of\s*)?([\d.]+)\s+min(?:utes?)?\s+on average', s, re.IGNORECASE)
if not m2:
m2 = re.match(r'-?\s*(.+?):\s+Average duration of ([\d.]+)\s+min', s, re.IGNORECASE)
if not m2:
m2 = re.match(r'-?\s*Average duration at (.+?):\s+([\d.]+)\s+min', s, re.IGNORECASE)
if not m2:
m2 = re.search(r'\bat ([A-Za-z][^(,]+?)\s*\(average ([\d.]+)\s*min', s, re.IGNORECASE)
if m2:
dur_map[m2.group(1).strip()] = float(m2.group(2))
# TOD format A: "65% morning, 23% afternoon, 6% evening, 5% night"
if not tod:
mA = re.search(r'(\d+)%\s*morning.*?(\d+)%\s*afternoon.*?(\d+)%\s*evening.*?(\d+)%\s*night', s, re.IGNORECASE)
if mA:
tod = {'Morning': int(mA.group(1)), 'Afternoon': int(mA.group(2)),
'Evening': int(mA.group(3)), 'Night': int(mA.group(4))}
# TOD format B: "morning: 40%, afternoon: 36%, ..."
if not tod:
mB = re.search(r'morning[:\s]+(\d+)%.*?afternoon[:\s]+(\d+)%.*?evening[:\s]+(\d+)%.*?night[:\s]+(\d+)%', s, re.IGNORECASE)
if mB:
tod = {'Morning': int(mB.group(1)), 'Afternoon': int(mB.group(2)),
'Evening': int(mB.group(3)), 'Night': int(mB.group(4))}
# TOD format C: "Afternoon (43%), morning (27%), ..."
if not tod:
parts = re.findall(r'(morning|afternoon|evening|night)\s*\(?(\d+)%\)?', s, re.IGNORECASE)
if len(parts) >= 3:
d = {k.capitalize(): int(v) for k, v in parts}
if all(k in d for k in ['Morning', 'Afternoon', 'Evening']):
d.setdefault('Night', 0)
tod = d
# Weekday/weekend
if not wk:
m4 = re.search(r'(\d+)%\s*weekday.*?(\d+)%\s*weekend', s, re.IGNORECASE)
if m4:
wk = {'Weekday': int(m4.group(1)), 'Weekend': int(m4.group(2))}
# Distance
if not dist:
m5 = re.search(r'average distance of approximately ([\d.]+)\s*(?:km|miles?)', s, re.IGNORECASE)
if m5:
dist = float(m5.group(1))
return [(n, v, dur_map.get(n)) for n, v in locations[:7]], tod, wk, dist
def _parse_s2(text):
DIMS = {
'ROUTINE': ['ROUTINE', 'SCHEDULE'],
'ECONOMIC': ['ECONOMIC', 'SPENDING'],
'SOCIAL': ['SOCIAL', 'LIFESTYLE'],
'STABILITY': ['STABILITY', 'REGULARITY', 'CONSISTENCY', 'URBAN'],
}
sections, current_key, current_lines = {}, None, []
for line in text.splitlines():
s = line.strip()
mA = re.match(r'^\d+\.\s+([A-Z][A-Z\s&]+?)(?:\s+ANALYSIS|\s+PATTERNS|\s+INDICATORS|\s+CHARACTERISTICS|\s+STABILITY)?:\s*$', s, re.IGNORECASE)
mB = re.match(r'^STEP\s+\d+:\s+([A-Z][A-Z\s&]+?)(?:\s+ANALYSIS|\s+PATTERNS|\s+INDICATORS|\s+CHARACTERISTICS|\s+STABILITY)?\s*$', s, re.IGNORECASE)
mm = mA or mB
if mm:
if current_key and current_lines:
sections[current_key] = ' '.join(current_lines)
current_key = mm.group(1).upper().strip()
current_lines = []
elif current_key and s:
if re.match(r'^\d+\.\d+', s):
sub = re.sub(r'^\d+\.\d+[^:]*:\s*', '', s)
if sub: current_lines.append(sub)
elif s.startswith('-'):
current_lines.append(s.lstrip('-').strip())
elif not re.match(r'^\d+\.', s):
current_lines.append(s)
if current_key and current_lines:
sections[current_key] = ' '.join(current_lines)
result = {}
for dim, keywords in DIMS.items():
for k, txt in sections.items():
if any(kw in k for kw in keywords) and txt:
sents = re.split(r'(?<=[.!?])\s+', txt.strip())
summary = ' '.join(sents[:2])
result[dim] = summary[:157] + '…' if len(summary) > 160 else summary
break
return result
def _parse_s3(text):
pred, conf, r_lines, in_r = '', 0, [], False
for line in text.splitlines():
s = line.strip()
if s.startswith('INCOME_PREDICTION:'):
pred = s.replace('INCOME_PREDICTION:', '').strip()
elif s.startswith('INCOME_CONFIDENCE:'):
try: conf = int(re.search(r'\d+', s).group())
except: pass
elif s.startswith('INCOME_REASONING:'):
in_r = True
r_lines.append(s.replace('INCOME_REASONING:', '').strip())
elif in_r:
if re.match(r'^2\.', s) or s.startswith('INCOME_'): break
if s: r_lines.append(s)
reasoning = ' '.join(r_lines).strip()
sents = re.split(r'(?<=[.!?])\s+', reasoning)
reasoning = ' '.join(sents[:3])
return pred, conf, (reasoning[:277] + '…' if len(reasoning) > 280 else reasoning)
PROMPT_BULLETS = {
1: [
"Extract objective factual features from the agent's mobility trajectory without any interpretation",
"Location inventory: list all visited POIs with visit counts and apparent price tier (budget / mid-range / high-end)",
"Temporal patterns: time-of-day distribution, weekday vs. weekend split, and regularity of routines",
"Spatial characteristics: activity radius, average movement distance between locations",
"Sequence observations: common location transitions and typical daily activity chains",
],
2: [
"Perform behavioral abstraction across four dimensions based on Step 1 features",
"Routine & Schedule: infer work schedule type (fixed hours, flexible, shift work, etc.) and daily structure",
"Economic Behavior: assess spending tier from venue choices, transportation costs, and lifestyle signals",
"Social & Lifestyle: identify social engagement patterns, leisure activities, and community involvement",
"Routine Stability: evaluate consistency and regularity of movement patterns over the observation period",
],
3: [
"Synthesize factual features (Step 1) and behavioral patterns (Step 2) to infer household income bracket",
"Score location economic indicators: luxury / mid-range / budget venue distribution",
"Consider transportation mode signals, activity diversity, and temporal flexibility as income proxies",
"Output: INCOME_PREDICTION — a single income range with confidence rating (1–5)",
"Output: INCOME_REASONING — evidence-grounded justification referencing specific mobility observations",
],
}
PROMPT_INPUTS = {
1: "② Activity Chronicles + ③ Visiting Summaries — detailed daily visit logs and weekly behavioral statistics generated from raw stay points",
2: "Stage 1 response — factual features extracted from Activity Chronicles",
3: "Stage 1 + Stage 2 responses — feature extraction and behavioral abstraction combined",
}
_INPUT_TAG = ('Input')
def _extract_prompt_instruction(prompt_text, stage):
bullets = PROMPT_BULLETS.get(stage, [])
if not bullets:
return ''
inp = PROMPT_INPUTS.get(stage, '')
input_block = ('
'
+ _INPUT_TAG + inp + '
')
items = ''.join('
' + b + '
' for b in bullets)
return input_block + '
' + items + '
'
# ── Body renderers ────────────────────────────────────────────────────────────
def _s1_body(text, active):
if not active:
return '
Press ▶ to start
'
if not text:
return _loading('Extracting features')
locs, tod, wk, dist = _parse_s1(text)
max_v = max((v for _, v, _ in locs), default=1)
rows = ''
for name, visits, dur in locs:
bar_w = int(60 * visits / max_v)
dur_str = f'{int(dur)}m' if dur else '—'
rows += (f'
'
f'
{name}
'
f'
'
f'{visits}
'
f'
{dur_str}
')
table = (f'
'
f'
Location
Visits
Avg Stay
'
f'{rows}
') if rows else ''
def seg_bar(data, seg_classes):
total = sum(data.values()) or 1
segs = ''.join(
f''
for (label, v), cls in zip(data.items(), seg_classes))
legend = ''.join(
f'
{label} {v}%
'
for (label, v), cls in zip(data.items(), seg_classes))
return f'