Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -93,9 +93,9 @@ def get_conversation_chain(vectorstore):
|
|
| 93 |
)
|
| 94 |
return conversation_chain
|
| 95 |
|
| 96 |
-
def main():
|
| 97 |
-
|
| 98 |
|
|
|
|
|
|
|
| 99 |
if "conversation" not in st.session_state:
|
| 100 |
st.session_state.conversation = None
|
| 101 |
if "chat_history" not in st.session_state:
|
|
@@ -108,11 +108,10 @@ def main():
|
|
| 108 |
user_question = st.text_input("Ask a question about your documents or data:")
|
| 109 |
|
| 110 |
if user_question:
|
| 111 |
-
handle_userinput(user_question, st.session_state.vectorstore, st.session_state.dfs)
|
| 112 |
|
| 113 |
with st.sidebar:
|
| 114 |
st.subheader("Your files")
|
| 115 |
-
st.sidebar.image("logoqb.jpeg", use_container_width=True)
|
| 116 |
uploaded_files = st.file_uploader(
|
| 117 |
"Upload PDFs, CSVs, or Excel files (up to 3)", accept_multiple_files=True
|
| 118 |
)
|
|
@@ -120,35 +119,53 @@ def main():
|
|
| 120 |
if st.button("Process"):
|
| 121 |
with st.spinner("Processing"):
|
| 122 |
pdf_docs = []
|
| 123 |
-
dfs = []
|
|
|
|
|
|
|
|
|
|
| 124 |
for uploaded_file in uploaded_files:
|
| 125 |
file_extension = uploaded_file.name.split(".")[-1].lower()
|
|
|
|
| 126 |
if file_extension == "pdf":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
pdf_docs.append(uploaded_file)
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
try:
|
| 130 |
if file_extension == 'csv':
|
| 131 |
df = pd.read_csv(uploaded_file)
|
| 132 |
else:
|
| 133 |
df = pd.read_excel(uploaded_file)
|
| 134 |
dfs.append(df)
|
|
|
|
| 135 |
except Exception as e:
|
| 136 |
st.error(f"Error reading {uploaded_file.name}: {e}")
|
| 137 |
-
st.stop()
|
| 138 |
|
| 139 |
-
if pdf_docs:
|
| 140 |
raw_text = get_pdf_text(pdf_docs)
|
| 141 |
text_chunks = get_text_chunks(raw_text)
|
| 142 |
st.session_state.vectorstore = get_vectorstore(text_chunks)
|
| 143 |
st.session_state.conversation = get_conversation_chain(st.session_state.vectorstore)
|
| 144 |
else:
|
| 145 |
-
st.session_state.vectorstore = None
|
|
|
|
| 146 |
|
| 147 |
-
if dfs:
|
| 148 |
st.session_state.dfs = dfs
|
| 149 |
else:
|
| 150 |
-
st.session_state.dfs = None
|
| 151 |
-
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
main()
|
|
|
|
|
|
| 93 |
)
|
| 94 |
return conversation_chain
|
| 95 |
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
def main():
|
| 98 |
+
|
| 99 |
if "conversation" not in st.session_state:
|
| 100 |
st.session_state.conversation = None
|
| 101 |
if "chat_history" not in st.session_state:
|
|
|
|
| 108 |
user_question = st.text_input("Ask a question about your documents or data:")
|
| 109 |
|
| 110 |
if user_question:
|
| 111 |
+
handle_userinput(user_question, st.session_state.vectorstore, st.session_state.dfs)
|
| 112 |
|
| 113 |
with st.sidebar:
|
| 114 |
st.subheader("Your files")
|
|
|
|
| 115 |
uploaded_files = st.file_uploader(
|
| 116 |
"Upload PDFs, CSVs, or Excel files (up to 3)", accept_multiple_files=True
|
| 117 |
)
|
|
|
|
| 119 |
if st.button("Process"):
|
| 120 |
with st.spinner("Processing"):
|
| 121 |
pdf_docs = []
|
| 122 |
+
dfs = []
|
| 123 |
+
pdf_uploaded = False
|
| 124 |
+
data_uploaded = False
|
| 125 |
+
|
| 126 |
for uploaded_file in uploaded_files:
|
| 127 |
file_extension = uploaded_file.name.split(".")[-1].lower()
|
| 128 |
+
|
| 129 |
if file_extension == "pdf":
|
| 130 |
+
if data_uploaded: # Check if data was already uploaded
|
| 131 |
+
if st.session_state.dfs:
|
| 132 |
+
st.session_state.dfs = None
|
| 133 |
+
data_uploaded = False
|
| 134 |
+
st.warning("Switching to PDF mode. Data files removed.") # Inform user
|
| 135 |
pdf_docs.append(uploaded_file)
|
| 136 |
+
pdf_uploaded = True
|
| 137 |
+
elif file_extension in ["csv", "xlsx", "xls"]:
|
| 138 |
+
if pdf_uploaded: # Check if PDF was already uploaded
|
| 139 |
+
if st.session_state.vectorstore:
|
| 140 |
+
st.session_state.vectorstore = None
|
| 141 |
+
st.session_state.conversation = None
|
| 142 |
+
pdf_uploaded = False
|
| 143 |
+
st.warning("Switching to Data mode. PDF files removed.") # Inform user
|
| 144 |
try:
|
| 145 |
if file_extension == 'csv':
|
| 146 |
df = pd.read_csv(uploaded_file)
|
| 147 |
else:
|
| 148 |
df = pd.read_excel(uploaded_file)
|
| 149 |
dfs.append(df)
|
| 150 |
+
data_uploaded = True
|
| 151 |
except Exception as e:
|
| 152 |
st.error(f"Error reading {uploaded_file.name}: {e}")
|
| 153 |
+
st.stop()
|
| 154 |
|
| 155 |
+
if pdf_docs:
|
| 156 |
raw_text = get_pdf_text(pdf_docs)
|
| 157 |
text_chunks = get_text_chunks(raw_text)
|
| 158 |
st.session_state.vectorstore = get_vectorstore(text_chunks)
|
| 159 |
st.session_state.conversation = get_conversation_chain(st.session_state.vectorstore)
|
| 160 |
else:
|
| 161 |
+
st.session_state.vectorstore = None
|
| 162 |
+
st.session_state.conversation = None
|
| 163 |
|
| 164 |
+
if dfs:
|
| 165 |
st.session_state.dfs = dfs
|
| 166 |
else:
|
| 167 |
+
st.session_state.dfs = None
|
|
|
|
| 168 |
|
| 169 |
if __name__ == "__main__":
|
| 170 |
main()
|
| 171 |
+
|