Spaces:
Sleeping
Sleeping
import streamlit as st | |
import sparknlp | |
import os | |
import pandas as pd | |
from sparknlp.base import * | |
from sparknlp.annotator import * | |
from pyspark.ml import Pipeline | |
from sparknlp.pretrained import PretrainedPipeline | |
from annotated_text import annotated_text | |
# Page configuration | |
st.set_page_config( | |
layout="wide", | |
initial_sidebar_state="auto" | |
) | |
# CSS for styling | |
st.markdown(""" | |
<style> | |
.main-title { | |
font-size: 36px; | |
color: #4A90E2; | |
font-weight: bold; | |
text-align: center; | |
} | |
.section { | |
background-color: #f9f9f9; | |
padding: 10px; | |
border-radius: 10px; | |
margin-top: 10px; | |
} | |
.section p, .section ul { | |
color: #666666; | |
} | |
</style> | |
""", unsafe_allow_html=True) | |
def init_spark(): | |
return sparknlp.start() | |
def create_pipeline(model): | |
document_assembler = DocumentAssembler() \ | |
.setInputCol("text") \ | |
.setOutputCol("document") | |
sentence_detector = SentenceDetector() \ | |
.setInputCols(["document"]) \ | |
.setOutputCol("sentence") | |
word_segmenter = WordSegmenterModel.pretrained("wordseg_kaist_ud", "ko") \ | |
.setInputCols(["sentence"]) \ | |
.setOutputCol("token") | |
embeddings = WordEmbeddingsModel.pretrained("glove_840B_300", "xx") \ | |
.setInputCols(["document", "token"]) \ | |
.setOutputCol("embeddings") | |
ner = NerDLModel.pretrained("ner_kmou_glove_840B_300d", "ko") \ | |
.setInputCols(["document", "token", "embeddings"]) \ | |
.setOutputCol("ner") | |
ner_converter = NerConverter().setInputCols(["document", "token", "ner"]).setOutputCol("ner_chunk") | |
pipeline = Pipeline(stages=[document_assembler, sentence_detector, word_segmenter, embeddings, ner, ner_converter]) | |
return pipeline | |
def fit_data(pipeline, data): | |
empty_df = spark.createDataFrame([['']]).toDF('text') | |
pipeline_model = pipeline.fit(empty_df) | |
model = LightPipeline(pipeline_model) | |
result = model.fullAnnotate(data) | |
return result | |
def annotate(data): | |
document, chunks, labels = data["Document"], data["NER Chunk"], data["NER Label"] | |
annotated_words = [] | |
for chunk, label in zip(chunks, labels): | |
parts = document.split(chunk, 1) | |
if parts[0]: | |
annotated_words.append(parts[0]) | |
annotated_words.append((chunk, label)) | |
document = parts[1] | |
if document: | |
annotated_words.append(document) | |
annotated_text(*annotated_words) | |
# Set up the page layout | |
st.markdown('<div class="main-title">Recognize entities in Urdu text</div>', unsafe_allow_html=True) | |
st.markdown(""" | |
<div class="section"> | |
<p>This model uses the pre-trained <code>glove_840B_300</code> embeddings model from WordEmbeddings annotator as an input</p> | |
</div> | |
""", unsafe_allow_html=True) | |
# Sidebar content | |
model = st.sidebar.selectbox( | |
"Choose the pretrained model", | |
["ner_kmou_glove_840B_300d"], | |
help="For more info about the models visit: https://sparknlp.org/models" | |
) | |
# Reference notebook link in sidebar | |
link = """ | |
<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/public/NER_KO.ipynb"> | |
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/> | |
</a> | |
""" | |
st.sidebar.markdown('Reference notebook:') | |
st.sidebar.markdown(link, unsafe_allow_html=True) | |
# Load examples | |
examples = [ | |
"""ARD , ZDF λ± κ³΅μ TV μ λ°μ΄μλ₯Έμ£Ό λ°©μ‘ , λΆλΆ λ μΌ λ°©μ‘ λ± μ μ΄ λ νκ΅ μ μ΄μ μμ κ³Ό κ΄λ ¨ , μ¬λΉ μ κ³Όλ°μ μμ ν보 μ μ λΉ μ λν μ¨ μ΄ μ΄λ² μ κ±° μ μ΅λ κ΄μ¬μ¬ μ΄ λΌκ³ 보λ ν γ΄ λ° μ μ΄ μ λ μκ° λΆν° λ μ μ°¨λ‘ μ κ±ΈμΉ μ΄ κ°ν μν© κ³Ό μ λΉ λ³ μμ μ λ§ μ μ보 λ‘ μ ν μ λ€ .""", | |
"""λ λλΌ κ΄κ³ λ μ€κ΅ μ μΈκΆ λ¬Έμ μ ν΅νμ° λ°©μ§ λ¬Έμ , ν΅μ λ¬Έμ λ° μ΅κ·Ό μ F 16 μ ν¬κΈ° λ λλ§ νλ§€ λ± μ λ κ³ μ΄λ―Έ μνμ μ μ μ€ μ μ λλ° ν΄λ¦°ν΄ νμ λΆ μ λ±μ₯ μΌλ‘ μκ΅ κ΄κ³ κ° λμ± κ²½μ λ γΉ κ² μ κ±±μ ν λ λΆμκΈ° .""", | |
"""μμΈλ 건μΆκ³΅ν κ³Ό λ₯Ό μ‘Έμ ν γ΄ μ΄ μ¨ λ νκ΅κ±΄μΆκ°νν""", | |
"""λ λ λ€μ μμ λ₯Ό μλ μμ λΉΌλ΄ κΈ° μν μμ μ°½μ νλ""", | |
"""ν€λΌμ μ μ μ±ν λ γ΄ μ§ λ³΄λ¦ , μ§κ΅¬ μ λ°λ°ν΄ λ₯Ό λ μ μ 주곡ν μ μ²«λ° μ λ΄λλ γ΄ μ΄λ λ‘ μ΄μ΄ν""", | |
"""λ€μ μ ν콩 μ κΆμμ§ λͺ 보 μ μΌλ³Έ λμΏ ( λκ²½ ) μ λ¬Έ μ΄ 24μΌ""", | |
"""μ΅ μμ¬ κ° μ°λ¦¬ μΈκ΅κ΄ μ΄ λ©° κ·Έ μ λ³λ³΄νΈ μ± μ μ΄ μ£Όμ¬κ΅ μ΄ γ΄ λ¬μμ μ μ λ€λ μ μμ λ¬μμ λ μ΄ κ° μ μ°λ¦¬ μ λΆ μꡬ μ μν μμΌ ν γΉ μ무 κ° μ λ€ .""", | |
"""ν μ λ° μ λ― ν γ΄ κΉ¨λ ν γ΄ κΈμ¨ λ‘ , μ²μ λ¨κ΅° λ μ΄ λ μ λΌ , λ°±μ , κ³ κ΅¬λ € μ΄ λ λμλμ μ΄λ₯Έ λ€ νν μ κ·κ²° λ‘ λ€μ΄μ€ λ μκΈ° λ€ μ΄ μ°Έλ§ λ‘ μ μ΄ μ μ λ€ .""" | |
] | |
selected_text = st.selectbox("Select an example", examples) | |
custom_input = st.text_input("Try it with your own Sentence!") | |
text_to_analyze = custom_input if custom_input else selected_text | |
st.subheader('Full example text') | |
HTML_WRAPPER = """<div class="scroll entities" style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem; white-space:pre-wrap">{}</div>""" | |
st.markdown(HTML_WRAPPER.format(text_to_analyze), unsafe_allow_html=True) | |
# Initialize Spark and create pipeline | |
spark = init_spark() | |
pipeline = create_pipeline(model) | |
output = fit_data(pipeline, text_to_analyze) | |
# Display matched sentence | |
st.subheader("Processed output:") | |
results = { | |
'Document': output[0]['document'][0].result, | |
'NER Chunk': [n.result for n in output[0]['ner_chunk']], | |
"NER Label": [n.metadata['entity'] for n in output[0]['ner_chunk']] | |
} | |
annotate(results) | |
with st.expander("View DataFrame"): | |
df = pd.DataFrame({'NER Chunk': results['NER Chunk'], 'NER Label': results['NER Label']}) | |
df.index += 1 | |
st.dataframe(df) | |