Spaces:
Sleeping
Sleeping
Update Demo.py
Browse files
Demo.py
CHANGED
@@ -1,158 +1,158 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import sparknlp
|
3 |
-
import os
|
4 |
-
import pandas as pd
|
5 |
-
|
6 |
-
from sparknlp.base import *
|
7 |
-
from sparknlp.annotator import *
|
8 |
-
from pyspark.ml import Pipeline
|
9 |
-
from sparknlp.pretrained import PretrainedPipeline
|
10 |
-
from annotated_text import annotated_text
|
11 |
-
|
12 |
-
# Page configuration
|
13 |
-
st.set_page_config(
|
14 |
-
layout="wide",
|
15 |
-
initial_sidebar_state="auto"
|
16 |
-
)
|
17 |
-
|
18 |
-
# CSS for styling
|
19 |
-
st.markdown("""
|
20 |
-
<style>
|
21 |
-
.main-title {
|
22 |
-
font-size: 36px;
|
23 |
-
color: #4A90E2;
|
24 |
-
font-weight: bold;
|
25 |
-
text-align: center;
|
26 |
-
}
|
27 |
-
.section {
|
28 |
-
background-color: #f9f9f9;
|
29 |
-
padding: 10px;
|
30 |
-
border-radius: 10px;
|
31 |
-
margin-top: 10px;
|
32 |
-
}
|
33 |
-
.section p, .section ul {
|
34 |
-
color: #666666;
|
35 |
-
}
|
36 |
-
</style>
|
37 |
-
""", unsafe_allow_html=True)
|
38 |
-
|
39 |
-
@st.cache_resource
|
40 |
-
def init_spark():
|
41 |
-
return sparknlp.start()
|
42 |
-
|
43 |
-
@st.cache_resource
|
44 |
-
def create_pipeline(model):
|
45 |
-
document_assembler = DocumentAssembler() \
|
46 |
-
.setInputCol("text") \
|
47 |
-
.setOutputCol("document")
|
48 |
-
|
49 |
-
sentence_detector = SentenceDetector() \
|
50 |
-
.setInputCols(["document"]) \
|
51 |
-
.setOutputCol("sentence")
|
52 |
-
|
53 |
-
word_segmenter = WordSegmenterModel.pretrained("wordseg_kaist_ud", "ko") \
|
54 |
-
.setInputCols(["sentence"]) \
|
55 |
-
.setOutputCol("token")
|
56 |
-
|
57 |
-
embeddings = WordEmbeddingsModel.pretrained("glove_840B_300", "xx") \
|
58 |
-
.setInputCols(["document", "token"]) \
|
59 |
-
.setOutputCol("embeddings")
|
60 |
-
|
61 |
-
ner = NerDLModel.pretrained("ner_kmou_glove_840B_300d", "ko") \
|
62 |
-
.setInputCols(["document", "token", "embeddings"]) \
|
63 |
-
.setOutputCol("ner")
|
64 |
-
|
65 |
-
ner_converter = NerConverter().setInputCols(["document", "token", "ner"]).setOutputCol("ner_chunk")
|
66 |
-
|
67 |
-
pipeline = Pipeline(stages=[document_assembler, sentence_detector, word_segmenter, embeddings, ner, ner_converter])
|
68 |
-
return
|
69 |
-
|
70 |
-
def fit_data(pipeline, data):
|
71 |
-
empty_df = spark.createDataFrame([['']]).toDF('text')
|
72 |
-
pipeline_model = pipeline.fit(empty_df)
|
73 |
-
model = LightPipeline(pipeline_model)
|
74 |
-
result = model.fullAnnotate(data)
|
75 |
-
return result
|
76 |
-
|
77 |
-
def annotate(data):
|
78 |
-
document, chunks, labels = data["Document"], data["NER Chunk"], data["NER Label"]
|
79 |
-
annotated_words = []
|
80 |
-
for chunk, label in zip(chunks, labels):
|
81 |
-
parts = document.split(chunk, 1)
|
82 |
-
if parts[0]:
|
83 |
-
annotated_words.append(parts[0])
|
84 |
-
annotated_words.append((chunk, label))
|
85 |
-
document = parts[1]
|
86 |
-
if document:
|
87 |
-
annotated_words.append(document)
|
88 |
-
annotated_text(*annotated_words)
|
89 |
-
|
90 |
-
# Set up the page layout
|
91 |
-
st.markdown('<div class="main-title">Recognize entities in Urdu text</div>', unsafe_allow_html=True)
|
92 |
-
st.markdown("""
|
93 |
-
<div class="section">
|
94 |
-
<p>This model uses the pre-trained <code>glove_840B_300</code> embeddings model from WordEmbeddings annotator as an input</p>
|
95 |
-
</div>
|
96 |
-
""", unsafe_allow_html=True)
|
97 |
-
|
98 |
-
# Sidebar content
|
99 |
-
model = st.sidebar.selectbox(
|
100 |
-
"Choose the pretrained model",
|
101 |
-
["ner_kmou_glove_840B_300d"],
|
102 |
-
help="For more info about the models visit: https://sparknlp.org/models"
|
103 |
-
)
|
104 |
-
|
105 |
-
# Reference notebook link in sidebar
|
106 |
-
link = """
|
107 |
-
<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/public/NER_KO.ipynb">
|
108 |
-
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
|
109 |
-
</a>
|
110 |
-
"""
|
111 |
-
st.sidebar.markdown('Reference notebook:')
|
112 |
-
st.sidebar.markdown(link, unsafe_allow_html=True)
|
113 |
-
|
114 |
-
# Load examples
|
115 |
-
examples = [
|
116 |
-
"""ARD , ZDF λ± κ³΅μ TV μ λ°μ΄μλ₯Έμ£Ό λ°©μ‘ , λΆλΆ λ
μΌ λ°©μ‘ λ± μ μ΄ λ νκ΅ μ μ΄μ μμ κ³Ό κ΄λ ¨ , μ¬λΉ μ κ³Όλ°μ μμ ν보 μ μ λΉ μ λν μ¨ μ΄ μ΄λ² μ κ±° μ μ΅λ κ΄μ¬μ¬ μ΄ λΌκ³ 보λ ν γ΄ λ° μ μ΄ μ λ
μκ° λΆν° λ μ μ°¨λ‘ μ κ±ΈμΉ μ΄ κ°ν μν© κ³Ό μ λΉ λ³ μμ μ λ§ μ μ보 λ‘ μ ν μ λ€ .""",
|
117 |
-
"""λ λλΌ κ΄κ³ λ μ€κ΅ μ μΈκΆ λ¬Έμ μ ν΅νμ° λ°©μ§ λ¬Έμ , ν΅μ λ¬Έμ λ° μ΅κ·Ό μ F 16 μ ν¬κΈ° λ λλ§ νλ§€ λ± μ λ κ³ μ΄λ―Έ μνμ μ μ μ€ μ μ λλ° ν΄λ¦°ν΄ νμ λΆ μ λ±μ₯ μΌλ‘ μκ΅ κ΄κ³ κ° λμ± κ²½μ λ γΉ κ² μ κ±±μ ν λ λΆμκΈ° .""",
|
118 |
-
"""μμΈλ 건μΆκ³΅ν κ³Ό λ₯Ό μ‘Έμ
ν γ΄ μ΄ μ¨ λ νκ΅κ±΄μΆκ°νν""",
|
119 |
-
"""λ λ λ€μ μμ λ₯Ό μλ μμ λΉΌλ΄ κΈ° μν μμ μ°½μ νλ""",
|
120 |
-
"""ν€λΌμ μ μ μ±ν λ γ΄ μ§ λ³΄λ¦ , μ§κ΅¬ μ λ°λ°ν΄ λ₯Ό λ μ μ 주곡ν μ μ²«λ° μ λ΄λλ γ΄ μ΄λ λ‘ μ΄μ΄ν""",
|
121 |
-
"""λ€μ μ ν콩 μ κΆμμ§ λͺ
보 μ μΌλ³Έ λμΏ ( λκ²½ ) μ λ¬Έ μ΄ 24μΌ""",
|
122 |
-
"""μ΅ μμ¬ κ° μ°λ¦¬ μΈκ΅κ΄ μ΄ λ©° κ·Έ μ λ³λ³΄νΈ μ±
μ μ΄ μ£Όμ¬κ΅ μ΄ γ΄ λ¬μμ μ μ λ€λ μ μμ λ¬μμ λ μ΄ κ° μ μ°λ¦¬ μ λΆ μꡬ
|
123 |
-
"""ν μ λ° μ λ― ν γ΄ κΉ¨λ ν γ΄ κΈμ¨ λ‘ , μ²μ λ¨κ΅° λ μ΄ λ μ λΌ , λ°±μ , κ³ κ΅¬λ € μ΄ λ λμλμ μ΄λ₯Έ λ€ νν
μ κ·κ²° λ‘ λ€μ΄μ€ λ μκΈ° λ€ μ΄ μ°Έλ§ λ‘ μ μ΄ μ μ λ€ ."""
|
124 |
-
]
|
125 |
-
|
126 |
-
selected_text = st.selectbox("Select an example", examples)
|
127 |
-
custom_input = st.text_input("Try it with your own Sentence!")
|
128 |
-
|
129 |
-
text_to_analyze = custom_input if custom_input else selected_text
|
130 |
-
|
131 |
-
st.subheader('Full example text')
|
132 |
-
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>"""
|
133 |
-
st.markdown(HTML_WRAPPER.format(text_to_analyze), unsafe_allow_html=True)
|
134 |
-
|
135 |
-
# Initialize Spark and create pipeline
|
136 |
-
spark = init_spark()
|
137 |
-
pipeline = create_pipeline(model)
|
138 |
-
output = fit_data(pipeline, text_to_analyze)
|
139 |
-
|
140 |
-
# Display matched sentence
|
141 |
-
st.subheader("Processed output:")
|
142 |
-
|
143 |
-
results = {
|
144 |
-
'Document': output[0]['document'][0].result,
|
145 |
-
'NER Chunk': [n.result for n in output[0]['ner_chunk']],
|
146 |
-
"NER Label": [n.metadata['entity'] for n in output[0]['ner_chunk']]
|
147 |
-
}
|
148 |
-
|
149 |
-
annotate(results)
|
150 |
-
|
151 |
-
with st.expander("View DataFrame"):
|
152 |
-
df = pd.DataFrame({'NER Chunk': results['NER Chunk'], 'NER Label': results['NER Label']})
|
153 |
-
df.index += 1
|
154 |
-
st.dataframe(df)
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import sparknlp
|
3 |
+
import os
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
from sparknlp.base import *
|
7 |
+
from sparknlp.annotator import *
|
8 |
+
from pyspark.ml import Pipeline
|
9 |
+
from sparknlp.pretrained import PretrainedPipeline
|
10 |
+
from annotated_text import annotated_text
|
11 |
+
|
12 |
+
# Page configuration
|
13 |
+
st.set_page_config(
|
14 |
+
layout="wide",
|
15 |
+
initial_sidebar_state="auto"
|
16 |
+
)
|
17 |
+
|
18 |
+
# CSS for styling
|
19 |
+
st.markdown("""
|
20 |
+
<style>
|
21 |
+
.main-title {
|
22 |
+
font-size: 36px;
|
23 |
+
color: #4A90E2;
|
24 |
+
font-weight: bold;
|
25 |
+
text-align: center;
|
26 |
+
}
|
27 |
+
.section {
|
28 |
+
background-color: #f9f9f9;
|
29 |
+
padding: 10px;
|
30 |
+
border-radius: 10px;
|
31 |
+
margin-top: 10px;
|
32 |
+
}
|
33 |
+
.section p, .section ul {
|
34 |
+
color: #666666;
|
35 |
+
}
|
36 |
+
</style>
|
37 |
+
""", unsafe_allow_html=True)
|
38 |
+
|
39 |
+
@st.cache_resource
|
40 |
+
def init_spark():
|
41 |
+
return sparknlp.start()
|
42 |
+
|
43 |
+
@st.cache_resource
|
44 |
+
def create_pipeline(model):
|
45 |
+
document_assembler = DocumentAssembler() \
|
46 |
+
.setInputCol("text") \
|
47 |
+
.setOutputCol("document")
|
48 |
+
|
49 |
+
sentence_detector = SentenceDetector() \
|
50 |
+
.setInputCols(["document"]) \
|
51 |
+
.setOutputCol("sentence")
|
52 |
+
|
53 |
+
word_segmenter = WordSegmenterModel.pretrained("wordseg_kaist_ud", "ko") \
|
54 |
+
.setInputCols(["sentence"]) \
|
55 |
+
.setOutputCol("token")
|
56 |
+
|
57 |
+
embeddings = WordEmbeddingsModel.pretrained("glove_840B_300", "xx") \
|
58 |
+
.setInputCols(["document", "token"]) \
|
59 |
+
.setOutputCol("embeddings")
|
60 |
+
|
61 |
+
ner = NerDLModel.pretrained("ner_kmou_glove_840B_300d", "ko") \
|
62 |
+
.setInputCols(["document", "token", "embeddings"]) \
|
63 |
+
.setOutputCol("ner")
|
64 |
+
|
65 |
+
ner_converter = NerConverter().setInputCols(["document", "token", "ner"]).setOutputCol("ner_chunk")
|
66 |
+
|
67 |
+
pipeline = Pipeline(stages=[document_assembler, sentence_detector, word_segmenter, embeddings, ner, ner_converter])
|
68 |
+
return pipeline
|
69 |
+
|
70 |
+
def fit_data(pipeline, data):
|
71 |
+
empty_df = spark.createDataFrame([['']]).toDF('text')
|
72 |
+
pipeline_model = pipeline.fit(empty_df)
|
73 |
+
model = LightPipeline(pipeline_model)
|
74 |
+
result = model.fullAnnotate(data)
|
75 |
+
return result
|
76 |
+
|
77 |
+
def annotate(data):
|
78 |
+
document, chunks, labels = data["Document"], data["NER Chunk"], data["NER Label"]
|
79 |
+
annotated_words = []
|
80 |
+
for chunk, label in zip(chunks, labels):
|
81 |
+
parts = document.split(chunk, 1)
|
82 |
+
if parts[0]:
|
83 |
+
annotated_words.append(parts[0])
|
84 |
+
annotated_words.append((chunk, label))
|
85 |
+
document = parts[1]
|
86 |
+
if document:
|
87 |
+
annotated_words.append(document)
|
88 |
+
annotated_text(*annotated_words)
|
89 |
+
|
90 |
+
# Set up the page layout
|
91 |
+
st.markdown('<div class="main-title">Recognize entities in Urdu text</div>', unsafe_allow_html=True)
|
92 |
+
st.markdown("""
|
93 |
+
<div class="section">
|
94 |
+
<p>This model uses the pre-trained <code>glove_840B_300</code> embeddings model from WordEmbeddings annotator as an input</p>
|
95 |
+
</div>
|
96 |
+
""", unsafe_allow_html=True)
|
97 |
+
|
98 |
+
# Sidebar content
|
99 |
+
model = st.sidebar.selectbox(
|
100 |
+
"Choose the pretrained model",
|
101 |
+
["ner_kmou_glove_840B_300d"],
|
102 |
+
help="For more info about the models visit: https://sparknlp.org/models"
|
103 |
+
)
|
104 |
+
|
105 |
+
# Reference notebook link in sidebar
|
106 |
+
link = """
|
107 |
+
<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/public/NER_KO.ipynb">
|
108 |
+
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
|
109 |
+
</a>
|
110 |
+
"""
|
111 |
+
st.sidebar.markdown('Reference notebook:')
|
112 |
+
st.sidebar.markdown(link, unsafe_allow_html=True)
|
113 |
+
|
114 |
+
# Load examples
|
115 |
+
examples = [
|
116 |
+
"""ARD , ZDF λ± κ³΅μ TV μ λ°μ΄μλ₯Έμ£Ό λ°©μ‘ , λΆλΆ λ
μΌ λ°©μ‘ λ± μ μ΄ λ νκ΅ μ μ΄μ μμ κ³Ό κ΄λ ¨ , μ¬λΉ μ κ³Όλ°μ μμ ν보 μ μ λΉ μ λν μ¨ μ΄ μ΄λ² μ κ±° μ μ΅λ κ΄μ¬μ¬ μ΄ λΌκ³ 보λ ν γ΄ λ° μ μ΄ μ λ
μκ° λΆν° λ μ μ°¨λ‘ μ κ±ΈμΉ μ΄ κ°ν μν© κ³Ό μ λΉ λ³ μμ μ λ§ μ μ보 λ‘ μ ν μ λ€ .""",
|
117 |
+
"""λ λλΌ κ΄κ³ λ μ€κ΅ μ μΈκΆ λ¬Έμ μ ν΅νμ° λ°©μ§ λ¬Έμ , ν΅μ λ¬Έμ λ° μ΅κ·Ό μ F 16 μ ν¬κΈ° λ λλ§ νλ§€ λ± μ λ κ³ μ΄λ―Έ μνμ μ μ μ€ μ μ λλ° ν΄λ¦°ν΄ νμ λΆ μ λ±μ₯ μΌλ‘ μκ΅ κ΄κ³ κ° λμ± κ²½μ λ γΉ κ² μ κ±±μ ν λ λΆμκΈ° .""",
|
118 |
+
"""μμΈλ 건μΆκ³΅ν κ³Ό λ₯Ό μ‘Έμ
ν γ΄ μ΄ μ¨ λ νκ΅κ±΄μΆκ°νν""",
|
119 |
+
"""λ λ λ€μ μμ λ₯Ό μλ μμ λΉΌλ΄ κΈ° μν μμ μ°½μ νλ""",
|
120 |
+
"""ν€λΌμ μ μ μ±ν λ γ΄ μ§ λ³΄λ¦ , μ§κ΅¬ μ λ°λ°ν΄ λ₯Ό λ μ μ 주곡ν μ μ²«λ° μ λ΄λλ γ΄ μ΄λ λ‘ μ΄μ΄ν""",
|
121 |
+
"""λ€μ μ ν콩 μ κΆμμ§ λͺ
보 μ μΌλ³Έ λμΏ ( λκ²½ ) μ λ¬Έ μ΄ 24μΌ""",
|
122 |
+
"""μ΅ μμ¬ κ° μ°λ¦¬ μΈκ΅κ΄ μ΄ λ©° κ·Έ μ λ³λ³΄νΈ μ±
μ μ΄ μ£Όμ¬κ΅ μ΄ γ΄ λ¬μμ μ μ λ€λ μ μμ λ¬μμ λ μ΄ κ° μ μ°λ¦¬ μ λΆ μꡬ μ μν μμΌ ν γΉ μ무 κ° μ λ€ .""",
|
123 |
+
"""ν μ λ° μ λ― ν γ΄ κΉ¨λ ν γ΄ κΈμ¨ λ‘ , μ²μ λ¨κ΅° λ μ΄ λ μ λΌ , λ°±μ , κ³ κ΅¬λ € μ΄ λ λμλμ μ΄λ₯Έ λ€ νν
μ κ·κ²° λ‘ λ€μ΄μ€ λ μκΈ° λ€ μ΄ μ°Έλ§ λ‘ μ μ΄ μ μ λ€ ."""
|
124 |
+
]
|
125 |
+
|
126 |
+
selected_text = st.selectbox("Select an example", examples)
|
127 |
+
custom_input = st.text_input("Try it with your own Sentence!")
|
128 |
+
|
129 |
+
text_to_analyze = custom_input if custom_input else selected_text
|
130 |
+
|
131 |
+
st.subheader('Full example text')
|
132 |
+
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>"""
|
133 |
+
st.markdown(HTML_WRAPPER.format(text_to_analyze), unsafe_allow_html=True)
|
134 |
+
|
135 |
+
# Initialize Spark and create pipeline
|
136 |
+
spark = init_spark()
|
137 |
+
pipeline = create_pipeline(model)
|
138 |
+
output = fit_data(pipeline, text_to_analyze)
|
139 |
+
|
140 |
+
# Display matched sentence
|
141 |
+
st.subheader("Processed output:")
|
142 |
+
|
143 |
+
results = {
|
144 |
+
'Document': output[0]['document'][0].result,
|
145 |
+
'NER Chunk': [n.result for n in output[0]['ner_chunk']],
|
146 |
+
"NER Label": [n.metadata['entity'] for n in output[0]['ner_chunk']]
|
147 |
+
}
|
148 |
+
|
149 |
+
annotate(results)
|
150 |
+
|
151 |
+
with st.expander("View DataFrame"):
|
152 |
+
df = pd.DataFrame({'NER Chunk': results['NER Chunk'], 'NER Label': results['NER Label']})
|
153 |
+
df.index += 1
|
154 |
+
st.dataframe(df)
|
155 |
+
|
156 |
+
|
157 |
+
|
158 |
+
|