Huzaifa367 commited on
Commit
e5b175e
·
verified ·
1 Parent(s): 6060238

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -10
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import streamlit as st
 
2
  from PyPDF2 import PdfReader
3
  from langchain.text_splitter import RecursiveCharacterTextSplitter
4
  from langchain_groq import ChatGroq
@@ -71,38 +72,41 @@ def main():
71
  st.markdown("<h1 style='font-size:20px;'>ChatBot by Muhammad Huzaifa</h1>", unsafe_allow_html=True)
72
  api_key = st.secrets["inference_api_key"]
73
 
 
 
74
  # Sidebar column for file upload
75
  with st.sidebar:
76
  st.header("Chat with PDF")
77
- pdf_docs = st.file_uploader("Upload your PDF Files", accept_multiple_files=True, type=["pdf"])
78
 
79
  # Main column for displaying extracted text and user interaction
80
  col1, col2 = st.columns([1, 2])
81
- raw_text = None
82
- if pdf_docs:
83
  with col1:
84
  if st.button("Submit"):
85
  with st.spinner("Processing..."):
86
- raw_text = get_pdf_text(pdf_docs)
87
- text_chunks = get_text_chunks(raw_text)
88
  get_vector_store(text_chunks, api_key)
89
  st.success("Processing Complete")
90
-
 
91
  # Check if PDF documents are uploaded and processing is complete
92
- if pdf_docs and raw_text:
93
  with col1:
94
  user_question = st.text_input("Ask a question from the Docs")
95
  if user_question:
96
  user_input(user_question, api_key)
97
 
98
  # Display extracted text if available
99
- if raw_text is not None:
100
  with col2:
101
  st.subheader("Extracted Text from PDF:")
102
- st.text(raw_text)
103
 
104
  # Show message if no PDF documents are uploaded
105
- if not pdf_docs:
106
  with col1:
107
  st.write("Please upload a document first to proceed.")
108
 
 
1
  import streamlit as st
2
+ from streamlit.state.session_state import SessionState
3
  from PyPDF2 import PdfReader
4
  from langchain.text_splitter import RecursiveCharacterTextSplitter
5
  from langchain_groq import ChatGroq
 
72
  st.markdown("<h1 style='font-size:20px;'>ChatBot by Muhammad Huzaifa</h1>", unsafe_allow_html=True)
73
  api_key = st.secrets["inference_api_key"]
74
 
75
+ session_state = SessionState.get(pdf_docs=None, raw_text=None, processing_complete=False)
76
+
77
  # Sidebar column for file upload
78
  with st.sidebar:
79
  st.header("Chat with PDF")
80
+ session_state.pdf_docs = st.file_uploader("Upload your PDF Files", accept_multiple_files=True, type=["pdf"])
81
 
82
  # Main column for displaying extracted text and user interaction
83
  col1, col2 = st.columns([1, 2])
84
+
85
+ if session_state.pdf_docs:
86
  with col1:
87
  if st.button("Submit"):
88
  with st.spinner("Processing..."):
89
+ session_state.raw_text = get_pdf_text(session_state.pdf_docs)
90
+ text_chunks = get_text_chunks(session_state.raw_text)
91
  get_vector_store(text_chunks, api_key)
92
  st.success("Processing Complete")
93
+ session_state.processing_complete = True
94
+
95
  # Check if PDF documents are uploaded and processing is complete
96
+ if session_state.pdf_docs and session_state.raw_text and session_state.processing_complete:
97
  with col1:
98
  user_question = st.text_input("Ask a question from the Docs")
99
  if user_question:
100
  user_input(user_question, api_key)
101
 
102
  # Display extracted text if available
103
+ if session_state.raw_text is not None:
104
  with col2:
105
  st.subheader("Extracted Text from PDF:")
106
+ st.text(session_state.raw_text)
107
 
108
  # Show message if no PDF documents are uploaded
109
+ if not session_state.pdf_docs:
110
  with col1:
111
  st.write("Please upload a document first to proceed.")
112