File size: 2,658 Bytes
98346d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_text_splitters import RecursiveCharacterTextSplitter
from src.utils.llm import get_llm, get_fast_llm

# Prompt for short text summarization

SHORT_SUMMARY_PROMPT = PromptTemplate(
    input_variables=["text"],
    template="""You are an expert at summarizing content clearly and concisely.

            Summarize the following text in a clear, concise way. 
            Capture the key points and main ideas.

    Text : {text}

    Summary :"""

)


# Prompt for summarizing each chunk (map step)

MAP_PROMPT = PromptTemplate(
    input_variables=["text"],
    template="""Summarize the following section, capturing all key points:

{text}

Section Summary:"""
)


# Prompt for combining chunk summaries (reduce step)
REDUCE_PROMPT = PromptTemplate(
    input_variables=["text"],
    template="""You are given multiple summaries of different sections of a document.
Combine them into one final, coherent summary that captures all key points.

Summaries:
{text}

Final Summary:"""
)

def summarize_text(text: str, mode: str = "concise") -> str:
    """
    Summarizes text. Handles both short and long documents.

    Args:
        text: Text to summarize
        mode: 'concise' for short summary, 'detailed' for longer summary

    Returns:
        Summary string
    """
    if not text.strip():
        return "⚠️ Please enter text to summarize."
    
    if len(text.split())<1000 :
        llm =get_fast_llm()
        chain = SHORT_SUMMARY_PROMPT | llm | StrOutputParser()
        return chain.invoke({"text": text})
    #Use MAP REDUCE concept for longer string 

    return _map_reduce_summarize(text, mode)

def _map_reduce_summarize(text: str, mode: str) -> str:
    """Handles long document summarization using map-reduce."""
    # Split text into chunks
    splitter = RecursiveCharacterTextSplitter(
        chunk_size=3000,
        chunk_overlap=200
    )
    chunks = splitter.split_text(text)
    print(f"📄 Split into {len(chunks)} chunks for summarization...")

    llm = get_fast_llm()
    parser = StrOutputParser()

    # Map — summarize each chunk
    map_chain = MAP_PROMPT | llm | parser
    chunk_summaries = []
    for i, chunk in enumerate(chunks):
        summary = map_chain.invoke({"text": chunk})
        chunk_summaries.append(summary)
        print(f"📝 Summarized chunk {i + 1}/{len(chunks)}")

    # Reduce — combine all chunk summaries
    reduce_chain = REDUCE_PROMPT | llm | parser
    final_summary = reduce_chain.invoke({"text": "\n".join(chunk_summaries)})

    return final_summary