Update app.py
Browse files
app.py
CHANGED
|
@@ -47,15 +47,15 @@ def handle_query(query):
|
|
| 47 |
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
| 48 |
index = load_index_from_storage(storage_context)
|
| 49 |
chat_text_qa_msgs = [
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
]
|
| 60 |
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
| 61 |
|
|
@@ -69,26 +69,29 @@ def handle_query(query):
|
|
| 69 |
else:
|
| 70 |
return "Sorry, I couldn't find an answer."
|
| 71 |
|
| 72 |
-
|
| 73 |
# Streamlit app initialization
|
| 74 |
-
st.
|
| 75 |
-
st.
|
| 76 |
-
st.markdown("
|
|
|
|
| 77 |
|
| 78 |
if 'messages' not in st.session_state:
|
| 79 |
st.session_state.messages = [{'role': 'assistant', "content": 'Hello! Upload a PDF and ask me anything about its content.'}]
|
| 80 |
|
| 81 |
with st.sidebar:
|
| 82 |
-
st.title("Menu
|
| 83 |
-
uploaded_file = st.file_uploader("Upload your PDF Files
|
| 84 |
if st.button("Submit & Process"):
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
user_prompt = st.chat_input("Ask me anything about the content of the PDF:")
|
| 94 |
if user_prompt:
|
|
@@ -96,6 +99,7 @@ if user_prompt:
|
|
| 96 |
response = handle_query(user_prompt)
|
| 97 |
st.session_state.messages.append({'role': 'assistant', "content": response})
|
| 98 |
|
|
|
|
| 99 |
for message in st.session_state.messages:
|
| 100 |
with st.chat_message(message['role']):
|
| 101 |
-
st.write(message['content'])
|
|
|
|
| 47 |
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
| 48 |
index = load_index_from_storage(storage_context)
|
| 49 |
chat_text_qa_msgs = [
|
| 50 |
+
(
|
| 51 |
+
"user",
|
| 52 |
+
"""You are a Q&A assistant named CHATTO, created by Suriya. You have a specific response programmed for when users specifically ask about your creator, Suriya. The response is: "I was created by Suriya, an enthusiast in Artificial Intelligence. He is dedicated to solving complex problems and delivering innovative solutions. With a strong focus on machine learning, deep learning, Python, generative AI, NLP, and computer vision, Suriya is passionate about pushing the boundaries of AI to explore new possibilities." For all other inquiries, your main goal is to provide answers as accurately as possible, based on the instructions and context you have been given. If a question does not match the provided context or is outside the scope of the document, kindly advise the user to ask questions within the context of the document.
|
| 53 |
+
Context:
|
| 54 |
+
{context_str}
|
| 55 |
+
Question:
|
| 56 |
+
{query_str}
|
| 57 |
+
"""
|
| 58 |
+
)
|
| 59 |
]
|
| 60 |
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
| 61 |
|
|
|
|
| 69 |
else:
|
| 70 |
return "Sorry, I couldn't find an answer."
|
| 71 |
|
|
|
|
| 72 |
# Streamlit app initialization
|
| 73 |
+
st.set_page_config(page_title="Chatbot for PDF - Vatsa Solution Pvt Ltd", page_icon="🗞️")
|
| 74 |
+
st.title("Chatbot for PDF Presented by Vatsa Solution Pvt Ltd")
|
| 75 |
+
st.markdown("### Retrieval-Augmented Generation")
|
| 76 |
+
st.markdown("Start chatting ... 🚀")
|
| 77 |
|
| 78 |
if 'messages' not in st.session_state:
|
| 79 |
st.session_state.messages = [{'role': 'assistant', "content": 'Hello! Upload a PDF and ask me anything about its content.'}]
|
| 80 |
|
| 81 |
with st.sidebar:
|
| 82 |
+
st.title("Menu")
|
| 83 |
+
uploaded_file = st.file_uploader("Upload your PDF Files", type=["pdf"])
|
| 84 |
if st.button("Submit & Process"):
|
| 85 |
+
if uploaded_file is not None:
|
| 86 |
+
with st.spinner("Processing..."):
|
| 87 |
+
filepath = "data/saved_pdf.pdf"
|
| 88 |
+
with open(filepath, "wb") as f:
|
| 89 |
+
f.write(uploaded_file.getbuffer())
|
| 90 |
+
data_ingestion() # Process PDF every time a new file is uploaded
|
| 91 |
+
st.success("PDF processed successfully!")
|
| 92 |
+
displayPDF(filepath) # Display the uploaded PDF
|
| 93 |
+
else:
|
| 94 |
+
st.warning("Please upload a PDF file.")
|
| 95 |
|
| 96 |
user_prompt = st.chat_input("Ask me anything about the content of the PDF:")
|
| 97 |
if user_prompt:
|
|
|
|
| 99 |
response = handle_query(user_prompt)
|
| 100 |
st.session_state.messages.append({'role': 'assistant', "content": response})
|
| 101 |
|
| 102 |
+
# Display chat messages
|
| 103 |
for message in st.session_state.messages:
|
| 104 |
with st.chat_message(message['role']):
|
| 105 |
+
st.write(message['content'])
|