Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,10 @@
|
|
1 |
-
from typing import Optional
|
2 |
import spacy
|
3 |
from spacy import displacy
|
4 |
-
from spacy.language import Language
|
5 |
import streamlit as st
|
6 |
-
from spacy_streamlit import visualize_parser
|
7 |
-
from spacy_streamlit import visualize_tokens
|
8 |
-
from spacy_streamlit import visualize_ner
|
9 |
import base64
|
10 |
from PIL import Image
|
11 |
-
#import pandas
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
|
16 |
st.set_page_config(layout="wide")
|
17 |
|
@@ -19,7 +12,7 @@ st.image("logo.png", use_column_width=False, width=150)
|
|
19 |
|
20 |
st.title("Ancient Greek Syntax and Named Entities")
|
21 |
|
22 |
-
st.markdown("Welcome to our analyzer. Here you can parse the parts of speech (POS) and the syntactic relationships of any ancient Greek sentence. This analysis is done by our language models trained with transformers and the NLP library spaCy.
|
23 |
|
24 |
st.header("Select a model:")
|
25 |
spacy_model = st.selectbox("Model", ["grc_proiel_lg","grc_proiel_trf","grc_proiel_sm","grc_perseus_lg","grc_perseus_trf","grc_perseus_sm","grc_ner_trf"])
|
@@ -27,21 +20,19 @@ spacy_model = st.selectbox("Model", ["grc_proiel_lg","grc_proiel_trf","grc_proie
|
|
27 |
st.header("Enter text:")
|
28 |
text = st.text_area("Greek text","ἐπὶ τοῦτον δὴ τὸν Ἄμασιν Καμβύσης ὁ Κύρου ἐστρατεύετο, ἄγων καί ἄλλους τῶν ἦρχε καὶ Ἑλλήνων Ἴωνάς τε καὶ Αἰολέας.")
|
29 |
|
30 |
-
|
31 |
-
#config = {"punct_chars": [".", ";", "·"]}
|
32 |
-
|
33 |
nlp = spacy.load(spacy_model)
|
34 |
-
#nlp.add_pipe("sentencizer", config=config, before="parser")
|
35 |
-
|
36 |
|
37 |
-
#
|
|
|
|
|
|
|
|
|
38 |
|
39 |
doc = nlp(text)
|
40 |
|
41 |
def get_html(html: str):
|
42 |
"""Convert HTML so it can be rendered."""
|
43 |
WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
|
44 |
-
# Newlines seem to mess with the rendering
|
45 |
html = html.replace("\n", " ")
|
46 |
return WRAPPER.format(html)
|
47 |
|
@@ -66,23 +57,18 @@ def visualize_parser(
|
|
66 |
)
|
67 |
options = {
|
68 |
"collapse_punct": cols[1].checkbox(
|
69 |
-
"Collapse punct", value=True, key=f"{key}_parser_collapse_punct"
|
70 |
),
|
71 |
"compact": cols[3].checkbox("Compact mode", value=True, key=f"{key}_parser_compact"),
|
72 |
}
|
73 |
docs = [span.as_doc() for span in doc.sents] if split_sents else [doc]
|
74 |
for sent in docs:
|
75 |
html = displacy.render(sent, options=options, style="dep")
|
76 |
-
# Double newlines seem to mess with the rendering
|
77 |
html = html.replace("\n\n", "\n")
|
78 |
if split_sents and len(docs) > 1:
|
79 |
st.markdown(f"> {sent.text}")
|
80 |
st.write(get_svg(html), unsafe_allow_html=True)
|
81 |
|
82 |
-
|
83 |
-
#displacy.render(doc, style="ent")
|
84 |
-
|
85 |
-
|
86 |
visualize_parser(doc)
|
87 |
|
88 |
visualize_ner(
|
@@ -92,9 +78,4 @@ visualize_ner(
|
|
92 |
title="Persons, locations, groups, gods, and languages",
|
93 |
)
|
94 |
|
95 |
-
#pd.set_option('display.max_colwidth', None)
|
96 |
-
|
97 |
-
|
98 |
visualize_tokens(doc, attrs=["text", "lemma_", "pos_", "dep_","ent_type_"], title="Table view:", key="tokens")
|
99 |
-
|
100 |
-
|
|
|
1 |
+
from typing import Optional
|
2 |
import spacy
|
3 |
from spacy import displacy
|
|
|
4 |
import streamlit as st
|
5 |
+
from spacy_streamlit import visualize_parser, visualize_tokens, visualize_ner
|
|
|
|
|
6 |
import base64
|
7 |
from PIL import Image
|
|
|
|
|
|
|
|
|
8 |
|
9 |
st.set_page_config(layout="wide")
|
10 |
|
|
|
12 |
|
13 |
st.title("Ancient Greek Syntax and Named Entities")
|
14 |
|
15 |
+
st.markdown("Welcome to our analyzer. Here you can parse the parts of speech (POS) and the syntactic relationships of any ancient Greek sentence. This analysis is done by our language models trained with transformers and the NLP library spaCy. Below, you can choose which model do you want to use (each model may produce a different analysis). Documentation about the linguistic terms used by our models to annotate your sentences can be found here. If you have any questions, please contact us at [email protected]")
|
16 |
|
17 |
st.header("Select a model:")
|
18 |
spacy_model = st.selectbox("Model", ["grc_proiel_lg","grc_proiel_trf","grc_proiel_sm","grc_perseus_lg","grc_perseus_trf","grc_perseus_sm","grc_ner_trf"])
|
|
|
20 |
st.header("Enter text:")
|
21 |
text = st.text_area("Greek text","ἐπὶ τοῦτον δὴ τὸν Ἄμασιν Καμβύσης ὁ Κύρου ἐστρατεύετο, ἄγων καί ἄλλους τῶν ἦρχε καὶ Ἑλλήνων Ἴωνάς τε καὶ Αἰολέας.")
|
22 |
|
|
|
|
|
|
|
23 |
nlp = spacy.load(spacy_model)
|
|
|
|
|
24 |
|
25 |
+
# Add the NER pipeline from grc_ner_trf if the selected model is grc_proiel_trf or grc_perseus_trf
|
26 |
+
if spacy_model in ["grc_proiel_trf", "grc_perseus_trf"]:
|
27 |
+
ner = spacy.load("grc_ner_trf")
|
28 |
+
for pipe_name, pipe in ner.pipeline:
|
29 |
+
nlp.add_pipe(pipe, name=pipe_name, source=ner)
|
30 |
|
31 |
doc = nlp(text)
|
32 |
|
33 |
def get_html(html: str):
|
34 |
"""Convert HTML so it can be rendered."""
|
35 |
WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
|
|
|
36 |
html = html.replace("\n", " ")
|
37 |
return WRAPPER.format(html)
|
38 |
|
|
|
57 |
)
|
58 |
options = {
|
59 |
"collapse_punct": cols[1].checkbox(
|
60 |
+
"Collapse punct", value=True, key=f"{key}_parser_collapse_punct"
|
61 |
),
|
62 |
"compact": cols[3].checkbox("Compact mode", value=True, key=f"{key}_parser_compact"),
|
63 |
}
|
64 |
docs = [span.as_doc() for span in doc.sents] if split_sents else [doc]
|
65 |
for sent in docs:
|
66 |
html = displacy.render(sent, options=options, style="dep")
|
|
|
67 |
html = html.replace("\n\n", "\n")
|
68 |
if split_sents and len(docs) > 1:
|
69 |
st.markdown(f"> {sent.text}")
|
70 |
st.write(get_svg(html), unsafe_allow_html=True)
|
71 |
|
|
|
|
|
|
|
|
|
72 |
visualize_parser(doc)
|
73 |
|
74 |
visualize_ner(
|
|
|
78 |
title="Persons, locations, groups, gods, and languages",
|
79 |
)
|
80 |
|
|
|
|
|
|
|
81 |
visualize_tokens(doc, attrs=["text", "lemma_", "pos_", "dep_","ent_type_"], title="Table view:", key="tokens")
|
|
|
|