1
0
Fork 0
mirror of https://github.com/qurator-spk/sbb_ner.git synced 2026-02-26 02:11:55 +01:00
sbb_ner/README.md

284 lines
5.8 KiB
Markdown
Raw Normal View History

2019-08-22 14:48:13 +02:00
![sbb-ner-demo example](.screenshots/sbb_ner_demo.png?raw=true)
2019-08-16 15:22:13 +02:00
2020-01-15 11:15:55 +01:00
How the models have been obtained is described in our [paper](https://corpora.linguistik.uni-erlangen.de/data/konvens/proceedings/papers/KONVENS2019_paper_4.pdf).
2019-08-27 10:53:28 +02:00
***
2019-08-27 10:57:39 +02:00
2019-08-27 10:57:49 +02:00
# Installation:
2019-08-27 10:53:28 +02:00
2025-04-04 15:26:06 +02:00
Recommended python version is 3.11.
Consider use of [pyenv](https://github.com/pyenv/pyenv) if that python version is not available on your system.
2019-08-27 10:53:28 +02:00
2025-04-04 15:26:06 +02:00
Activate virtual environment (virtualenv):
2019-08-27 10:53:28 +02:00
```
source venv/bin/activate
```
2025-04-04 15:26:06 +02:00
or (pyenv):
```
pyenv activate my-python-3.11-virtualenv
```
2019-08-27 10:53:28 +02:00
2025-04-04 15:26:06 +02:00
Update pip:
2019-08-27 10:53:28 +02:00
```
pip install -U pip
```
2025-04-09 08:38:26 +02:00
Install sbb_ner:
2019-08-27 10:53:28 +02:00
```
2025-04-04 15:26:06 +02:00
pip install git+https://github.com/qurator-spk/sbb_ner.git
2019-08-27 10:53:28 +02:00
```
Download required models: https://zenodo.org/records/18634575.
2019-08-27 10:53:28 +02:00
Extract model archive:
```
tar -xzf models.tar.gz
```
2025-04-04 15:32:04 +02:00
Copy [config file](qurator/sbb_ner/webapp/config.json) into working directory.
Set USE_CUDA environment variable to True/False depending on GPU availability.
2019-08-27 10:53:28 +02:00
Run webapp directly:
```
2025-04-04 15:32:04 +02:00
env CONFIG=config.json env FLASK_APP=qurator/sbb_ner/webapp/app.py env FLASK_ENV=development env USE_CUDA=True/False flask run --host=0.0.0.0
2019-08-27 10:53:28 +02:00
```
For production purposes rather use
```
2025-04-04 15:32:04 +02:00
env CONFIG=config.json env USE_CUDA=True/False gunicorn --bind 0.0.0.0:5000 qurator.sbb_ner.webapp.wsgi:app
```
2019-08-27 10:53:28 +02:00
# Docker
## CPU-only:
```
docker build --build-arg http_proxy=$http_proxy -t qurator/webapp-ner-cpu -f Dockerfile.cpu .
```
```
docker run -ti --rm=true --mount type=bind,source=data/konvens2019,target=/usr/src/qurator-sbb-ner/data/konvens2019 -p 5000:5000 qurator/webapp-ner-cpu
```
## GPU:
Make sure that your GPU is correctly set up and that nvidia-docker has been installed.
```
docker build --build-arg http_proxy=$http_proxy -t qurator/webapp-ner-gpu -f Dockerfile .
```
```
docker run -ti --rm=true --mount type=bind,source=data/konvens2019,target=/usr/src/qurator-sbb-ner/data/konvens2019 -p 5000:5000 qurator/webapp-ner-gpu
```
NER web-interface is availabe at http://localhost:5000 .
# REST - Interface
Get available models:
```
curl http://localhost:5000/models
```
Output:
```
[
{
"default": true,
"id": 1,
"model_dir": "data/konvens2019/build-wd_0.03/bert-all-german-de-finetuned",
"name": "DC-SBB + CONLL + GERMEVAL"
},
{
"default": false,
"id": 2,
"model_dir": "data/konvens2019/build-on-all-german-de-finetuned/bert-sbb-de-finetuned",
"name": "DC-SBB + CONLL + GERMEVAL + SBB"
},
{
"default": false,
"id": 3,
"model_dir": "data/konvens2019/build-wd_0.03/bert-sbb-de-finetuned",
"name": "DC-SBB + SBB"
},
{
"default": false,
"id": 4,
"model_dir": "data/konvens2019/build-wd_0.03/bert-all-german-baseline",
"name": "CONLL + GERMEVAL"
}
]
```
Perform NER using model 1:
```
curl -d '{ "text": "Paris Hilton wohnt im Hilton Paris in Paris." }' -H "Content-Type: application/json" http://localhost:5000/ner/1
```
Output:
```
[
[
{
"prediction": "B-PER",
"word": "Paris"
},
{
"prediction": "I-PER",
"word": "Hilton"
},
{
"prediction": "O",
"word": "wohnt"
},
{
"prediction": "O",
"word": "im"
},
{
"prediction": "B-ORG",
"word": "Hilton"
},
{
"prediction": "I-ORG",
"word": "Paris"
},
{
"prediction": "O",
"word": "in"
},
{
"prediction": "B-LOC",
"word": "Paris"
},
{
"prediction": "O",
"word": "."
}
]
]
```
2020-08-18 11:49:16 +02:00
The JSON above is the expected input format of the
[SBB named entity linking and disambiguation system](https://github.com/qurator-spk/sbb_ned).
2019-08-27 10:53:28 +02:00
# Model-Training
2019-08-16 15:22:13 +02:00
***
2019-08-27 10:53:28 +02:00
## Preprocessing of NER ground-truth:
2019-08-16 15:22:13 +02:00
2019-08-27 10:53:28 +02:00
### compile_conll
2019-08-16 15:22:13 +02:00
Read CONLL 2003 ner ground truth files from directory and
write the outcome of the data parsing to some pandas DataFrame that is
stored as pickle.
2019-08-27 10:53:28 +02:00
#### Usage
2019-08-16 15:22:13 +02:00
```
compile_conll --help
```
2019-08-27 10:53:28 +02:00
### compile_germ_eval
2019-08-16 15:22:13 +02:00
Read germ eval .tsv files from directory and write the
outcome of the data parsing to some pandas DataFrame that is stored as
pickle.
2019-08-27 10:53:28 +02:00
#### Usage
2019-08-16 15:22:13 +02:00
```
compile_germ_eval --help
```
2019-08-27 10:53:28 +02:00
### compile_europeana_historic
2019-08-16 15:22:13 +02:00
Read europeana historic ner ground truth .bio files from directory
and write the outcome of the data parsing to some pandas
DataFrame that is stored as pickle.
2019-08-27 10:53:28 +02:00
#### Usage
2019-08-16 15:22:13 +02:00
```
compile_europeana_historic --help
```
2019-08-27 10:53:28 +02:00
### compile_wikiner
2019-08-16 15:22:13 +02:00
Read wikiner files from directory and write the outcome
of the data parsing to some pandas DataFrame that is stored as pickle.
2019-08-27 10:53:28 +02:00
#### Usage
2019-08-16 15:22:13 +02:00
```
compile_wikiner --help
```
***
2019-08-27 10:53:28 +02:00
## Train BERT - NER model:
2019-08-16 15:22:13 +02:00
2019-08-27 10:53:28 +02:00
### bert-ner
2019-08-16 15:22:13 +02:00
Perform BERT for NER supervised training and test/cross-validation.
2019-08-27 10:53:28 +02:00
#### Usage
2019-08-16 15:22:13 +02:00
```
bert-ner --help
```
2022-02-21 16:27:17 +01:00
2022-02-21 16:40:16 +01:00
## BERT-Pre-training:
### collectcorpus
```
collectcorpus --help
Usage: collectcorpus [OPTIONS] FULLTEXT_FILE SELECTION_FILE CORPUS_FILE
Reads the fulltext from a CSV or SQLITE3 file (see also altotool) and
write it to one big text file.
FULLTEXT_FILE: The CSV or SQLITE3 file to read from.
SELECTION_FILE: Consider only a subset of all pages that is defined by the
DataFrame that is stored in <selection_file>.
CORPUS_FILE: The output file that can be used by bert-pregenerate-trainingdata.
Options:
--chunksize INTEGER Process the corpus in chunks of <chunksize>.
default:10**4
--processes INTEGER Number of parallel processes. default: 6
--min-line-len INTEGER Lower bound of line length in output file.
default:80
--help Show this message and exit.
```
2022-02-21 16:27:17 +01:00
### bert-pregenerate-trainingdata
Generate data for BERT pre-training from a corpus text file where
the documents are separated by an empty line (output of corpuscollect).
#### Usage
```
bert-pregenerate-trainingdata --help
```
### bert-finetune
Perform BERT pre-training on pre-generated data.
#### Usage
```
bert-finetune --help
```