spark-nlp 5.4.0rc1__py2.py3-none-any.whl → 5.4.1__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of spark-nlp might be problematic. Click here for more details.
- com/johnsnowlabs/ml/__init__.py +0 -0
- com/johnsnowlabs/ml/ai/__init__.py +10 -0
- {spark_nlp-5.4.0rc1.dist-info → spark_nlp-5.4.1.dist-info}/METADATA +50 -60
- {spark_nlp-5.4.0rc1.dist-info → spark_nlp-5.4.1.dist-info}/RECORD +20 -15
- sparknlp/__init__.py +3 -2
- sparknlp/annotator/classifier_dl/__init__.py +1 -0
- sparknlp/annotator/classifier_dl/mpnet_for_token_classification.py +173 -0
- sparknlp/annotator/classifier_dl/xlm_roberta_for_token_classification.py +3 -3
- sparknlp/annotator/embeddings/bge_embeddings.py +2 -0
- sparknlp/annotator/embeddings/e5_embeddings.py +2 -0
- sparknlp/annotator/embeddings/mpnet_embeddings.py +2 -0
- sparknlp/annotator/openai/openai_embeddings.py +43 -69
- sparknlp/annotator/seq2seq/__init__.py +2 -0
- sparknlp/annotator/seq2seq/m2m100_transformer.py +2 -2
- sparknlp/annotator/seq2seq/mistral_transformer.py +349 -0
- sparknlp/annotator/seq2seq/phi2_transformer.py +326 -0
- sparknlp/internal/__init__.py +428 -142
- {spark_nlp-5.4.0rc1.dist-info → spark_nlp-5.4.1.dist-info}/.uuid +0 -0
- {spark_nlp-5.4.0rc1.dist-info → spark_nlp-5.4.1.dist-info}/WHEEL +0 -0
- {spark_nlp-5.4.0rc1.dist-info → spark_nlp-5.4.1.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# Copyright 2017-2022 John Snow Labs
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
"""Contains classes for MPNetForTokenClassification."""
|
|
15
|
+
|
|
16
|
+
from sparknlp.common import *
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class MPNetForTokenClassification(AnnotatorModel,
|
|
20
|
+
HasCaseSensitiveProperties,
|
|
21
|
+
HasBatchedAnnotate,
|
|
22
|
+
HasEngine,
|
|
23
|
+
HasMaxSentenceLengthLimit):
|
|
24
|
+
"""MPNetForTokenClassification can load XLM-RoBERTa Models with a token
|
|
25
|
+
classification head on top (a linear layer on top of the hidden-states
|
|
26
|
+
output) e.g. for Named-Entity-Recognition (NER) tasks.
|
|
27
|
+
|
|
28
|
+
Pretrained models can be loaded with :meth:`.pretrained` of the companion
|
|
29
|
+
object:
|
|
30
|
+
|
|
31
|
+
>>> token_classifier = MPNetForTokenClassification.pretrained() \\
|
|
32
|
+
... .setInputCols(["token", "document"]) \\
|
|
33
|
+
... .setOutputCol("label")
|
|
34
|
+
The default model is ``"mpnet_base_token_classifier"``, if no
|
|
35
|
+
name is provided.
|
|
36
|
+
|
|
37
|
+
For available pretrained models please see the `Models Hub
|
|
38
|
+
<https://sparknlp.org/models?task=Named+Entity+Recognition>`__.
|
|
39
|
+
To see which models are compatible and how to import them see
|
|
40
|
+
`Import Transformers into Spark NLP 🚀
|
|
41
|
+
<https://github.com/JohnSnowLabs/spark-nlp/discussions/5669>`_.
|
|
42
|
+
|
|
43
|
+
====================== ======================
|
|
44
|
+
Input Annotation types Output Annotation type
|
|
45
|
+
====================== ======================
|
|
46
|
+
``DOCUMENT, TOKEN`` ``NAMED_ENTITY``
|
|
47
|
+
====================== ======================
|
|
48
|
+
|
|
49
|
+
Parameters
|
|
50
|
+
----------
|
|
51
|
+
batchSize
|
|
52
|
+
Batch size. Large values allows faster processing but requires more
|
|
53
|
+
memory, by default 8
|
|
54
|
+
caseSensitive
|
|
55
|
+
Whether to ignore case in tokens for embeddings matching, by default
|
|
56
|
+
True
|
|
57
|
+
configProtoBytes
|
|
58
|
+
ConfigProto from tensorflow, serialized into byte array.
|
|
59
|
+
maxSentenceLength
|
|
60
|
+
Max sentence length to process, by default 128
|
|
61
|
+
|
|
62
|
+
Examples
|
|
63
|
+
--------
|
|
64
|
+
>>> import sparknlp
|
|
65
|
+
>>> from sparknlp.base import *
|
|
66
|
+
>>> from sparknlp.annotator import *
|
|
67
|
+
>>> from pyspark.ml import Pipeline
|
|
68
|
+
>>> documentAssembler = DocumentAssembler() \\
|
|
69
|
+
... .setInputCol("text") \\
|
|
70
|
+
... .setOutputCol("document")
|
|
71
|
+
>>> tokenizer = Tokenizer() \\
|
|
72
|
+
... .setInputCols(["document"]) \\
|
|
73
|
+
... .setOutputCol("token")
|
|
74
|
+
>>> tokenClassifier = MPNetForTokenClassification.pretrained() \\
|
|
75
|
+
... .setInputCols(["token", "document"]) \\
|
|
76
|
+
... .setOutputCol("label") \\
|
|
77
|
+
... .setCaseSensitive(True)
|
|
78
|
+
>>> pipeline = Pipeline().setStages([
|
|
79
|
+
... documentAssembler,
|
|
80
|
+
... tokenizer,
|
|
81
|
+
... tokenClassifier
|
|
82
|
+
... ])
|
|
83
|
+
>>> data = spark.createDataFrame([["John Lenon was born in London and lived in Paris. My name is Sarah and I live in London"]]).toDF("text")
|
|
84
|
+
>>> result = pipeline.fit(data).transform(data)
|
|
85
|
+
>>> result.select("label.result").show(truncate=False)
|
|
86
|
+
+------------------------------------------------------------------------------------+
|
|
87
|
+
|result |
|
|
88
|
+
+------------------------------------------------------------------------------------+
|
|
89
|
+
|[B-PER, I-PER, O, O, O, B-LOC, O, O, O, B-LOC, O, O, O, O, B-PER, O, O, O, O, B-LOC]|
|
|
90
|
+
+------------------------------------------------------------------------------------+
|
|
91
|
+
"""
|
|
92
|
+
name = "MPNetForTokenClassification"
|
|
93
|
+
|
|
94
|
+
inputAnnotatorTypes = [AnnotatorType.DOCUMENT, AnnotatorType.TOKEN]
|
|
95
|
+
|
|
96
|
+
outputAnnotatorType = AnnotatorType.NAMED_ENTITY
|
|
97
|
+
|
|
98
|
+
configProtoBytes = Param(Params._dummy(),
|
|
99
|
+
"configProtoBytes",
|
|
100
|
+
"ConfigProto from tensorflow, serialized into byte array. Get with config_proto.SerializeToString()",
|
|
101
|
+
TypeConverters.toListInt)
|
|
102
|
+
|
|
103
|
+
def getClasses(self):
|
|
104
|
+
"""
|
|
105
|
+
Returns labels used to train this model
|
|
106
|
+
"""
|
|
107
|
+
return self._call_java("getClasses")
|
|
108
|
+
|
|
109
|
+
def setConfigProtoBytes(self, b):
|
|
110
|
+
"""Sets configProto from tensorflow, serialized into byte array.
|
|
111
|
+
|
|
112
|
+
Parameters
|
|
113
|
+
----------
|
|
114
|
+
b : List[int]
|
|
115
|
+
ConfigProto from tensorflow, serialized into byte array
|
|
116
|
+
"""
|
|
117
|
+
return self._set(configProtoBytes=b)
|
|
118
|
+
|
|
119
|
+
@keyword_only
|
|
120
|
+
def __init__(self, classname="com.johnsnowlabs.nlp.annotators.classifier.dl.MPNetForTokenClassification",
|
|
121
|
+
java_model=None):
|
|
122
|
+
super(MPNetForTokenClassification, self).__init__(
|
|
123
|
+
classname=classname,
|
|
124
|
+
java_model=java_model
|
|
125
|
+
)
|
|
126
|
+
self._setDefault(
|
|
127
|
+
batchSize=8,
|
|
128
|
+
maxSentenceLength=128,
|
|
129
|
+
caseSensitive=True
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
@staticmethod
|
|
133
|
+
def loadSavedModel(folder, spark_session):
|
|
134
|
+
"""Loads a locally saved model.
|
|
135
|
+
|
|
136
|
+
Parameters
|
|
137
|
+
----------
|
|
138
|
+
folder : str
|
|
139
|
+
Folder of the saved model
|
|
140
|
+
spark_session : pyspark.sql.SparkSession
|
|
141
|
+
The current SparkSession
|
|
142
|
+
|
|
143
|
+
Returns
|
|
144
|
+
-------
|
|
145
|
+
XlmRoBertaForTokenClassification
|
|
146
|
+
The restored model
|
|
147
|
+
"""
|
|
148
|
+
from sparknlp.internal import _MPNetForTokenClassifierLoader
|
|
149
|
+
jModel = _MPNetForTokenClassifierLoader(folder, spark_session._jsparkSession)._java_obj
|
|
150
|
+
return MPNetForTokenClassification(java_model=jModel)
|
|
151
|
+
|
|
152
|
+
@staticmethod
|
|
153
|
+
def pretrained(name="mpnet_base_token_classifier", lang="en", remote_loc=None):
|
|
154
|
+
"""Downloads and loads a pretrained model.
|
|
155
|
+
|
|
156
|
+
Parameters
|
|
157
|
+
----------
|
|
158
|
+
name : str, optional
|
|
159
|
+
Name of the pretrained model, by default
|
|
160
|
+
"mpnet_base_token_classifier"
|
|
161
|
+
lang : str, optional
|
|
162
|
+
Language of the pretrained model, by default "en"
|
|
163
|
+
remote_loc : str, optional
|
|
164
|
+
Optional remote address of the resource, by default None. Will use
|
|
165
|
+
Spark NLPs repositories otherwise.
|
|
166
|
+
|
|
167
|
+
Returns
|
|
168
|
+
-------
|
|
169
|
+
XlmRoBertaForTokenClassification
|
|
170
|
+
The restored model
|
|
171
|
+
"""
|
|
172
|
+
from sparknlp.pretrained import ResourceDownloader
|
|
173
|
+
return ResourceDownloader.downloadModel(MPNetForTokenClassification, name, lang, remote_loc)
|
|
@@ -31,7 +31,7 @@ class XlmRoBertaForTokenClassification(AnnotatorModel,
|
|
|
31
31
|
>>> token_classifier = XlmRoBertaForTokenClassification.pretrained() \\
|
|
32
32
|
... .setInputCols(["token", "document"]) \\
|
|
33
33
|
... .setOutputCol("label")
|
|
34
|
-
The default model is ``"
|
|
34
|
+
The default model is ``"mpnet_base_token_classifier"``, if no
|
|
35
35
|
name is provided.
|
|
36
36
|
|
|
37
37
|
For available pretrained models please see the `Models Hub
|
|
@@ -150,14 +150,14 @@ class XlmRoBertaForTokenClassification(AnnotatorModel,
|
|
|
150
150
|
return XlmRoBertaForTokenClassification(java_model=jModel)
|
|
151
151
|
|
|
152
152
|
@staticmethod
|
|
153
|
-
def pretrained(name="
|
|
153
|
+
def pretrained(name="mpnet_base_token_classifier", lang="en", remote_loc=None):
|
|
154
154
|
"""Downloads and loads a pretrained model.
|
|
155
155
|
|
|
156
156
|
Parameters
|
|
157
157
|
----------
|
|
158
158
|
name : str, optional
|
|
159
159
|
Name of the pretrained model, by default
|
|
160
|
-
"
|
|
160
|
+
"mpnet_base_token_classifier"
|
|
161
161
|
lang : str, optional
|
|
162
162
|
Language of the pretrained model, by default "en"
|
|
163
163
|
remote_loc : str, optional
|
|
@@ -26,6 +26,8 @@ class BGEEmbeddings(AnnotatorModel,
|
|
|
26
26
|
|
|
27
27
|
BGE, or BAAI General Embeddings, a model that can map any text to a low-dimensional dense
|
|
28
28
|
vector which can be used for tasks like retrieval, classification, clustering, or semantic search.
|
|
29
|
+
|
|
30
|
+
Note that this annotator is only supported for Spark Versions 3.4 and up.
|
|
29
31
|
|
|
30
32
|
Pretrained models can be loaded with `pretrained` of the companion object:
|
|
31
33
|
|
|
@@ -25,6 +25,8 @@ class E5Embeddings(AnnotatorModel,
|
|
|
25
25
|
"""Sentence embeddings using E5.
|
|
26
26
|
|
|
27
27
|
E5, a weakly supervised text embedding model that can generate text embeddings tailored to any task (e.g., classification, retrieval, clustering, text evaluation, etc.)
|
|
28
|
+
Note that this annotator is only supported for Spark Versions 3.4 and up.
|
|
29
|
+
|
|
28
30
|
Pretrained models can be loaded with :meth:`.pretrained` of the companion
|
|
29
31
|
object:
|
|
30
32
|
|
|
@@ -28,6 +28,8 @@ class MPNetEmbeddings(AnnotatorModel,
|
|
|
28
28
|
to inherit the advantages of masked language modeling and permuted language modeling for
|
|
29
29
|
natural language understanding.
|
|
30
30
|
|
|
31
|
+
Note that this annotator is only supported for Spark Versions 3.4 and up.
|
|
32
|
+
|
|
31
33
|
Pretrained models can be loaded with :meth:`.pretrained` of the companion
|
|
32
34
|
object:
|
|
33
35
|
|