Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,45 @@ the matrix of matches between all items), and `query_similarity` (the degree of
}
```

### Suggesting a topic for each item

`POST /text/topics` suggests a topic for each questionnaire item, from a fixed list of 22 common topics (depression, anxiety, sleep, ADHD, trauma, ...). `GET /text/topics` lists them.

The topics returned by `/text/match` work at the questionnaire level: each item takes the tags of its nearest item in the Mental Health Catalogue, and the catalogue tags whole questionnaires, so every item of a questionnaire with several subscales gets the same topics. `/text/topics` labels each item on its own, by comparing it with about 5,600 reference items that were labelled one by one. No language model runs at request time: a request costs one embedding per item with the requested model, the same as matching. The reference items are embedded once per model and kept in memory: for the default model (MiniLM) in the background when the API starts, which took about 8 seconds on a 12-core server CPU, and for any other model on its first request.

```
curl -X 'POST' \
'https://api.harmonydata.ac.uk/text/topics' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"texts": ["Restless, overactive, cannot stay still for long", "Many worries, often seems worried"]}'
```

```
{
"items": [
{
"text": "Restless, overactive, cannot stay still for long",
"topic": "adhd",
"name": "ADHD, attention and concentration",
"construct": "hyperactivity_impulsivity",
"agreement": 0.9,
"auto": true,
"suggestions": [
{"topic": "adhd", "name": "ADHD, attention and concentration", "share": 0.9},
{"topic": "anxiety", "name": "Anxiety", "share": 0.1},
{"topic": "mania", "name": "Mania and bipolar", "share": 0.0}
]
},
...
]
}
```

A label marked `auto` can be applied without asking; for the rest, show the suggestions and let the user choose. On 520 items from 32 families of standard questionnaires, with the default MiniLM model, the best topic was right for 78% of items and the right topic was among the three suggestions for 95%. 52% of items were marked `auto`, and 92% of those were right. For questionnaires that are not in the Mental Health Catalogue, expect about 70% right and 89% among the suggestions. The test items' labels have not yet been reviewed by a researcher, and the topic list is a draft: both need review by someone on the research side.

The topic list and reference items are in `harmony_api/resources/topics/`. The reference items are Mental Health Catalogue items, each labelled on its own by a language model, plus example items a language model wrote for the thinner topics.


# License

Expand Down Expand Up @@ -538,6 +577,8 @@ URL: https://fastdatascience.com
- 3.2 POST /text/match
- 3.3 POST /text/examples
- 3.4 GET /text/cache
- 3.5 GET /text/topics
- 3.6 POST /text/topics

## API

Expand Down Expand Up @@ -963,6 +1004,97 @@ Array of object:
}
```

## 3.5 GET /text/topics

List Topics
The topics that POST /text/topics labels items with

REQUEST

```
No request parameters
```

RESPONSE

```
STATUS CODE - 200: Successful Response
```

```
RESPONSE MODEL - application/json
{
topics* [{
Array of object:
topic* string Topic id, e.g. sleep
name* string Display name, e.g. Sleep
description* string What the topic covers
reference_items* integer Reference items with this topic
}]
reference_items* integer Reference items in all
ready_models* [string] Models whose reference vectors are already computed
}
```

## 3.6 POST /text/topics

Label Topics
Suggest a topic for each questionnaire item

REQUEST

```
REQUEST BODY SCHEMA - application/json
{
texts* [string] Questionnaire items to label (up to 2000)
parameters {
Model to embed the items with, as in /text/match
framework string DEFAULT:huggingface
model string DEFAULT:sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2
}
suggestions integer DEFAULT:3 How many distinct topics to suggest per item (1 to 10)
auto_threshold number DEFAULT:0.8 Mark the label "auto" when at least this share of the 10 nearest reference items agree
neighbours integer DEFAULT:0 Return this many nearest reference items per item (0 to 25)
}
```

RESPONSE

```
STATUS CODE - 200: Successful Response
```

```
RESPONSE MODEL - application/json
{
items* [{
Array of object, one per text, in the order sent:
text* string The item, as sent
topic* string Best topic id
name* string Display name of the best topic
construct string Finer construct under the best topic, e.g. worry (less reliable than the topic)
agreement* number Share of the 10 nearest reference items that have the best topic
auto* boolean agreement >= auto_threshold
suggestions* [{
topic* string
name* string
share* number Share of the 10 nearest reference items with this topic
}]
neighbours [{
Only when asked for:
text* string A reference item
topic* string
construct string
similarity* number Cosine similarity to the item
}]
}]
}
```

```
STATUS CODE - 422: Validation Error, or a model that does not exist or is not available
```




Expand Down
10 changes: 10 additions & 0 deletions harmony_api/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ def model_from_match_body_is_available(match_body: MatchBody) -> bool:
return True


def check_model_is_available(model_dict: dict) -> bool:
"""
Check model availability, for endpoints whose body is not a MatchBody.
"""

__check_model(model_dict)

return True


def __check_model(model_dict: dict):
if model_dict not in constants.ALL_HARMONY_API_MODELS:
raise http_exceptions.CouldNotProcessRequestHTTPException(
Expand Down
Loading
Loading