Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,16 +17,14 @@ 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 |
-
|
28 |
-
if "OPENAI_API_KEY" in os.environ:
|
29 |
-
st.header('Multiple File Upload')
|
30 |
uploaded_files = st.file_uploader('Upload your files', accept_multiple_files=True, type=['txt', 'pdf', 'csv', 'ppt', 'doc', 'xls', 'pptx', 'xlsx'])
|
31 |
|
32 |
def load_file(file):
|
@@ -55,6 +53,20 @@ if "OPENAI_API_KEY" in os.environ:
|
|
55 |
os.unlink(temp_file_path)
|
56 |
return documents
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
# Process uploaded files
|
59 |
if uploaded_files:
|
60 |
if st.button("Process Documents"):
|
@@ -69,7 +81,21 @@ if "OPENAI_API_KEY" in os.environ:
|
|
69 |
# Store processed documents in session state
|
70 |
st.session_state.processed_documents = all_splits
|
71 |
st.success("Documents processed successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
|
|
|
|
73 |
# Initialize components
|
74 |
@st.cache_resource
|
75 |
def initialize_components():
|
@@ -79,8 +105,7 @@ if "OPENAI_API_KEY" in os.environ:
|
|
79 |
return chat, embeddings
|
80 |
|
81 |
# Load components
|
82 |
-
|
83 |
-
chat, embeddings = initialize_components()
|
84 |
|
85 |
# Create vectorstore and retriever only if documents are processed
|
86 |
if 'processed_documents' in st.session_state:
|
|
|
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.header('Document Upload and Processing')
|
|
|
|
|
28 |
uploaded_files = st.file_uploader('Upload your files', accept_multiple_files=True, type=['txt', 'pdf', 'csv', 'ppt', 'doc', 'xls', 'pptx', 'xlsx'])
|
29 |
|
30 |
def load_file(file):
|
|
|
53 |
os.unlink(temp_file_path)
|
54 |
return documents
|
55 |
|
56 |
+
def summarize_documents(documents):
|
57 |
+
chat = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0.2)
|
58 |
+
|
59 |
+
combined_text = " ".join([doc.page_content for doc in documents])
|
60 |
+
|
61 |
+
prompt = f"""Summarize the following document in a concise manner, highlighting the key points:
|
62 |
+
|
63 |
+
{combined_text}
|
64 |
+
|
65 |
+
Summary:"""
|
66 |
+
|
67 |
+
response = chat.invoke(prompt)
|
68 |
+
return response.content
|
69 |
+
|
70 |
# Process uploaded files
|
71 |
if uploaded_files:
|
72 |
if st.button("Process Documents"):
|
|
|
81 |
# Store processed documents in session state
|
82 |
st.session_state.processed_documents = all_splits
|
83 |
st.success("Documents processed successfully!")
|
84 |
+
|
85 |
+
# Add a button for summarization
|
86 |
+
if st.button("Generate Summary"):
|
87 |
+
with st.spinner("Generating summary..."):
|
88 |
+
summary = summarize_documents(st.session_state.processed_documents)
|
89 |
+
st.session_state.document_summary = summary
|
90 |
+
st.success("Summary generated successfully!")
|
91 |
+
|
92 |
+
# Display the summary if it exists
|
93 |
+
if 'document_summary' in st.session_state:
|
94 |
+
st.subheader("Document Summary")
|
95 |
+
st.write(st.session_state.document_summary)
|
96 |
|
97 |
+
# Main app logic
|
98 |
+
if "OPENAI_API_KEY" in os.environ:
|
99 |
# Initialize components
|
100 |
@st.cache_resource
|
101 |
def initialize_components():
|
|
|
105 |
return chat, embeddings
|
106 |
|
107 |
# Load components
|
108 |
+
chat, embeddings = initialize_components()
|
|
|
109 |
|
110 |
# Create vectorstore and retriever only if documents are processed
|
111 |
if 'processed_documents' in st.session_state:
|