Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,49 @@
|
|
| 1 |
-
from dotenv import load_dotenv
|
| 2 |
import streamlit as st
|
| 3 |
-
|
| 4 |
-
from langchain.text_splitter import CharacterTextSplitter
|
| 5 |
-
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 6 |
-
from langchain.vectorstores import FAISS
|
| 7 |
-
from langchain.chains.question_answering import load_qa_chain
|
| 8 |
-
from langchain.llms import OpenAI
|
| 9 |
-
import time
|
| 10 |
-
import logging
|
| 11 |
-
import os
|
| 12 |
-
|
| 13 |
-
# Setup logging
|
| 14 |
-
logging.basicConfig(level=logging.INFO,
|
| 15 |
-
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 16 |
-
filename='app_log.log', # Log file name
|
| 17 |
-
filemode='a') # Append mode
|
| 18 |
|
| 19 |
def process_pdf(pdf):
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return text
|
| 28 |
|
| 29 |
def main():
|
| 30 |
load_dotenv()
|
| 31 |
st.set_page_config(page_title="EstateSphere Consulting")
|
| 32 |
-
st.header("EstateSphere Consulting")
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
pdfs = st.file_uploader("Upload your PDF files", type="pdf", accept_multiple_files=True)
|
| 38 |
|
| 39 |
if pdfs:
|
| 40 |
-
|
| 41 |
-
start_time = time.time()
|
| 42 |
text = ""
|
| 43 |
for pdf in pdfs:
|
| 44 |
text += process_pdf(pdf)
|
| 45 |
|
| 46 |
if not text:
|
| 47 |
-
st.
|
| 48 |
return
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
text_chunks = char_text_splitter.split_text(text)
|
| 56 |
-
|
| 57 |
-
embeddings = OpenAIEmbeddings()
|
| 58 |
-
docsearch = FAISS.from_texts(text_chunks, embeddings)
|
| 59 |
-
llm = OpenAI()
|
| 60 |
-
chain = load_qa_chain(llm, chain_type="stuff")
|
| 61 |
-
|
| 62 |
-
query = st.text_input("Type your question:")
|
| 63 |
-
|
| 64 |
-
if query:
|
| 65 |
-
qa_start_time = time.time()
|
| 66 |
-
docs = docsearch.similarity_search(query)
|
| 67 |
-
response = chain.run(input_documents=docs, question=query)
|
| 68 |
-
qa_end_time = time.time()
|
| 69 |
|
| 70 |
-
|
| 71 |
-
processing_info = f"Processing Time: {qa_end_time - qa_start_time:.2f} seconds"
|
| 72 |
-
st.session_state.chat_history.append({"question": query, "answer": response, "time": processing_info})
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
for chat in st.session_state.chat_history:
|
| 80 |
-
history_text += f"Q: {chat['question']}\nA: {chat['answer']}\n{chat['time']}\n---\n"
|
| 81 |
-
st.text_area("Chat History", history_text, height=300)
|
| 82 |
|
| 83 |
-
|
| 84 |
-
logging.error(f"An error occurred: {e}")
|
| 85 |
-
st.error(f"An error occurred: {e}")
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
main()
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
# Other imports remain the same
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def process_pdf(pdf):
|
| 5 |
+
# Function remains the same
|
| 6 |
+
|
| 7 |
+
def display_chat_history():
|
| 8 |
+
history_text = ""
|
| 9 |
+
for chat in st.session_state.chat_history:
|
| 10 |
+
history_text += f"Q: {chat['question']}\nA: {chat['answer']}\n{chat['time']}\n---\n"
|
| 11 |
+
st.text_area("Chat History", history_text, height=300)
|
|
|
|
| 12 |
|
| 13 |
def main():
|
| 14 |
load_dotenv()
|
| 15 |
st.set_page_config(page_title="EstateSphere Consulting")
|
| 16 |
+
st.header("🏢 EstateSphere Consulting")
|
| 17 |
+
|
| 18 |
+
# File uploader
|
| 19 |
+
pdfs = st.file_uploader("Upload your PDF files", type="pdf", accept_multiple_files=True, help="Upload PDF files for analysis")
|
|
|
|
|
|
|
| 20 |
|
| 21 |
if pdfs:
|
| 22 |
+
with st.spinner("Processing PDFs, please wait..."):
|
|
|
|
| 23 |
text = ""
|
| 24 |
for pdf in pdfs:
|
| 25 |
text += process_pdf(pdf)
|
| 26 |
|
| 27 |
if not text:
|
| 28 |
+
st.warning("No text could be extracted from the PDFs.")
|
| 29 |
return
|
| 30 |
|
| 31 |
+
# Chat interface
|
| 32 |
+
query = st.text_input("Type your question:", key="query")
|
| 33 |
+
if query:
|
| 34 |
+
with st.spinner("Finding your answer..."):
|
| 35 |
+
# Processing logic remains the same
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
display_chat_history()
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Help and support
|
| 40 |
+
st.sidebar.header("Help & Support")
|
| 41 |
+
st.sidebar.write("Need assistance? Reach out to our support team.")
|
| 42 |
|
| 43 |
+
# Footer
|
| 44 |
+
st.sidebar.text("© 2024 EstateSphere Consulting")
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Logging and exception handling remain the same
|
|
|
|
|
|
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
| 49 |
main()
|