""" Choice — Check what you chose. Built for the Build Small Hackathon 2026. Log your decisions. Pick your path. Come back later. The Oracle watches your patterns and tells you what you already know but haven't admitted yet. "Your cautious choices work out better." "You do better when you decide quickly." "Most of your decisions are about money. That's where your attention lives." The past isn't fixed. It's a pattern. And patterns can be read. Everything stays in your browser. Nothing leaves your device. """ import gradio as gr import json import time import os import re from openai import OpenAI # --- NVIDIA Nemotron --- NVIDIA_API_KEY = os.environ.get("NVIDIA_API_KEY", "") NEMOTRON_MODEL = "nvidia/nemotron-3-nano-omni-30b-a3b-reasoning" nvidia_client = None if NVIDIA_API_KEY: nvidia_client = OpenAI( base_url="https://integrate.api.nvidia.com/v1", api_key=NVIDIA_API_KEY, ) print("NVIDIA Nemotron connected.") else: print("No NVIDIA_API_KEY — running in fallback mode.") # --- Oracle Engine (ported from choice/src/lib/oracle.js) --- DOMAIN_KEYWORDS = { "money": ["buy", "sell", "spend", "invest", "pay", "loan", "debt", "price", "cost", "rent", "salary", "save", "budget", "finance"], "work": ["job", "career", "boss", "hire", "quit", "project", "deadline", "client", "meeting", "promotion", "work", "business", "startup"], "people": ["friend", "family", "partner", "date", "relationship", "talk", "tell", "trust", "text", "call", "forgive", "apologize", "mom", "dad", "kid"], "health": ["eat", "sleep", "exercise", "gym", "doctor", "medicine", "therapy", "mental", "stress", "drink", "smoke", "sober"], "risk": ["risk", "safe", "gamble", "chance", "bet", "try", "leap", "bold", "scared", "afraid", "comfortable"], "home": ["move", "apartment", "house", "rent", "lease", "renovate", "fix", "clean"], } POSITIVE_WORDS = ["good", "great", "worked", "glad", "happy", "right", "best", "perfect", "worth", "amazing", "yes"] NEGATIVE_WORDS = ["bad", "wrong", "regret", "mistake", "terrible", "worse", "failed", "awful", "wish", "disaster"] def detect_domains(text): lower = text.lower() found = [dom for dom, kws in DOMAIN_KEYWORDS.items() if any(kw in lower for kw in kws)] return found if found else ["general"] def outcome_sentiment(actual): if not actual: return "unknown" lower = actual.lower() pos = sum(1 for w in POSITIVE_WORDS if w in lower) neg = sum(1 for w in NEGATIVE_WORDS if w in lower) if pos > neg: return "positive" if neg > pos: return "negative" return "neutral" def choice_style(chosen_index, num_options): if chosen_index == 0: return "safe" if num_options > 2 and chosen_index == num_options - 1: return "bold" return "moderate" def consult_oracle(decisions): """The Oracle: find patterns in resolved decisions.""" resolved = [d for d in decisions if d.get("resolved")] if len(resolved) < 3: return { "ready": False, "message": f"The Oracle needs at least 3 resolved decisions. You have {len(resolved)}.", "insights": [] } domain_counts = {} domain_outcomes = {} style_outcomes = {"safe": {"positive": 0, "negative": 0, "neutral": 0}, "moderate": {"positive": 0, "negative": 0, "neutral": 0}, "bold": {"positive": 0, "negative": 0, "neutral": 0}} timings = [] for d in resolved: domains = detect_domains(d["question"]) style = choice_style(d.get("chosen_index", 0), len(d.get("options", ["a", "b"]))) sentiment = outcome_sentiment(d.get("actual", "")) for dom in domains: domain_counts[dom] = domain_counts.get(dom, 0) + 1 if dom not in domain_outcomes: domain_outcomes[dom] = {"positive": 0, "negative": 0, "neutral": 0} domain_outcomes[dom][sentiment] += 1 style_outcomes[style][sentiment] += 1 if d.get("created_at") and d.get("resolved_at"): days = (d["resolved_at"] - d["created_at"]) / 86400 timings.append({"days": days, "sentiment": sentiment, "style": style}) insights = [] # Domain insights for dom, outcomes in domain_outcomes.items(): total = sum(outcomes.values()) if total >= 2: pos_rate = round(outcomes["positive"] / total * 100) neg_rate = round(outcomes["negative"] / total * 100) if outcomes["positive"] > outcomes["negative"]: insights.append(f"Your {dom} decisions have gone well {pos_rate}% of the time ({total} resolved).") elif outcomes["negative"] > outcomes["positive"]: insights.append(f"{neg_rate}% of your {dom} decisions had negative outcomes ({total} resolved). Slow down on this one.") # Style insights bold_total = sum(style_outcomes["bold"].values()) safe_total = sum(style_outcomes["safe"].values()) if bold_total >= 2 and safe_total >= 2: bold_pos = style_outcomes["bold"]["positive"] / bold_total if bold_total else 0 safe_pos = style_outcomes["safe"]["positive"] / safe_total if safe_total else 0 if bold_pos > safe_pos + 0.15: insights.append(f"When you take the bolder path, outcomes are better — {round(bold_pos*100)}% vs {round(safe_pos*100)}% for safe choices.") elif safe_pos > bold_pos + 0.15: insights.append(f"Your cautious choices work out better — {round(safe_pos*100)}% positive vs {round(bold_pos*100)}% for bold moves.") # Timing insights fast = [t for t in timings if t["days"] < 3] slow = [t for t in timings if t["days"] >= 7] if len(fast) >= 2 and len(slow) >= 2: fast_pos = sum(1 for t in fast if t["sentiment"] == "positive") / len(fast) slow_pos = sum(1 for t in slow if t["sentiment"] == "positive") / len(slow) if fast_pos > slow_pos + 0.15: insights.append(f"You do better when you decide quickly — {round(fast_pos*100)}% positive vs {round(slow_pos*100)}% when you deliberate.") elif slow_pos > fast_pos + 0.15: insights.append(f"Sleeping on it works for you — {round(slow_pos*100)}% positive when you take time vs {round(fast_pos*100)}% for snap decisions.") # Volume insight if domain_counts: top_dom = max(domain_counts, key=domain_counts.get) if domain_counts[top_dom] >= 3: insights.append(f"Most of your decisions are about {top_dom} ({domain_counts[top_dom]} of {len(resolved)}). That's where your attention lives.") return { "ready": True, "message": f"Based on {len(resolved)} resolved decisions:", "insights": insights } def ai_reflect(question, options, decisions): """AI-generated reflection on a new decision, given history.""" if not nvidia_client: return "" history_summary = "" resolved = [d for d in decisions if d.get("resolved")] if resolved: recent = resolved[-5:] lines = [] for d in recent: sent = outcome_sentiment(d.get("actual", "")) lines.append(f"- \"{d['question']}\" → chose \"{d.get('chosen', '?')}\" → {sent}") history_summary = f"\n\nRecent decisions:\n" + "\n".join(lines) try: response = nvidia_client.chat.completions.create( model=NEMOTRON_MODEL, messages=[ {"role": "system", "content": "You are a wise, direct advisor. Given someone's decision and their history of past choices, offer ONE brief insight (2-3 sentences max). Be honest. No fluff. If you see a pattern, name it."}, {"role": "user", "content": f"I'm deciding: {question}\nOptions: {', '.join(options)}{history_summary}"} ], max_tokens=400, temperature=0.5, ) return response.choices[0].message.content.strip() except: return "" # --- State Management --- def add_decision(question, option_a, option_b, option_c, state): """Add a new decision fork.""" decisions = state or [] options = [o for o in [option_a, option_b, option_c] if o and o.strip()] if not question or len(options) < 2: return state, render_decisions(decisions), "Need a question and at least 2 options." decision = { "id": str(int(time.time() * 1000)), "question": question.strip(), "options": options, "chosen": None, "chosen_index": None, "actual": None, "resolved": False, "created_at": time.time(), "resolved_at": None, } decisions.insert(0, decision) reflection = ai_reflect(question, options, decisions) return decisions, render_decisions(decisions), reflection if reflection else "Fork logged. Choose when you're ready." def make_choice(decision_id, choice_text, state): """Record which option was chosen.""" decisions = state or [] for d in decisions: if d["id"] == decision_id: if choice_text in d["options"]: d["chosen"] = choice_text d["chosen_index"] = d["options"].index(choice_text) return decisions, render_decisions(decisions) def resolve_decision(decision_id, actual_outcome, state): """Record what actually happened.""" decisions = state or [] for d in decisions: if d["id"] == decision_id: d["actual"] = actual_outcome d["resolved"] = True d["resolved_at"] = time.time() return decisions, render_decisions(decisions), render_oracle(decisions) # --- Rendering --- def render_decisions(decisions): if not decisions: return """
Check what you chose.