imvladikon's picture
Update app.py
0d10901
raw
history blame
1.93 kB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import streamlit as st
from spacy import displacy
import span_marker
import spacy
import spacy_udpipe
spacy_udpipe.download("ar")
nlp = spacy_udpipe.load("ar")
nlp.add_pipe("span_marker",
config={"model": "iahlt/span-marker-xlm-roberta-base-ar"})
def get_html(html: str):
"""Convert HTML so it can be rendered."""
WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
# Newlines seem to mess with the rendering
html = html.replace("\n", " ")
return WRAPPER.format(html)
def page_init():
st.header("Named Entity Recognition Demo")
if __name__ == '__main__':
page_init()
displacy_options = {}
sample_text = """
ุฃุจูˆ ุนูŽุจุฏ ุงู„ู„ู‡ ู…ูุญูŽู…ูŽู‘ุฏ ุจู† ู…ููˆุณูŽู‰ ุงู„ุฎูŽูˆุงุฑูุฒู…ูŠ ุนุงู„ู… ุฑูŠุงุถูŠุงุช ูˆูู„ูƒ ูˆุฌุบุฑุงููŠุง ู…ุณู„ู…. ูŠูƒู†ู‰ ุจุฃุจูŠ ุฌุนูุฑ. ู‚ูŠู„ ุฃู†ู‡ ูˆู„ุฏ ุญูˆุงู„ูŠ 164ู‡ู€ 781ู… ูˆู‚ูŠู„ ุฃู†ู‡ ุชูˆููŠูŽ ุจุนุฏ 232 ู‡ู€ ุฃูŠ (ุจุนุฏ 847ู…). ูŠุนุชุจุฑ ู…ู† ุฃูˆุงุฆู„ ุนู„ู…ุงุก ุงู„ุฑูŠุงุถูŠุงุช ุงู„ู…ุณู„ู…ูŠู† ุญูŠุซ ุณุงู‡ู…ุช ุฃุนู…ุงู„ู‡ ุจุฏูˆุฑ ูƒุจูŠุฑ ููŠ ุชู‚ุฏู… ุงู„ุฑูŠุงุถูŠุงุช ููŠ ุนุตุฑู‡.
""".strip()
text = st.text_area("Text", sample_text, height=200, max_chars=1000)
btn = st.button("Annotate")
if text and btn:
doc = nlp(text)
html = displacy.render(
doc,
style="ent",
options=displacy_options,
manual=False,
)
style = "<style>mark.entity { display: inline-block }</style>"
st.write(f"{style}{get_html(html)}", unsafe_allow_html=True)
else:
st.write("")
st.markdown(
"""
<style>
textarea {
direction: rtl;
}
</style>
""",
unsafe_allow_html=True,
)