Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,15 @@
|
|
1 |
import os
|
2 |
import logging
|
3 |
-
import subprocess
|
4 |
import tempfile
|
5 |
from typing import List
|
6 |
from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
-
from
|
9 |
-
from
|
10 |
from langchain.prompts import PromptTemplate
|
11 |
from langchain.schema import Document
|
12 |
from langchain.text_splitter import CharacterTextSplitter
|
13 |
-
from langchain.chains import MapReduceDocumentsChain
|
14 |
-
from langchain.runnables import RunnableMap, RunnableLambda
|
15 |
|
16 |
# Set up logging
|
17 |
logging.basicConfig(level=logging.INFO)
|
@@ -89,22 +87,18 @@ def summarize_report(documents: List[Document], llm) -> str:
|
|
89 |
map_prompt = PromptTemplate.from_template(map_template)
|
90 |
|
91 |
# Reduce prompt
|
92 |
-
reduce_template = """Combine these summaries into a final summary
|
93 |
reduce_prompt = PromptTemplate.from_template(reduce_template)
|
94 |
|
95 |
-
#
|
96 |
-
map_chain =
|
97 |
-
llm_chain=lambda text: llm(text=map_prompt.format(text=text))
|
|
|
|
|
|
|
98 |
)
|
99 |
|
100 |
-
|
101 |
-
reduce_chain = RunnableLambda(
|
102 |
-
llm_chain=lambda doc_summaries: llm(text=reduce_prompt.format(doc_summaries=doc_summaries))
|
103 |
-
)
|
104 |
-
|
105 |
-
# Run map-reduce sequence
|
106 |
-
summaries = map_chain.run([doc.page_content for doc in documents])
|
107 |
-
summary = reduce_chain.run({"doc_summaries": summaries})
|
108 |
|
109 |
return summary
|
110 |
|
|
|
1 |
import os
|
2 |
import logging
|
|
|
3 |
import tempfile
|
4 |
from typing import List
|
5 |
from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
|
6 |
from sentence_transformers import SentenceTransformer
|
7 |
+
from langchain_community.vectorstores import FAISS
|
8 |
+
from langchain_community.document_loaders import PyPDFLoader
|
9 |
from langchain.prompts import PromptTemplate
|
10 |
from langchain.schema import Document
|
11 |
from langchain.text_splitter import CharacterTextSplitter
|
12 |
+
from langchain.chains import MapReduceDocumentsChain, ReduceDocumentsChain
|
|
|
13 |
|
14 |
# Set up logging
|
15 |
logging.basicConfig(level=logging.INFO)
|
|
|
87 |
map_prompt = PromptTemplate.from_template(map_template)
|
88 |
|
89 |
# Reduce prompt
|
90 |
+
reduce_template = """Combine these summaries into a final summary:\n\nSummary:\n{doc_summaries}\n\nFinal Summary:"""
|
91 |
reduce_prompt = PromptTemplate.from_template(reduce_template)
|
92 |
|
93 |
+
# Create the chains
|
94 |
+
map_chain = MapReduceDocumentsChain(
|
95 |
+
llm_chain=lambda text: llm(text=map_prompt.format(text=text)),
|
96 |
+
reduce_documents_chain=ReduceDocumentsChain(
|
97 |
+
combine_documents_chain=lambda summaries: llm(text=reduce_prompt.format(doc_summaries=summaries))
|
98 |
+
),
|
99 |
)
|
100 |
|
101 |
+
summary = map_chain.run(documents)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
return summary
|
104 |
|