AkashVD26 commited on
Commit
55d5c44
1 Parent(s): bd11f04

better UI for HF space

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -31,14 +31,14 @@ if "messages" not in st.session_state:
31
  st.session_state["messages"] = [
32
  {"role": "assistant", "content": "Hi! I am PDFSense. Upload your PDF and ask me anything related to it."}
33
  ]
34
-
35
  # Process PDFs if uploaded
36
  if uploaded_files:
37
  documents = []
38
  for uploaded_file in uploaded_files:
39
  temppdf = "./temp.pdf"
40
  with open(temppdf, "wb") as file:
41
- file.write(uploaded_file.read())
42
  docs = PyPDFLoader(temppdf).load()
43
  documents.extend(docs)
44
  os.remove("./temp.pdf") # Clean up temporary file
@@ -70,20 +70,22 @@ if uploaded_files:
70
  rag_chain = create_retrieval_chain(history_aware_ret, qa_chain)
71
 
72
  # Display chat history
73
- for msg in st.session_state["messages"]:
74
- st.chat_message(msg["role"]).write(msg["content"])
75
 
76
  # User input handling
77
- if user_input := st.chat_input(placeholder="Ask a question about your uploaded PDF..."):
78
- st.session_state["messages"].append({"role": "user", "content": user_input})
79
- st.chat_message("user").write(user_input)
80
 
81
- # Run retrieval and answer generation using invoke()
82
- with st.chat_message("assistant"):
83
- chat_history = [{"role": msg["role"], "content": msg["content"]} for msg in st.session_state["messages"]]
84
- result = rag_chain.invoke({"input": user_input, "chat_history": chat_history})
85
-
86
- # Extract and display only the answer
87
- answer = result.get("answer", "I don't know.")
88
- st.session_state["messages"].append({"role": "assistant", "content": answer})
89
- st.write(answer)
 
 
 
31
  st.session_state["messages"] = [
32
  {"role": "assistant", "content": "Hi! I am PDFSense. Upload your PDF and ask me anything related to it."}
33
  ]
34
+ st.text("If the application fails to read the PDFs, try refreshing the webpage.")
35
  # Process PDFs if uploaded
36
  if uploaded_files:
37
  documents = []
38
  for uploaded_file in uploaded_files:
39
  temppdf = "./temp.pdf"
40
  with open(temppdf, "wb") as file:
41
+ file.write(uploaded_file.getvalue())
42
  docs = PyPDFLoader(temppdf).load()
43
  documents.extend(docs)
44
  os.remove("./temp.pdf") # Clean up temporary file
 
70
  rag_chain = create_retrieval_chain(history_aware_ret, qa_chain)
71
 
72
  # Display chat history
73
+ for msg in st.session_state["messages"]:
74
+ st.chat_message(msg["role"]).write(msg["content"])
75
 
76
  # User input handling
77
+ if user_input := st.chat_input(placeholder="Ask a question about your uploaded PDF..."):
78
+ st.session_state["messages"].append({"role": "user", "content": user_input})
79
+ st.chat_message("user").write(user_input)
80
 
81
+ # Run retrieval and answer generation using invoke()
82
+ with st.chat_message("assistant"):
83
+ chat_history = [{"role": msg["role"], "content": msg["content"]} for msg in st.session_state["messages"]]
84
+ result = rag_chain.invoke({"input": user_input, "chat_history": chat_history})
85
+
86
+ # Extract and display only the answer
87
+ answer = result.get("answer", "I don't know.")
88
+ st.session_state["messages"].append({"role": "assistant", "content": answer})
89
+ st.write(answer)
90
+ else:
91
+ st.error("Enter PDFs.")