DrishtiSharma commited on
Commit
f172bb5
Β·
verified Β·
1 Parent(s): 44e6288

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -21
app.py CHANGED
@@ -28,10 +28,18 @@ if "vector_store" not in st.session_state:
28
  st.session_state.vector_store = None
29
  if "documents" not in st.session_state:
30
  st.session_state.documents = None
31
-
32
- # Step 1: Choose PDF Source
33
- pdf_source = st.radio("Upload or provide a link to a PDF:", ["Upload a PDF file", "Enter a PDF URL"], index=0, horizontal=True)
34
-
 
 
 
 
 
 
 
 
35
  if pdf_source == "Upload a PDF file":
36
  uploaded_file = st.file_uploader("Upload your PDF file", type="pdf")
37
  if uploaded_file:
@@ -39,6 +47,7 @@ if pdf_source == "Upload a PDF file":
39
  with open(pdf_path, "wb") as f:
40
  f.write(uploaded_file.getbuffer())
41
  st.success("βœ… PDF Uploaded Successfully!")
 
42
 
43
  elif pdf_source == "Enter a PDF URL":
44
  pdf_url = st.text_input("Enter PDF URL:")
@@ -51,17 +60,12 @@ elif pdf_source == "Enter a PDF URL":
51
  with open(pdf_path, "wb") as f:
52
  f.write(response.content)
53
  st.success("βœ… PDF Downloaded Successfully!")
 
54
  else:
55
  st.error("❌ Failed to download PDF. Check the URL.")
56
- pdf_path = None
57
- except Exception as e:
58
- st.error(f"Error downloading PDF: {e}")
59
- pdf_path = None
60
- else:
61
- pdf_path = None
62
 
63
  # Step 2: Process PDF and Create Vector Store (Only if Not Processed)
64
- if pdf_path and st.session_state.vector_store is None:
65
  with st.spinner("Loading and processing PDF..."):
66
  loader = PDFPlumberLoader(pdf_path)
67
  docs = loader.load()
@@ -73,7 +77,7 @@ if pdf_path and st.session_state.vector_store is None:
73
  embedding_model = HuggingFaceEmbeddings(model_name=model_name, model_kwargs={'device': 'cpu'})
74
  text_splitter = SemanticChunker(embedding_model)
75
  documents = text_splitter.split_documents(docs)
76
- st.session_state.documents = documents # Store in session state
77
  st.success(f"βœ… **Document Chunked!** Total Chunks: {len(documents)}")
78
 
79
  # Step 4: Setup Vectorstore
@@ -85,7 +89,8 @@ if pdf_path and st.session_state.vector_store is None:
85
  )
86
  vector_store.add_documents(documents)
87
  num_documents = len(vector_store.get()["documents"])
88
- st.session_state.vector_store = vector_store # Store vector store in session state
 
89
  st.success(f"βœ… **Vector Store Created!** Total documents stored: {num_documents}")
90
 
91
  # Step 5: Query Input (Only allow if vector store exists)
@@ -137,13 +142,5 @@ if st.session_state.vector_store:
137
  st.subheader("πŸŸ₯ RAG Final Response")
138
  st.success(final_response['final_response'])
139
 
140
- # Step 10: Display Workflow Breakdown
141
- st.subheader("πŸ” **Workflow Breakdown:**")
142
- st.json({
143
- "Context Relevancy Evaluation": relevancy_response["relevancy_response"],
144
- "Relevant Contexts": relevant_response["context_number"],
145
- "Extracted Contexts": final_contexts["relevant_contexts"],
146
- "Final Answer": final_response["final_response"]
147
- })
148
  else:
149
  st.warning("πŸ“„ Please upload or provide a PDF URL first.")
 
28
  st.session_state.vector_store = None
29
  if "documents" not in st.session_state:
30
  st.session_state.documents = None
31
+ if "processed" not in st.session_state:
32
+ st.session_state.processed = False # Prevent redundant processing
33
+
34
+ # Step 1: Choose PDF Source (Horizontal radio buttons)
35
+ pdf_source = st.radio(
36
+ "Upload or provide a link to a PDF:",
37
+ ["Upload a PDF file", "Enter a PDF URL"],
38
+ index=0,
39
+ horizontal=True
40
+ )
41
+
42
+ pdf_path = None
43
  if pdf_source == "Upload a PDF file":
44
  uploaded_file = st.file_uploader("Upload your PDF file", type="pdf")
45
  if uploaded_file:
 
47
  with open(pdf_path, "wb") as f:
48
  f.write(uploaded_file.getbuffer())
49
  st.success("βœ… PDF Uploaded Successfully!")
50
+ st.session_state.processed = False # Reset processing
51
 
52
  elif pdf_source == "Enter a PDF URL":
53
  pdf_url = st.text_input("Enter PDF URL:")
 
60
  with open(pdf_path, "wb") as f:
61
  f.write(response.content)
62
  st.success("βœ… PDF Downloaded Successfully!")
63
+ st.session_state.processed = False # Reset processing
64
  else:
65
  st.error("❌ Failed to download PDF. Check the URL.")
 
 
 
 
 
 
66
 
67
  # Step 2: Process PDF and Create Vector Store (Only if Not Processed)
68
+ if pdf_path and not st.session_state.processed:
69
  with st.spinner("Loading and processing PDF..."):
70
  loader = PDFPlumberLoader(pdf_path)
71
  docs = loader.load()
 
77
  embedding_model = HuggingFaceEmbeddings(model_name=model_name, model_kwargs={'device': 'cpu'})
78
  text_splitter = SemanticChunker(embedding_model)
79
  documents = text_splitter.split_documents(docs)
80
+ st.session_state.documents = documents
81
  st.success(f"βœ… **Document Chunked!** Total Chunks: {len(documents)}")
82
 
83
  # Step 4: Setup Vectorstore
 
89
  )
90
  vector_store.add_documents(documents)
91
  num_documents = len(vector_store.get()["documents"])
92
+ st.session_state.vector_store = vector_store # Store in session state
93
+ st.session_state.processed = True # Mark as processed
94
  st.success(f"βœ… **Vector Store Created!** Total documents stored: {num_documents}")
95
 
96
  # Step 5: Query Input (Only allow if vector store exists)
 
142
  st.subheader("πŸŸ₯ RAG Final Response")
143
  st.success(final_response['final_response'])
144
 
 
 
 
 
 
 
 
 
145
  else:
146
  st.warning("πŸ“„ Please upload or provide a PDF URL first.")