Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
-
import streamlit as st
|
| 4 |
from PyPDF2 import PdfReader
|
| 5 |
from langchain.text_splitter import CharacterTextSplitter
|
| 6 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
|
@@ -11,9 +10,10 @@ import time
|
|
| 11 |
import logging
|
| 12 |
import pdfplumber
|
| 13 |
import os
|
|
|
|
| 14 |
|
| 15 |
def process_pdf(pdf):
|
| 16 |
-
|
| 17 |
text = ""
|
| 18 |
with pdfplumber.open(pdf) as pdf_reader:
|
| 19 |
for page in pdf_reader.pages:
|
|
@@ -23,11 +23,23 @@ def process_pdf(pdf):
|
|
| 23 |
return text
|
| 24 |
|
| 25 |
def display_chat_history():
|
|
|
|
|
|
|
| 26 |
history_text = ""
|
| 27 |
for chat in st.session_state.chat_history:
|
| 28 |
history_text += f"Q: {chat['question']}\nA: {chat['answer']}\n{chat['time']}\n---\n"
|
| 29 |
st.text_area("Chat History", history_text, height=300)
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def read_pdf(file_path):
|
| 32 |
with open(file_path, "rb") as file:
|
| 33 |
pdf_reader = PdfReader(file)
|
|
@@ -52,11 +64,11 @@ def read_documents_from_directory(directory):
|
|
| 52 |
elif filename.endswith(".docx"):
|
| 53 |
combined_text += read_word(file_path)
|
| 54 |
return combined_text
|
| 55 |
-
|
|
|
|
| 56 |
train_directory = r'C:\Users\writa\Downloads\Crypto'
|
| 57 |
|
| 58 |
def main():
|
| 59 |
-
|
| 60 |
load_dotenv()
|
| 61 |
st.set_page_config(page_title="EstateSphere")
|
| 62 |
st.header("🏢 EstateSphere")
|
|
@@ -75,13 +87,16 @@ def main():
|
|
| 75 |
|
| 76 |
# Chat interface
|
| 77 |
query = st.text_input("Type your question:", key="query")
|
|
|
|
| 78 |
if query:
|
| 79 |
with st.spinner("Finding your answer..."):
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# Help and support in the sidebar
|
| 87 |
st.sidebar.header("Help & Support")
|
|
@@ -89,10 +104,8 @@ def main():
|
|
| 89 |
|
| 90 |
# Footer
|
| 91 |
st.sidebar.text("© 2024 MosaicAI")
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
# Implement function to update chat history
|
| 95 |
-
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from dotenv import load_dotenv
|
|
|
|
| 3 |
from PyPDF2 import PdfReader
|
| 4 |
from langchain.text_splitter import CharacterTextSplitter
|
| 5 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
|
|
|
| 10 |
import logging
|
| 11 |
import pdfplumber
|
| 12 |
import os
|
| 13 |
+
import docx # Importing docx for Word document processing
|
| 14 |
|
| 15 |
def process_pdf(pdf):
|
| 16 |
+
start_time = time.time()
|
| 17 |
text = ""
|
| 18 |
with pdfplumber.open(pdf) as pdf_reader:
|
| 19 |
for page in pdf_reader.pages:
|
|
|
|
| 23 |
return text
|
| 24 |
|
| 25 |
def display_chat_history():
|
| 26 |
+
if 'chat_history' not in st.session_state:
|
| 27 |
+
st.session_state.chat_history = []
|
| 28 |
history_text = ""
|
| 29 |
for chat in st.session_state.chat_history:
|
| 30 |
history_text += f"Q: {chat['question']}\nA: {chat['answer']}\n{chat['time']}\n---\n"
|
| 31 |
st.text_area("Chat History", history_text, height=300)
|
| 32 |
|
| 33 |
+
def update_chat_history(question, answer):
|
| 34 |
+
if 'chat_history' not in st.session_state:
|
| 35 |
+
st.session_state.chat_history = []
|
| 36 |
+
st.session_state.chat_history.append({
|
| 37 |
+
"question": question,
|
| 38 |
+
"answer": answer,
|
| 39 |
+
"time": time.strftime("%Y-%m-%d %H:%M:%S")
|
| 40 |
+
})
|
| 41 |
+
|
| 42 |
+
|
| 43 |
def read_pdf(file_path):
|
| 44 |
with open(file_path, "rb") as file:
|
| 45 |
pdf_reader = PdfReader(file)
|
|
|
|
| 64 |
elif filename.endswith(".docx"):
|
| 65 |
combined_text += read_word(file_path)
|
| 66 |
return combined_text
|
| 67 |
+
|
| 68 |
+
|
| 69 |
train_directory = r'C:\Users\writa\Downloads\Crypto'
|
| 70 |
|
| 71 |
def main():
|
|
|
|
| 72 |
load_dotenv()
|
| 73 |
st.set_page_config(page_title="EstateSphere")
|
| 74 |
st.header("🏢 EstateSphere")
|
|
|
|
| 87 |
|
| 88 |
# Chat interface
|
| 89 |
query = st.text_input("Type your question:", key="query")
|
| 90 |
+
|
| 91 |
if query:
|
| 92 |
with st.spinner("Finding your answer..."):
|
| 93 |
+
try:
|
| 94 |
+
docs = docsearch.similarity_search(query)
|
| 95 |
+
response = chain.run(input_documents=docs, question=query)
|
| 96 |
+
update_chat_history(query, response)
|
| 97 |
+
display_chat_history()
|
| 98 |
+
except Exception as e:
|
| 99 |
+
st.error(f"An error occurred: {e}")
|
| 100 |
|
| 101 |
# Help and support in the sidebar
|
| 102 |
st.sidebar.header("Help & Support")
|
|
|
|
| 104 |
|
| 105 |
# Footer
|
| 106 |
st.sidebar.text("© 2024 MosaicAI")
|
| 107 |
+
|
| 108 |
+
update_chat_history(question, answer)
|
|
|
|
|
|
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
| 111 |
main()
|