File size: 1,626 Bytes
c8e875f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Text content processor for summarization.
"""
from typing import List, Any, Callable

from langchain_google_genai        import ChatGoogleGenerativeAI
from langchain_core.prompts        import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

from src.config             import MODEL_NAME
from src.processors.prompts import TEXT_SUMMARY_PROMPT


class TextProcessor:
    """Text content processor for summarization."""
    
    def __init__(self, model_name: str = MODEL_NAME):
        """
        Initialize the text processor.
        
        Args:
            model_name (str): Name of the LLM model to use
        """
        self.llm   = ChatGoogleGenerativeAI(model=model_name)
        self.chain = self._create_summary_chain()
        
        
    def _create_summary_chain(self) -> Callable:
        """
        Create the text summarization chain.
        
        Returns:
            Callable: The text summarization chain
        """
        return (
            {'text': lambda x: x}
            | ChatPromptTemplate.from_template(TEXT_SUMMARY_PROMPT)
            | self.llm
            | StrOutputParser()
        )
    
    
    def process(self, texts: List[Any]) -> List[str]:
        """
        Process and summarize text elements.
        
        Args:
            texts (List[Any]): List of text elements to summarize
            
        Returns:
            List[str]: List of text summaries
        """
        summaries = []
        for text in texts:
            summary = self.chain.invoke(text.text)
            summaries.append(summary) 
        return summaries