File size: 38,643 Bytes
2b9ab5b |
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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 |
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)
# Introduction to BERT Annotators in Spark NLP
st.markdown('<div class="main-title">Introduction to BERT Annotators in Spark NLP</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>Spark NLP provides a range of BERT-based annotators that leverage the power of Bidirectional Encoder Representations from Transformers (BERT) for various natural language processing tasks. These annotators are designed to deliver high performance and scalability in production environments. Below, we provide a detailed overview of four key BERT-based annotators available in Spark NLP:</p>
</div>
""", unsafe_allow_html=True)
st.write("")
tab1, tab2, tab3, tab4 = st.tabs(["BERT for Token Classification", "BERT for Zero-Shot Classification", "BERT for Sequence Classification", "BERT for Question Answering"])
with tab1:
st.markdown("""
<div class="section">
<h2>BERT for Token Classification</h2>
<p>The <strong>BertForTokenClassification</strong> annotator is fine-tuned for Named Entity Recognition (NER) tasks. Token classification involves labeling tokens, which are the smallest units of meaning in a text, with tags that represent specific entities. This process is crucial for understanding and extracting valuable information from text data. By identifying entities like names of people, organizations, locations, and more, token classification enables a wide range of applications, including:</p>
<ul>
<li><strong>Information Extraction:</strong> Automatically pulling out important information from large volumes of text.</li>
<li><strong>Document Categorization:</strong> Enhancing the organization and retrieval of documents based on identified entities.</li>
<li><strong>Improved Search Engine Relevancy:</strong> Enabling more accurate and context-aware search results.</li>
</ul>
<p>This annotator is highly effective for applications requiring precise entity recognition, ensuring that the identified entities are accurate and contextually relevant.</p>
<table class="benchmark-table">
<tr>
<th>Entity</th>
<th>Label</th>
</tr>
<tr>
<td>Apple</td>
<td>ORGANIZATION</td>
</tr>
<tr>
<td>Steve Jobs</td>
<td>PERSON</td>
</tr>
<tr>
<td>California</td>
<td>LOCATION</td>
</tr>
</table>
</div>
""", unsafe_allow_html=True)
# BERT Token Classification - NER CoNLL
st.markdown('<div class="sub-title">BERT Token Classification - NER CoNLL</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>The <strong>bert_base_token_classifier_conll03</strong> is a fine-tuned BERT model ready to use for Named Entity Recognition (NER) tasks. This model recognizes four types of entities: location (LOC), organizations (ORG), person (PER), and Miscellaneous (MISC).</p>
</div>
""", unsafe_allow_html=True)
# How to Use the Model - Token Classification
st.markdown('<div class="sub-title">How to Use the Model</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
document_assembler = DocumentAssembler() \\
.setInputCol('text') \\
.setOutputCol('document')
sentence_detector = SentenceDetector() \\
.setInputCols(['document']) \\
.setOutputCol('sentence')
tokenizer = Tokenizer() \\
.setInputCols(['sentence']) \\
.setOutputCol('token')
tokenClassifier = BertForTokenClassification \\
.pretrained('bert_base_token_classifier_conll03', 'en') \\
.setInputCols(['token', 'sentence']) \\
.setOutputCol('ner') \\
.setCaseSensitive(True) \\
.setMaxSentenceLength(512)
ner_converter = NerConverter() \\
.setInputCols(['sentence', 'token', 'ner']) \\
.setOutputCol('entities')
pipeline = Pipeline(stages=[
document_assembler,
sentence_detector,
tokenizer,
tokenClassifier,
ner_converter
])
example = spark.createDataFrame([["""Apple Inc. is planning to open a new headquarters in Cupertino, California. The CEO, Tim Cook, announced this during the company's annual event on March 25th, 2023. Barack Obama, the 44th President of the United States, was born on August 4th, 1961, in Honolulu, Hawaii. He attended Harvard Law School and later became a community organizer in Chicago. Amazon reported a net revenue of $125.6 billion in Q4 of 2022, an increase of 9% compared to the previous year. Jeff Bezos, the founder of Amazon, mentioned that the company's growth in cloud computing has significantly contributed to this rise. Paris, the capital city of France, is renowned for its art, fashion, and culture. Key attractions include the Eiffel Tower, the Louvre Museum, and the Notre-Dame Cathedral. Visitors often enjoy a stroll along the Seine River and dining at local bistros. The study, conducted at the Mayo Clinic in Rochester, Minnesota, examined the effects of a new drug on patients with Type 2 diabetes. Results showed a significant reduction in blood sugar levels over a 12-month period. Serena Williams won her 24th Grand Slam title at the Wimbledon Championships in London, England. She defeated Naomi Osaka in a thrilling final match on July 13th, 2023. Google's latest smartphone, the Pixel 6, was unveiled at an event in New York City. Sundar Pichai, the CEO of Google, highlighted the phone's advanced AI capabilities and improved camera features. The Declaration of Independence was signed on July 4th, 1776, in Philadelphia, Pennsylvania. Thomas Jefferson, Benjamin Franklin, and John Adams were among the key figures who drafted this historic document."""]]).toDF("text")
result = pipeline.fit(example).transform(example)
result.select(
expr("explode(entities) as ner_chunk")
).select(
col("ner_chunk.result").alias("chunk"),
col("ner_chunk.metadata.entity").alias("ner_label")
).show(truncate=False)
''', language='python')
st.text("""
+--------------------+---------+
|chunk |ner_label|
+--------------------+---------+
|Apple Inc. |ORG |
|Cupertino |LOC |
|California |LOC |
|Tim Cook |PER |
|Barack Obama |PER |
|United States |LOC |
|Honolulu |LOC |
|Hawaii |LOC |
|Harvard Law School |ORG |
|Chicago |LOC |
|Amazon |ORG |
|Jeff Bezos |PER |
|Amazon |ORG |
|Paris |LOC |
|France |LOC |
|Eiffel Tower |LOC |
|Louvre Museum |LOC |
|Notre-Dame Cathedral|LOC |
|Seine River |LOC |
|Mayo Clinic |ORG |
+--------------------+---------+
""")
# Model Information - Token Classification
st.markdown('<div class="sub-title">Model Information</div>', unsafe_allow_html=True)
st.markdown("""
<table class="benchmark-table">
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
<tr>
<td><strong>Model Name</strong></td>
<td>bert_base_token_classifier_conll03</td>
</tr>
<tr>
<td><strong>Compatibility</strong></td>
<td>Spark NLP 3.2.0+</td>
</tr>
<tr>
<td><strong>License</strong></td>
<td>Open Source</td>
</tr>
<tr>
<td><strong>Edition</strong></td>
<td>Official</td>
</tr>
<tr>
<td><strong>Input Labels</strong></td>
<td>[token, document]</td>
</tr>
<tr>
<td><strong>Output Labels</strong></td>
<td>[ner]</td>
</tr>
<tr>
<td><strong>Language</strong></td>
<td>en</td>
</tr>
<tr>
<td><strong>Size</strong></td>
<td>404.3 MB</td>
</tr>
<tr>
<td><strong>Case sensitive</strong></td>
<td>true</td>
</tr>
<tr>
<td><strong>Max sentence length</strong></td>
<td>512</td>
</tr>
</table>
""", unsafe_allow_html=True)
# References - Token Classification
st.markdown('<div class="sub-title">References</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<ul>
<li><a class="link" href="https://github.com/google-research/bert" target="_blank" rel="noopener">Google Research BERT</a></li>
<li><a class="link" href="https://arxiv.org/abs/1810.04805" target="_blank" rel="noopener">BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding</a></li>
<li><a class="link" href="https://huggingface.co/bert-base-uncased" target="_blank" rel="noopener">Hugging Face BERT Models</a></li>
</ul>
</div>
""", unsafe_allow_html=True)
with tab2:
st.markdown("""
<div class="section">
<h2>BERT for Zero-Shot Classification</h2>
<p>The <strong>BertForZeroShotClassification</strong> annotator is designed to classify text into labels it has not seen during training. This is achieved using natural language inference (NLI) to determine the relationship between input text and potential labels. This capability is essential for applications where predefined categories are either unavailable or frequently change. Zero-shot classification is particularly useful for:</p>
<ul>
<li><strong>Dynamic Content Tagging:</strong> Automatically categorizing content without the need for a pre-existing label set.</li>
<li><strong>Sentiment Analysis:</strong> Analyzing sentiment for new and emerging topics without retraining the model.</li>
</ul>
<p>By leveraging this annotator, you can ensure flexibility and adaptability in text classification tasks, making it suitable for ever-changing data environments.</p>
<table class="benchmark-table">
<tr>
<th>Text</th>
<th>Predicted Category</th>
</tr>
<tr>
<td>"The new iPhone has amazing features"</td>
<td>Technology</td>
</tr>
<tr>
<td>"The economic growth has been significant this year"</td>
<td>Finance</td>
</tr>
</table>
</div>
""", unsafe_allow_html=True)
# BERT Zero-Shot Classification Base - MNLI
st.markdown('<div class="sub-title">BERT Zero-Shot Classification Base - MNLI</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>The <strong>bert_zero_shot_classifier_mnli</strong> model is designed for zero-shot text classification, making it suitable for scenarios where predefined categories are not available or frequently change. This model is fine-tuned on the MNLI dataset and leverages natural language inference (NLI) to determine relationships between input text and candidate labels. It allows for dynamic classification without a fixed number of classes, providing flexibility and adaptability for various applications.</p>
</div>
""", unsafe_allow_html=True)
# How to Use the Model - Zero-Shot Classification
st.markdown('<div class="sub-title">How to Use the Model</div>', unsafe_allow_html=True)
st.code('''
from sparknlp.base import *
from sparknlp.annotator import *
from pyspark.ml import Pipeline
document_assembler = DocumentAssembler() \\
.setInputCol('text') \\
.setOutputCol('document')
tokenizer = Tokenizer() \\
.setInputCols(['document']) \\
.setOutputCol('token')
zeroShotClassifier = BertForZeroShotClassification \\
.pretrained('bert_zero_shot_classifier_mnli', 'xx') \\
.setInputCols(['token', 'document']) \\
.setOutputCol('class') \\
.setCaseSensitive(True) \\
.setMaxSentenceLength(512) \\
.setCandidateLabels(["urgent", "mobile", "travel", "movie", "music", "sport", "weather", "technology"])
pipeline = Pipeline(stages=[
document_assembler,
tokenizer,
zeroShotClassifier
])
example = spark.createDataFrame([['In today’s world, staying updated with urgent information is crucial as events can unfold rapidly and require immediate attention.']]).toDF("text")
result = pipeline.fit(example).transform(example)
result.select('document.result', 'class.result').show(truncate=False)
''', language='python')
st.text("""
+------------------------------------------------------------------------------------------------------------------------------------+--------+
|result |result |
+------------------------------------------------------------------------------------------------------------------------------------+--------+
|[In today’s world, staying updated with urgent information is crucial as events can unfold rapidly and require immediate attention.]|[urgent]|
+------------------------------------------------------------------------------------------------------------------------------------+--------+
""")
# Model Information - Zero-Shot Classification
st.markdown('<div class="sub-title">Model Information</div>', unsafe_allow_html=True)
st.markdown("""
<table class="benchmark-table">
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
<tr>
<td><strong>Model Name</strong></td>
<td>bert_zero_shot_classifier_mnli</td>
</tr>
<tr>
<td><strong>Compatibility</strong></td>
<td>Spark NLP 5.2.4+</td>
</tr>
<tr>
<td><strong>License</strong></td>
<td>Open Source</td>
</tr>
<tr>
<td><strong>Edition</strong></td>
<td>Official</td>
</tr>
<tr>
<td><strong>Input Labels</strong></td>
<td>[token, document]</td>
</tr>
<tr>
<td><strong>Output Labels</strong></td>
<td>[label]</td>
</tr>
<tr>
<td><strong>Language</strong></td>
<td>xx</td>
</tr>
<tr>
<td><strong>Size</strong></td>
<td>409.1 MB</td>
</tr>
<tr>
<td><strong>Case sensitive</strong></td>
<td>true</td>
</tr>
</table>
""", unsafe_allow_html=True)
# References - Zero-Shot Classification
st.markdown('<div class="sub-title">References</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<ul>
<li><a class="link" href="https://github.com/google-research/bert" target="_blank" rel="noopener">Google Research BERT</a></li>
<li><a class="link" href="https://arxiv.org/abs/1810.04805" target="_blank" rel="noopener">BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding</a></li>
<li><a class="link" href="https://huggingface.co/bert-base-uncased" target="_blank" rel="noopener">Hugging Face BERT Models</a></li>
</ul>
</div>
""", unsafe_allow_html=True)
with tab3:
st.markdown("""
<div class="section">
<h2>BERT for Sequence Classification</h2>
<p>The <strong>BertForSequenceClassification</strong> annotator is fine-tuned to classify entire sequences of text. This involves understanding the context of the entire sequence, which is crucial for tasks that require a holistic view of the input text. Sequence classification is highly effective for:</p>
<ul>
<li><strong>Sentiment Analysis:</strong> Determining the overall sentiment of a given piece of text.</li>
<li><strong>Spam Detection:</strong> Identifying unsolicited or irrelevant messages.</li>
<li><strong>Document Classification:</strong> Categorizing documents into predefined categories.</li>
</ul>
<p>With its ability to deliver accurate classification results, this annotator is widely used in various text analysis applications.</p>
<table class="benchmark-table">
<tr>
<th>Text</th>
<th>Predicted Sentiment</th>
</tr>
<tr>
<td>"I love this product, it's fantastic!"</td>
<td>Positive</td>
</tr>
<tr>
<td>"The service was terrible, I'm very disappointed."</td>
<td>Negative</td>
</tr>
</table>
</div>
""", unsafe_allow_html=True)
# English BertForSequenceClassification Cased model (from yonichi)
st.markdown('<div class="sub-title">English BertForSequenceClassification Cased model (from yonichi)</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>The <strong>bert_classifier_cbert</strong> model is a pretrained BertForSequenceClassification model. Adapted from Hugging Face and curated for scalability and production-readiness using Spark NLP, this model is designed for sequence classification tasks such as sentiment analysis. It is capable of classifying text into positive, negative, and neutral sentiments, providing valuable insights for various applications.</p>
</div>
""", unsafe_allow_html=True)
# How to Use the Model - Sequence Classification
st.markdown('<div class="sub-title">How to Use the Model</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
# Document Assembler
document_assembler = DocumentAssembler() \\
.setInputCol('text') \\
.setOutputCol('document')
# Sentence Detector
sentence_detector = SentenceDetector() \\
.setInputCols(['document']) \\
.setOutputCol('sentence')
# Tokenizer
tokenizer = Tokenizer() \\
.setInputCols(['sentence']) \\
.setOutputCol('token')
# Sequence Classifier
sequence_classifier = BertForSequenceClassification.pretrained("bert_classifier_cbert", "en") \\
.setInputCols(['sentence', 'token']) \\
.setOutputCol('class')
# Pipeline
pipeline = Pipeline(stages=[
document_assembler,
sentence_detector,
tokenizer,
sequence_classifier
])
# Create example DataFrame
example = spark.createDataFrame([("Apple Inc. is planning to open a new headquarters in Cupertino, California. The CEO, Tim Cook, announced this during the company's annual event on March 25th, 2023. Barack Obama, the 44th President of the United States, was born on August 4th, 1961, in Honolulu, Hawaii. He attended Harvard Law School and later became a community organizer in Chicago. Amazon reported a net revenue of $125.6 billion in Q4 of 2022, an increase of 9% compared to the previous year. Jeff Bezos, the founder of Amazon, mentioned that the company's growth in cloud computing has significantly contributed to this rise. Paris, the capital city of France, is renowned for its art, fashion, and culture. Key attractions include the Eiffel Tower, the Louvre Museum, and the Notre-Dame Cathedral. Visitors often enjoy a stroll along the Seine River and dining at local bistros. The study, conducted at the Mayo Clinic in Rochester, Minnesota, examined the effects of a new drug on patients with Type 2 diabetes. Results showed a significant reduction in blood sugar levels over a 12-month period. Serena Williams won her 24th Grand Slam title at the Wimbledon Championships in London, England. She defeated Naomi Osaka in a thrilling final match on July 13th, 2023. Google's latest smartphone, the Pixel 6, was unveiled at an event in New York City. Sundar Pichai, the CEO of Google, highlighted the phone's advanced AI capabilities and improved camera features. The Declaration of Independence was signed on July 4th, 1776, in Philadelphia, Pennsylvania. Thomas Jefferson, Benjamin Franklin, and John Adams were among the key figures who drafted this historic document.",)], ["text"])
# Fit and transform the data
model = pipeline.fit(example)
result = model.transform(example)
from pyspark.sql.functions import col
# Show results in a structured format for sentence-based classification
result.select(
col('sentence.result').alias('sentences'),
col('class.result').alias('classifications')
).rdd.flatMap(lambda row: list(zip(row['sentences'], row['classifications']))).toDF(['sentence', 'classification']).show(truncate=False)
''', language='python')
st.text("""
+-------------------------------------------------------------------------------------------------------------------------------------+--------------+
|sentence |classification|
+-------------------------------------------------------------------------------------------------------------------------------------+--------------+
|Apple Inc. is planning to open a new headquarters in Cupertino, California. |Neutral |
|The CEO, Tim Cook, announced this during the company's annual event on March 25th, 2023. |Dovish |
|Barack Obama, the 44th President of the United States, was born on August 4th, 1961, in Honolulu, Hawaii. |Neutral |
|He attended Harvard Law School and later became a community organizer in Chicago. |Neutral |
|Amazon reported a net revenue of $125.6 billion in Q4 of 2022, an increase of 9% compared to the previous year. |Neutral |
|Jeff Bezos, the founder of Amazon, mentioned that the company's growth in cloud computing has significantly contributed to this rise.|Dovish |
|Paris, the capital city of France, is renowned for its art, fashion, and culture. |Neutral |
|Key attractions include the Eiffel Tower, the Louvre Museum, and the Notre-Dame Cathedral. |Neutral |
|Visitors often enjoy a stroll along the Seine River and dining at local bistros. |Neutral |
|The study, conducted at the Mayo Clinic in Rochester, Minnesota, examined the effects of a new drug on patients with Type 2 diabetes.|Dovish |
|Results showed a significant reduction in blood sugar levels over a 12-month period. |Neutral |
|Serena Williams won her 24th Grand Slam title at the Wimbledon Championships in London, England. |Hawkish |
|She defeated Naomi Osaka in a thrilling final match on July 13th, 2023. |Neutral |
|Google's latest smartphone, the Pixel 6, was unveiled at an event in New York City. |Dovish |
|Sundar Pichai, the CEO of Google, highlighted the phone's advanced AI capabilities and improved camera features. |Dovish |
|The Declaration of Independence was signed on July 4th, 1776, in Philadelphia, Pennsylvania. |Hawkish |
|Thomas Jefferson, Benjamin Franklin, and John Adams were among the key figures who drafted this historic document. |Neutral |
+-------------------------------------------------------------------------------------------------------------------------------------+--------------+
""")
# Model Information - Sequence Classification
st.markdown('<div class="sub-title">Model Information</div>', unsafe_allow_html=True)
st.markdown("""
<table class="benchmark-table">
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
<tr>
<td><strong>Model Name</strong></td>
<td>bert_classifier_cbert</td>
</tr>
<tr>
<td><strong>Compatibility</strong></td>
<td>Spark NLP 4.2.0+</td>
</tr>
<tr>
<td><strong>License</strong></td>
<td>Open Source</td>
</tr>
<tr>
<td><strong>Edition</strong></td>
<td>Official</td>
</tr>
<tr>
<td><strong>Input Labels</strong></td>
<td>[document, token]</td>
</tr>
<tr>
<td><strong>Output Labels</strong></td>
<td>[class]</td>
</tr>
<tr>
<td><strong>Language</strong></td>
<td>en</td>
</tr>
<tr>
<td><strong>Size</strong></td>
<td>412.2 MB</td>
</tr>
<tr>
<td><strong>Case sensitive</strong></td>
<td>true</td>
</tr>
<tr>
<td><strong>Max sentence length</strong></td>
<td>256</td>
</tr>
</table>
""", unsafe_allow_html=True)
# References - Sequence Classification
st.markdown('<div class="sub-title">References</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<ul>
<li><a class="link" href="https://huggingface.co/yonichi/cbert" target="_blank" rel="noopener">Hugging Face cBERT Model</a></li>
</ul>
</div>
""", unsafe_allow_html=True)
with tab4:
st.markdown("""
<div class="section">
<h2>BERT for Question Answering</h2>
<p>The <strong>BertForQuestionAnswering</strong> annotator is fine-tuned to provide answers to questions based on a given context. This involves extracting relevant information from a passage of text in response to a specific query, making it ideal for applications requiring precise information retrieval. Question answering is particularly useful for:</p>
<ul>
<li><strong>Building Question-Answering Systems:</strong> Creating systems that can automatically answer user queries.</li>
<li><strong>Customer Support Bots:</strong> Providing accurate and timely responses to customer inquiries.</li>
<li><strong>Information Retrieval:</strong> Extracting specific information from large volumes of text.</li>
</ul>
<p>By leveraging this annotator, you can enhance the ability to extract and deliver accurate information from text data.</p>
<table class="benchmark-table">
<tr>
<th>Context</th>
<th>Question</th>
<th>Predicted Answer</th>
</tr>
<tr>
<td>"The Eiffel Tower is one of the most recognizable structures in the world. It was constructed in 1889 as the entrance arch to the 1889 World's Fair held in Paris, France."</td>
<td>"When was the Eiffel Tower constructed?"</td>
<td>1889</td>
</tr>
<tr>
<td>"The Amazon rainforest, also known as Amazonia, is a vast tropical rainforest in South America. It is home to an incredible diversity of flora and fauna."</td>
<td>"What is the Amazon rainforest also known as?"</td>
<td>Amazonia</td>
</tr>
</table>
</div>
""", unsafe_allow_html=True)
# English BertForQuestionAnswering Large Uncased Model
st.markdown('<div class="sub-title">bert_qa_large_uncased_whole_word_masking_finetuned_squad</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<p>This model is a pretrained BERT model, adapted from Hugging Face, curated to provide scalability and production-readiness using Spark NLP. It is designed to handle question-answering tasks effectively.</p>
</div>
""", unsafe_allow_html=True)
# How to Use the Model - Question Answering
st.markdown('<div class="sub-title">How to Use the Model</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
# Document Assembler
document_assembler = MultiDocumentAssembler()\\
.setInputCols(["question", "context"]) \\
.setOutputCols(["document_question", "document_context"])
# BertForQuestionAnswering
spanClassifier = BertForQuestionAnswering.pretrained("bert_qa_large_uncased_whole_word_masking_finetuned_squad","en") \\
.setInputCols(["document_question", "document_context"]) \\
.setOutputCol("answer") \\
.setCaseSensitive(True)
# Pipeline
pipeline = Pipeline().setStages([
document_assembler,
spanClassifier
])
# Create example DataFrame
example = spark.createDataFrame([["What's my name?", "My name is Clara and I live in Berkeley."]]).toDF("question", "context")
# Fit and transform the data
model = pipeline.fit(example)
result = model.transform(example)
# Show results
result.select('document_question.result', 'answer.result').show(truncate=False)
''', language='python')
st.text("""
+-----------------+-------+
|result |result |
+-----------------+-------+
|[What's my name?]|[Clara]|
+-----------------+-------+
""")
# Model Information - Question Answering
st.markdown('<div class="sub-title">Model Information</div>', unsafe_allow_html=True)
st.markdown("""
<table class="benchmark-table">
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
<tr>
<td><strong>Model Name</strong></td>
<td>bert_qa_large_uncased_whole_word_masking_finetuned_squad</td>
</tr>
<tr>
<td><strong>Compatibility</strong></td>
<td>Spark NLP 4.4.0+</td>
</tr>
<tr>
<td><strong>License</strong></td>
<td>Open Source</td>
</tr>
<tr>
<td><strong>Edition</strong></td>
<td>Official</td>
</tr>
<tr>
<td><strong>Input Labels</strong></td>
<td>[document_question, document_context]</td>
</tr>
<tr>
<td><strong>Output Labels</strong></td>
<td>[answer]</td>
</tr>
<tr>
<td><strong>Language</strong></td>
<td>en</td>
</tr>
<tr>
<td><strong>Size</strong></td>
<td>1.3 GB</td>
</tr>
<tr>
<td><strong>Case sensitive</strong></td>
<td>false</td>
</tr>
<tr>
<td><strong>Max sentence length</strong></td>
<td>512</td>
</tr>
</table>
""", unsafe_allow_html=True)
# References - Question Answering
st.markdown('<div class="sub-title">References</div>', unsafe_allow_html=True)
st.markdown("""
<div class="section">
<ul>
<li><a class="link" href="https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad" target="_blank" rel="noopener">BertForQuestionAnswering Model</a></li>
<li><a class="link" href="https://arxiv.org/abs/1810.04805" target="_blank" rel="noopener">BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding</a></li>
<li><a class="link" href="https://github.com/google-research/bert" target="_blank" rel="noopener">Google Research BERT</a></li>
</ul>
</div>
""", unsafe_allow_html=True)
st.markdown("""
<div class="section">
<h2>Conclusion</h2>
<p>In this guide, we've explored a range of BERT-based annotators and models available in Spark NLP, each tailored to specific natural language processing tasks. Here's a summary of the four key BERT annotators and their respective models:</p>
<ul>
<li><strong>BERT for Sequence Classification</strong> - The <code>BertForSequenceClassification</code> annotator, exemplified by the <code>bert_classifier_cbert</code> model, excels in classifying entire sequences of text. This model is particularly useful for tasks like sentiment analysis, spam detection, and document categorization, providing insights into the overall sentiment of a text or its classification into predefined categories.</li>
<li><strong>BERT for Token Classification</strong> - The <code>BertForTokenClassification</code> annotator, with the <code>bert_base_token_classifier_conll03</code> model, specializes in Named Entity Recognition (NER). This annotator identifies entities such as people, organizations, locations, and more within a text, making it invaluable for information extraction and document categorization.</li>
<li><strong>BERT for Zero-Shot Classification</strong> - The <code>BertForZeroShotClassification</code> annotator, represented by the <code>bert_zero_shot_classifier_mnli</code> model, offers a flexible approach to text classification without requiring a predefined set of categories. It leverages natural language inference (NLI) to classify text into dynamically chosen labels, making it ideal for applications with evolving or unknown categories.</li>
<li><strong>BERT for Question Answering</strong> - The <code>BertForQuestionAnswering</code> annotator, though not highlighted in this specific instance, is designed to extract answers from a given context based on a query. This model is highly effective for building question-answering systems and customer support bots, facilitating precise information retrieval from large text corpora.</li>
</ul>
<p>Each of these models and annotators demonstrates the versatility and power of BERT-based approaches in natural language processing. Whether you need to classify sequences, identify entities, handle zero-shot classification, or answer questions, Spark NLP provides robust tools to enhance your text analysis capabilities. Leveraging these models allows for scalable and production-ready solutions in various applications, from sentiment analysis to dynamic content tagging.</p>
</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) |