Spaces:
Runtime error
Runtime error
#!/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, | |
) | |