Spaces:
Sleeping
Sleeping
Commit Β·
529e548
1
Parent(s): 144e51b
clear1
Browse files3 unmodified lines456789101112131415161732 unmodified lines5051525354555657585930 unmodified lines9091929394959624 unmodified lines1211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214225 unmodified lines4284294304314324334344354364374384394404414424434444454 unmodified lines4504514524534544554564574584594604614624634644652 unmodified lines46846947047147247347447547647747847948048148248362 unmodified lines5465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955962 unmodified lines5996006016026036046056066076086096106116128 unmodified lines6216226236246256266276283 unmodified lines6326336346356366376386396406416424 unmodified lines6476486496506516526536546553 unmodified linesimport numpy as npimport osimport refrom huggingface_hub import InferenceClientBASE = os.path.dirname(os.path.abspath(__file__))STAY_POINTS = os.path.join(BASE, "data", "stay_points_sampled.csv")POI_PATH = os.path.join(BASE, "data", "poi_sampled.csv")DEMO_PATH = os.path.join(BASE, "data", "demographics_sampled.csv")MODEL_ID = "meta-llama/Llama-3.2-1B-Instruct"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",32 unmodified linesreturn str(x)sp["act_label"] = sp["act_types"].apply(parse_act_types)sample_agents = sorted(sp["agent_id"].unique().tolist())print(f"Ready. {len(sample_agents)} agents loaded.")# ββ Mobility text builders ββββββββββββββββββββββββββββββββββββββββββββββββββββdef build_mobility_summary(agent_sp):30 unmodified linesreturn "night"agent_sp2["tod"] = agent_sp2["hour"].apply(tod)tod_pct = (agent_sp2["tod"].value_counts(normalize=True) * 100).round(0).astype(int)agent_sp2["is_weekend"] = agent_sp2["start_datetime"].dt.dayofweek >= 5wd_pct = int((~agent_sp2["is_weekend"]).mean() * 100)24 unmodified linesreturn "\n".join(lines)# ββ Prompts βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββSTEP1_SYSTEM = """You are an expert mobility analyst. Extract objective features from the trajectory data.Respond with EXACTLY this structure, keep each point to one short sentence:LOCATION INVENTORY:- Top venues: [list top 3 with visit counts]- Price level: [budget/mid-range/high-end mix]- Neighborhood: [residential/commercial/urban/suburban]TEMPORAL PATTERNS:- Active hours: [time range]- Weekday/Weekend: [ratio]- Routine: [consistent/variable]SEQUENCE:- Typical chain: [e.g. Home β Work β Home]- Notable pattern: [one observation]Do NOT interpret or infer demographics. Be concise."""STEP2_SYSTEM = """You are an expert mobility analyst. Based on the extracted features, analyze behavioral patterns.Respond with EXACTLY this structure, one short sentence per point:SCHEDULE: [fixed/flexible/shift β one sentence]ECONOMIC: [budget/mid-range/premium spending β one sentence]SOCIAL: [family/individual/community focus β one sentence]LIFESTYLE: [urban professional/suburban/student/other β one sentence]STABILITY: [routine consistency β one sentence]Do NOT make income predictions yet. Be concise."""STEP3_SYSTEM = """You are an expert mobility analyst performing final income inference.Based on the trajectory features and behavioral analysis, output EXACTLY:INCOME_PREDICTION: [Very Low (<$15k) | Low ($15k-$35k) | Middle ($35k-$75k) | Upper-Middle ($75k-$125k) | High ($125k-$200k) | Very High (>$200k)]INCOME_CONFIDENCE: [1-5]INCOME_REASONING: [2-3 sentences linking specific mobility evidence to the prediction]ALTERNATIVES: [2nd most likely] | [3rd most likely]"""def call_llm(client, system_prompt, user_content, max_tokens=400):response = client.chat.completions.create(model=MODEL_ID,messages=[{"role": "system", "content": system_prompt},{"role": "user", "content": user_content},],max_tokens=max_tokens,temperature=0.3,)return response.choices[0].message.content.strip()# ββ HTML rendering ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββCHAIN_CSS = """<style>@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600&family=IBM+Plex+Sans:wght@300;400;600&display=swap');.hicotraj-chain {font-family: 'IBM Plex Sans', sans-serif;padding: 12px 4px;max-width: 100%;}/* Stage cards */.stage-card {border-radius: 10px;padding: 16px 18px;margin-bottom: 0;position: relative;transition: box-shadow 0.3s;}.stage-card.dim { opacity: 0.35; filter: grayscale(0.4); }.stage-card.active { box-shadow: 0 4px 20px rgba(0,0,0,0.12); opacity: 1; filter: none; }.stage-card.s1 { background: #f8f9fc; border: 1.5px solid #c8d0e0; }.stage-card.s2 { background: #fdf6f0; border: 1.5px solid #e8c9a8; }.stage-card.s3 { background: #fff8f8; border: 2px solid #c0392b; }.stage-header {display: flex;align-items: center;gap: 10px;margin-bottom: 10px;}.stage-badge {font-family: 'IBM Plex Mono', monospace;font-size: 10px;font-weight: 600;letter-spacing: 0.08em;padding: 3px 8px;border-radius: 4px;text-transform: uppercase;}.s1 .stage-badge { background: #dde3f0; color: #3a4a6b; }.s2 .stage-badge { background: #f0dcc8; color: #7a4010; }.s3 .stage-badge { background: #c0392b; color: #fff; }.stage-title {font-size: 13px;font-weight: 600;color: #1a1a2e;}/* Content inside cards */.tag-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }.tag {font-family: 'IBM Plex Mono', monospace;font-size: 11px;background: #e8ecf5;color: #2c3e60;padding: 3px 8px;border-radius: 4px;white-space: nowrap;}.s2 .tag { background: #f5e8d8; color: #6b3a10; }.behavior-row {display: grid;grid-template-columns: 100px 1fr;gap: 4px 10px;margin-top: 2px;font-size: 12px;line-height: 1.5;}.bkey {font-family: 'IBM Plex Mono', monospace;font-size: 11px;font-weight: 600;color: #9b6a3a;padding-top: 1px;}.bval { color: #3a2a1a; }/* Prediction block */.pred-block { margin-top: 8px; }.pred-label {font-size: 11px;font-family: 'IBM Plex Mono', monospace;color: #888;text-transform: uppercase;letter-spacing: 0.06em;margin-bottom: 4px;}.pred-value {font-size: 22px;font-weight: 600;color: #c0392b;letter-spacing: -0.01em;margin-bottom: 8px;}.confidence-bar-wrap {display: flex;align-items: center;gap: 10px;margin-bottom: 10px;}.confidence-bar-bg {flex: 1;height: 6px;background: #f0d0cf;border-radius: 3px;overflow: hidden;}.confidence-bar-fill {height: 100%;background: linear-gradient(90deg, #e74c3c, #8b0000);border-radius: 3px;transition: width 0.8s ease;}.confidence-label {font-family: 'IBM Plex Mono', monospace;font-size: 11px;color: #c0392b;font-weight: 600;white-space: nowrap;}.reasoning-text {font-size: 12px;color: #4a2a2a;line-height: 1.6;border-left: 3px solid #e8c0be;padding-left: 10px;margin-top: 6px;}.alternatives {margin-top: 10px;font-size: 11px;font-family: 'IBM Plex Mono', monospace;color: #999;}.alternatives span { color: #c0392b; opacity: 0.7; }/* Arrow connector */.chain-arrow {display: flex;flex-direction: column;align-items: center;margin: 0;padding: 4px 0;gap: 0;}.arrow-line {width: 2px;height: 18px;background: linear-gradient(180deg, #c8d0e0, #e8c9a8);}.arrow-label {font-family: 'IBM Plex Mono', monospace;font-size: 10px;color: #aaa;letter-spacing: 0.06em;text-transform: uppercase;background: white;padding: 2px 8px;border: 1px solid #e0e0e0;border-radius: 10px;margin: 2px 0;}.arrow-tip {width: 0; height: 0;border-left: 5px solid transparent;border-right: 5px solid transparent;border-top: 7px solid #e8c9a8;}/* Waiting state */.waiting-dot {display: inline-block;width: 7px; height: 7px;border-radius: 50%;background: #ccc;margin: 0 2px;animation: pulse 1.2s ease-in-out infinite;}.waiting-dot:nth-child(2) { animation-delay: 0.2s; }.waiting-dot:nth-child(3) { animation-delay: 0.4s; }@keyframes pulse {0%, 100% { opacity: 0.3; transform: scale(0.8); }50% { opacity: 1; transform: scale(1.1); }}</style>"""def _waiting_dots():return '<span class="waiting-dot"></span><span class="waiting-dot"></span><span class="waiting-dot"></span>'def render_chain(s1_text="", s2_text="", s3_text="", status="idle"):"""status: idle | running1 | running2 | running3 | done"""s1_active = status in ("running1", "running2", "running3", "done")s2_active = status in ("running2", "running3", "done")s3_active = status in ("running3", "done")# ββ Stage 1 content ββββββββββββββββββββββββββββββββββββββββββββββββββββββif status == "running1":s1_content = f'<div style="padding:8px 0; color:#888; font-size:13px;">Extracting features {_waiting_dots()}</div>'elif s1_text:# Parse tags from the response β pull out short bullet points as tagstags = []for line in s1_text.splitlines():line = line.strip().lstrip("-").strip()if line and len(line) < 60 and not line.endswith(":"):tags.append(line)if len(tags) >= 8:breaktag_html = "".join(f'<span class="tag">{t}</span>' for t in tags[:8])s1_content = f'<div class="tag-row">{tag_html}</div>'else:s1_content = '<div style="font-size:12px;color:#bbb;padding:6px 0;">Run inference to see results</div>'# ββ Stage 2 content ββββββββββββββββββββββββββββββββββββββββββββββββββββββBEHAVIOR_KEYS = ["SCHEDULE", "ECONOMIC", "SOCIAL", "LIFESTYLE", "STABILITY"]if status == "running2":s2_content = f'<div style="padding:8px 0; color:#a06030; font-size:13px;">Analyzing behavior {_waiting_dots()}</div>'elif s2_text:rows_html = ""for key in BEHAVIOR_KEYS:pattern = rf"{key}[:\s]+(.+)"m = re.search(pattern, s2_text, re.IGNORECASE)val = m.group(1).strip().rstrip(".") if m else "β"if len(val) > 80:val = val[:77] + "..."rows_html += f'<div class="bkey">{key}</div><div class="bval">{val}</div>'s2_content = f'<div class="behavior-row">{rows_html}</div>'else:s2_content = '<div style="font-size:12px;color:#bbb;padding:6px 0;">Run inference to see results</div>'# ββ Stage 3 content ββββββββββββββββββββββββββββββββββββββββββββββββββββββif status == "running3":s3_content = f'<div style="padding:8px 0; color:#c0392b; font-size:13px;">Inferring demographics {_waiting_dots()}</div>'elif s3_text:# Parse structured outputpred = conf_raw = reasoning = alts = ""for line in s3_text.splitlines():line = line.strip()5 unmodified linesreasoning = line.replace("INCOME_REASONING:", "").strip()elif line.startswith("ALTERNATIVES:"):alts = line.replace("ALTERNATIVES:", "").strip()# Confidence bartry:conf_int = int(re.search(r"\d", conf_raw).group())except:conf_int = 3bar_pct = conf_int * 20alts_html = ""if alts:alts_html = f'<div class="alternatives">Also possible: <span>{alts}</span></div>'s3_content = f"""<div class="pred-block"><div class="pred-label">Income Prediction</div>4 unmodified lines</div><div class="confidence-label">Confidence {conf_int}/5</div></div><div class="reasoning-text">{reasoning or s3_text[:200]}</div>{alts_html}</div>"""else:s3_content = '<div style="font-size:12px;color:#bbb;padding:6px 0;">Run inference to see results</div>'def card(cls, badge, title, content, active):dim_cls = "active" if active else "dim"return f"""<div class="stage-card {cls} {dim_cls}"><div class="stage-header"><span class="stage-badge">{badge}</span><span class="stage-title">{title}</span>2 unmodified lines</div>"""def arrow(label, active):opacity = "1" if active else "0.3"return f"""<div class="chain-arrow" style="opacity:{opacity}"><div class="arrow-line"></div><div class="arrow-label">{label}</div><div class="arrow-line"></div><div class="arrow-tip"></div></div>"""html = CHAIN_CSS + '<div class="hicotraj-chain">'html += card("s1", "Stage 1", "Factual Feature Extraction", s1_content, s1_active)html += arrow("behavioral abstraction", s2_active)html += card("s2", "Stage 2", "Behavioral Pattern Analysis", s2_content, s2_active)62 unmodified linesmap_html = build_map(agent_sp)demo_text = build_demo_text(agent_demo)raw_text = build_mobility_summary(agent_sp) + "\n\n" + build_weekly_checkin(agent_sp)chain_html = render_chain(status="idle")return map_html, raw_text, demo_text, chain_htmldef run_inference(agent_id, hf_token):if not hf_token or not hf_token.strip():yield render_chain(s3_text="β οΈ Please enter your Hugging Face token first.", status="done")returnagent_id = int(agent_id)agent_sp = sp[sp["agent_id"] == agent_id].sort_values("start_datetime")traj_text = build_mobility_summary(agent_sp) + "\n\n" + build_weekly_checkin(agent_sp)try:client = InferenceClient(token=hf_token.strip())yield render_chain(status="running1")s1 = call_llm(client, STEP1_SYSTEM, traj_text, max_tokens=400)yield render_chain(s1_text=s1, status="running2")s2_input = f"Features:\n{s1}\n\nNow analyze behavioral patterns."s2 = call_llm(client, STEP2_SYSTEM, s2_input, max_tokens=300)yield render_chain(s1_text=s1, s2_text=s2, status="running3")s3_input = f"Features:\n{s1}\n\nBehavioral analysis:\n{s2}\n\nNow infer income."s3 = call_llm(client, STEP3_SYSTEM, s3_input, max_tokens=300)yield render_chain(s1_text=s1, s2_text=s2, s3_text=s3, status="done")except Exception as e:yield render_chain(s3_text=f"β Error: {str(e)}", status="done")def call_llm(client, system_prompt, user_content, max_tokens=400):response = client.chat.completions.create(model=MODEL_ID,messages=[{"role": "system", "content": system_prompt},{"role": "user", "content": user_content},],max_tokens=max_tokens,temperature=0.3,)return response.choices[0].message.content.strip()# ββ UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ2 unmodified linesgr.Markdown("## HiCoTraj β Trajectory Visualization & Hierarchical CoT Demo")gr.Markdown("*Zero-Shot Demographic Reasoning via Hierarchical Chain-of-Thought Prompting from Trajectory*")with gr.Row():hf_token_box = gr.Textbox(label="Hugging Face Token",placeholder="hf_...",type="password",scale=2)with gr.Row():agent_dd = gr.Dropdown(choices=[str(a) for a in sample_agents],8 unmodified lines)with gr.Row():# LEFT: map + NUMOSIM datawith gr.Column(scale=1):gr.Markdown("### Trajectory Map")map_out = gr.HTML()3 unmodified lineslabel="Mobility Summary + Weekly Check-in")# RIGHT: reasoning chainwith gr.Column(scale=1):gr.Markdown("### Hierarchical Chain-of-Thought Reasoning")run_btn = gr.Button("βΆ Run HiCoTraj Inference", variant="primary")chain_out = gr.HTML(value=render_chain(status="idle"))agent_dd.change(fn=on_select, inputs=agent_dd,4 unmodified linesoutputs=[map_out, raw_out, demo_label, chain_out])run_btn.click(fn=run_inference,inputs=[agent_dd, hf_token_box],outputs=[chain_out])if __name__ == "__main__":3 unmodified lines4567891011121314151632 unmodified lines49505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710830 unmodified lines13914014114214314424 unmodified lines1691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083095 unmodified lines3153163173183193203213223233243253264 unmodified lines3313323333343353363373383393403413423433443453462 unmodified lines34935035135235335435535635735835936036136236336462 unmodified lines4274284294304314324334344354364374384394404414424434444454464474484492 unmodified lines4524534544554564578 unmodified lines4664674684694704713 unmodified lines4754764774784794804814824834844 unmodified lines4894904914924934944954963 unmodified linesimport numpy as npimport osimport reimport jsonBASE = os.path.dirname(os.path.abspath(__file__))STAY_POINTS = os.path.join(BASE, "data", "stay_points_sampled.csv")POI_PATH = os.path.join(BASE, "data", "poi_sampled.csv")DEMO_PATH = os.path.join(BASE, "data", "demographics_sampled.csv")COT_PATH = os.path.join(BASE, "data", "cot_results.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",32 unmodified linesreturn 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)for result in cot_raw.get("inference_results", []):cot_by_agent[result["agent_id"]] = resultprint(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.")# ββ Mock CoT (fallback when agent not in JSON) ββββββββββββββββββββββββββββββββMOCK_S1 = """LOCATION INVENTORY:- Top venues: residence (36 visits), Clinton Mobile Estates (9 visits), 7-Eleven (8 visits)- Price level: budget (7-Eleven, car wash) and mid-range (Euro Caffe, Pepper Shaker Cafe)- Neighborhood: residential and commercial urban mixTEMPORAL PATTERNS:- Active hours: 09:00-23:00- Weekday/Weekend: 66% weekday, 34% weekend- Routine: consistent morning start timesSEQUENCE:- Typical chain: Home to Exercise/Work to Home- Notable pattern: weekend religious visits every Sunday morning"""MOCK_S2 = """SCHEDULE: Fixed weekday routine with flexible afternoon activitiesECONOMIC: Budget-conscious with occasional mid-range diningSOCIAL: Community-engaged through regular religious attendanceLIFESTYLE: Urban working-class with active recreational habitsSTABILITY: Highly consistent 4-week pattern with minimal deviation"""MOCK_S3 = """INCOME_PREDICTION: Middle ($35k-$75k)INCOME_CONFIDENCE: 4INCOME_REASONING: Frequent budget venue visits (7-Eleven, self-service car wash) signal cost awareness, while occasional mid-range dining and stable employment-like patterns at Clinton Mobile Estates suggest a steady middle income. No luxury venue signals detected.ALTERNATIVES: Low ($15k-$35k) | Upper-Middle ($75k-$125k)"""def get_cot(agent_id):"""Return (s1, s2, s3) text for agent, falling back to mock."""result = cot_by_agent.get(agent_id)if result:s1 = result.get("step1_response", MOCK_S1)s2 = result.get("step2_response", MOCK_S2)s3 = result.get("step3_response", MOCK_S3)else:s1, s2, s3 = MOCK_S1, MOCK_S2, MOCK_S3return s1, s2, s3# ββ Mobility text builders ββββββββββββββββββββββββββββββββββββββββββββββββββββdef build_mobility_summary(agent_sp):30 unmodified linesreturn "night"agent_sp2["tod"] = agent_sp2["hour"].apply(tod)tod_pct = (agent_sp2["tod"].value_counts(normalize=True) * 100).round(0).astype(int)agent_sp2["is_weekend"] = agent_sp2["start_datetime"].dt.dayofweek >= 5wd_pct = int((~agent_sp2["is_weekend"]).mean() * 100)24 unmodified linesreturn "\n".join(lines)# ββ HTML reasoning chain ββββββββββββββββββββββββββββββββββββββββββββββββββββββCHAIN_CSS = """<style>@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600&family=IBM+Plex+Sans:wght@300;400;600&display=swap');.hicotraj-chain { font-family: 'IBM Plex Sans', sans-serif; padding: 12px 4px; }.stage-card {border-radius: 10px; padding: 16px 18px; margin-bottom: 0;transition: opacity 0.4s, filter 0.4s;}.stage-card.dim { opacity: 0.32; filter: grayscale(0.5); }.stage-card.active { opacity: 1; filter: none; }.stage-card.s1 { background: #f8f9fc; border: 1.5px solid #c8d0e0; }.stage-card.s2 { background: #fdf6f0; border: 1.5px solid #e8c9a8; }.stage-card.s3 { background: #fff8f8; border: 2px solid #c0392b; }.stage-header { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }.stage-badge {font-family: 'IBM Plex Mono', monospace;font-size: 10px; font-weight: 600; letter-spacing: 0.08em;padding: 3px 8px; border-radius: 4px; text-transform: uppercase;}.s1 .stage-badge { background: #dde3f0; color: #3a4a6b; }.s2 .stage-badge { background: #f0dcc8; color: #7a4010; }.s3 .stage-badge { background: #c0392b; color: #fff; }.stage-title { font-size: 13px; font-weight: 600; color: #1a1a2e; }.tag-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }.tag {font-family: 'IBM Plex Mono', monospace; font-size: 11px;background: #e8ecf5; color: #2c3e60;padding: 3px 8px; border-radius: 4px; white-space: nowrap;}.behavior-row {display: grid; grid-template-columns: 100px 1fr;gap: 4px 10px; margin-top: 2px; font-size: 12px; line-height: 1.6;}.bkey {font-family: 'IBM Plex Mono', monospace; font-size: 11px;font-weight: 600; color: #9b6a3a; padding-top: 1px;}.bval { color: #3a2a1a; }.pred-block { margin-top: 4px; }.pred-label {font-size: 11px; font-family: 'IBM Plex Mono', monospace; color: #888;text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 4px;}.pred-value { font-size: 22px; font-weight: 600; color: #c0392b; margin-bottom: 8px; }.confidence-bar-wrap { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }.confidence-bar-bg { flex: 1; height: 6px; background: #f0d0cf; border-radius: 3px; overflow: hidden; }.confidence-bar-fill { height: 100%; background: linear-gradient(90deg, #e74c3c, #8b0000); border-radius: 3px; }.confidence-label { font-family: 'IBM Plex Mono', monospace; font-size: 11px; color: #c0392b; font-weight: 600; white-space: nowrap; }.reasoning-text { font-size: 12px; color: #4a2a2a; line-height: 1.6; border-left: 3px solid #e8c0be; padding-left: 10px; margin-top: 6px; }.alternatives { margin-top: 10px; font-size: 11px; font-family: 'IBM Plex Mono', monospace; color: #999; }.alternatives span { color: #c0392b; opacity: 0.7; }.chain-arrow { display: flex; flex-direction: column; align-items: center; padding: 4px 0; transition: opacity 0.4s; }.arrow-line { width: 2px; height: 16px; background: #d0c0b0; }.arrow-label {font-family: 'IBM Plex Mono', monospace; font-size: 10px; color: #aaa;letter-spacing: 0.06em; text-transform: uppercase;background: white; padding: 2px 8px; border: 1px solid #e0e0e0; border-radius: 10px; margin: 2px 0;}.arrow-tip { width: 0; height: 0; border-left: 5px solid transparent; border-right: 5px solid transparent; border-top: 7px solid #d0c0b0; }.thinking { font-size: 13px; color: #888; padding: 8px 0; }.empty-hint { font-size: 12px; color: #ccc; padding: 6px 0; }.wd {display: inline-block; width: 6px; height: 6px; border-radius: 50%;background: currentColor; margin: 0 2px; opacity: 0.3;animation: wd-pulse 1.2s ease-in-out infinite;}.wd:nth-child(2) { animation-delay: 0.2s; }.wd:nth-child(3) { animation-delay: 0.4s; }@keyframes wd-pulse {0%, 100% { opacity: 0.2; transform: scale(0.8); }50% { opacity: 1; transform: scale(1.1); }}</style>"""def _dots():return '<span class="wd"></span><span class="wd"></span><span class="wd"></span>'def render_chain(s1_text, s2_text, s3_text, status="done"):# status: idle | running1 | running2 | running3 | dones1_active = status in ("running1", "running2", "running3", "done")s2_active = status in ("running2", "running3", "done")s3_active = status in ("running3", "done")# ββ Stage 1 βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββif status == "running1":s1_content = f'<div class="thinking">Extracting features {_dots()}</div>'elif s1_text:tags = []for line in s1_text.splitlines():line = line.strip().lstrip("-").strip()if line and len(line) < 65 and not line.endswith(":"):tags.append(line)if len(tags) >= 9:breaks1_content = '<div class="tag-row">' + \"".join(f'<span class="tag">{t}</span>' for t in tags[:9]) + \'</div>'else:s1_content = '<div class="empty-hint">Press βΆ Run HiCoTraj to start</div>'# ββ Stage 2 βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββKEYS = ["SCHEDULE", "ECONOMIC", "SOCIAL", "LIFESTYLE", "STABILITY"]if status == "running2":s2_content = f'<div class="thinking" style="color:#a06030">Analyzing behavior {_dots()}</div>'elif s2_text:rows_html = ""for key in KEYS:m = re.search(rf"{key}[:\s]+(.+)", s2_text, re.IGNORECASE)val = m.group(1).strip().rstrip(".") if m else "β"if len(val) > 85:val = val[:82] + "..."rows_html += f'<div class="bkey">{key}</div><div class="bval">{val}</div>'s2_content = f'<div class="behavior-row">{rows_html}</div>'else:s2_content = '<div class="empty-hint">Waiting...</div>'# ββ Stage 3 βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββif status == "running3":s3_content = f'<div class="thinking" style="color:#c0392b">Inferring demographics {_dots()}</div>'elif s3_text:pred = conf_raw = reasoning = alts = ""for line in s3_text.splitlines():line = line.strip()5 unmodified linesreasoning = line.replace("INCOME_REASONING:", "").strip()elif line.startswith("ALTERNATIVES:"):alts = line.replace("ALTERNATIVES:", "").strip()try:conf_int = int(re.search(r"\d", conf_raw).group())except:conf_int = 3bar_pct = conf_int * 20alts_html = f'<div class="alternatives">Also possible: <span>{alts}</span></div>' if alts else ""s3_content = f"""<div class="pred-block"><div class="pred-label">Income Prediction</div>4 unmodified lines</div><div class="confidence-label">Confidence {conf_int}/5</div></div><div class="reasoning-text">{reasoning}</div>{alts_html}</div>"""else:s3_content = '<div class="empty-hint">Waiting...</div>'def card(cls, badge, title, content, active):dim = "active" if active else "dim"return f"""<div class="stage-card {cls} {dim}"><div class="stage-header"><span class="stage-badge">{badge}</span><span class="stage-title">{title}</span>2 unmodified lines</div>"""def arrow(label, active):op = "1" if active else "0.25"return f"""<div class="chain-arrow" style="opacity:{op}"><div class="arrow-line"></div><div class="arrow-label">{label}</div><div class="arrow-line"></div><div class="arrow-tip"></div></div>"""html = CHAIN_CSS + '<div class="hicotraj-chain">'html += card("s1", "Stage 1", "Factual Feature Extraction", s1_content, s1_active)html += arrow("behavioral abstraction", s2_active)html += card("s2", "Stage 2", "Behavioral Pattern Analysis", s2_content, s2_active)62 unmodified linesmap_html = build_map(agent_sp)demo_text = build_demo_text(agent_demo)raw_text = build_mobility_summary(agent_sp) + "\n\n" + build_weekly_checkin(agent_sp)chain_html = render_chain("", "", "", status="idle")return map_html, raw_text, demo_text, chain_htmldef run_reveal(agent_id):import timeagent_id = int(agent_id)s1, s2, s3 = get_cot(agent_id)yield render_chain("", "", "", status="running1")time.sleep(0.8)yield render_chain(s1, "", "", status="running2")time.sleep(0.8)yield render_chain(s1, s2, "", status="running3")time.sleep(0.8)yield render_chain(s1, s2, s3, status="done")# ββ UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ2 unmodified linesgr.Markdown("## HiCoTraj β Trajectory Visualization & Hierarchical CoT Demo")gr.Markdown("*Zero-Shot Demographic Reasoning via Hierarchical Chain-of-Thought Prompting from Trajectory*")with gr.Row():agent_dd = gr.Dropdown(choices=[str(a) for a in sample_agents],8 unmodified lines)with gr.Row():with gr.Column(scale=1):gr.Markdown("### Trajectory Map")map_out = gr.HTML()3 unmodified lineslabel="Mobility Summary + Weekly Check-in")with gr.Column(scale=1):gr.Markdown("### Hierarchical Chain-of-Thought Reasoning")run_btn = gr.Button("βΆ Run HiCoTraj", variant="primary")chain_out = gr.HTML(value=render_chain("", "", "", status="idle"))agent_dd.change(fn=on_select, inputs=agent_dd,4 unmodified linesoutputs=[map_out, raw_out, demo_label, chain_out])run_btn.click(fn=run_reveal, inputs=agent_dd,outputs=chain_out)if __name__ == "__main__":
app.py+143-302