Inspiration

I am from Palestine, and even though I didn't grow up there, I see and hear about what life is like there all the time. These sorts of tragedies highlight the value of hospitals being able to run well and being ready for the day they are needed. When a flood of patients arrives, or staff runs short, or the roads close, the hospitals need all the help they can get to save lives.

It's important to know when the ER is full, the ICU is almost full, and when one nurse is covering too many beds, but those facts live on different screens and different heads. I wanted to build something that puts all of it in one place, shows what is about to happen, and helps people decide what to do about it.

What it does

Hospital Twin is a live digital twin of a three-hospital network in Phoenix. A simulation generates patients, staff, vitals, and ambulance traffic, all from real-structure synthetic records. From there:

  • Models score every patient's risk of a critical event in the next six hours and show the top three reasons. They also forecast ER arrivals and how long each patient will stay.
  • A Gemini agent recommends actions like diverting ambulances, discharging low-risk patients, or calling in staff. Every number it cites is checked against the data, and a person approves before anything changes.
  • When a patient turns high risk, a pop-up goes to the nurse responsible for them, and that nurse walks to the bedside in the 3D hospital.
  • You can fly through a pixel-art 3D hospital where every person is a live record, or hold a key and ask "who is most likely to crash in the ICU?" and watch the camera fly to them.
  • Governance watches the models themselves for drift and unfairness, keeps an audit log, and lets a person roll a model back.
  • Because hospitals can't share patient records, a federated model learns across all three while only model weights leave each building.

It's live at hospital-twin.us.

How I built it

The backend is Python with FastAPI. A SimPy simulation runs one tick per simulated minute and writes to TimescaleDB on Tiger Cloud: hypertables for the time series, continuous aggregates for the dashboards, and compression for older data. Patients come from Synthea FHIR records.

The models are LightGBM with SHAP explanations, compared against NEWS2, the early-warning score hospitals actually use. The federated model is a PyTorch network trained with Flower. The agent and the voice questions use the Gemini API with structured output and function calling, with a deterministic fallback in both cases. The frontend is React, TypeScript and three.js, driven live over a WebSocket. It runs on a Vultr VM with Docker and Caddy for HTTPS on our own domain.

A rule I kept all weekend: nothing on screen is hardcoded. Everything comes from the simulation, the database or the models.

Challenges

  • Trusting an AI in a hospital setting. Language models invent things. So I built a checker that compares every number and patient id the agent cites with the data it was given. If the check fails, it retries once, then falls back to a plain rules engine, and the screen says which one answered.
  • Keeping the models honest. The simulation has a hidden "true severity" that the models never see, and the data is split by time so nothing leaks from the future. The score I'm proudest of is the length-of-stay model beating its baseline, because it only did after I made the simulation realistic about why people stay longer.
  • Drift detection that wasn't noisy. My first version raised alarms on perfectly normal days. Fixing it took per-hospital baselines, bias correction for small samples and finer bins at the tails.
  • The real service behaves differently from the tests. Three bugs only showed up against live Gemini, so I fixed each with a test first. I also added a list of models to fall back through, and a local-model option for when it hangs.
  • Deployment. CPU-only PyTorch in a container, a cloud database and HTTPS on a custom domain.

What I learned

I learned how much of "AI in healthcare" is not the model. It's the checking around it: baselines, drift, fairness, an audit trail, and a person who stays in charge. I also learned a lot of new tools along the way, including federated learning, TimescaleDB's time-series features, and testing a 3D scene in a headless browser. And I learned that testing against the real service early saves you the most time.

What's next

Real EHR integration through FHIR, differential privacy for the federated clients, and testing with clinicians on real data. Everything here is simulated and the models learn the simulator's own rules, so none of it is ready for clinical use. But I think the idea is right: hospitals should see trouble coming, and a tool should help them prepare for it.

Built With

Share this project:

Updates

Submission history