Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,85 +17,91 @@ st.set_page_config(page_title="Enterprise document search + chat", layout="wide"
|
|
17 |
# Streamlit app header
|
18 |
st.title("Enterprise document helpdesk")
|
19 |
|
|
|
|
|
|
|
|
|
20 |
# Sidebar
|
21 |
with st.sidebar:
|
22 |
st.header("Configuration")
|
23 |
api_key = st.text_input("Enter your OpenAI API Key:", type="password")
|
24 |
if api_key:
|
25 |
os.environ["OPENAI_API_KEY"] = api_key
|
|
|
26 |
|
27 |
-
st.
|
28 |
-
|
29 |
-
|
30 |
-
def load_file(file):
|
31 |
-
file_extension = os.path.splitext(file.name)[1].lower()
|
32 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=file_extension) as temp_file:
|
33 |
-
temp_file.write(file.getvalue())
|
34 |
-
temp_file_path = temp_file.name
|
35 |
-
|
36 |
-
if file_extension == '.txt':
|
37 |
-
loader = TextLoader(temp_file_path)
|
38 |
-
elif file_extension == '.pdf':
|
39 |
-
loader = PyPDFLoader(temp_file_path)
|
40 |
-
elif file_extension == '.csv':
|
41 |
-
loader = CSVLoader(temp_file_path)
|
42 |
-
elif file_extension in ['.ppt', '.pptx']:
|
43 |
-
loader = UnstructuredPowerPointLoader(temp_file_path)
|
44 |
-
elif file_extension in ['.doc', '.docx']:
|
45 |
-
loader = UnstructuredWordDocumentLoader(temp_file_path)
|
46 |
-
elif file_extension in ['.xls', '.xlsx']:
|
47 |
-
loader = UnstructuredExcelLoader(temp_file_path)
|
48 |
-
else:
|
49 |
-
os.unlink(temp_file_path)
|
50 |
-
raise ValueError(f"Unsupported file type: {file_extension}")
|
51 |
-
|
52 |
-
documents = loader.load()
|
53 |
-
os.unlink(temp_file_path)
|
54 |
-
return documents
|
55 |
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
58 |
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
|
|
|
|
62 |
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
return response.content
|
69 |
-
|
70 |
-
# Process uploaded files
|
71 |
-
if uploaded_files:
|
72 |
-
if st.button("Process Documents"):
|
73 |
-
with st.spinner("Processing documents..."):
|
74 |
-
all_documents = []
|
75 |
-
for file in uploaded_files:
|
76 |
-
all_documents.extend(load_file(file))
|
77 |
-
|
78 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
79 |
-
all_splits = text_splitter.split_documents(all_documents)
|
80 |
-
|
81 |
-
# Store processed documents in session state
|
82 |
-
st.session_state.processed_documents = all_splits
|
83 |
-
st.success("Documents processed successfully!")
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
# Main app logic
|
98 |
-
if
|
99 |
# Initialize components
|
100 |
@st.cache_resource
|
101 |
def initialize_components():
|
@@ -208,10 +214,10 @@ if "OPENAI_API_KEY" in os.environ:
|
|
208 |
st.session_state.memory.save_context({"input": prompt}, {"output": full_response})
|
209 |
|
210 |
else:
|
211 |
-
st.
|
212 |
|
213 |
else:
|
214 |
-
st.
|
215 |
|
216 |
# Add a footer
|
217 |
st.markdown("---")
|
|
|
17 |
# Streamlit app header
|
18 |
st.title("Enterprise document helpdesk")
|
19 |
|
20 |
+
# Initialize session state
|
21 |
+
if 'api_key_entered' not in st.session_state:
|
22 |
+
st.session_state.api_key_entered = False
|
23 |
+
|
24 |
# Sidebar
|
25 |
with st.sidebar:
|
26 |
st.header("Configuration")
|
27 |
api_key = st.text_input("Enter your OpenAI API Key:", type="password")
|
28 |
if api_key:
|
29 |
os.environ["OPENAI_API_KEY"] = api_key
|
30 |
+
st.session_state.api_key_entered = True
|
31 |
|
32 |
+
if st.session_state.api_key_entered:
|
33 |
+
st.header('Document Upload and Processing')
|
34 |
+
uploaded_files = st.file_uploader('Upload your files', accept_multiple_files=True, type=['txt', 'pdf', 'csv', 'ppt', 'doc', 'xls', 'pptx', 'xlsx'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
def load_file(file):
|
37 |
+
file_extension = os.path.splitext(file.name)[1].lower()
|
38 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=file_extension) as temp_file:
|
39 |
+
temp_file.write(file.getvalue())
|
40 |
+
temp_file_path = temp_file.name
|
41 |
|
42 |
+
if file_extension == '.txt':
|
43 |
+
loader = TextLoader(temp_file_path)
|
44 |
+
elif file_extension == '.pdf':
|
45 |
+
loader = PyPDFLoader(temp_file_path)
|
46 |
+
elif file_extension == '.csv':
|
47 |
+
loader = CSVLoader(temp_file_path)
|
48 |
+
elif file_extension in ['.ppt', '.pptx']:
|
49 |
+
loader = UnstructuredPowerPointLoader(temp_file_path)
|
50 |
+
elif file_extension in ['.doc', '.docx']:
|
51 |
+
loader = UnstructuredWordDocumentLoader(temp_file_path)
|
52 |
+
elif file_extension in ['.xls', '.xlsx']:
|
53 |
+
loader = UnstructuredExcelLoader(temp_file_path)
|
54 |
+
else:
|
55 |
+
os.unlink(temp_file_path)
|
56 |
+
raise ValueError(f"Unsupported file type: {file_extension}")
|
57 |
|
58 |
+
documents = loader.load()
|
59 |
+
os.unlink(temp_file_path)
|
60 |
+
return documents
|
61 |
|
62 |
+
def summarize_documents(documents):
|
63 |
+
chat = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0.2)
|
64 |
+
|
65 |
+
combined_text = " ".join([doc.page_content for doc in documents])
|
66 |
+
|
67 |
+
prompt = f"""Summarize the following document in a concise manner, highlighting the key points:
|
68 |
|
69 |
+
{combined_text}
|
70 |
+
|
71 |
+
Summary:"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
response = chat.invoke(prompt)
|
74 |
+
return response.content
|
75 |
+
|
76 |
+
# Process uploaded files
|
77 |
+
if uploaded_files:
|
78 |
+
if st.button("Process Documents"):
|
79 |
+
with st.spinner("Processing documents..."):
|
80 |
+
all_documents = []
|
81 |
+
for file in uploaded_files:
|
82 |
+
all_documents.extend(load_file(file))
|
83 |
+
|
84 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
85 |
+
all_splits = text_splitter.split_documents(all_documents)
|
86 |
+
|
87 |
+
# Store processed documents in session state
|
88 |
+
st.session_state.processed_documents = all_splits
|
89 |
+
st.success("Documents processed successfully!")
|
90 |
+
|
91 |
+
# Add a button for summarization
|
92 |
+
if st.button("Generate Summary"):
|
93 |
+
with st.spinner("Generating summary..."):
|
94 |
+
summary = summarize_documents(st.session_state.processed_documents)
|
95 |
+
st.session_state.document_summary = summary
|
96 |
+
st.success("Summary generated successfully!")
|
97 |
+
|
98 |
+
# Display the summary if it exists
|
99 |
+
if 'document_summary' in st.session_state:
|
100 |
+
st.subheader("Document Summary")
|
101 |
+
st.write(st.session_state.document_summary)
|
102 |
|
103 |
# Main app logic
|
104 |
+
if st.session_state.api_key_entered:
|
105 |
# Initialize components
|
106 |
@st.cache_resource
|
107 |
def initialize_components():
|
|
|
214 |
st.session_state.memory.save_context({"input": prompt}, {"output": full_response})
|
215 |
|
216 |
else:
|
217 |
+
st.info("Please upload and process documents to start chatting.")
|
218 |
|
219 |
else:
|
220 |
+
st.info("Please enter your OpenAI API Key in the sidebar to start.")
|
221 |
|
222 |
# Add a footer
|
223 |
st.markdown("---")
|