adorkin's picture
Restore example caching
2ab647f
from typing import Dict, List, Union
import gradio as gr
from lexenlem.preprocessing.adhoc import AdHocLemmatizer
title = "Lexicon-enhanced lemmatization for Estonian"
with open("./article.md") as file:
article: str = file.read()
with open("./description.txt") as file:
description: str = file.read()
models: Dict[str, AdHocLemmatizer] = {
"Base": AdHocLemmatizer(path="vb_stanza_no_compound_no_deriv.pt", use_stanza=True),
"Compound separators": AdHocLemmatizer(
path="vb_stanza_compound.pt", use_stanza=True, allow_compound_separator=True, allow_derivation_sign=False
),
"UD format": AdHocLemmatizer(
path="vb_stanza_symbols.pt", use_stanza=True, allow_compound_separator=True, allow_derivation_sign=True
),
}
choices = list(models.keys())
examples: List[List[Union[str, bool]]] = []
with open("examples.tsv") as file:
for line in file:
ex, index = line.split("\t")
index = int(index)
examples.append(
[ex, choices[index]]
)
def predict(text: str, key: str) -> List[str]:
if key not in models:
raise RuntimeError("Unknown model")
return models[key](text)
demo = gr.Interface(
fn=predict,
title=title,
description=description,
article=article,
inputs=[
gr.inputs.Textbox(lines=7, label="Text to lemmatize"),
gr.inputs.Radio(choices=choices, label="Mode")
],
outputs=[
gr.outputs.Textbox(label="Lemmas")
],
examples=examples,
allow_screenshot=False,
allow_flagging="never",
)
demo.launch(
debug=False,
enable_queue=True,
cache_examples=True,
)