Spaces:
Running
Running
first commit
Browse files
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: streamlit
|
7 |
app_file: app.py
|
8 |
pinned: false
|
|
|
1 |
---
|
2 |
+
title: Ancient_Greek_Spacy_Models
|
3 |
+
emoji: 🐨
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: green
|
6 |
sdk: streamlit
|
7 |
app_file: app.py
|
8 |
pinned: false
|
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, Sequence, Tuple, Optional, Dict, Union, Callable
|
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 |
+
import base64
|
8 |
+
from PIL import Image
|
9 |
+
import deplacy
|
10 |
+
import graphviz
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
st.set_page_config(layout="wide")
|
17 |
+
|
18 |
+
st.title("Ancient Greek Analyzer")
|
19 |
+
|
20 |
+
st.markdown("Here you'll find four spaCy models for processing ancient Greek. They have been trained with the Universal Dependencies datasets *Perseus* and *Proiel*. We provide two types of models for each dataset. The '_lg' models were built with tok2vec pretrained embeddings and fasttext vectors, while the '_tfr' models have a transfomers layer. You can choose among models to compare their performance. More information about the models can be found in the [Huggingface Models Hub] (https://huggingface.co/Jacobo).")
|
21 |
+
|
22 |
+
st.sidebar.image("logo.png", use_column_width=False, width=150, caption="\n provided by Diogenet")
|
23 |
+
|
24 |
+
st.sidebar.title("Choose model:")
|
25 |
+
spacy_model = st.sidebar.selectbox("", ["grc_ud_perseus_lg", "grc_ud_proiel_lg"])
|
26 |
+
|
27 |
+
st.header("Text to analyze:")
|
28 |
+
text = st.text_area("", "Πλάτων ὁ Περικτιόνης τὸ γένος ἀνέφερεν εἰς Σόλωνα.")
|
29 |
+
|
30 |
+
|
31 |
+
nlp = spacy.load(spacy_model)
|
32 |
+
doc = nlp(text)
|
33 |
+
|
34 |
+
def get_html(html: str):
|
35 |
+
"""Convert HTML so it can be rendered."""
|
36 |
+
WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
|
37 |
+
# Newlines seem to mess with the rendering
|
38 |
+
html = html.replace("\n", " ")
|
39 |
+
return WRAPPER.format(html)
|
40 |
+
|
41 |
+
def get_svg(svg: str, style: str = "", wrap: bool = True):
|
42 |
+
"""Convert an SVG to a base64-encoded image."""
|
43 |
+
b64 = base64.b64encode(svg.encode("utf-8")).decode("utf-8")
|
44 |
+
html = f'<img src="data:image/svg+xml;base64,{b64}" style="{style}"/>'
|
45 |
+
return get_html(html) if wrap else html
|
46 |
+
|
47 |
+
def visualize_parser(
|
48 |
+
doc: spacy.tokens.Doc,
|
49 |
+
*,
|
50 |
+
title: Optional[str] = "Dependency parse & part of speech",
|
51 |
+
key: Optional[str] = None,
|
52 |
+
) -> None:
|
53 |
+
"""Visualizer for dependency parses."""
|
54 |
+
if title:
|
55 |
+
st.header(title)
|
56 |
+
cols = st.columns(4)
|
57 |
+
split_sents = cols[0].checkbox(
|
58 |
+
"Split sentences", value=True, key=f"{key}_parser_split_sents"
|
59 |
+
)
|
60 |
+
options = {
|
61 |
+
"collapse_punct": cols[1].checkbox(
|
62 |
+
"Collapse punct", value=True, key=f"{key}_parser_collapse_punct"
|
63 |
+
),
|
64 |
+
"compact": cols[3].checkbox("Compact mode", value=True, key=f"{key}_parser_compact"),
|
65 |
+
}
|
66 |
+
docs = [span.as_doc() for span in doc.sents] if split_sents else [doc]
|
67 |
+
for sent in docs:
|
68 |
+
html = displacy.render(sent, options=options, style="dep")
|
69 |
+
# Double newlines seem to mess with the rendering
|
70 |
+
html = html.replace("\n\n", "\n")
|
71 |
+
if split_sents and len(docs) > 1:
|
72 |
+
st.markdown(f"> {sent.text}")
|
73 |
+
st.write(get_svg(html), unsafe_allow_html=True)
|
74 |
+
|
75 |
+
|
76 |
+
visualize_parser(doc)
|
77 |
+
|
78 |
+
#graph_r = deplacy.render(doc)
|
79 |
+
|
80 |
+
#st.graphviz_chart(graph_r)
|
81 |
+
|
82 |
+
|
83 |
+
graph_dot = deplacy.dot(doc)
|
84 |
+
|
85 |
+
#graphviz.Source(deplacy.dot(doc))
|
86 |
+
|
87 |
+
st.graphviz_chart(graph_dot)
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
#st.sidebar.title("Model 2")
|
94 |
+
#spacy_model2 = st.sidebar.selectbox("Model 2", ["grc_ud_perseus_lg", "grc_ud_proiel_lg"])
|
95 |
+
|
96 |
+
#st.header("Text to analyze:")
|
97 |
+
#text = st.text_area("", "Πλάτων ὁ Περικτιόνης τὸ γένος ἀνέφερεν εἰς Σόλωνα.")
|
98 |
+
|
99 |
+
|
100 |
+
#nlp = spacy.load(spacy_model2)
|
101 |
+
#doc2 = nlp(text)
|
102 |
+
|
103 |
+
#visualize_parser(doc2)
|
104 |
+
|
105 |
+
#visualizers = ["pos", "dep"]
|
106 |
+
#spacy_streamlit.visualize(models, default_text,visualizers)
|
logo.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
spacy==3.1.4
|
2 |
+
spacy-streamlit==1.0.2
|
3 |
+
watchdog==2.1.6
|
4 |
+
deplacy
|
5 |
+
graphviz
|
6 |
+
https://huggingface.co/Jacobo/grc_ud_perseus_lg/resolve/main/grc_ud_perseus_lg-any-py3-none-any.whl
|
7 |
+
https://huggingface.co/Jacobo/grc_ud_perseus_trf/resolve/main/grc_ud_perseus_trf-any-py3-none-any.whl
|
8 |
+
|
9 |
+
|
10 |
+
|