←

DiabetesSLM

2023 · Python, PyTorch, Flask, Hugging Face

DiabetesSLM is a small language model fine-tuned to answer questions about diabetes, served through a simple web interface on Hugging Face Spaces. I wanted to know whether a fine-tuned Flan-T5-large could answer diabetes questions reliably, without the cost or latency of a frontier model.

This is an educational demo, not medical advice. Anyone with a real health concern should talk to a qualified professional.

DiabetesSLM question and answer interface

Why

General-purpose models are capable but awkward for narrow jobs. They’re expensive to run, they can be confidently wrong outside what they’ve seen, and depending on an external API makes self-hosting and privacy-sensitive uses hard. In a focused domain most of a large model goes unused, so a smaller, tuned model should give the same answers for a fraction of the compute.

What it does

I fine-tuned Google’s Flan-T5-large on question and answer pairs built from the NHS’s diabetes content. It answers common questions about symptoms, management, diet and complications, runs comfortably on modest hardware, and usually responds in a few hundred milliseconds.

The model answering a question

A question typed into the chat goes to app.py, which hands it to model_runner.py. That cleans up and tokenises the input, the model generates an answer, and the decoded text comes back to the chat window.

How it’s built

It’s Python throughout, on PyTorch, using transformers to load the model, datasets to turn the CSV into a training set, sentencepiece for tokenisation, and accelerate to train on a single GPU. Flask serves the API and Gradio provides the chat interface, which meant writing no frontend at all.

The Space rebuilds from the Git repository on every push and serves the model on a free CPU instance. The weights are stored with Git LFS, and a Dockerfile is included for running it on your own hardware. The training script, dataset and inference code are all in the repository, so anyone can fine-tune their own version in a few hours.

A sample answer

What I learned

Small models hold up in narrow domains. Flan-T5-large has 780 million parameters, a fraction of a GPT-class model, yet after fine-tuning it’s competitive at this one task. Ask it about anything else and it confidently answers nonsense, so the domain has to stay narrow.

Consistent data beats more data. The training set is a few thousand pairs, all from one source, in one voice, with one structure, and that consistency is what the model learned. Adding data from varied sources made the answers worse.

Spaces is a good home for demos. Push to Git, it builds, and you get a public URL, with no servers, no scaling config and no card on file. The free CPU tier is slow for large models but fine at this size.

Be honest about the limits. The model hallucinates, knows nothing of recent research, and struggles with compound or ambiguous questions. Rather than hide that, the README documents it and the interface opens with a clear disclaimer. A small model pretending to be a doctor is the worst way this could fail.