wuenlp_tools.utils.summarize
1from typing import List, Type 2 3from pathlib import Path 4 5from loguru import logger 6from pydantic import BaseModel 7from tqdm import tqdm 8from wuenlp.impl.UIMANLPStructs import UIMADocument, UIMAAnnotation, UIMASystemScene, UIMASpan 9 10from wuenlp_tools.pipeline import PipelineProcessor 11from wuenlp_tools.utils.prompting import LLM, default_llm 12 13 14class Summary(BaseModel): 15 summary: str 16 keywords: List[str] 17 18 19class Summarizer(LLM): 20 def __init__(self, model=default_llm, system_prompt=None, num_sentences=3): 21 if system_prompt is None: 22 system_prompt = f"You are given a piece of text. Please summarize it in about {num_sentences} sentences." 23 if "{}" in system_prompt: 24 system_prompt = system_prompt.format(num_sentences) 25 if "{num_sentences}" in system_prompt: 26 system_prompt = system_prompt.format(num_sentences=num_sentences) 27 super().__init__(model, system_prompt, output_format=Summary) 28 29 30def summarize_doc(doc: UIMADocument, summarizer: Summarizer = Summarizer(), 31 unit_type: Type[UIMAAnnotation] = UIMASystemScene, 32 summary_variable_name="llama_summary", 33 keyword_variable_name="llama_keywords", overwrite: bool = False, ) -> UIMADocument: 34 segments = doc._get_annos_of_type(unit_type) 35 for i, segment in tqdm(enumerate(segments), total=len(segments)): 36 if not overwrite and summary_variable_name in segment.additional_features and segment.additional_features[ 37 summary_variable_name]: 38 continue 39 segment_text = segment.text 40 summary: Summary = summarizer(segment_text) 41 segment.additional_features[summary_variable_name] = summary.summary 42 segment.additional_features[keyword_variable_name] = summary.keywords 43 logger.info(summary) 44 return doc 45 46 47class SummarizeProcessor(PipelineProcessor): 48 def __init__(self, summarizer: Summarizer, overwrite: bool = False, summary_variable_name="llama_summary", 49 keyword_variable_name="llama_keywords"): 50 self.summarizer = summarizer 51 self.overwrite = overwrite 52 self.summary_variable_name = summary_variable_name 53 self.keyword_variable_name = keyword_variable_name 54 55 def __call__(self, doc: UIMADocument, unit_type: Type[UIMASpan] = UIMASystemScene, overwrite: bool = False, 56 **kwargs) -> UIMADocument: 57 return summarize_doc(doc, summary_variable_name=self.summary_variable_name, 58 keyword_variable_name=self.keyword_variable_name, unit_type=unit_type, 59 summarizer=self.summarizer, overwrite=overwrite)
!!! abstract "Usage Documentation" Models
A base class for creating Pydantic models.
Attributes:
__class_vars__: The names of the class variables defined on the model.
__private_attributes__: Metadata about the private attributes of the model.
__signature__: The synthesized __init__ [Signature][inspect.Signature] of the model.
__pydantic_complete__: Whether model building is completed, or if there are still undefined fields.
__pydantic_core_schema__: The core schema of the model.
__pydantic_custom_init__: Whether the model has a custom `__init__` function.
__pydantic_decorators__: Metadata containing the decorators defined on the model.
This replaces `Model.__validators__` and `Model.__root_validators__` from Pydantic V1.
__pydantic_generic_metadata__: A dictionary containing metadata about generic Pydantic models.
The `origin` and `args` items map to the [`__origin__`][genericalias.__origin__]
and [`__args__`][genericalias.__args__] attributes of [generic aliases][types-genericalias],
and the `parameter` item maps to the `__parameter__` attribute of generic classes.
__pydantic_parent_namespace__: Parent namespace of the model, used for automatic rebuilding of models.
__pydantic_post_init__: The name of the post-init method for the model, if defined.
__pydantic_root_model__: Whether the model is a [`RootModel`][pydantic.root_model.RootModel].
__pydantic_serializer__: The `pydantic-core` `SchemaSerializer` used to dump instances of the model.
__pydantic_validator__: The `pydantic-core` `SchemaValidator` used to validate instances of the model.
__pydantic_fields__: A dictionary of field names and their corresponding [`FieldInfo`][pydantic.fields.FieldInfo] objects.
__pydantic_computed_fields__: A dictionary of computed field names and their corresponding [`ComputedFieldInfo`][pydantic.fields.ComputedFieldInfo] objects.
__pydantic_extra__: A dictionary containing extra values, if [`extra`][pydantic.config.ConfigDict.extra]
is set to `'allow'`.
__pydantic_fields_set__: The names of fields explicitly set during instantiation.
__pydantic_private__: Values of private attributes set on the model instance.
20class Summarizer(LLM): 21 def __init__(self, model=default_llm, system_prompt=None, num_sentences=3): 22 if system_prompt is None: 23 system_prompt = f"You are given a piece of text. Please summarize it in about {num_sentences} sentences." 24 if "{}" in system_prompt: 25 system_prompt = system_prompt.format(num_sentences) 26 if "{num_sentences}" in system_prompt: 27 system_prompt = system_prompt.format(num_sentences=num_sentences) 28 super().__init__(model, system_prompt, output_format=Summary)
21 def __init__(self, model=default_llm, system_prompt=None, num_sentences=3): 22 if system_prompt is None: 23 system_prompt = f"You are given a piece of text. Please summarize it in about {num_sentences} sentences." 24 if "{}" in system_prompt: 25 system_prompt = system_prompt.format(num_sentences) 26 if "{num_sentences}" in system_prompt: 27 system_prompt = system_prompt.format(num_sentences=num_sentences) 28 super().__init__(model, system_prompt, output_format=Summary)
31def summarize_doc(doc: UIMADocument, summarizer: Summarizer = Summarizer(), 32 unit_type: Type[UIMAAnnotation] = UIMASystemScene, 33 summary_variable_name="llama_summary", 34 keyword_variable_name="llama_keywords", overwrite: bool = False, ) -> UIMADocument: 35 segments = doc._get_annos_of_type(unit_type) 36 for i, segment in tqdm(enumerate(segments), total=len(segments)): 37 if not overwrite and summary_variable_name in segment.additional_features and segment.additional_features[ 38 summary_variable_name]: 39 continue 40 segment_text = segment.text 41 summary: Summary = summarizer(segment_text) 42 segment.additional_features[summary_variable_name] = summary.summary 43 segment.additional_features[keyword_variable_name] = summary.keywords 44 logger.info(summary) 45 return doc
48class SummarizeProcessor(PipelineProcessor): 49 def __init__(self, summarizer: Summarizer, overwrite: bool = False, summary_variable_name="llama_summary", 50 keyword_variable_name="llama_keywords"): 51 self.summarizer = summarizer 52 self.overwrite = overwrite 53 self.summary_variable_name = summary_variable_name 54 self.keyword_variable_name = keyword_variable_name 55 56 def __call__(self, doc: UIMADocument, unit_type: Type[UIMASpan] = UIMASystemScene, overwrite: bool = False, 57 **kwargs) -> UIMADocument: 58 return summarize_doc(doc, summary_variable_name=self.summary_variable_name, 59 keyword_variable_name=self.keyword_variable_name, unit_type=unit_type, 60 summarizer=self.summarizer, overwrite=overwrite)
Base class for protocol classes.
Protocol classes are defined as::
class Proto(Protocol):
def meth(self) -> int:
...
Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing).
For example::
class C:
def meth(self) -> int:
return 0
def func(x: Proto) -> int:
return x.meth()
func(C()) # Passes static type check
See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as::
class GenProto[T](Protocol):
def meth(self) -> T:
...
49 def __init__(self, summarizer: Summarizer, overwrite: bool = False, summary_variable_name="llama_summary", 50 keyword_variable_name="llama_keywords"): 51 self.summarizer = summarizer 52 self.overwrite = overwrite 53 self.summary_variable_name = summary_variable_name 54 self.keyword_variable_name = keyword_variable_name