File size: 1,265 Bytes
132646e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain.document_loaders import PyPDFLoader
from langchain_community.document_loaders import Docx2txtLoader
from langchain_community.document_loaders import UnstructuredPowerPointLoader
from langchain_cohere.llms import Cohere
from langchain.chains.summarize import load_summarize_chain
from pathlib import Path
import os

def summarize_files(method, files):
    # Initialize the LLM
    llm = Cohere(temperature=0)
    summaries = []
    # Load and read each file
    for file in os.listdir(files):
        
        file_path = os.path.join(files, file)
        ext = Path(file_path).suffix.lower()
        if ext == '.pdf':
            loader = PyPDFLoader(file_path)
        elif ext == '.docx':
            loader = Docx2txtLoader(file_path)
        elif ext == '.pptx':
            loader = UnstructuredPowerPointLoader(file_path)
        else:
            raise ValueError(f"Unsupported file extension: {ext}")
        
        docs = loader.load_and_split()
        # Initialize a summarization chain with the specified method
        summarization_chain = load_summarize_chain(llm=llm, chain_type=method)
        summary = summarization_chain.run(docs)
        summaries.append(summary)    
    
    return summaries