Alanturner2 commited on
Commit
505c4ce
·
verified ·
1 Parent(s): 1b7db6a

Upload 2 files

Browse files
Files changed (2) hide show
  1. .env +1 -0
  2. custom_summarization_app.py +117 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENAI_API_KEY=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
custom_summarization_app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import streamlit as st
3
+ import os
4
+ from langchain.document_loaders import PyPDFLoader
5
+ from langchain import PromptTemplate
6
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
7
+ from langchain.chains.summarize import load_summarize_chain
8
+ from langchain.chat_models import ChatOpenAI
9
+
10
+ openai.api_key = os.environ["OPENAI_API_KEY"]
11
+
12
+ @st.cache_data
13
+ def setup_documents(pdf_file_path,chunk_size,chunk_overlap):
14
+ loader = PyPDFLoader(pdf_file_path)
15
+ docs_raw = loader.load()
16
+ docs_raw_text = [doc.page_content for doc in docs_raw]
17
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size,
18
+ chunk_overlap=chunk_overlap)
19
+ docs = text_splitter.create_documents(docs_raw_text)
20
+
21
+ return docs
22
+
23
+
24
+
25
+ def custom_summary(docs, llm, custom_prompt, chain_type, num_summaries):
26
+ custom_prompt = custom_prompt + """:\n {text}"""
27
+ COMBINE_PROMPT = PromptTemplate(template=custom_prompt, input_variables = ["text"])
28
+ MAP_PROMPT = PromptTemplate(template="Summarize:\n{text}", input_variables=["text"])
29
+ if chain_type == "map_reduce":
30
+ chain = load_summarize_chain(llm,chain_type=chain_type,
31
+ map_prompt=MAP_PROMPT,
32
+ combine_prompt=COMBINE_PROMPT)
33
+ else:
34
+ chain = load_summarize_chain(llm,chain_type=chain_type)
35
+
36
+ summaries = []
37
+ for i in range(num_summaries):
38
+ summary_output = chain({"input_documents": docs}, return_only_outputs=True)["output_text"]
39
+ summaries.append(summary_output)
40
+
41
+ return summaries
42
+
43
+ @st.cache_data
44
+ def color_chunks(text: str, chunk_size: int, overlap_size: int) -> str:
45
+ overlap_color = "#808080"
46
+ chunk_colors = ["#a8d08d", "#c6dbef", "#e6550d", "#fd8d3c", "#fdae6b", "#fdd0a2"] # Different shades of green for chunks
47
+
48
+ colored_text = ""
49
+ overlap = ""
50
+ color_index = 0
51
+ for i in range(0, len(text), chunk_size-overlap_size):
52
+ chunk = text[i:i+chunk_size]
53
+ if overlap:
54
+ colored_text += f'<mark style="background-color: {overlap_color};">{overlap}</mark>'
55
+ chunk = chunk[len(overlap):]
56
+ colored_text += f'<mark style="background-color: {chunk_colors[color_index]};">{chunk}</mark>'
57
+ color_index = (color_index + 1) % len(chunk_colors)
58
+ overlap = text[i+chunk_size-overlap_size:i+chunk_size]
59
+
60
+ return colored_text
61
+
62
+
63
+ def main():
64
+ st.set_page_config(layout="wide")
65
+ st.title("Custom Summarization App")
66
+ llm = st.sidebar.selectbox("LLM",["ChatGPT", "GPT4", "Other (open source in the future)"])
67
+ chain_type = st.sidebar.selectbox("Chain Type", ["map_reduce", "stuff", "refine"])
68
+ chunk_size = st.sidebar.slider("Chunk Size", min_value=20, max_value = 10000,
69
+ step=10, value=2000)
70
+ chunk_overlap = st.sidebar.slider("Chunk Overlap", min_value=5, max_value = 5000,
71
+ step=10, value=200)
72
+
73
+ if st.sidebar.checkbox("Debug chunk size"):
74
+ st.header("Interactive Text Chunk Visualization")
75
+
76
+ text_input = st.text_area("Input Text", "This is a test text to showcase the functionality of the interactive text chunk visualizer.")
77
+
78
+ # Set the minimum to 1, the maximum to 5000 and default to 100
79
+ html_code = color_chunks(text_input, chunk_size, chunk_overlap)
80
+ st.markdown(html_code, unsafe_allow_html=True)
81
+
82
+ else:
83
+ user_prompt = st.text_input("Enter the custom summary prompt")
84
+ pdf_file_path = st.text_input("Enther the pdf file path")
85
+
86
+ temperature = st.sidebar.number_input("Set the ChatGPT Temperature",
87
+ min_value = 0.0,
88
+ max_value=1.0,
89
+ step=0.1,
90
+ value=0.5)
91
+ num_summaries = st.sidebar.number_input("Number of summaries",
92
+ min_value = 1,
93
+ max_value = 10,
94
+ step = 1,
95
+ value=1)
96
+ if pdf_file_path != "":
97
+ docs = setup_documents(pdf_file_path, chunk_size, chunk_overlap)
98
+ st.write("PDF loaded successfully")
99
+
100
+ if llm=="ChatGPT":
101
+ llm = ChatOpenAI(temperature=temperature)
102
+ elif llm=="GPT4":
103
+ llm = ChatOpenAI(model_name="gpt-4",temperature=temperature)
104
+ else:
105
+ st.write("Using ChatGPT while open source models are not implemented!")
106
+ llm = ChatOpenAI(temperature=temperature)
107
+
108
+ if st.button("Summarize"):
109
+ result = custom_summary(docs, llm, user_prompt, chain_type, num_summaries)
110
+ st.write("Summary:")
111
+ for summary in result:
112
+ st.write(summary)
113
+
114
+
115
+ if __name__=="__main__":
116
+ main()
117
+