Spaces:
Sleeping
Sleeping
import streamlit as st | |
# 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; | |
} | |
.benchmark-table { | |
width: 100%; | |
border-collapse: collapse; | |
margin-top: 20px; | |
} | |
.benchmark-table th, .benchmark-table td { | |
border: 1px solid #ddd; | |
padding: 8px; | |
text-align: left; | |
} | |
.benchmark-table th { | |
background-color: #4A90E2; | |
color: white; | |
} | |
.benchmark-table td { | |
background-color: #f2f2f2; | |
} | |
</style> | |
""", unsafe_allow_html=True) | |
# Main Title | |
st.markdown('<div class="main-title">Detect Entities in Twitter Texts</div>', unsafe_allow_html=True) | |
# Description | |
st.markdown(""" | |
<div class="section"> | |
<p><strong>Detect Entities in Twitter Texts</strong> is a specialized NLP task focusing on identifying entities within Twitter-based texts. This app utilizes the <strong>bert_token_classifier_ner_btc</strong> model, which is trained on the Broad Twitter Corpus (BTC) dataset to detect entities with high accuracy. The model is based on BERT base-cased embeddings, which are integrated into the model, eliminating the need for separate embeddings in the NLP pipeline.</p> | |
</div> | |
""", unsafe_allow_html=True) | |
# What is Entity Recognition | |
st.markdown('<div class="sub-title">What is Entity Recognition?</div>', unsafe_allow_html=True) | |
st.markdown(""" | |
<div class="section"> | |
<p><strong>Entity Recognition</strong> is a task in Natural Language Processing (NLP) that involves identifying and classifying named entities in text into predefined categories. For Twitter texts, this model focuses on detecting entities such as people, locations, and organizations, which are crucial for understanding and analyzing social media content.</p> | |
</div> | |
""", unsafe_allow_html=True) | |
# Model Importance and Applications | |
st.markdown('<div class="sub-title">Model Importance and Applications</div>', unsafe_allow_html=True) | |
st.markdown(""" | |
<div class="section"> | |
<p>The <strong>bert_token_classifier_ner_btc</strong> model is highly effective for extracting named entities from Twitter texts. Its applications include:</p> | |
<ul> | |
<li><strong>Social Media Monitoring:</strong> The model can be used to identify and track mentions of people, organizations, and locations in social media posts, which is valuable for sentiment analysis and brand monitoring.</li> | |
<li><strong>Event Detection:</strong> By recognizing key entities, the model helps in detecting and summarizing events discussed on Twitter, such as breaking news or trending topics.</li> | |
<li><strong>Market Research:</strong> Companies can use the model to analyze customer opinions and identify trends related to their products or services based on entity mentions.</li> | |
<li><strong>Content Classification:</strong> The model aids in categorizing Twitter content based on the detected entities, which can be useful for organizing and filtering large volumes of social media data.</li> | |
</ul> | |
<p>Why use the <strong>bert_token_classifier_ner_btc</strong> model?</p> | |
<ul> | |
<li><strong>Pre-trained on BTC Dataset:</strong> The model is specifically trained on Twitter data, making it well-suited for handling social media text.</li> | |
<li><strong>Integrated BERT Embeddings:</strong> It uses BERT base-cased embeddings, providing strong performance without needing additional embedding components.</li> | |
<li><strong>High Accuracy:</strong> The model achieves impressive precision and recall, ensuring reliable entity detection.</li> | |
<li><strong>Ease of Use:</strong> Simplifies the process of entity recognition with minimal setup required.</li> | |
</ul> | |
</div> | |
""", unsafe_allow_html=True) | |
# Predicted Entities | |
st.markdown('<div class="sub-title">Predicted Entities</div>', unsafe_allow_html=True) | |
st.markdown(""" | |
<div class="section"> | |
<ul> | |
<li><strong>PER:</strong> Person's name.</li> | |
<li><strong>LOC:</strong> Location or place.</li> | |
<li><strong>ORG:</strong> Organization or company name.</li> | |
</ul> | |
</div> | |
""", unsafe_allow_html=True) | |
# How to Use the Model | |
st.markdown('<div class="sub-title">How to Use the Model</div>', unsafe_allow_html=True) | |
st.markdown(""" | |
<div class="section"> | |
<p>To use this model in Python, follow these steps:</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 | |
import pandas as pd | |
# Define the components of the pipeline | |
document_assembler = DocumentAssembler() \\ | |
.setInputCol("text") \\ | |
.setOutputCol("document") | |
tokenizer = Tokenizer() \\ | |
.setInputCols(["document"]) \\ | |
.setOutputCol("token") | |
tokenClassifier = BertForTokenClassification.pretrained("bert_token_classifier_ner_btc", "en") \\ | |
.setInputCols("token", "document") \\ | |
.setOutputCol("ner") \\ | |
.setCaseSensitive(True) | |
ner_converter = NerConverter() \\ | |
.setInputCols(["document", "token", "ner"]) \\ | |
.setOutputCol("ner_chunk") | |
# Create the pipeline | |
pipeline = Pipeline(stages=[ | |
document_assembler, | |
tokenizer, | |
tokenClassifier, | |
ner_converter | |
]) | |
# Create some example data | |
test_sentences = ["Pentagram's Dominic Lippa is working on a new identity for University of Arts London."] | |
data = spark.createDataFrame(pd.DataFrame({'text': test_sentences})) | |
# Apply the pipeline to the data | |
model = pipeline.fit(spark.createDataFrame(pd.DataFrame({'text': ['']}))) | |
result = model.transform(data) | |
# Display results | |
result.select( | |
expr("explode(ner_chunk) as ner_chunk") | |
).select( | |
col("ner_chunk.result").alias("chunk"), | |
col("ner_chunk.metadata.entity").alias("ner_label") | |
).show(truncate=False) | |
''', language='python') | |
# Results | |
st.text(""" | |
+-------------------------+---------+ | |
|chunk |ner_label| | |
+-------------------------+---------+ | |
|Pentagram's |ORG | | |
|Dominic Lippa |PER | | |
|University of Arts London|ORG | | |
+-------------------------+---------+ | |
""") | |
# Model Information | |
st.markdown('<div class="sub-title">Model Information</div>', unsafe_allow_html=True) | |
st.markdown(""" | |
<div class="section"> | |
<table class="benchmark-table"> | |
<tr> | |
<th>Model Name</th> | |
<td>bert_token_classifier_ner_btc</td> | |
</tr> | |
<tr> | |
<th>Compatibility</th> | |
<td>Spark NLP 3.2.2+</td> | |
</tr> | |
<tr> | |
<th>License</th> | |
<td>Open Source</td> | |
</tr> | |
<tr> | |
<th>Edition</th> | |
<td>Official</td> | |
</tr> | |
<tr> | |
<th>Input Labels</th> | |
<td>[sentence, token]</td> | |
</tr> | |
<tr> | |
<th>Output Labels</th> | |
<td>[ner]</td> | |
</tr> | |
<tr> | |
<th>Language</th> | |
<td>en</td> | |
</tr> | |
<tr> | |
<th>Case Sensitive</th> | |
<td>true</td> | |
</tr> | |
<tr> | |
<th>Max Sentence Length</th> | |
<td>128</td> | |
</tr> | |
</table> | |
</div> | |
""", unsafe_allow_html=True) | |
# Data Source | |
st.markdown('<div class="sub-title">Data Source</div>', unsafe_allow_html=True) | |
st.markdown(""" | |
<div class="section"> | |
<p>For more information about the dataset used to train this model, visit the <a class="link" href="https://github.com/juand-r/entity-recognition-datasets/tree/master/data/BTC" target="_blank">Broad Twitter Corpus (BTC)</a>.</p> | |
</div> | |
""", unsafe_allow_html=True) | |
# Benchmark | |
st.markdown('<div class="sub-title">Benchmarking</div>', unsafe_allow_html=True) | |
st.markdown(""" | |
<div class="section"> | |
<p>The <strong>bert_token_classifier_ner_btc</strong> model has been evaluated on various benchmarks, including the following metrics:</p> | |
<table class="benchmark-table"> | |
<tr> | |
<th>Label</th> | |
<th>Precision</th> | |
<th>Recall</th> | |
<th>F1 Score</th> | |
<th>Support</th> | |
</tr> | |
<tr> | |
<td>PER</td> | |
<td>0.93</td> | |
<td>0.92</td> | |
<td>0.92</td> | |
<td>1200</td> | |
</tr> | |
<tr> | |
<td>LOC</td> | |
<td>0.90</td> | |
<td>0.89</td> | |
<td>0.89</td> | |
<td>800</td> | |
</tr> | |
<tr> | |
<td>ORG</td> | |
<td>0.94</td> | |
<td>0.93</td> | |
<td>0.93</td> | |
<td>1000</td> | |
</tr> | |
<tr> | |
<td>Average</td> | |
<td>0.92</td> | |
<td>0.91</td> | |
<td>0.91</td> | |
<td>3000</td> | |
</tr> | |
</table> | |
</div> | |
""", unsafe_allow_html=True) | |
# Conclusion | |
st.markdown('<div class="sub-title">Conclusion</div>', unsafe_allow_html=True) | |
st.markdown(""" | |
<div class="section"> | |
<p>The <strong>bert_token_classifier_ner_btc</strong> model offers a powerful and effective solution for detecting entities in Twitter texts. Its training on the Broad Twitter Corpus (BTC) ensures that it is well-adapted to handle the unique characteristics of social media language.</p> | |
<p>With high accuracy in identifying people, locations, and organizations, this model is invaluable for applications ranging from social media monitoring to market research and event detection. Its integration of BERT base-cased embeddings allows for robust entity recognition with minimal setup required.</p> | |
<p>For anyone looking to enhance their social media analysis capabilities or improve their NLP workflows, leveraging this model can significantly streamline the process of extracting and classifying named entities from Twitter content.</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/com/johnsnowlabs/nlp/annotators/classifier/dl/BertForTokenClassification.html" target="_blank" rel="noopener">BertForTokenClassification</a> annotator documentation</li> | |
<li>Model Used: <a class="link" href="https://sparknlp.org/2021/09/09/bert_token_classifier_ner_btc_en.html" rel="noopener">bert_token_classifier_ner_btc_en</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://join.slack.com/t/spark-nlp/shared_invite/zt-198dipu77-L3UWNe_AJ8xqDk0ivmih5Q" target="_blank">Slack</a>: Live discussion with the community and team</li> | |
<li><a class="link" href="https://github.com/JohnSnowLabs/spark-nlp" target="_blank">GitHub</a>: Bug reports, feature requests, and contributions</li> | |
<li><a class="link" href="https://medium.com/spark-nlp" target="_blank">Medium</a>: Spark NLP articles</li> | |
<li><a class="link" href="https://www.youtube.com/channel/UCmFOjlpYEhxf_wJUDuz6xxQ/videos" target="_blank">YouTube</a>: Video tutorials</li> | |
</ul> | |
</div> | |
""", unsafe_allow_html=True) |