spark-nlp 5.5.0rc1__py2.py3-none-any.whl → 5.5.2__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.
- spark_nlp-5.5.2.dist-info/METADATA +345 -0
- {spark_nlp-5.5.0rc1.dist-info → spark_nlp-5.5.2.dist-info}/RECORD +25 -19
- sparknlp/__init__.py +12 -6
- sparknlp/annotator/classifier_dl/__init__.py +1 -0
- sparknlp/annotator/classifier_dl/bert_for_multiple_choice.py +161 -0
- sparknlp/annotator/cv/__init__.py +1 -0
- sparknlp/annotator/cv/blip_for_question_answering.py +172 -0
- sparknlp/annotator/embeddings/__init__.py +1 -0
- sparknlp/annotator/embeddings/auto_gguf_embeddings.py +538 -0
- sparknlp/annotator/embeddings/nomic_embeddings.py +3 -3
- sparknlp/annotator/seq2seq/auto_gguf_model.py +17 -27
- sparknlp/annotator/seq2seq/cpm_transformer.py +5 -5
- sparknlp/annotator/seq2seq/nllb_transformer.py +4 -4
- sparknlp/annotator/seq2seq/phi3_transformer.py +4 -4
- sparknlp/annotator/seq2seq/qwen_transformer.py +3 -3
- sparknlp/base/__init__.py +1 -0
- sparknlp/base/image_assembler.py +11 -0
- sparknlp/base/light_pipeline.py +20 -9
- sparknlp/base/prompt_assembler.py +207 -0
- sparknlp/internal/__init__.py +22 -0
- sparknlp/reader/__init__.py +15 -0
- sparknlp/reader/sparknlp_reader.py +121 -0
- spark_nlp-5.5.0rc1.dist-info/METADATA +0 -1357
- {spark_nlp-5.5.0rc1.dist-info → spark_nlp-5.5.2.dist-info}/.uuid +0 -0
- {spark_nlp-5.5.0rc1.dist-info → spark_nlp-5.5.2.dist-info}/WHEEL +0 -0
- {spark_nlp-5.5.0rc1.dist-info → spark_nlp-5.5.2.dist-info}/top_level.txt +0 -0
|
@@ -32,7 +32,7 @@ class NLLBTransformer(AnnotatorModel, HasBatchedAnnotate, HasEngine):
|
|
|
32
32
|
... .setOutputCol("generation")
|
|
33
33
|
|
|
34
34
|
|
|
35
|
-
The default model is ``"
|
|
35
|
+
The default model is ``"nllb_distilled_600M_8int"``, if no name is provided. For available
|
|
36
36
|
pretrained models please see the `Models Hub
|
|
37
37
|
<https://sparknlp.org/models?q=nllb>`__.
|
|
38
38
|
|
|
@@ -164,7 +164,7 @@ class NLLBTransformer(AnnotatorModel, HasBatchedAnnotate, HasEngine):
|
|
|
164
164
|
>>> documentAssembler = DocumentAssembler() \\
|
|
165
165
|
... .setInputCol("text") \\
|
|
166
166
|
... .setOutputCol("documents")
|
|
167
|
-
>>> nllb = NLLBTransformer.pretrained("
|
|
167
|
+
>>> nllb = NLLBTransformer.pretrained("nllb_distilled_600M_8int") \\
|
|
168
168
|
... .setInputCols(["documents"]) \\
|
|
169
169
|
... .setMaxOutputLength(50) \\
|
|
170
170
|
... .setOutputCol("generation") \\
|
|
@@ -398,13 +398,13 @@ class NLLBTransformer(AnnotatorModel, HasBatchedAnnotate, HasEngine):
|
|
|
398
398
|
return NLLBTransformer(java_model=jModel)
|
|
399
399
|
|
|
400
400
|
@staticmethod
|
|
401
|
-
def pretrained(name="
|
|
401
|
+
def pretrained(name="nllb_distilled_600M_8int", lang="xx", remote_loc=None):
|
|
402
402
|
"""Downloads and loads a pretrained model.
|
|
403
403
|
|
|
404
404
|
Parameters
|
|
405
405
|
----------
|
|
406
406
|
name : str, optional
|
|
407
|
-
Name of the pretrained model, by default "
|
|
407
|
+
Name of the pretrained model, by default "nllb_distilled_600M_8int"
|
|
408
408
|
lang : str, optional
|
|
409
409
|
Language of the pretrained model, by default "en"
|
|
410
410
|
remote_loc : str, optional
|
|
@@ -37,7 +37,7 @@ class Phi3Transformer(AnnotatorModel, HasBatchedAnnotate, HasEngine):
|
|
|
37
37
|
... .setOutputCol("generation")
|
|
38
38
|
|
|
39
39
|
|
|
40
|
-
The default model is ``
|
|
40
|
+
The default model is ``phi_3_mini_128k_instruct``, if no name is provided. For available
|
|
41
41
|
pretrained models please see the `Models Hub
|
|
42
42
|
<https://sparknlp.org/models?q=phi3>`__.
|
|
43
43
|
|
|
@@ -112,7 +112,7 @@ class Phi3Transformer(AnnotatorModel, HasBatchedAnnotate, HasEngine):
|
|
|
112
112
|
>>> documentAssembler = DocumentAssembler() \\
|
|
113
113
|
... .setInputCol("text") \\
|
|
114
114
|
... .setOutputCol("documents")
|
|
115
|
-
>>> phi3 = Phi3Transformer.pretrained(
|
|
115
|
+
>>> phi3 = Phi3Transformer.pretrained(phi_3_mini_128k_instruct) \\
|
|
116
116
|
... .setInputCols(["documents"]) \\
|
|
117
117
|
... .setMaxOutputLength(50) \\
|
|
118
118
|
... .setOutputCol("generation")
|
|
@@ -308,13 +308,13 @@ class Phi3Transformer(AnnotatorModel, HasBatchedAnnotate, HasEngine):
|
|
|
308
308
|
return Phi3Transformer(java_model=jModel)
|
|
309
309
|
|
|
310
310
|
@staticmethod
|
|
311
|
-
def pretrained(name="
|
|
311
|
+
def pretrained(name="phi_3_mini_128k_instruct", lang="en", remote_loc=None):
|
|
312
312
|
"""Downloads and loads a pretrained model.
|
|
313
313
|
|
|
314
314
|
Parameters
|
|
315
315
|
----------
|
|
316
316
|
name : str, optional
|
|
317
|
-
Name of the pretrained model, by default
|
|
317
|
+
Name of the pretrained model, by default phi_3_mini_128k_instruct
|
|
318
318
|
lang : str, optional
|
|
319
319
|
Language of the pretrained model, by default "en"
|
|
320
320
|
remote_loc : str, optional
|
|
@@ -121,7 +121,7 @@ class QwenTransformer(AnnotatorModel, HasBatchedAnnotate, HasEngine):
|
|
|
121
121
|
>>> documentAssembler = DocumentAssembler() \\
|
|
122
122
|
... .setInputCol("text") \\
|
|
123
123
|
... .setOutputCol("documents")
|
|
124
|
-
>>> qwen = QwenTransformer.pretrained("
|
|
124
|
+
>>> qwen = QwenTransformer.pretrained("qwen_7.5b_chat") \\
|
|
125
125
|
... .setInputCols(["documents"]) \\
|
|
126
126
|
... .setMaxOutputLength(50) \\
|
|
127
127
|
... .setOutputCol("generation")
|
|
@@ -317,13 +317,13 @@ class QwenTransformer(AnnotatorModel, HasBatchedAnnotate, HasEngine):
|
|
|
317
317
|
return QwenTransformer(java_model=jModel)
|
|
318
318
|
|
|
319
319
|
@staticmethod
|
|
320
|
-
def pretrained(name="
|
|
320
|
+
def pretrained(name="qwen_7.5b_chat", lang="en", remote_loc=None):
|
|
321
321
|
"""Downloads and loads a pretrained model.
|
|
322
322
|
|
|
323
323
|
Parameters
|
|
324
324
|
----------
|
|
325
325
|
name : str, optional
|
|
326
|
-
Name of the pretrained model, by default "
|
|
326
|
+
Name of the pretrained model, by default "qwen_7.5b_chat"
|
|
327
327
|
lang : str, optional
|
|
328
328
|
Language of the pretrained model, by default "en"
|
|
329
329
|
remote_loc : str, optional
|
sparknlp/base/__init__.py
CHANGED
sparknlp/base/image_assembler.py
CHANGED
|
@@ -65,6 +65,7 @@ class ImageAssembler(AnnotatorTransformer):
|
|
|
65
65
|
outputAnnotatorType = AnnotatorType.IMAGE
|
|
66
66
|
|
|
67
67
|
inputCol = Param(Params._dummy(), "inputCol", "input column name", typeConverter=TypeConverters.toString)
|
|
68
|
+
textCol = Param(Params._dummy(), "textCol", "text column name", typeConverter=TypeConverters.toString)
|
|
68
69
|
outputCol = Param(Params._dummy(), "outputCol", "output column name", typeConverter=TypeConverters.toString)
|
|
69
70
|
name = 'ImageAssembler'
|
|
70
71
|
|
|
@@ -101,3 +102,13 @@ class ImageAssembler(AnnotatorTransformer):
|
|
|
101
102
|
def getOutputCol(self):
|
|
102
103
|
"""Gets output column name of annotations."""
|
|
103
104
|
return self.getOrDefault(self.outputCol)
|
|
105
|
+
|
|
106
|
+
def setTextCol(self, value):
|
|
107
|
+
"""Sets an optional text column name.
|
|
108
|
+
|
|
109
|
+
Parameters
|
|
110
|
+
----------
|
|
111
|
+
value : str
|
|
112
|
+
Name of an optional input text column
|
|
113
|
+
"""
|
|
114
|
+
return self._set(inputCol=value)
|
sparknlp/base/light_pipeline.py
CHANGED
|
@@ -277,7 +277,7 @@ class LightPipeline:
|
|
|
277
277
|
|
|
278
278
|
return result
|
|
279
279
|
|
|
280
|
-
def fullAnnotateImage(self, path_to_image):
|
|
280
|
+
def fullAnnotateImage(self, path_to_image, text=None):
|
|
281
281
|
"""Annotates the data provided into `Annotation` type results.
|
|
282
282
|
|
|
283
283
|
The data should be either a list or a str.
|
|
@@ -287,27 +287,38 @@ class LightPipeline:
|
|
|
287
287
|
path_to_image : list or str
|
|
288
288
|
Source path of image, list of paths to images
|
|
289
289
|
|
|
290
|
+
text: list or str, optional
|
|
291
|
+
Optional list or str of texts. If None, defaults to empty list if path_to_image is a list, or empty string if path_to_image is a string.
|
|
292
|
+
|
|
290
293
|
Returns
|
|
291
294
|
-------
|
|
292
295
|
List[AnnotationImage]
|
|
293
296
|
The result of the annotation
|
|
294
297
|
"""
|
|
298
|
+
if not isinstance(path_to_image, (str, list)):
|
|
299
|
+
raise TypeError("argument for path_to_image must be 'str' or 'list[str]'")
|
|
300
|
+
|
|
301
|
+
if text is None:
|
|
302
|
+
text = "" if isinstance(path_to_image, str) else []
|
|
303
|
+
|
|
304
|
+
if type(path_to_image) != type(text):
|
|
305
|
+
raise ValueError("`path_to_image` and `text` must be of the same type")
|
|
306
|
+
|
|
295
307
|
stages = self.pipeline_model.stages
|
|
296
308
|
if not self._skipPipelineValidation(stages):
|
|
297
309
|
self._validateStagesInputCols(stages)
|
|
298
310
|
|
|
299
|
-
if
|
|
311
|
+
if isinstance(path_to_image, str):
|
|
300
312
|
path_to_image = [path_to_image]
|
|
313
|
+
text = [text]
|
|
301
314
|
|
|
302
|
-
|
|
303
|
-
result = []
|
|
315
|
+
result = []
|
|
304
316
|
|
|
305
|
-
|
|
306
|
-
|
|
317
|
+
for image_result in self._lightPipeline.fullAnnotateImageJava(path_to_image, text):
|
|
318
|
+
result.append(self.__buildStages(image_result))
|
|
319
|
+
|
|
320
|
+
return result
|
|
307
321
|
|
|
308
|
-
return result
|
|
309
|
-
else:
|
|
310
|
-
raise TypeError("argument for annotation may be 'str' or list[str]")
|
|
311
322
|
|
|
312
323
|
def __buildStages(self, annotations_result):
|
|
313
324
|
stages = {}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Copyright 2017-2024 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 the PromptAssembler."""
|
|
15
|
+
|
|
16
|
+
from pyspark import keyword_only
|
|
17
|
+
from pyspark.ml.param import TypeConverters, Params, Param
|
|
18
|
+
|
|
19
|
+
from sparknlp.common import AnnotatorType
|
|
20
|
+
from sparknlp.internal import AnnotatorTransformer
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class PromptAssembler(AnnotatorTransformer):
|
|
24
|
+
"""Assembles a sequence of messages into a single string using a template. These strings can then
|
|
25
|
+
be used as prompts for large language models.
|
|
26
|
+
|
|
27
|
+
This annotator expects an array of two-tuples as the type of the input column (one array of
|
|
28
|
+
tuples per row). The first element of the tuples should be the role and the second element is
|
|
29
|
+
the text of the message. Possible roles are "system", "user" and "assistant".
|
|
30
|
+
|
|
31
|
+
An assistant header can be added to the end of the generated string by using
|
|
32
|
+
``setAddAssistant(True)``.
|
|
33
|
+
|
|
34
|
+
At the moment, this annotator uses llama.cpp as a backend to parse and apply the templates.
|
|
35
|
+
llama.cpp uses basic pattern matching to determine the type of the template, then applies a
|
|
36
|
+
basic version of the template to the messages. This means that more advanced templates are not
|
|
37
|
+
supported.
|
|
38
|
+
|
|
39
|
+
For an extended example see the
|
|
40
|
+
`example notebook <https://github.com/JohnSnowLabs/spark-nlp/blob/master/examples/python/llama.cpp/PromptAssember_with_AutoGGUFModel.ipynb>`__.
|
|
41
|
+
|
|
42
|
+
====================== ======================
|
|
43
|
+
Input Annotation types Output Annotation type
|
|
44
|
+
====================== ======================
|
|
45
|
+
``NONE`` ``DOCUMENT``
|
|
46
|
+
====================== ======================
|
|
47
|
+
|
|
48
|
+
Parameters
|
|
49
|
+
----------
|
|
50
|
+
inputCol
|
|
51
|
+
Input column name
|
|
52
|
+
outputCol
|
|
53
|
+
Output column name
|
|
54
|
+
chatTemplate
|
|
55
|
+
Template used for the chat
|
|
56
|
+
addAssistant
|
|
57
|
+
Whether to add an assistant header to the end of the generated string
|
|
58
|
+
|
|
59
|
+
Examples
|
|
60
|
+
--------
|
|
61
|
+
>>> from sparknlp.base import *
|
|
62
|
+
>>> messages = [
|
|
63
|
+
... [
|
|
64
|
+
... ("system", "You are a helpful assistant."),
|
|
65
|
+
... ("assistant", "Hello there, how can I help you?"),
|
|
66
|
+
... ("user", "I need help with organizing my room."),
|
|
67
|
+
... ]
|
|
68
|
+
... ]
|
|
69
|
+
>>> df = spark.createDataFrame([messages]).toDF("messages")
|
|
70
|
+
>>> template = (
|
|
71
|
+
... "{{- bos_token }} {%- if custom_tools is defined %} {%- set tools = custom_tools %} {%- "
|
|
72
|
+
... "endif %} {%- if not tools_in_user_message is defined %} {%- set tools_in_user_message = true %} {%- "
|
|
73
|
+
... 'endif %} {%- if not date_string is defined %} {%- set date_string = "26 Jul 2024" %} {%- endif %} '
|
|
74
|
+
... "{%- if not tools is defined %} {%- set tools = none %} {%- endif %} {#- This block extracts the "
|
|
75
|
+
... "system message, so we can slot it into the right place. #} {%- if messages[0]['role'] == 'system' %}"
|
|
76
|
+
... " {%- set system_message = messages[0]['content']|trim %} {%- set messages = messages[1:] %} {%- else"
|
|
77
|
+
... ' %} {%- set system_message = "" %} {%- endif %} {#- System message + builtin tools #} {{- '
|
|
78
|
+
... '"<|start_header_id|>system<|end_header_id|>\\n\\n" }} {%- if builtin_tools is defined or tools is '
|
|
79
|
+
... 'not none %} {{- "Environment: ipython\\n" }} {%- endif %} {%- if builtin_tools is defined %} {{- '
|
|
80
|
+
... '"Tools: " + builtin_tools | reject(\\'equalto\', \\'code_interpreter\\') | join(", ") + "\\n\\n"}} '
|
|
81
|
+
... '{%- endif %} {{- "Cutting Knowledge Date: December 2023\\n" }} {{- "Today Date: " + date_string '
|
|
82
|
+
... '+ "\\n\\n" }} {%- if tools is not none and not tools_in_user_message %} {{- "You have access to '
|
|
83
|
+
... 'the following functions. To call a function, please respond with JSON for a function call." }} {{- '
|
|
84
|
+
... '\\'Respond in the format {"name": function name, "parameters": dictionary of argument name and its'
|
|
85
|
+
... ' value}.\\' }} {{- "Do not use variables.\\n\\n" }} {%- for t in tools %} {{- t | tojson(indent=4) '
|
|
86
|
+
... '}} {{- "\\n\\n" }} {%- endfor %} {%- endif %} {{- system_message }} {{- "<|eot_id|>" }} {#- '
|
|
87
|
+
... "Custom tools are passed in a user message with some extra guidance #} {%- if tools_in_user_message "
|
|
88
|
+
... "and not tools is none %} {#- Extract the first user message so we can plug it in here #} {%- if "
|
|
89
|
+
... "messages | length != 0 %} {%- set first_user_message = messages[0]['content']|trim %} {%- set "
|
|
90
|
+
... 'messages = messages[1:] %} {%- else %} {{- raise_exception("Cannot put tools in the first user '
|
|
91
|
+
... "message when there's no first user message!\\") }} {%- endif %} {{- "
|
|
92
|
+
... "'<|start_header_id|>user<|end_header_id|>\\n\\n' -}} {{- \\"Given the following functions, please "
|
|
93
|
+
... 'respond with a JSON for a function call " }} {{- "with its proper arguments that best answers the '
|
|
94
|
+
... 'given prompt.\\n\\n" }} {{- \\'Respond in the format {"name": function name, "parameters": '
|
|
95
|
+
... 'dictionary of argument name and its value}.\\' }} {{- "Do not use variables.\\n\\n" }} {%- for t in '
|
|
96
|
+
... 'tools %} {{- t | tojson(indent=4) }} {{- "\\n\\n" }} {%- endfor %} {{- first_user_message + '
|
|
97
|
+
... "\\"<|eot_id|>\\"}} {%- endif %} {%- for message in messages %} {%- if not (message.role == 'ipython' "
|
|
98
|
+
... "or message.role == 'tool' or 'tool_calls' in message) %} {{- '<|start_header_id|>' + message['role']"
|
|
99
|
+
... " + '<|end_header_id|>\\n\\n'+ message['content'] | trim + '<|eot_id|>' }} {%- elif 'tool_calls' in "
|
|
100
|
+
... 'message %} {%- if not message.tool_calls|length == 1 %} {{- raise_exception("This model only '
|
|
101
|
+
... 'supports single tool-calls at once!") }} {%- endif %} {%- set tool_call = message.tool_calls[0]'
|
|
102
|
+
... ".function %} {%- if builtin_tools is defined and tool_call.name in builtin_tools %} {{- "
|
|
103
|
+
... "'<|start_header_id|>assistant<|end_header_id|>\\n\\n' -}} {{- \\"<|python_tag|>\\" + tool_call.name + "
|
|
104
|
+
... '".call(" }} {%- for arg_name, arg_val in tool_call.arguments | items %} {{- arg_name + \\'="\\' + '
|
|
105
|
+
... 'arg_val + \\'"\\' }} {%- if not loop.last %} {{- ", " }} {%- endif %} {%- endfor %} {{- ")" }} {%- '
|
|
106
|
+
... "else %} {{- '<|start_header_id|>assistant<|end_header_id|>\\n\\n' -}} {{- '{\\"name\": \\"' + "
|
|
107
|
+
... 'tool_call.name + \\'", \\' }} {{- \\'"parameters": \\' }} {{- tool_call.arguments | tojson }} {{- "}" '
|
|
108
|
+
... "}} {%- endif %} {%- if builtin_tools is defined %} {#- This means we're in ipython mode #} {{- "
|
|
109
|
+
... '"<|eom_id|>" }} {%- else %} {{- "<|eot_id|>" }} {%- endif %} {%- elif message.role == "tool" '
|
|
110
|
+
... 'or message.role == "ipython" %} {{- "<|start_header_id|>ipython<|end_header_id|>\\n\\n" }} {%- '
|
|
111
|
+
... "if message.content is mapping or message.content is iterable %} {{- message.content | tojson }} {%- "
|
|
112
|
+
... 'else %} {{- message.content }} {%- endif %} {{- "<|eot_id|>" }} {%- endif %} {%- endfor %} {%- if '
|
|
113
|
+
... "add_generation_prompt %} {{- '<|start_header_id|>assistant<|end_header_id|>\\n\\n' }} {%- endif %} "
|
|
114
|
+
... )
|
|
115
|
+
>>> prompt_assembler = (
|
|
116
|
+
... PromptAssembler()
|
|
117
|
+
... .setInputCol("messages")
|
|
118
|
+
... .setOutputCol("prompt")
|
|
119
|
+
... .setChatTemplate(template)
|
|
120
|
+
... )
|
|
121
|
+
>>> prompt_assembler.transform(df).select("prompt.result").show(truncate=False)
|
|
122
|
+
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
123
|
+
|result |
|
|
124
|
+
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
125
|
+
|[<|start_header_id|>system<|end_header_id|>\n\nYou are a helpful assistant.<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\nHello there, how can I help you?<|eot_id|><|start_header_id|>user<|end_header_id|>\n\nI need help with organizing my room.<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n]|
|
|
126
|
+
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
127
|
+
"""
|
|
128
|
+
|
|
129
|
+
outputAnnotatorType = AnnotatorType.DOCUMENT
|
|
130
|
+
|
|
131
|
+
inputCol = Param(
|
|
132
|
+
Params._dummy(),
|
|
133
|
+
"inputCol",
|
|
134
|
+
"input column name",
|
|
135
|
+
typeConverter=TypeConverters.toString,
|
|
136
|
+
)
|
|
137
|
+
outputCol = Param(
|
|
138
|
+
Params._dummy(),
|
|
139
|
+
"outputCol",
|
|
140
|
+
"output column name",
|
|
141
|
+
typeConverter=TypeConverters.toString,
|
|
142
|
+
)
|
|
143
|
+
chatTemplate = Param(
|
|
144
|
+
Params._dummy(),
|
|
145
|
+
"chatTemplate",
|
|
146
|
+
"Template used for the chat",
|
|
147
|
+
typeConverter=TypeConverters.toString,
|
|
148
|
+
)
|
|
149
|
+
addAssistant = Param(
|
|
150
|
+
Params._dummy(),
|
|
151
|
+
"addAssistant",
|
|
152
|
+
"Whether to add an assistant header to the end of the generated string",
|
|
153
|
+
typeConverter=TypeConverters.toBoolean,
|
|
154
|
+
)
|
|
155
|
+
name = "PromptAssembler"
|
|
156
|
+
|
|
157
|
+
@keyword_only
|
|
158
|
+
def __init__(self):
|
|
159
|
+
super(PromptAssembler, self).__init__(
|
|
160
|
+
classname="com.johnsnowlabs.nlp.PromptAssembler"
|
|
161
|
+
)
|
|
162
|
+
self._setDefault(outputCol="prompt", addAssistant=True)
|
|
163
|
+
|
|
164
|
+
@keyword_only
|
|
165
|
+
def setParams(self):
|
|
166
|
+
kwargs = self._input_kwargs
|
|
167
|
+
return self._set(**kwargs)
|
|
168
|
+
|
|
169
|
+
def setInputCol(self, value):
|
|
170
|
+
"""Sets input column name.
|
|
171
|
+
|
|
172
|
+
Parameters
|
|
173
|
+
----------
|
|
174
|
+
value : str
|
|
175
|
+
Name of the input column
|
|
176
|
+
"""
|
|
177
|
+
return self._set(inputCol=value)
|
|
178
|
+
|
|
179
|
+
def setOutputCol(self, value):
|
|
180
|
+
"""Sets output column name.
|
|
181
|
+
|
|
182
|
+
Parameters
|
|
183
|
+
----------
|
|
184
|
+
value : str
|
|
185
|
+
Name of the Output Column
|
|
186
|
+
"""
|
|
187
|
+
return self._set(outputCol=value)
|
|
188
|
+
|
|
189
|
+
def setChatTemplate(self, value):
|
|
190
|
+
"""Sets the chat template.
|
|
191
|
+
|
|
192
|
+
Parameters
|
|
193
|
+
----------
|
|
194
|
+
value : str
|
|
195
|
+
Template used for the chat
|
|
196
|
+
"""
|
|
197
|
+
return self._set(chatTemplate=value)
|
|
198
|
+
|
|
199
|
+
def setAddAssistant(self, value):
|
|
200
|
+
"""Sets whether to add an assistant header to the end of the generated string.
|
|
201
|
+
|
|
202
|
+
Parameters
|
|
203
|
+
----------
|
|
204
|
+
value : bool
|
|
205
|
+
Whether to add an assistant header to the end of the generated string
|
|
206
|
+
"""
|
|
207
|
+
return self._set(addAssistant=value)
|
sparknlp/internal/__init__.py
CHANGED
|
@@ -113,6 +113,13 @@ class _BertQuestionAnsweringLoader(ExtendedJavaWrapper):
|
|
|
113
113
|
jspark,
|
|
114
114
|
)
|
|
115
115
|
|
|
116
|
+
class _BertMultipleChoiceLoader(ExtendedJavaWrapper):
|
|
117
|
+
def __init__(self, path, jspark):
|
|
118
|
+
super(_BertMultipleChoiceLoader, self).__init__(
|
|
119
|
+
"com.johnsnowlabs.nlp.annotators.classifier.dl.BertForMultipleChoice.loadSavedModel",
|
|
120
|
+
path,
|
|
121
|
+
jspark,
|
|
122
|
+
)
|
|
116
123
|
|
|
117
124
|
class _DeBERTaLoader(ExtendedJavaWrapper):
|
|
118
125
|
def __init__(self, path, jspark):
|
|
@@ -999,3 +1006,18 @@ class _SnowFlakeEmbeddingsLoader(ExtendedJavaWrapper):
|
|
|
999
1006
|
super(_SnowFlakeEmbeddingsLoader, self).__init__(
|
|
1000
1007
|
"com.johnsnowlabs.nlp.embeddings.SnowFlakeEmbeddings.loadSavedModel", path, jspark
|
|
1001
1008
|
)
|
|
1009
|
+
|
|
1010
|
+
|
|
1011
|
+
class _AutoGGUFEmbeddingsLoader(ExtendedJavaWrapper):
|
|
1012
|
+
def __init__(self, path, jspark):
|
|
1013
|
+
super(_AutoGGUFEmbeddingsLoader, self).__init__(
|
|
1014
|
+
"com.johnsnowlabs.nlp.embeddings.AutoGGUFEmbeddings.loadSavedModel", path, jspark)
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
class _BLIPForQuestionAnswering(ExtendedJavaWrapper):
|
|
1018
|
+
def __init__(self, path, jspark):
|
|
1019
|
+
super(_BLIPForQuestionAnswering, self).__init__(
|
|
1020
|
+
"com.johnsnowlabs.nlp.annotators.cv.BLIPForQuestionAnswering.loadSavedModel",
|
|
1021
|
+
path,
|
|
1022
|
+
jspark,
|
|
1023
|
+
)
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
"""Module for reading different files types."""
|
|
15
|
+
from sparknlp.reader.sparknlp_reader import *
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# Copyright 2017-2024 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
|
+
from sparknlp.internal import ExtendedJavaWrapper
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class SparkNLPReader(ExtendedJavaWrapper):
|
|
18
|
+
"""Instantiates class to read HTML files.
|
|
19
|
+
|
|
20
|
+
Two types of input paths are supported,
|
|
21
|
+
|
|
22
|
+
htmlPath: this is a path to a directory of HTML files or a path to an HTML file
|
|
23
|
+
E.g. "path/html/files"
|
|
24
|
+
|
|
25
|
+
url: this is the URL or set of URLs of a website . E.g., "https://www.wikipedia.org"
|
|
26
|
+
|
|
27
|
+
Parameters
|
|
28
|
+
----------
|
|
29
|
+
params : spark
|
|
30
|
+
Spark session
|
|
31
|
+
params : dict, optional
|
|
32
|
+
Parameter with custom configuration
|
|
33
|
+
|
|
34
|
+
Examples
|
|
35
|
+
--------
|
|
36
|
+
>>> from sparknlp.reader import SparkNLPReader
|
|
37
|
+
>>> html_df = SparkNLPReader().html(spark, "https://www.wikipedia.org")
|
|
38
|
+
|
|
39
|
+
You can use SparkNLP for one line of code
|
|
40
|
+
>>> import sparknlp
|
|
41
|
+
>>> html_df = sparknlp.read().html("https://www.wikipedia.org")
|
|
42
|
+
>>> html_df.show(truncate=False)
|
|
43
|
+
|
|
44
|
+
+--------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
45
|
+
|url |html |
|
|
46
|
+
+--------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
47
|
+
|https://example.com/|[{Title, Example Domain, {pageNumber -> 1}}, {NarrativeText, 0, This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission., {pageNumber -> 1}}, {NarrativeText, 0, More information... More information..., {pageNumber -> 1}}] |
|
|
48
|
+
+--------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
49
|
+
>>> html_df.printSchema()
|
|
50
|
+
|
|
51
|
+
root
|
|
52
|
+
|-- url: string (nullable = true)
|
|
53
|
+
|-- html: array (nullable = true)
|
|
54
|
+
| |-- element: struct (containsNull = true)
|
|
55
|
+
| | |-- elementType: string (nullable = true)
|
|
56
|
+
| | |-- content: string (nullable = true)
|
|
57
|
+
| | |-- metadata: map (nullable = true)
|
|
58
|
+
| | | |-- key: string
|
|
59
|
+
| | | |-- value: string (valueContainsNull = true)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
Instantiates class to read email files.
|
|
64
|
+
|
|
65
|
+
emailPath: this is a path to a directory of HTML files or a path to an HTML file E.g.
|
|
66
|
+
"path/html/emails"
|
|
67
|
+
|
|
68
|
+
Examples
|
|
69
|
+
--------
|
|
70
|
+
>>> from sparknlp.reader import SparkNLPReader
|
|
71
|
+
>>> email_df = SparkNLPReader().email(spark, "home/user/emails-directory")
|
|
72
|
+
|
|
73
|
+
You can use SparkNLP for one line of code
|
|
74
|
+
>>> import sparknlp
|
|
75
|
+
>>> email_df = sparknlp.read().email("home/user/emails-directory")
|
|
76
|
+
>>> email_df.show(truncate=False)
|
|
77
|
+
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
78
|
+
|email |
|
|
79
|
+
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
80
|
+
|[{Title, Email Text Attachments, {sent_to -> Danilo Burbano <danilo@johnsnowlabs.com>, sent_from -> Danilo Burbano <danilo@johnsnowlabs.com>}}, {NarrativeText, Email test with two text attachments\r\n\r\nCheers,\r\n\r\n, {sent_to -> Danilo Burbano <danilo@johnsnowlabs.com>, sent_from -> Danilo Burbano <danilo@johnsnowlabs.com>, mimeType -> text/plain}}, {NarrativeText, <html>\r\n<head>\r\n<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">\r\n<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>\r\n</head>\r\n<body dir="ltr">\r\n<span style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">Email test with two text attachments</span>\r\n<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">\r\n<br>\r\n</div>\r\n<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">\r\nCheers,</div>\r\n<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">\r\n<br>\r\n</div>\r\n</body>\r\n</html>\r\n, {sent_to -> Danilo Burbano <danilo@johnsnowlabs.com>, sent_from -> Danilo Burbano <danilo@johnsnowlabs.com>, mimeType -> text/html}}, {Attachment, filename.txt, {sent_to -> Danilo Burbano <danilo@johnsnowlabs.com>, sent_from -> Danilo Burbano <danilo@johnsnowlabs.com>, contentType -> text/plain; name="filename.txt"}}, {NarrativeText, This is the content of the file.\n, {sent_to -> Danilo Burbano <danilo@johnsnowlabs.com>, sent_from -> Danilo Burbano <danilo@johnsnowlabs.com>, mimeType -> text/plain}}, {Attachment, filename2.txt, {sent_to -> Danilo Burbano <danilo@johnsnowlabs.com>, sent_from -> Danilo Burbano <danilo@johnsnowlabs.com>, contentType -> text/plain; name="filename2.txt"}}, {NarrativeText, This is an additional content file.\n, {sent_to -> Danilo Burbano <danilo@johnsnowlabs.com>, sent_from -> Danilo Burbano <danilo@johnsnowlabs.com>, mimeType -> text/plain}}]|
|
|
81
|
+
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
82
|
+
email_df.printSchema()
|
|
83
|
+
root
|
|
84
|
+
|-- path: string (nullable = true)
|
|
85
|
+
|-- content: array (nullable = true)
|
|
86
|
+
|-- email: array (nullable = true)
|
|
87
|
+
| |-- element: struct (containsNull = true)
|
|
88
|
+
| | |-- elementType: string (nullable = true)
|
|
89
|
+
| | |-- content: string (nullable = true)
|
|
90
|
+
| | |-- metadata: map (nullable = true)
|
|
91
|
+
| | | |-- key: string
|
|
92
|
+
| | | |-- value: string (valueContainsNull = true)
|
|
93
|
+
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
def __init__(self, spark, params=None):
|
|
97
|
+
if params is None:
|
|
98
|
+
params = {}
|
|
99
|
+
super(SparkNLPReader, self).__init__("com.johnsnowlabs.reader.SparkNLPReader", params)
|
|
100
|
+
self.spark = spark
|
|
101
|
+
|
|
102
|
+
def html(self, htmlPath):
|
|
103
|
+
if not isinstance(htmlPath, (str, list)) or (isinstance(htmlPath, list) and not all(isinstance(item, str) for item in htmlPath)):
|
|
104
|
+
raise TypeError("htmlPath must be a string or a list of strings")
|
|
105
|
+
jdf = self._java_obj.html(htmlPath)
|
|
106
|
+
dataframe = self.getDataFrame(self.spark, jdf)
|
|
107
|
+
return dataframe
|
|
108
|
+
|
|
109
|
+
def email(self, filePath):
|
|
110
|
+
if not isinstance(filePath, str):
|
|
111
|
+
raise TypeError("filePath must be a string")
|
|
112
|
+
jdf = self._java_obj.email(filePath)
|
|
113
|
+
dataframe = self.getDataFrame(self.spark, jdf)
|
|
114
|
+
return dataframe
|
|
115
|
+
|
|
116
|
+
def doc(self, docPath):
|
|
117
|
+
if not isinstance(docPath, str):
|
|
118
|
+
raise TypeError("docPath must be a string")
|
|
119
|
+
jdf = self._java_obj.doc(docPath)
|
|
120
|
+
dataframe = self.getDataFrame(self.spark, jdf)
|
|
121
|
+
return dataframe
|