first commit
Browse files- app.py +126 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from nltk.tokenize.treebank import TreebankWordDetokenizer
|
| 3 |
+
from somajo import SoMaJo
|
| 4 |
+
|
| 5 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM, AutoModelForSeq2SeqLM
|
| 6 |
+
from datasets import Dataset
|
| 7 |
+
from transformers.pipelines.pt_utils import KeyDataset
|
| 8 |
+
from hybrid_textnorm.lexicon import Lexicon
|
| 9 |
+
from hybrid_textnorm.normalization import predict_type_normalization, reranked_normalization, prior_normalization
|
| 10 |
+
from hybrid_textnorm.preprocess import recombine_tokens, german_transliterate
|
| 11 |
+
|
| 12 |
+
text_tokenizer = SoMaJo("de_CMC", split_camel_case=True)
|
| 13 |
+
lexicon_dataset_name = 'aehrm/dtaec-lexicon'
|
| 14 |
+
train_lexicon = Lexicon.from_dataset(lexicon_dataset_name, split='train')
|
| 15 |
+
|
| 16 |
+
def predict(input_str, model_name, progress=gr.Progress()):
|
| 17 |
+
tokenized_sentences = list(text_tokenizer.tokenize_text([input_str]))
|
| 18 |
+
|
| 19 |
+
if model_name == 'type normalizer':
|
| 20 |
+
output_sentences = predict_only_type_transformer(tokenized_sentences, progress)
|
| 21 |
+
elif model_name == 'type normalizer + lm':
|
| 22 |
+
output_sentences = predict_type_transformer_with_lm(tokenized_sentences, progress)
|
| 23 |
+
elif model_name == 'transnormer':
|
| 24 |
+
output_sentences = predict_transnormer(tokenized_sentences, progress)
|
| 25 |
+
|
| 26 |
+
if type(output_sentences[0]) == list:
|
| 27 |
+
detok = TreebankWordDetokenizer()
|
| 28 |
+
return "<br>".join([detok.detokenize(recombine_tokens(sent)) for sent in output_sentences])
|
| 29 |
+
else:
|
| 30 |
+
return "<br>".join(output_sentences)
|
| 31 |
+
|
| 32 |
+
def predict_transnormer(tokenized_sentences, progress):
|
| 33 |
+
model_name = 'ybracke/transnormer-19c-beta-v02'
|
| 34 |
+
|
| 35 |
+
progress(0, desc='running normalization')
|
| 36 |
+
pipe = pipeline(model='ybracke/transnormer-19c-beta-v02')
|
| 37 |
+
|
| 38 |
+
raw_sentences = []
|
| 39 |
+
for tokenized_sent in tokenized_sentences:
|
| 40 |
+
raw_sentences.append(''.join(tok.text + (' ' if tok.space_after else '') for tok in tokenized_sent))
|
| 41 |
+
|
| 42 |
+
progress(0, desc='running normalization')
|
| 43 |
+
ds = KeyDataset(Dataset.from_dict(dict(types=list(raw_sentences))), "types")
|
| 44 |
+
|
| 45 |
+
output_sentences = []
|
| 46 |
+
for out_sentence in progress.tqdm(pipe(ds, num_beams=4, max_length=1000)):
|
| 47 |
+
output_sentences.append(out_sentence[0]['generated_text'])
|
| 48 |
+
|
| 49 |
+
return output_sentences
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def predict_only_type_transformer(tokenized_sentences, progress):
|
| 54 |
+
type_model_name = 'aehrm/dtaec-type-normalizer'
|
| 55 |
+
progress(0, desc='loading model')
|
| 56 |
+
|
| 57 |
+
pipe = pipeline('text2text-generation', type_model_name)
|
| 58 |
+
|
| 59 |
+
transliterated_sentences = []
|
| 60 |
+
for sentence in tokenized_sentences:
|
| 61 |
+
transliterated_sentences.append([german_transliterate(tok.text) for tok in sentence])
|
| 62 |
+
|
| 63 |
+
oov_types = set(tok for sent in transliterated_sentences for tok in sent) - train_lexicon.keys()
|
| 64 |
+
oov_normalizations = {}
|
| 65 |
+
|
| 66 |
+
progress(0, desc='running normalization')
|
| 67 |
+
ds = KeyDataset(Dataset.from_dict(dict(types=list(oov_types))), "types")
|
| 68 |
+
for in_type, out in zip(ds, progress.tqdm(pipe(ds))):
|
| 69 |
+
oov_normalizations[in_type] = out[0]['generated_text']
|
| 70 |
+
|
| 71 |
+
output_sentences = []
|
| 72 |
+
for sent in transliterated_sentences:
|
| 73 |
+
output_sent = []
|
| 74 |
+
for t in sent:
|
| 75 |
+
if t in train_lexicon.keys():
|
| 76 |
+
output_sent.append(train_lexicon[t].most_common(1)[0][0])
|
| 77 |
+
elif t in oov_normalizations.keys():
|
| 78 |
+
output_sent.append(oov_normalizations[t])
|
| 79 |
+
else:
|
| 80 |
+
raise ValueError()
|
| 81 |
+
|
| 82 |
+
output_sentences.append(output_sent)
|
| 83 |
+
|
| 84 |
+
return output_sentences
|
| 85 |
+
|
| 86 |
+
def predict_type_transformer_with_lm(tokenized_sentences, progress):
|
| 87 |
+
type_model_name = 'aehrm/dtaec-type-normalizer'
|
| 88 |
+
language_model_name = 'dbmdz/german-gpt2'
|
| 89 |
+
|
| 90 |
+
progress(0, desc='loading model')
|
| 91 |
+
type_model_tokenizer = AutoTokenizer.from_pretrained(type_model_name)
|
| 92 |
+
type_model = AutoModelForSeq2SeqLM.from_pretrained(type_model_name)
|
| 93 |
+
language_model_tokenizer = AutoTokenizer.from_pretrained(language_model_name)
|
| 94 |
+
language_model = AutoModelForCausalLM.from_pretrained(language_model_name)
|
| 95 |
+
if 'pad_token' not in language_model_tokenizer.special_tokens_map:
|
| 96 |
+
language_model_tokenizer.add_special_tokens({'pad_token': '<pad>'})
|
| 97 |
+
|
| 98 |
+
transliterated_sentences = []
|
| 99 |
+
for sentence in tokenized_sentences:
|
| 100 |
+
transliterated_sentences.append([german_transliterate(tok.text) for tok in sentence])
|
| 101 |
+
|
| 102 |
+
oov_types = set(tok for sent in transliterated_sentences for tok in sent) - train_lexicon.keys()
|
| 103 |
+
oov_replacement_probabilities = {}
|
| 104 |
+
|
| 105 |
+
progress(0, desc='running normalization')
|
| 106 |
+
for input_type, probas in progress.tqdm(predict_type_normalization(oov_types, type_model_tokenizer, type_model, batch_size=8), total=len(oov_types)):
|
| 107 |
+
oov_replacement_probabilities[input_type] = probas
|
| 108 |
+
|
| 109 |
+
output_sentences = []
|
| 110 |
+
for hist_sent in progress.tqdm(transliterated_sentences):
|
| 111 |
+
predictions = reranked_normalization(hist_sent, train_lexicon, oov_replacement_probabilities, language_model_tokenizer, language_model, batch_size=1)
|
| 112 |
+
best_pred, _, _, _ = predictions[0]
|
| 113 |
+
output_sentences.append(best_pred)
|
| 114 |
+
|
| 115 |
+
return output_sentences
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
gradio_app = gr.Interface(
|
| 119 |
+
predict,
|
| 120 |
+
inputs=[gr.Textbox(value="Die Königinn ſaß auf des Pallaſtes mittlerer Tribune."), gr.Dropdown([('aehrm/dtaec-type-normalizer (FAST)', 'type normalizer'), ('aehrm/dtaec-type-normalizer + dbmdz/german-gpt2 (Fast)', 'type normalizer + lm'), ('ybracke/transnormer-19c-beta-v02 (fast)', 'transnormer')])],
|
| 121 |
+
outputs=gr.HTML(),
|
| 122 |
+
title="German Historical Text Normalization",
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
if __name__ == "__main__":
|
| 126 |
+
gradio_app.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
nltk
|
| 2 |
+
somajo
|
| 3 |
+
hybrid-textnorm @ git+https://github.com/aehrm/hybrid_textnorm@8619fd8961caac5d5f961df0e689f6a9ad3948cd
|