Spaces:
Sleeping
Sleeping
Update summarizer.py
Browse files- summarizer.py +32 -33
summarizer.py
CHANGED
@@ -1,33 +1,32 @@
|
|
1 |
-
from
|
2 |
-
from langchain_community.document_loaders import
|
3 |
-
from
|
4 |
-
from
|
5 |
-
from
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
ext
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
return summaries
|
|
|
1 |
+
from langchain_community.document_loaders import Docx2txtLoader, PyPDFLoader
|
2 |
+
from langchain_community.document_loaders import UnstructuredPowerPointLoader
|
3 |
+
from langchain_cohere.llms import Cohere
|
4 |
+
from langchain.chains.summarize import load_summarize_chain
|
5 |
+
from pathlib import Path
|
6 |
+
import os
|
7 |
+
|
8 |
+
def summarize_files(method, files):
|
9 |
+
# Initialize the LLM
|
10 |
+
llm = Cohere(temperature=0)
|
11 |
+
summaries = []
|
12 |
+
# Load and read each file
|
13 |
+
for file in os.listdir(files):
|
14 |
+
|
15 |
+
file_path = os.path.join(files, file)
|
16 |
+
ext = Path(file_path).suffix.lower()
|
17 |
+
if ext == '.pdf':
|
18 |
+
loader = PyPDFLoader(file_path)
|
19 |
+
elif ext == '.docx':
|
20 |
+
loader = Docx2txtLoader(file_path)
|
21 |
+
elif ext == '.pptx':
|
22 |
+
loader = UnstructuredPowerPointLoader(file_path)
|
23 |
+
else:
|
24 |
+
raise ValueError(f"Unsupported file extension: {ext}")
|
25 |
+
|
26 |
+
docs = loader.load_and_split()
|
27 |
+
# Initialize a summarization chain with the specified method
|
28 |
+
summarization_chain = load_summarize_chain(llm=llm, chain_type=method)
|
29 |
+
summary = summarization_chain.run(docs)
|
30 |
+
summaries.append(summary)
|
31 |
+
|
32 |
+
return summaries
|
|