Spaces:
Runtime error
Runtime error
Added v1 of srl demo
Browse files- app.py +57 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import transformers as tr
|
| 3 |
+
import spacy as sp
|
| 4 |
+
|
| 5 |
+
@st.cache
|
| 6 |
+
def load_pipeline(name: str):
|
| 7 |
+
return tr.pipeline('token-classification', model=name)
|
| 8 |
+
|
| 9 |
+
pipeline = load_pipeline('Rexhaif/rubert-base-srl-seqlabeling')
|
| 10 |
+
|
| 11 |
+
def convert_to_spacy(text, result):
|
| 12 |
+
output = {
|
| 13 |
+
'text': text,
|
| 14 |
+
'title': None
|
| 15 |
+
}
|
| 16 |
+
ents = []
|
| 17 |
+
for res in result:
|
| 18 |
+
if not res['word'].startswith("##"):
|
| 19 |
+
ents.append({
|
| 20 |
+
'start': res['start'],
|
| 21 |
+
'end': res['end'],
|
| 22 |
+
'label': res['entity'].replace("B-", "")
|
| 23 |
+
})
|
| 24 |
+
else:
|
| 25 |
+
ents[-1]['end'] = res['end']
|
| 26 |
+
|
| 27 |
+
output['ents'] = ents
|
| 28 |
+
return output
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
colors = {
|
| 32 |
+
'PREDICATE': "#80bdff",
|
| 33 |
+
'КАУЗАТИВ': "#73ffbe",
|
| 34 |
+
'КАУЗАТОР': "#ff5b5e",
|
| 35 |
+
'ЭКСПЕРИЕНЦЕР': "#efff42",
|
| 36 |
+
'ДРУГОЕ': "#924fff",
|
| 37 |
+
'ИНСТРУМЕНТ': "#28fff1"
|
| 38 |
+
}
|
| 39 |
+
options = {
|
| 40 |
+
'ents': list(colors.keys()), 'colors': colors
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
st.title("Semantic Role Labeling for Russian Language")
|
| 44 |
+
|
| 45 |
+
st.header("Type your sentence to see predicate, arguments and their roles")
|
| 46 |
+
|
| 47 |
+
text = st.text_input('Sentence', 'представители силовых ведомств удивлены такой наглости')
|
| 48 |
+
|
| 49 |
+
result = pipeline(text)
|
| 50 |
+
html = sp.displacy.render(
|
| 51 |
+
convert_to_spacy(text, result=result),
|
| 52 |
+
style='ent',
|
| 53 |
+
manual=True,
|
| 54 |
+
options=options,
|
| 55 |
+
jupyter=False
|
| 56 |
+
)
|
| 57 |
+
st.markdown(html, unsafe_allow_html=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
spacy==3.2.0
|
| 2 |
+
transformers>="4.0.0"
|