Bengali-NER / pages /Workflow & Model Overview.py
abdullahmubeen10's picture
Upload 5 files
1d22e44 verified
import streamlit as st
import pandas as pd
# Custom CSS for better styling
st.markdown("""
<style>
.main-title {
font-size: 36px;
color: #4A90E2;
font-weight: bold;
text-align: center;
}
.sub-title {
font-size: 24px;
color: #4A90E2;
margin-top: 20px;
}
.section {
background-color: #f9f9f9;
padding: 15px;
border-radius: 10px;
margin-top: 20px;
}
.section h2 {
font-size: 22px;
color: #4A90E2;
}
.section p, .section ul {
color: #666666;
}
.link {
color: #4A90E2;
text-decoration: none;
}
</style>
""", unsafe_allow_html=True)
# Main Title
st.markdown('<div class="main-title">Named Entity Recognition (NER) Model in Bengali (bengaliner_cc_300d)</div>', unsafe_allow_html=True)
# Introduction
st.markdown("""
<div class="section">
<p>Named Entity Recognition (NER) is the task of identifying important words in a text and associating them with a category. In this post, we introduce an NER model designed for Bengali language texts. This model can detect four different types of entities in Indian text, leveraging the power of Spark NLP.</p>
</div>
""", unsafe_allow_html=True)
# Model Description
st.markdown('<div class="sub-title">Description</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>The <code>bengaliner_cc_300d</code> model is designed to detect four different types of entities in Bengali text. The predicted entities include:</p>
<ul>
<li>PER (Person)</li>
<li>ORG (Organization)</li>
<li>LOC (Location)</li>
<li>TIME (Time)</li>
</ul>
</div>
""", unsafe_allow_html=True)
# Setup Instructions
st.markdown('<div class="sub-title">Setup</div>', unsafe_allow_html=True)
st.markdown('<p>To install Spark NLP in Python, use your favorite package manager (conda, pip, etc.). For example:</p>', unsafe_allow_html=True)
st.code("""
pip install spark-nlp
pip install pyspark
""", language="bash")
st.markdown("<p>Then, import Spark NLP and start a Spark session:</p>", unsafe_allow_html=True)
st.code("""
import sparknlp
# Start Spark Session
spark = sparknlp.start()
""", language='python')
# Example Usage
st.markdown('<div class="sub-title">Example Usage with Bengali NER Model</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>Below is an example of how to set up and use the <code>bengaliner_cc_300d</code> model for named entity recognition in Bengali:</p>
</div>
""", unsafe_allow_html=True)
st.code('''
from sparknlp.base import *
from sparknlp.annotator import *
from pyspark.ml import Pipeline
from pyspark.sql.functions import col, expr, round, concat, lit
# Document Assembler
document_assembler = DocumentAssembler()\\
.setInputCol("text")\\
.setOutputCol("document")
# Tokenizer
tokenizer = Tokenizer()\\
.setInputCols(["document"])\\
.setOutputCol("token")
# Word Embeddings
embeddings = WordEmbeddingsModel.pretrained("bengali_cc_300d", "bn")\\
.setInputCols(["document", "token"])\\
.setOutputCol("embeddings")
# NER Model
ner = NerDLModel.pretrained("bengaliner_cc_300d", "bn")\\
.setInputCols(["document", "token", "embeddings"])\\
.setOutputCol("ner")
# NER Converter
ner_converter = NerConverter()\\
.setInputCols(["document", "token", "ner"])\\
.setOutputCol("ner_chunk")
# Pipeline
pipeline = Pipeline(stages=[document_assembler, tokenizer, embeddings, ner, ner_converter])
# Example sentence
example = spark.createDataFrame([["উইলিয়াম হেনরি গেটস III (জন্ম অক্টোবর ২৮, ১৯৫৫) একজন আমেরিকান ব্যবসায়ী, সফটওয়্যার ডেভেলপার, বিনিয়োগকারী এবং দাতা। তিনি মাইক্রোসফট কর্পোরেশনের সহ-প্রতিষ্ঠাতা হিসেবে সর্বাধিক পরিচিত। মাইক্রোসফটে তাঁর কর্মজীবনে, গেটস চেয়ারম্যান, প্রধান নির্বাহী কর্মকর্তা (CEO), প্রেসিডেন্ট এবং প্রধান সফটওয়্যার স্থপতি হিসেবে দায়িত্ব পালন করেছেন, পাশাপাশি ২০১৪ সালের মে পর্যন্ত সর্বাধিক শেয়ারহোল্ডার ছিলেন। তিনি ১৯৭০ এবং ১৯৮০ এর দশকের মাইক্রোকম্পিউটার বিপ্লবের অন্যতম পরিচিত উদ্যোক্তা এবং অগ্রদূত। সিয়াটল, ওয়াশিংটনে জন্মগ্রহণ ও বড় হওয়া গেটস, ১৯৭৫ সালে শৈশবের বন্ধু পল অ্যালেনের সাথে আলবুকার্কি, নিউ মেক্সিকোতে মাইক্রোসফটের সহ-প্রতিষ্ঠা করেন; এটি বিশ্বের বৃহত্তম ব্যক্তিগত কম্পিউটার সফটওয়্যার কোম্পানিতে পরিণত হয়। গেটস ২০০০ সালের জানুয়ারি পর্যন্ত চেয়ারম্যান ও CEO হিসেবে কোম্পানিকে নেতৃত্ব দেন, কিন্তু তিনি চেয়ারম্যান হিসাবে রয়ে যান এবং প্রধান সফটওয়্যার স্থপতি হন। ১৯৯০-এর দশকের শেষের দিকে, গেটসের ব্যবসায়িক কৌশলগুলোর জন্য সমালোচিত হয়েছিলেন, যা প্রতিযোগিতাহীন বলে বিবেচিত হয়েছে। এই মতামতটি বিভিন্ন আদালতের রায় দ্বারা সমর্থিত হয়েছে। ২০০৬ সালের জুনে, গেটস ঘোষণা করেছিলেন যে তিনি মাইক্রোসফটে আংশিক সময়ের ভূমিকা এবং বিল ও মেলিন্ডা গেটস ফাউন্ডেশনে পূর্ণকালীন কাজ করবেন, যা তিনি এবং তাঁর স্ত্রী মেলিন্ডা গেটস ২০০০ সালে প্রতিষ্ঠা করেছিলেন। তিনি ধীরে ধীরে তার দায়িত্বগুলি রে ওজি এবং ক্রেইগ মন্ডিকে হস্তান্তর করেন। তিনি ২০১৪ সালের ফেব্রুয়ারিতে মাইক্রোসফটের চেয়ারম্যান পদ থেকে সরে যান এবং নতুন নিয়োগপ্রাপ্ত CEO সত্য নাদেলার সমর্থনে প্রযুক্তি উপদেষ্টা হিসাবে নতুন পদ গ্রহণ করেন।"]], ["text"])
# Transforming data
result = pipeline.fit(example).transform(example)
# Select the result, entity, and confidence columns
result.select(
expr("explode(ner_chunk) as ner_chunk")
).select(
col("ner_chunk.result").alias("chunk"),
col("ner_chunk.metadata").getItem("entity").alias("ner_label")
).show(truncate=False)
''', language="python")
data = {
"Chunk": ["হেনরি গেটস", "সালের মে", "সিয়াটল", "সালে", "পল অ্যালেনের", "আলবুকার্কি", "নিউ মেক্সিকোতে", "সালের", "১৯৯০-এর", "সালের জুনে", "সালে", "ক্রেইগ মন্ডিকে", "সালের ফেব্রুয়ারিতে"],
"NER Label": ["PER", "TIME", "LOC", "TIME", "PER", "ORG", "LOC", "TIME", "TIME", "TIME", "TIME", "PER", "TIME"]
}
st.dataframe(pd.DataFrame(data))
# Model Information
st.markdown('<div class="sub-title">Model Information</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>The <code>bengaliner_cc_300d</code> model details are as follows:</p>
<ul>
<li><strong>Model Name:</strong> bengaliner_cc_300d</li>
<li><strong>Type:</strong> ner</li>
<li><strong>Compatibility:</strong> Spark NLP 2.7.3+</li>
<li><strong>License:</strong> Open Source</li>
<li><strong>Edition:</strong> Official</li>
<li><strong>Input Labels:</strong> [document, token, word_embeddings]</li>
<li><strong>Output Labels:</strong> [ner]</li>
<li><strong>Language:</strong> bn</li>
<li><strong>Data Source:</strong> This model is trained on data obtained from <a class="link" href="https://ieeexplore.ieee.org/document/8944804" target="_blank" rel="noopener">this paper</a></li>
</ul>
</div>
""", unsafe_allow_html=True)
# Benchmark Section
st.markdown('<div class="sub-title">Benchmark</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<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>bengaliner_cc_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>
</div>
""", unsafe_allow_html=True)
st.markdown("""
---
| Label | TP | FP | FN | Precision | Recall | F1-Score |
|--------|------|------|------|-----------|--------|----------|
| I-TIME | 167 | 37 | 25 | 0.8186 | 0.8698 | 0.8434 |
| B-LOC | 678 | 114 | 195 | 0.8561 | 0.7766 | 0.8144 |
| I-ORG | 287 | 104 | 143 | 0.7340 | 0.6674 | 0.6991 |
| B-TIME | 414 | 54 | 123 | 0.8846 | 0.7710 | 0.8239 |
| I-LOC | 98 | 50 | 76 | 0.6622 | 0.5632 | 0.6087 |
| I-PER | 805 | 38 | 55 | 0.9549 | 0.9360 | 0.9454 |
| B-ORG | 446 | 108 | 225 | 0.8051 | 0.6647 | 0.7282 |
| B-PER | 764 | 48 | 183 | 0.9409 | 0.8068 | 0.8687 |
| Macro-average | - | - | - | 0.8320 | 0.7569 | 0.7927 |
| Micro-average | - | - | - | 0.8687 | 0.7812 | 0.8226 |
---
""", unsafe_allow_html=True)
# Summary
st.markdown('<div class="sub-title">Summary</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>In this article, we discussed named entity recognition using the <code>bengaliner_cc_300d</code> model for Bengali texts. We introduced how to perform NER with Spark NLP, set up the environment, and use the model. We also provided a detailed benchmark for the model's performance, demonstrating its effectiveness across various named entity categories.</p>
<p>For more information, refer to the <a class="link" href="https://sparknlp.org/2021/02/10/bengaliner_cc_300d_bn.html" target="_blank" rel="noopener">official documentation</a>.</p>
</div>
""", unsafe_allow_html=True)
# References
st.markdown('<div class="sub-title">References</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<ul>
<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>
<li>Model Used: <a class="link" href="https://sparknlp.org/2021/02/10/bengaliner_cc_300d_bn.html" target="_blank" rel="noopener">bengaliner_cc_300d_bn</a></li>
<li><a class="link" href="https://nlp.johnsnowlabs.com/recognize_entitie" target="_blank" rel="noopener">Visualization demos for NER in Spark NLP</a></li>
<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>
</ul>
</div>
""", unsafe_allow_html=True)
# Community & Support
st.markdown('<div class="sub-title">Community & Support</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<ul>
<li><a class="link" href="https://sparknlp.org/" target="_blank">Official Website</a>: Documentation and examples</li>
<li><a class="link" href="https://github.com/JohnSnowLabs/spark-nlp" target="_blank">GitHub Repository</a>: Report issues or contribute</li>
<li><a class="link" href="https://forum.johnsnowlabs.com/" target="_blank">Community Forum</a>: Ask questions, share ideas, and get support</li>
</ul>
</div>
""", unsafe_allow_html=True)