Spaces:
Sleeping
Sleeping
File size: 12,641 Bytes
adb4d87 |
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
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) |