Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,6 +16,8 @@ from PIL import Image
|
|
| 16 |
import io
|
| 17 |
import base64
|
| 18 |
|
|
|
|
|
|
|
| 19 |
class StreamLitResponse(ResponseParser):
|
| 20 |
def __init__(self, context):
|
| 21 |
super().__init__(context)
|
|
@@ -128,8 +130,7 @@ def get_vectorstore(text_chunks):
|
|
| 128 |
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
| 129 |
return vectorstore
|
| 130 |
|
| 131 |
-
|
| 132 |
-
# Handling user questions
|
| 133 |
def handle_userinput(question, pdf_vectorstore, dfs):
|
| 134 |
if pdf_vectorstore and st.session_state.conversation:
|
| 135 |
response = st.session_state.conversation({"question": question})
|
|
@@ -137,18 +138,16 @@ def handle_userinput(question, pdf_vectorstore, dfs):
|
|
| 137 |
|
| 138 |
assistant_response = response['answer']
|
| 139 |
|
| 140 |
-
# Check if the response contains image or dataframe information (adapt as needed)
|
| 141 |
-
|
| 142 |
if isinstance(assistant_response, dict) and 'value' in assistant_response:
|
| 143 |
-
content_type = assistant_response.get('type')
|
| 144 |
content_value = assistant_response['value']
|
| 145 |
|
| 146 |
if content_type == "dataframe":
|
| 147 |
-
st.session_state.chat_history.append({"role": "assistant", "content": "DataFrame"})
|
| 148 |
-
st.session_state.chat_history.append({"role": "assistant", "dataframe": content_value})
|
| 149 |
-
elif content_type == "plot":
|
| 150 |
-
st.session_state.chat_history.append({"role": "assistant", "content": "Plot"})
|
| 151 |
-
st.session_state.chat_history.append({"role": "assistant", "plot": content_value})
|
| 152 |
else:
|
| 153 |
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
| 154 |
|
|
@@ -157,25 +156,12 @@ def handle_userinput(question, pdf_vectorstore, dfs):
|
|
| 157 |
|
| 158 |
st.rerun()
|
| 159 |
|
| 160 |
-
elif dfs:
|
| 161 |
-
|
| 162 |
-
st.session_state.chat_history.append({"role": "user", "content": question})
|
| 163 |
-
|
| 164 |
-
if isinstance(answer, dict) and 'value' in answer:
|
| 165 |
-
content_type = answer.get('type')
|
| 166 |
-
content_value = answer['value']
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
elif content_type == "plot":
|
| 172 |
-
st.session_state.chat_history.append({"role": "assistant", "content": "Plot"})
|
| 173 |
-
st.session_state.chat_history.append({"role": "assistant", "plot": content_value})
|
| 174 |
-
else:
|
| 175 |
-
st.session_state.chat_history.append({"role": "assistant", "content": answer})
|
| 176 |
-
|
| 177 |
-
else:
|
| 178 |
-
st.session_state.chat_history.append({"role": "assistant", "content": answer})
|
| 179 |
|
| 180 |
st.rerun()
|
| 181 |
|
|
@@ -192,43 +178,50 @@ def get_conversation_chain(vectorstore):
|
|
| 192 |
)
|
| 193 |
return conversation_chain
|
| 194 |
|
| 195 |
-
|
| 196 |
def main():
|
| 197 |
-
|
|
|
|
| 198 |
if "conversation" not in st.session_state:
|
| 199 |
st.session_state.conversation = None
|
| 200 |
if "chat_history" not in st.session_state:
|
| 201 |
st.session_state.chat_history = []
|
| 202 |
-
if "vectorstore" not in st.session_state:
|
| 203 |
st.session_state.vectorstore = None
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
st.header("Chat with PDFs and Data :books: :bar_chart:")
|
| 207 |
|
| 208 |
-
|
| 209 |
|
| 210 |
# Chat display
|
| 211 |
for message in st.session_state.chat_history:
|
| 212 |
with st.chat_message(message["role"]):
|
| 213 |
if "dataframe" in message:
|
| 214 |
-
st.dataframe(message["dataframe"])
|
| 215 |
elif "plot" in message:
|
| 216 |
if isinstance(message["plot"], Image.Image):
|
| 217 |
st.image(message["plot"])
|
| 218 |
elif isinstance(message["plot"], go.Figure):
|
| 219 |
st.plotly_chart(message["plot"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
else:
|
| 221 |
st.write("Unsupported plot format")
|
| 222 |
else:
|
| 223 |
st.write(message["content"])
|
| 224 |
|
|
|
|
|
|
|
| 225 |
if user_question:
|
| 226 |
handle_userinput(user_question, st.session_state.vectorstore, st.session_state.dfs)
|
| 227 |
|
| 228 |
with st.sidebar:
|
| 229 |
st.subheader("Your files")
|
| 230 |
uploaded_files = st.file_uploader(
|
| 231 |
-
"Upload PDFs, CSVs, or Excel files (up to 3)", accept_multiple_files=True, key="file_uploader"
|
| 232 |
)
|
| 233 |
|
| 234 |
if st.button("Process"):
|
|
@@ -247,7 +240,6 @@ def main():
|
|
| 247 |
st.session_state.dfs = None
|
| 248 |
data_uploaded = False
|
| 249 |
st.warning("Switching to PDF mode. Data files removed.")
|
| 250 |
-
# Rerun to clear file uploader
|
| 251 |
pdf_docs.append(uploaded_file)
|
| 252 |
pdf_uploaded = True
|
| 253 |
elif file_extension in ["csv", "xlsx", "xls"]:
|
|
@@ -257,7 +249,6 @@ def main():
|
|
| 257 |
st.session_state.conversation = None
|
| 258 |
pdf_uploaded = False
|
| 259 |
st.warning("Switching to Data mode. PDF files removed.")
|
| 260 |
-
# Rerun to clear file uploader
|
| 261 |
try:
|
| 262 |
if file_extension == 'csv':
|
| 263 |
df = pd.read_csv(uploaded_file)
|
|
@@ -288,4 +279,4 @@ def main():
|
|
| 288 |
st.rerun()
|
| 289 |
|
| 290 |
if __name__ == "__main__":
|
| 291 |
-
main()
|
|
|
|
| 16 |
import io
|
| 17 |
import base64
|
| 18 |
|
| 19 |
+
|
| 20 |
+
|
| 21 |
class StreamLitResponse(ResponseParser):
|
| 22 |
def __init__(self, context):
|
| 23 |
super().__init__(context)
|
|
|
|
| 130 |
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
| 131 |
return vectorstore
|
| 132 |
|
| 133 |
+
#handle user input
|
|
|
|
| 134 |
def handle_userinput(question, pdf_vectorstore, dfs):
|
| 135 |
if pdf_vectorstore and st.session_state.conversation:
|
| 136 |
response = st.session_state.conversation({"question": question})
|
|
|
|
| 138 |
|
| 139 |
assistant_response = response['answer']
|
| 140 |
|
|
|
|
|
|
|
| 141 |
if isinstance(assistant_response, dict) and 'value' in assistant_response:
|
| 142 |
+
content_type = assistant_response.get('type')
|
| 143 |
content_value = assistant_response['value']
|
| 144 |
|
| 145 |
if content_type == "dataframe":
|
| 146 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "DataFrame"})
|
| 147 |
+
st.session_state.chat_history.append({"role": "assistant", "dataframe": content_value})
|
| 148 |
+
elif content_type == "plot":
|
| 149 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "Plot"})
|
| 150 |
+
st.session_state.chat_history.append({"role": "assistant", "plot": content_value})
|
| 151 |
else:
|
| 152 |
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
| 153 |
|
|
|
|
| 156 |
|
| 157 |
st.rerun()
|
| 158 |
|
| 159 |
+
elif dfs:
|
| 160 |
+
results = generateResponse(question, dfs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
+
st.session_state.chat_history.append({"role": "user", "content": question})
|
| 163 |
+
for result in results:
|
| 164 |
+
st.session_state.chat_history.append({"role": "assistant", **result})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
st.rerun()
|
| 167 |
|
|
|
|
| 178 |
)
|
| 179 |
return conversation_chain
|
| 180 |
|
|
|
|
| 181 |
def main():
|
| 182 |
+
st.set_page_config(page_title="Chat with PDFs and Data", page_icon=":books:")
|
| 183 |
+
|
| 184 |
if "conversation" not in st.session_state:
|
| 185 |
st.session_state.conversation = None
|
| 186 |
if "chat_history" not in st.session_state:
|
| 187 |
st.session_state.chat_history = []
|
| 188 |
+
if "vectorstore" not in st.session_state:
|
| 189 |
st.session_state.vectorstore = None
|
| 190 |
+
if "dfs" not in st.session_state:
|
| 191 |
+
st.session_state.dfs = None
|
|
|
|
| 192 |
|
| 193 |
+
st.title("Chat with PDFs and Data :books: :bar_chart:")
|
| 194 |
|
| 195 |
# Chat display
|
| 196 |
for message in st.session_state.chat_history:
|
| 197 |
with st.chat_message(message["role"]):
|
| 198 |
if "dataframe" in message:
|
| 199 |
+
st.dataframe(message["dataframe"])
|
| 200 |
elif "plot" in message:
|
| 201 |
if isinstance(message["plot"], Image.Image):
|
| 202 |
st.image(message["plot"])
|
| 203 |
elif isinstance(message["plot"], go.Figure):
|
| 204 |
st.plotly_chart(message["plot"])
|
| 205 |
+
elif isinstance(message["plot"], bytes):
|
| 206 |
+
try:
|
| 207 |
+
image = Image.open(io.BytesIO(message["plot"]))
|
| 208 |
+
st.image(image)
|
| 209 |
+
except Exception as e:
|
| 210 |
+
st.error(f"Error displaying image: {e}")
|
| 211 |
else:
|
| 212 |
st.write("Unsupported plot format")
|
| 213 |
else:
|
| 214 |
st.write(message["content"])
|
| 215 |
|
| 216 |
+
user_question = st.chat_input("Ask a question about your documents or data:")
|
| 217 |
+
|
| 218 |
if user_question:
|
| 219 |
handle_userinput(user_question, st.session_state.vectorstore, st.session_state.dfs)
|
| 220 |
|
| 221 |
with st.sidebar:
|
| 222 |
st.subheader("Your files")
|
| 223 |
uploaded_files = st.file_uploader(
|
| 224 |
+
"Upload PDFs, CSVs, or Excel files (up to 3)", accept_multiple_files=True, key="file_uploader"
|
| 225 |
)
|
| 226 |
|
| 227 |
if st.button("Process"):
|
|
|
|
| 240 |
st.session_state.dfs = None
|
| 241 |
data_uploaded = False
|
| 242 |
st.warning("Switching to PDF mode. Data files removed.")
|
|
|
|
| 243 |
pdf_docs.append(uploaded_file)
|
| 244 |
pdf_uploaded = True
|
| 245 |
elif file_extension in ["csv", "xlsx", "xls"]:
|
|
|
|
| 249 |
st.session_state.conversation = None
|
| 250 |
pdf_uploaded = False
|
| 251 |
st.warning("Switching to Data mode. PDF files removed.")
|
|
|
|
| 252 |
try:
|
| 253 |
if file_extension == 'csv':
|
| 254 |
df = pd.read_csv(uploaded_file)
|
|
|
|
| 279 |
st.rerun()
|
| 280 |
|
| 281 |
if __name__ == "__main__":
|
| 282 |
+
main()
|