Upload 10 files
Browse files- .streamlit/config.toml +3 -0
- Demo.py +153 -0
- Dockerfile +70 -0
- inputs/hebrewner_cc_300d/Example1.txt +2 -0
- inputs/hebrewner_cc_300d/Example2.txt +2 -0
- inputs/hebrewner_cc_300d/Example3.txt +2 -0
- inputs/hebrewner_cc_300d/Example4.txt +2 -0
- inputs/hebrewner_cc_300d/Example5.txt +2 -0
- pages/Workflow & Model Overview.py +269 -0
- requirements.txt +6 -0
.streamlit/config.toml
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
[theme]
|
2 |
+
base="light"
|
3 |
+
primaryColor="#29B4E8"
|
Demo.py
ADDED
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
documentAssembler = DocumentAssembler()\
|
46 |
+
.setInputCol("text")\
|
47 |
+
.setOutputCol("document")
|
48 |
+
|
49 |
+
tokenizer = Tokenizer() \
|
50 |
+
.setInputCols(["document"]) \
|
51 |
+
.setOutputCol("token")
|
52 |
+
|
53 |
+
embeddings = WordEmbeddingsModel.pretrained("urduvec_140M_300d", "ur") \
|
54 |
+
.setInputCols(["document", "token"]) \
|
55 |
+
.setOutputCol("embeddings")
|
56 |
+
|
57 |
+
ner = NerDLModel.pretrained("uner_mk_140M_300d", "ur" ) \
|
58 |
+
.setInputCols(["document", "token", "embeddings"]) \
|
59 |
+
.setOutputCol("ner")
|
60 |
+
|
61 |
+
ner_converter = NerConverter() \
|
62 |
+
.setInputCols(["document", "token", "ner"]) \
|
63 |
+
.setOutputCol("ner_chunk")
|
64 |
+
|
65 |
+
nlpPipeline = Pipeline(stages=[documentAssembler, tokenizer, embeddings, ner, ner_converter])
|
66 |
+
return nlpPipeline
|
67 |
+
|
68 |
+
def fit_data(pipeline, data):
|
69 |
+
empty_df = spark.createDataFrame([['']]).toDF('text')
|
70 |
+
pipeline_model = pipeline.fit(empty_df)
|
71 |
+
model = LightPipeline(pipeline_model)
|
72 |
+
result = model.fullAnnotate(data)
|
73 |
+
return result
|
74 |
+
|
75 |
+
def annotate(data):
|
76 |
+
document, chunks, labels = data["Document"], data["NER Chunk"], data["NER Label"]
|
77 |
+
annotated_words = []
|
78 |
+
for chunk, label in zip(chunks, labels):
|
79 |
+
parts = document.split(chunk, 1)
|
80 |
+
if parts[0]:
|
81 |
+
annotated_words.append(parts[0])
|
82 |
+
annotated_words.append((chunk, label))
|
83 |
+
document = parts[1]
|
84 |
+
if document:
|
85 |
+
annotated_words.append(document)
|
86 |
+
annotated_text(*annotated_words)
|
87 |
+
|
88 |
+
# Set up the page layout
|
89 |
+
st.markdown('<div class="main-title">Recognize entities in Urdu text</div>', unsafe_allow_html=True)
|
90 |
+
st.markdown("""
|
91 |
+
<div class="section">
|
92 |
+
<p>This demo utilizes embeddings-based NER model for Urdu texts, using the urduvec_140M_300d word embeddings</p>
|
93 |
+
</div>
|
94 |
+
""", unsafe_allow_html=True)
|
95 |
+
|
96 |
+
# Sidebar content
|
97 |
+
model = st.sidebar.selectbox(
|
98 |
+
"Choose the pretrained model",
|
99 |
+
["uner_mk_140M_300d"],
|
100 |
+
help="For more info about the models visit: https://sparknlp.org/models"
|
101 |
+
)
|
102 |
+
|
103 |
+
# Reference notebook link in sidebar
|
104 |
+
link = """
|
105 |
+
<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/streamlit_notebooks/public/NER_UR.ipynb">
|
106 |
+
<img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
|
107 |
+
</a>
|
108 |
+
"""
|
109 |
+
st.sidebar.markdown('Reference notebook:')
|
110 |
+
st.sidebar.markdown(link, unsafe_allow_html=True)
|
111 |
+
|
112 |
+
# Load examples
|
113 |
+
examples = [
|
114 |
+
"""کھیل کے لیے سب سے مناسب تاریخ سال کا آخر ہے کیونکہ سنہ دوہزارباءیس کے آغاز میں سرمائی اولمپکس ہوتی ہیں۔ بلیٹر نے بین الاقوامی اولمپک کمیٹی کو یقین دلایا کہ ورلڈ کپ اور سرمائی اولمپکس کی تاریخوں میں کوئی ٹکراؤ نہیں ہو گا، جو کہ فروری دوہزارباءیس میں قزاقستان یا چین میں ہونی ہیں۔ فٹبال کلب کی تاریخ میں سنیچر 25اکتوبر کو ریئل میڈرڈ اور بارسلونا کے درمیان اہم ترین میچ کھیلا جائے گا۔ اس میچ میں فٹبال کی دنیا کے دو بہترین کھلاڑی حصہ لیں گے۔ اس میچ کی ایک خاص بات اور بھی ہے کہ اس میں فٹبال کے عالمی کپ میں سب سے زیادہ گول کرنے والے کھلاڑی کے علاوہ تاریخ کے پانچ میں سے چار مہنگے ترین کھلاڑی بھی حصہ لیں گے۔""",
|
115 |
+
"""اس میچ کی سب سے زیادہ سحر انگيزی یوروگوائے کے مشہور کھلاڑی لوئس سواریز کی شمولیت ہے۔ خیال رہے کہ لوئس سواریز نے برازیل میں کھیلے جانے والے فٹبال کے عالمی کپ کے ایک میچ کے دوران اطالوی کھلاڑی جیورجیو چیلینی کو کاٹ کھایا تھا جس کے بعد فیفا نے ان پر نو بین الاقوامی میچوں کی پابندی عائد کی تھی۔ اس میچ میں ارجنٹائن سے تعلق رکھنے والے فٹبال کے شہرۂ آفاق کھلاڑی لیونل میسی اور برازیل کے سٹار کھلاڑی نیمار بھی حصہ لیں گے۔""",
|
116 |
+
"""سائنسدانوں نے صرف بیس ڈالر کا مواد استعمال کرتے ہوئے صرف بارہ گھنٹے میں ایبولا کا پروٹو ٹائپ بنا کر اپنی ٹیسٹ کی تکنیک کو ثابت کر دیا۔ اس ٹیسٹ میں حیاتیاتی اجزا استعمال ہوتے ہیں جس میں آر این اے کے جنیاتی اجزا بھی شامل ہوتے ہیں۔ سائنسدانوں کا کہنا ہے کہ ان اجزا کو ٹھنڈا اور خشک کرکے عام کاغذ پر بھی سٹور کیا جا سکتا ہے۔ تحقیقی ٹیم کے سربراہ جیم کولنز جو بوسٹن اور ہارورڈ دونوں یونیورسٹیوں میں تعینات ہیں، کہتے ہیں کہ ان حیاتیاتی اجزا کو صرف پانی ملانے سے متحرک کیا جا سکتا ہے۔ انھوں نے بی بی سی کو بتایا کہ ہمیں اس بات پر حیرت ہوئی کہ ٹھنڈا اور خشک کرنے کے بعد ان اجزا نے کتنے اچھے طریقے سے کام کیا۔""",
|
117 |
+
"""ان کے بائیں ہاتھ میں ہیئر لائن فریکچر ہے اور وہ جمعے کو میدان میں فیلڈنگ کے لیے بھی نہیں آئے۔ تین میچوں کی اس ٹیسٹ سیریز میں پاکستان کو ایک صفر کی برتری حاصل ہے اور وہ سری لنکا کی سرزمین پر نو سال میں پہلی ٹیسٹ سیریز جیتنے کے لیے کوشاں ہے۔ پاکستانی ٹیم نے آخری بار سری لنکا میں ٹیسٹ سیریز سنہ 2006 میں انضمام الحق کی قیادت میں جیتی تھی جس کے بعد لگاتار تین مرتبہ اسے ٹیسٹ سیریز میں شکست ہو چکی ہے۔""",
|
118 |
+
"""انھوں نے کہا کہ گذشتہ ایشیز سیریز میں پانچ صفر کی جیت اور ورلڈ کپ کا فاتح بننا کلارک کے کریئر کے دو اہم واقعات ہیں۔ اس کے علاوہ بھی انھوں نے اپنے ملک کے لیے بحیثیت بیٹسمین اور کپتان غیرمعمولی کارکردگی کا مظاہرہ کیا۔ڈربن میں کھیلے جانے والے تیسرے ایک روزہ میچ میں جنوبی افریقہ نے نیوزی لینڈ کو 62 رنز سے شکست دے کر تین میچوں پر مشتمل سیریز دو ایک سے جیت لی۔"""
|
119 |
+
]
|
120 |
+
|
121 |
+
selected_text = st.selectbox("Select an example", examples)
|
122 |
+
custom_input = st.text_input("Try it with your own Sentence!")
|
123 |
+
|
124 |
+
text_to_analyze = custom_input if custom_input else selected_text
|
125 |
+
|
126 |
+
st.subheader('Full example text')
|
127 |
+
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>"""
|
128 |
+
st.markdown(HTML_WRAPPER.format(text_to_analyze), unsafe_allow_html=True)
|
129 |
+
|
130 |
+
# Initialize Spark and create pipeline
|
131 |
+
spark = init_spark()
|
132 |
+
pipeline = create_pipeline(model)
|
133 |
+
output = fit_data(pipeline, text_to_analyze)
|
134 |
+
|
135 |
+
# Display matched sentence
|
136 |
+
st.subheader("Processed output:")
|
137 |
+
|
138 |
+
results = {
|
139 |
+
'Document': output[0]['document'][0].result,
|
140 |
+
'NER Chunk': [n.result for n in output[0]['ner_chunk']],
|
141 |
+
"NER Label": [n.metadata['entity'] for n in output[0]['ner_chunk']]
|
142 |
+
}
|
143 |
+
|
144 |
+
annotate(results)
|
145 |
+
|
146 |
+
with st.expander("View DataFrame"):
|
147 |
+
df = pd.DataFrame({'NER Chunk': results['NER Chunk'], 'NER Label': results['NER Label']})
|
148 |
+
df.index += 1
|
149 |
+
st.dataframe(df)
|
150 |
+
|
151 |
+
|
152 |
+
|
153 |
+
|
Dockerfile
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Download base image ubuntu 18.04
|
2 |
+
FROM ubuntu:18.04
|
3 |
+
|
4 |
+
# Set environment variables
|
5 |
+
ENV NB_USER jovyan
|
6 |
+
ENV NB_UID 1000
|
7 |
+
ENV HOME /home/${NB_USER}
|
8 |
+
|
9 |
+
# Install required packages
|
10 |
+
RUN apt-get update && apt-get install -y \
|
11 |
+
tar \
|
12 |
+
wget \
|
13 |
+
bash \
|
14 |
+
rsync \
|
15 |
+
gcc \
|
16 |
+
libfreetype6-dev \
|
17 |
+
libhdf5-serial-dev \
|
18 |
+
libpng-dev \
|
19 |
+
libzmq3-dev \
|
20 |
+
python3 \
|
21 |
+
python3-dev \
|
22 |
+
python3-pip \
|
23 |
+
unzip \
|
24 |
+
pkg-config \
|
25 |
+
software-properties-common \
|
26 |
+
graphviz \
|
27 |
+
openjdk-8-jdk \
|
28 |
+
ant \
|
29 |
+
ca-certificates-java \
|
30 |
+
&& apt-get clean \
|
31 |
+
&& update-ca-certificates -f;
|
32 |
+
|
33 |
+
# Install Python 3.8 and pip
|
34 |
+
RUN add-apt-repository ppa:deadsnakes/ppa \
|
35 |
+
&& apt-get update \
|
36 |
+
&& apt-get install -y python3.8 python3-pip \
|
37 |
+
&& apt-get clean;
|
38 |
+
|
39 |
+
# Set up JAVA_HOME
|
40 |
+
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
|
41 |
+
RUN mkdir -p ${HOME} \
|
42 |
+
&& echo "export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/" >> ${HOME}/.bashrc \
|
43 |
+
&& chown -R ${NB_UID}:${NB_UID} ${HOME}
|
44 |
+
|
45 |
+
# Create a new user named "jovyan" with user ID 1000
|
46 |
+
RUN useradd -m -u ${NB_UID} ${NB_USER}
|
47 |
+
|
48 |
+
# Switch to the "jovyan" user
|
49 |
+
USER ${NB_USER}
|
50 |
+
|
51 |
+
# Set home and path variables for the user
|
52 |
+
ENV HOME=/home/${NB_USER} \
|
53 |
+
PATH=/home/${NB_USER}/.local/bin:$PATH
|
54 |
+
|
55 |
+
# Set the working directory to the user's home directory
|
56 |
+
WORKDIR ${HOME}
|
57 |
+
|
58 |
+
# Upgrade pip and install Python dependencies
|
59 |
+
RUN python3.8 -m pip install --upgrade pip
|
60 |
+
COPY requirements.txt /tmp/requirements.txt
|
61 |
+
RUN python3.8 -m pip install -r /tmp/requirements.txt
|
62 |
+
|
63 |
+
# Copy the application code into the container at /home/jovyan
|
64 |
+
COPY --chown=${NB_USER}:${NB_USER} . ${HOME}
|
65 |
+
|
66 |
+
# Expose port for Streamlit
|
67 |
+
EXPOSE 7860
|
68 |
+
|
69 |
+
# Define the entry point for the container
|
70 |
+
ENTRYPOINT ["streamlit", "run", "Demo.py", "--server.port=7860", "--server.address=0.0.0.0"]
|
inputs/hebrewner_cc_300d/Example1.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
Example 1
|
2 |
+
והתוצאה : ספרו הפך לרב מכר ענק ובסיס לוויכוחים תיאולוגיים ודיונים נזעמים , כמו גם התקפות והאשמות כלפי בראון מחוגי הכנסייה כפי שמעולם לא התעוררו כתוצאה מספריהם של וואלאס או לאדלום , ואף גרם לסופר מצליח בזכות עצמו , דן בורסטין , לערוך את הספר " הסודות שמאחורי צופן דה וינצ'י " , שבו הוא בודק אחת לאחת את העובדות וההנחות שעליהן מסתמך בראון על ידי שפע של מאמרים , חלקם מקוריים וחלקם לקוחים מספרים , כתבי עת וראיונות עם חוקרים שונים .
|
inputs/hebrewner_cc_300d/Example2.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
Example 2
|
2 |
+
בגלל קוצר היריעה לא נתעסק כאן בכל הנושאים המגוונים שבהם דן הספר , כמו למשל דמותה של מרים המגדלית , הדעות האזוטריות של ליאונרדו דה וינצי וכן הלאה , אלא נתמקד בנושא אחד - באגודת הסתר " מסדר ציון " - מסדר חשאי הקיים כביכול מזה אלף שנה , ותפקידו להגן על צאצאי השושלת המ ֶרוב ּינגית הקדומה של צרפת , שהם למעשה צאצאי ישוע ומרים המגדלית , ולפיכך הם , לדעת חברי המסדר , השושלת המלכותית הלגיטימית של צרפת , מה שאומר כמובן שמלכי צרפת הם ממוצא יהודי .
|
inputs/hebrewner_cc_300d/Example3.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
Example 3
|
2 |
+
ב 32 באוקטובר התפעלה ממנו בעלת טור בעיתון " בוסטון גלוב " במלים היאות למעריצה בת 21 : " הוא עשה בחודשים אחדים למען צחות הדיבור מה שלקח לחברה שנים כדי לעשות למען טלוויזיה צבעונית ... אם דיבור היה ספורט אולימפי , הוא היה זוכה במדליית הזהב ... סילבר כה טוב , עד שהוא גורם לאנגלית להישמע כמו צרפתית ... אם ייבחר , תהיה לכולנו ההזדמנות ללמוד ממנו להיות סטודנטים בכיתתו הענקית , הנקראת מסצוסטס " .
|
inputs/hebrewner_cc_300d/Example4.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
Example 4
|
2 |
+
לא מינה ולא מקצתה ! הרי שם סיפרתי על ההגעה בקרונות החנק , על המתים שטואטאו מהקרונות , על " קומנדו קנדה " , על אנשי הס"ס וכלביהם האמתניים , על אלומות האור מנקרות העיניים ששלחו הזרקורים , על בכי ילדים שנקרעו מזרועות אמותיהם , ולעתים נשארו האמהות הצעירות בחיים , ואתה מותיר רק מלים בודדות על ה"סלקציה " .
|
inputs/hebrewner_cc_300d/Example5.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
Example 5
|
2 |
+
שוויד חושף את תמחורי המוצרים היציבים של החברה: " המחירים נותרו זהים : 70 דולר לאבטחת עסק קטן , 300 דולר לאבטחת רשת בעסק קטן , בין 1,500 ל - 3,500 דולר לאבטחת חברות גדולות עם אתר ראשי ועד 500 מחשבים , באמצעות מוצרי הצ'ק פוינט אקספרס , ובין 15,000 ל - 20,000 דולר לעסק עם 3 עד 4 אתרים , חברות גדולות עם מחזורי מכירות משמעותיים .
|
pages/Workflow & Model Overview.py
ADDED
@@ -0,0 +1,269 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
|
4 |
+
# Custom CSS for better styling
|
5 |
+
st.markdown("""
|
6 |
+
<style>
|
7 |
+
.main-title {
|
8 |
+
font-size: 36px;
|
9 |
+
color: #4A90E2;
|
10 |
+
font-weight: bold;
|
11 |
+
text-align: center;
|
12 |
+
}
|
13 |
+
.sub-title {
|
14 |
+
font-size: 24px;
|
15 |
+
color: #4A90E2;
|
16 |
+
margin-top: 20px;
|
17 |
+
}
|
18 |
+
.section {
|
19 |
+
background-color: #f9f9f9;
|
20 |
+
padding: 15px;
|
21 |
+
border-radius: 10px;
|
22 |
+
margin-top: 20px;
|
23 |
+
}
|
24 |
+
.section h2 {
|
25 |
+
font-size: 22px;
|
26 |
+
color: #4A90E2;
|
27 |
+
}
|
28 |
+
.section p, .section ul {
|
29 |
+
color: #666666;
|
30 |
+
}
|
31 |
+
.link {
|
32 |
+
color: #4A90E2;
|
33 |
+
text-decoration: none;
|
34 |
+
}
|
35 |
+
</style>
|
36 |
+
""", unsafe_allow_html=True)
|
37 |
+
|
38 |
+
# Main Title
|
39 |
+
st.markdown('<div class="main-title">Detect Entities in Urdu (urduvec_140M_300d embeddings)</div>', unsafe_allow_html=True)
|
40 |
+
|
41 |
+
# Introduction
|
42 |
+
st.markdown("""
|
43 |
+
<div class="section">
|
44 |
+
<p>Named Entity Recognition (NER) models identify and categorize important entities in a text. This page details a word embeddings-based NER model for Urdu texts, using the <code>urduvec_140M_300d</code> word embeddings. The model is pretrained and available for use with Spark NLP.</p>
|
45 |
+
</div>
|
46 |
+
""", unsafe_allow_html=True)
|
47 |
+
|
48 |
+
# Model Description
|
49 |
+
st.markdown('<div class="sub-title">Description</div>', unsafe_allow_html=True)
|
50 |
+
st.markdown("""
|
51 |
+
<div class="section">
|
52 |
+
<p>This model uses Urdu word embeddings to find 7 different types of entities in Urdu text. It is trained using <code>urduvec_140M_300d</code> word embeddings, so please use the same embeddings in the pipeline. It can identify the following types of entities:</p>
|
53 |
+
<ul>
|
54 |
+
<li>PER (Persons)</li>
|
55 |
+
<li>LOC (Locations)</li>
|
56 |
+
<li>ORG (Organizations)</li>
|
57 |
+
<li>DATE (Dates)</li>
|
58 |
+
<li>TIME (Times)</li>
|
59 |
+
<li>DESIGNATION (Designations)</li>
|
60 |
+
<li>NUMBER (Numbers)</li>
|
61 |
+
</ul>
|
62 |
+
</div>
|
63 |
+
""", unsafe_allow_html=True)
|
64 |
+
|
65 |
+
# Setup Instructions
|
66 |
+
st.markdown('<div class="sub-title">Setup</div>', unsafe_allow_html=True)
|
67 |
+
st.markdown('<p>To use the model, you need Spark NLP installed. You can install it using pip:</p>', unsafe_allow_html=True)
|
68 |
+
st.code("""
|
69 |
+
pip install spark-nlp
|
70 |
+
pip install pyspark
|
71 |
+
""", language="bash")
|
72 |
+
|
73 |
+
st.markdown("<p>Then, import Spark NLP and start a Spark session:</p>", unsafe_allow_html=True)
|
74 |
+
st.code("""
|
75 |
+
import sparknlp
|
76 |
+
|
77 |
+
# Start Spark Session
|
78 |
+
spark = sparknlp.start()
|
79 |
+
""", language='python')
|
80 |
+
|
81 |
+
# Example Usage
|
82 |
+
st.markdown('<div class="sub-title">Example Usage with Urdu NER Model</div>', unsafe_allow_html=True)
|
83 |
+
st.markdown("""
|
84 |
+
<div class="section">
|
85 |
+
<p>Below is an example of how to set up and use the <code>uner_mk_140M_300d</code> model for named entity recognition in Urdu:</p>
|
86 |
+
</div>
|
87 |
+
""", unsafe_allow_html=True)
|
88 |
+
st.code('''
|
89 |
+
from sparknlp.base import *
|
90 |
+
from sparknlp.annotator import *
|
91 |
+
from pyspark.ml import Pipeline
|
92 |
+
|
93 |
+
# Define the components of the pipeline
|
94 |
+
documentAssembler = DocumentAssembler() \\
|
95 |
+
.setInputCol("text") \\
|
96 |
+
.setOutputCol("document")
|
97 |
+
|
98 |
+
sentence_detector = SentenceDetector() \\
|
99 |
+
.setInputCols(["document"]) \\
|
100 |
+
.setOutputCol("sentence")
|
101 |
+
|
102 |
+
tokenizer = Tokenizer() \\
|
103 |
+
.setInputCols(["sentence"]) \\
|
104 |
+
.setOutputCol("token")
|
105 |
+
|
106 |
+
word_embeddings = WordEmbeddingsModel.pretrained("urduvec_140M_300d", "ur") \\
|
107 |
+
.setInputCols(["sentence", "token"]) \\
|
108 |
+
.setOutputCol("embeddings")
|
109 |
+
|
110 |
+
ner = NerDLModel.pretrained("uner_mk_140M_300d", "ur") \\
|
111 |
+
.setInputCols(["sentence", "token", "embeddings"]) \\
|
112 |
+
.setOutputCol("ner")
|
113 |
+
|
114 |
+
ner_converter = NerConverter().setInputCols(["sentence", "token", "ner"]).setOutputCol("ner_chunk")
|
115 |
+
|
116 |
+
# Create the pipeline
|
117 |
+
pipeline = Pipeline(stages=[documentAssembler, sentence_detector, tokenizer, word_embeddings, ner, ner_converter])
|
118 |
+
|
119 |
+
# Create sample data
|
120 |
+
example = """
|
121 |
+
بریگیڈیئر ایڈ بٹلر سنہ دوہزارچھ میں ہلمند کے فوجی کمانڈر تھے۔
|
122 |
+
"""
|
123 |
+
data = spark.createDataFrame([[example]]).toDF("text")
|
124 |
+
|
125 |
+
# Fit and transform data with the pipeline
|
126 |
+
result = pipeline.fit(data).transform(data)
|
127 |
+
|
128 |
+
# Select the result, entity
|
129 |
+
result.select(
|
130 |
+
expr("explode(ner_chunk) as ner_chunk")
|
131 |
+
).select(
|
132 |
+
col("ner_chunk.result").alias("chunk"),
|
133 |
+
col("ner_chunk.metadata").getItem("entity").alias("ner_label")
|
134 |
+
).show(truncate=False)
|
135 |
+
''', language="python")
|
136 |
+
|
137 |
+
import pandas as pd
|
138 |
+
|
139 |
+
# Create the data for the DataFrame
|
140 |
+
data = {
|
141 |
+
"chunk": [
|
142 |
+
"بریگیڈیئر",
|
143 |
+
"ایڈ بٹلر",
|
144 |
+
"سنہ دوہزارچھ",
|
145 |
+
"ہلمند"
|
146 |
+
],
|
147 |
+
"ner_label": [
|
148 |
+
"DESIGNATION",
|
149 |
+
"PERSON",
|
150 |
+
"DATE",
|
151 |
+
"LOCATION"
|
152 |
+
]
|
153 |
+
}
|
154 |
+
|
155 |
+
# Creating the DataFrame
|
156 |
+
df = pd.DataFrame(data)
|
157 |
+
df.index += 1
|
158 |
+
st.dataframe(df)
|
159 |
+
|
160 |
+
# Model Information
|
161 |
+
st.markdown('<div class="sub-title">Model Information</div>', unsafe_allow_html=True)
|
162 |
+
st.markdown("""
|
163 |
+
<div class="section">
|
164 |
+
<p>The <code>uner_mk_140M_300d</code> model details are as follows:</p>
|
165 |
+
<ul>
|
166 |
+
<li><strong>Model Name:</strong> uner_mk_140M_300d</li>
|
167 |
+
<li><strong>Type:</strong> ner</li>
|
168 |
+
<li><strong>Compatibility:</strong> Spark NLP 4.0.2+</li>
|
169 |
+
<li><strong>License:</strong> Open Source</li>
|
170 |
+
<li><strong>Edition:</strong> Official</li>
|
171 |
+
<li><strong>Input Labels:</strong> [document, token, word_embeddings]</li>
|
172 |
+
<li><strong>Output Labels:</strong> [ner]</li>
|
173 |
+
<li><strong>Language:</strong> ur</li>
|
174 |
+
<li><strong>Size:</strong> 14.8 MB</li>
|
175 |
+
</ul>
|
176 |
+
</div>
|
177 |
+
""", unsafe_allow_html=True)
|
178 |
+
|
179 |
+
# Benchmark Section
|
180 |
+
st.markdown('<div class="sub-title">Benchmark</div>', unsafe_allow_html=True)
|
181 |
+
st.markdown("""
|
182 |
+
<div class="section">
|
183 |
+
<p>Evaluating the performance of NER models is crucial to understanding their effectiveness in real-world applications. Below are the benchmark results for the <code>uner_mk_140M_300d</code> model, focusing on various named entity categories. The metrics used include precision, recall, and F1-score, which are standard for evaluating classification models.</p>
|
184 |
+
</div>
|
185 |
+
""", unsafe_allow_html=True)
|
186 |
+
st.markdown("""
|
187 |
+
---
|
188 |
+
| Label | TP | FP | FN | Precision | Recall | F1-Score |
|
189 |
+
|------------------|-------|-----|-----|-----------|---------|----------|
|
190 |
+
| I-TIME | 12 | 10 | 1 | 0.545455 | 0.923077| 0.685714 |
|
191 |
+
| B-PERSON | 2808 | 846 | 535 | 0.768473 | 0.839964| 0.802630 |
|
192 |
+
| B-DATE | 34 | 6 | 6 | 0.850000 | 0.850000| 0.850000 |
|
193 |
+
| I-DATE | 45 | 1 | 2 | 0.978261 | 0.957447| 0.967742 |
|
194 |
+
| B-DESIGNATION | 49 | 30 | 16 | 0.620253 | 0.753846| 0.680556 |
|
195 |
+
| I-LOCATION | 2110 | 750 | 701 | 0.737762 | 0.750623| 0.744137 |
|
196 |
+
| B-TIME | 11 | 9 | 3 | 0.550000 | 0.785714| 0.647059 |
|
197 |
+
| I-ORGANIZATION | 2006 | 772 | 760 | 0.722102 | 0.725235| 0.723665 |
|
198 |
+
| I-NUMBER | 18 | 6 | 2 | 0.750000 | 0.900000| 0.818182 |
|
199 |
+
| B-LOCATION | 5428 | 1255| 582 | 0.812210 | 0.903161| 0.855275 |
|
200 |
+
| B-NUMBER | 194 | 36 | 27 | 0.843478 | 0.877828| 0.860298 |
|
201 |
+
| B-ORGANIZATION | 4364 | 1092| 990 | 0.799926 | 0.815058| 0.807421 |
|
202 |
+
| I-DESIGNATION | 57 | 15 | 10 | 0.791667 | 0.850746| 0.820896 |
|
203 |
+
| B-MISC | 18 | 19 | 13 | 0.486486 | 0.580645| 0.529412 |
|
204 |
+
| I-MISC | 10 | 11 | 10 | 0.476190 | 0.500000| 0.487805 |
|
205 |
+
| I-PERSON | 1891 | 689 | 622 | 0.732723 | 0.752499| 0.742486 |
|
206 |
+
---
|
207 |
+
""", unsafe_allow_html=True)
|
208 |
+
|
209 |
+
st.markdown("""
|
210 |
+
<div class="section">
|
211 |
+
<p>These results demonstrate the model's ability to accurately identify and classify named entities in Urdu text. Precision measures the accuracy of the positive predictions, recall measures the model's ability to find all relevant instances, and F1-score provides a balance between precision and recall.</p>
|
212 |
+
</div>
|
213 |
+
""", unsafe_allow_html=True)
|
214 |
+
|
215 |
+
# Try the Model
|
216 |
+
st.markdown('<div class="sub-title">Try the Model</div>', unsafe_allow_html=True)
|
217 |
+
st.markdown("""
|
218 |
+
<div class="section">
|
219 |
+
<p>You can use the <code>LightPipeline</code> to quickly test the model on small texts. Here is an example:</p>
|
220 |
+
</div>
|
221 |
+
""", unsafe_allow_html=True)
|
222 |
+
st.code('''
|
223 |
+
from sparknlp.base import LightPipeline
|
224 |
+
|
225 |
+
# Create a LightPipeline
|
226 |
+
light_pipeline = LightPipeline(pipeline.fit(data))
|
227 |
+
|
228 |
+
# Annotate a simple text
|
229 |
+
example_text = "بریگیڈیئر ایڈ بٹلر سنہ دوہزارچھ میں ہلمند کے فوجی کمانڈر تھے۔"
|
230 |
+
annotations = light_pipeline.fullAnnotate(example_text)
|
231 |
+
|
232 |
+
# Display the annotations
|
233 |
+
for annotation in annotations[0]['ner_chunk']:
|
234 |
+
print(annotation.result, "->", annotation.metadata['entity'])
|
235 |
+
''', language="python")
|
236 |
+
|
237 |
+
# Conclusion/Summary
|
238 |
+
st.markdown('<div class="sub-title">Conclusion</div>', unsafe_allow_html=True)
|
239 |
+
st.markdown("""
|
240 |
+
<div class="section">
|
241 |
+
<p>The <code>uner_mk_140M_300d</code> model demonstrates effective named entity recognition in Urdu texts, with strong performance metrics across various entity types. This model leverages <code>urduvec_140M_300d</code> embeddings to enhance its understanding and accuracy in identifying entities like persons, locations, organizations, and more. Its integration into Spark NLP allows for efficient and scalable processing of Urdu text data, making it a valuable tool for researchers and developers working with Urdu language applications.</p>
|
242 |
+
</div>
|
243 |
+
""", unsafe_allow_html=True)
|
244 |
+
|
245 |
+
# References
|
246 |
+
st.markdown('<div class="sub-title">References</div>', unsafe_allow_html=True)
|
247 |
+
st.markdown("""
|
248 |
+
<div class="section">
|
249 |
+
<ul>
|
250 |
+
<li><a class="link" href="https://sparknlp.org/api/python/reference/autosummary/sparknlp/annotator/ner/ner_dl/index.html" target="_blank" rel="noopener">NerDLModel</a> annotator documentation</li>
|
251 |
+
<li>Model Used: <a class="link" href="https://sparknlp.org/2022/08/09/uner_mk_140M_300d_ur_3_0.html" rel="noopener">uner_mk_140M_300d_ur_3_0</a></li>
|
252 |
+
<li><a class="link" href="https://www.cs.bgu.ac.il/~elhadad/nlpproj/naama/" target="_blank" rel="noopener">Data Source</a></li>
|
253 |
+
<li><a class="link" href="https://nlp.johnsnowlabs.com/recognize_entitie" target="_blank" rel="noopener">Visualization demos for NER in Spark NLP</a></li>
|
254 |
+
<li><a class="link" href="https://www.johnsnowlabs.com/named-entity-recognition-ner-with-bert-in-spark-nlp/">Named Entity Recognition (NER) with BERT in Spark NLP</a></li>
|
255 |
+
</ul>
|
256 |
+
</div>
|
257 |
+
""", unsafe_allow_html=True)
|
258 |
+
|
259 |
+
# Community & Support
|
260 |
+
st.markdown('<div class="sub-title">Community & Support</div>', unsafe_allow_html=True)
|
261 |
+
st.markdown("""
|
262 |
+
<div class="section">
|
263 |
+
<ul>
|
264 |
+
<li><a class="link" href="https://sparknlp.org/" target="_blank">Official Website</a>: Documentation and examples</li>
|
265 |
+
<li><a class="link" href="https://github.com/JohnSnowLabs/spark-nlp" target="_blank">GitHub Repository</a>: Report issues or contribute</li>
|
266 |
+
<li><a class="link" href="https://forum.johnsnowlabs.com/" target="_blank">Community Forum</a>: Ask questions, share ideas, and get support</li>
|
267 |
+
</ul>
|
268 |
+
</div>
|
269 |
+
""", unsafe_allow_html=True)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
st-annotated-text
|
3 |
+
pandas
|
4 |
+
numpy
|
5 |
+
spark-nlp
|
6 |
+
pyspark
|