Harmony API
You can also join our Discord server!
Who to contact?
You can contact Harmony team at https://harmonydata.ac.uk/, or Thomas Wood at http://fastdatascience.com/.
How to contribute
Read our guide to contributing to Harmony here.
You can raise an issue in the issue tracker, and you can open
a pull request.
Please contact us at https://harmonydata.ac.uk/contact or write to thomas@fastdatascience.com if you would like to be
involved in the project.
Looking for the Harmony Python library?
Please visit https://github.com/harmonydata/harmony
Looking for the original (Plotly Dash-based) Harmony?
Please visit https://github.com/harmonydata/harmony_original
About Harmony
Harmony is a data harmonisation project that uses Natural Language Processing to help researchers make better use of
existing data from different studies by supporting them with the harmonisation of various measures and items used in
different studies. Harmony is a collaboration project between
the University of Ulster, University College London,
the Universidade Federal de Santa Maria in Brazil,
and Fast Data Science Ltd in London.
You can read more at https://harmonydata.ac.uk.
There is a live demo at: https://harmonydata.ac.uk/app

How does Harmony work in layman's terms?
Harmony compares questions from different instruments by converting them to a vector representation and calculating
their similarity. You can read more at https://harmonydata.ac.uk/how-does-harmony-work/
Do you want to run Harmony in your browser locally from a pre-built Docker container?
Download and install Docker:
Open a Terminal and run
docker run -p 8000:8000 -p 3000:3000 harmonydata/harmonylocal
Then go to http://localhost:3000 in your browser.
Docker images
If you are a Docker user, you can run Harmony from a pre-built Docker image.
Getting started: running and developing the API on your computer using Docker
A prerequisite is Tika, which is a PDF parsing library. This must run as a server in Java. We use the Tika Python
bindings.
First, clone the API and make sure to clone with --recurse-submodules.
git clone --recurse-submodules git@github.com:harmonydata/harmonyapi.git
The Harmony API includes the harmony repo as a submodule.
Troubleshooting the submodules after git clone
After you have cloned the repository, if the folder inside called harmony is empty, or at any point you get an error like the below, please check you have cloned with --recurse-submodules as below:

git clone --recurse-submodules https://github.com/harmonydata/harmonyapi.git
1. Run Tika
Download and install Java if you don't have it already. Download and install Apache Tika and run it on your
computer https://tika.apache.org/download.html
java -jar tika-server-standard-2.3.0.jar
2. Build Docker container
docker build -t harmonyapi .
3. Run Docker container
Don't forget to expose port 8080:
docker run -p 8080:80 harmonyapi
You should now be able to visit http://0.0.0.0:8080/docs and view the data.
If you want to run the Harmony API container and execute Bash commands inside it, you can run:
docker run -it harmonyapi bash
Architecture of deployed Harmony API server
On-premises deployment
Harmony team only: see details of how Harmony is deployed on-premises here:
https://github.com/harmonydata/harmony_deployment_ulster_private
Alternative Docker Compose deployment
You can deploy Harmony with Docker Compose - see docker_compose.yml.
MHC data
When the app is run, there is an environment variable HARMONY_DATA_PATH which is set to /data on the production
server, and that's where you need to put any data files. But you could set it to anything you like on your local machine
e.g. /home/xxx/data/ and put the files there and it will find them.
These 3 files are the files it looks for in the /data folder, although the app will run without them. It's a cached
version of the Mental Health Catalogue:
mhc_all_metadatas.json
mhc_embeddings.npy
mhc_questions.json
When Harmony is deployed to Azure, there is an Azure blob storage which is mounted under /data.
The data files can be found here: https://github.com/harmonydata/harmony_deployment_ulster_private
Environment variables
There are also other environment variables which tell the API where to look to load the sentence transformer or contact
Tika:
environment:
HARMONY_DATA_PATH: /data
HARMONY_SENTENCE_TRANSFORMER_PATH: /data/paraphrase-multilingual-MiniLM-L12-v2
OPENAI_API_KEY:
GOOGLE_APPLICATION_CREDENTIALS:
AZURE_OPENAI_API_KEY:
AZURE_OPENAI_ENDPOINT:
TIKA_SERVER_ENDPOINT: http://tika:9998
HARMONY_DATA_PATH - This path will be used to store for example the cache files. Defaults to the HOME DIRECTORY.
OPENAI_API_KEY - The OpenAI API key.
GOOGLE_APPLICATION_CREDENTIALS - To make use of Google's Vertex AI, fill in this environment variable.
This should be the content of your service account file, so a JSON object is expected as the value for the
environment. Make sure to give the service account the required Vertex AI role.
AZURE_OPENAI_API_KEY - The Azure OpenAI API key.
AZURE_OPENAI_ENDPOINT - The Azure OpenAI endpoint.
TIKA_SERVER_ENDPOINT - This is the endpoint where Tika is served from.
AZURE_STORAGE_URL - The Azure Blob storage URL. This is required for downloading the
catalogue data.
You can ideally set these environment variables to show Harmony where to look for dependencies and data, but it will
work without it (it will download the sentence transformer from HuggingFace Hub, etc).
The deployed Harmony uses an Azure Function to run spaCy, available in the repository
here: https://github.com/harmonydata/spacyfunctionapp
So to run locally with Docker Compose you can do:
docker compose up
Providing environment variables in .env file
If you are working with external third-party services in the API, you may find it convenient to make an .env file in the base folder of the project. You can connect an IDE such as Pycharm to use this .env file. It will be ignored by .gitignore so you don't need to worry about accidentally committing your credentials to the repo.

Example content of the .env file:
GOOGLE_APPLICATION_CREDENTIALS='{ "type": "service_account", ... }'
AZURE_OPENAI_API_KEY=f46axxxxxxxxxxxxxxxxxxxxxxxxxxxd
AZURE_OPENAI_ENDPOINT=https://xxxxxxxxx.openai.azure.com/
OPENAI_API_TYPE=azure
OPENAI_API_VERSION=2023-12-01-preview
OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxX
When deploying, you can use these environment variables in your Docker run command, e.g.
docker run -d -p 80:80 -p 3000:3000 -e GOOGLE_APPLICATION_CREDENTIALS=xxx -e AZURE_OPENAI_API_KEY=xxxx -e "HARMONY_DATA_PATH=/data" -v /home/thomaswood/data:/data harmonydata/harmonyapi:[DOCKER_TAG_HERE]
Harmony FastAPI API implementation
If you are not running with Docker, you can run the individual components of the Harmony API separately.
Architecture of the Harmony implementation as it runs with FastAPI on an on-premises server (the current setup) or on an alternative e.g. a cloud server such as Azure or EC2:

Getting started with the Harmony Python library
Installing Python library
You can install from PyPI.
pip install harmonydata
You can read the user guide at ./harmony_pypi_package/README.md.
Troubleshooting running Harmony API on port e.g. 8000 on local machine
By default, Harmony API runs on port 8000 (see screenshot below)

If you are having errors running the API on the port it could be
- a different program is already using port 8000
- you are trying to run on a forbidden port e.g. on port 80 which is private and your computer doesn't give permission to do this
In particular on Windows, you may need to give some kind of permission to a Python program to use any port.
Calling the Harmony API
Parsing a raw file into an Instrument
If you want to read in a raw (unstructured) PDF or Excel file, you can do this via a POST request to the REST API. This
will convert the file into an Instrument object in JSON.