Spaces:
Sleeping
Sleeping
File size: 13,364 Bytes
1d22e44 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
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)
|