danishjameel003 commited on
Commit
cef3ca5
·
verified ·
1 Parent(s): a7cd9a0

update app.py

Browse files

Changing the Interface of the web Remove the Data Web source.

Files changed (1) hide show
  1. app.py +24 -46
app.py CHANGED
@@ -11,8 +11,6 @@ from dotenv import load_dotenv
11
 
12
  load_dotenv()
13
 
14
-
15
-
16
  # Creating custom template to guide LLM model
17
  custom_template = """Given the following conversation and a follow-up question, rephrase the follow-up question to be a standalone question, in its original language.
18
  Chat History:
@@ -69,19 +67,16 @@ def get_conversationchain(vectorstore):
69
 
70
  def conversation_chain(inputs):
71
  question = inputs['question']
72
- # Extract text content from Document objects
73
  documents = vectorstore.similarity_search(question, k=5)
74
 
75
- # If no similar documents are found or similarity is too low
76
  if not documents:
77
  answer = "Sorry, I couldn't find relevant information in the document. Please ask a question related to the document."
78
  memory.save_context({"user_input": question}, {"answer": answer})
79
  return {"chat_history": memory.chat_memory.messages, "answer": answer}
80
 
81
- context = "\n".join([doc.page_content for doc in documents]) # Extract `page_content` from each Document
82
  answer, score = qa_function(question, context)
83
 
84
- # Define a threshold for confidence (e.g., 0.5)
85
  if score < 0.5:
86
  answer = "Sorry, I couldn't find relevant information in the document. Please ask a question related to the document."
87
 
@@ -127,51 +122,34 @@ def main():
127
  preview_folders = {subject: os.path.join(preview_folder, subject.replace(' ', '_')) for subject in subjects}
128
  selected_subject = st.sidebar.selectbox("Select a Subject:", subjects)
129
 
130
- st.sidebar.info(f"You have selected: {selected_subject}") # Display selected subject
131
-
132
- # Option to upload documents or use preloaded subject data
133
- use_preloaded = st.sidebar.radio("Select Data Source:", ("Use Preloaded Notes", "Upload Your Documents"))
134
-
135
- if use_preloaded == "Use Preloaded Notes":
136
- # Load preview content
137
- preview_folder_path = preview_folders[selected_subject]
138
- if os.path.exists(preview_folder_path):
139
- preview_text = get_text_files_content(preview_folder_path)
140
- st.subheader("Preview of Notes")
141
- st.text_area("Preview Content:", preview_text, height=300, disabled=True)
142
- else:
143
- st.error(f"No preview available for {selected_subject}.")
144
-
145
- # Process data folder for question answering
146
- subject_folder_path = subject_folders[selected_subject]
147
- if os.path.exists(subject_folder_path):
148
- raw_text = get_text_files_content(subject_folder_path)
149
- if raw_text:
150
- text_chunks = get_chunks(raw_text)
151
- vectorstore = get_vectorstore(text_chunks)
152
- st.session_state.conversation = get_conversationchain(vectorstore)
153
- else:
154
- st.error("Could not load the content for question answering.")
155
  else:
156
- st.error(f"No data available for {selected_subject}.")
157
-
158
- else: # Upload documents option
159
- docs = st.sidebar.file_uploader("Upload your text files here:", accept_multiple_files=True, type=['txt'])
160
- if docs:
161
- st.sidebar.info(f"Uploaded {len(docs)} file(s).")
162
- if st.sidebar.button("Process"):
163
- with st.spinner("Processing uploaded documents..."):
164
- raw_text = "".join([doc.read().decode('utf-8') for doc in docs])
165
- st.subheader("Uploaded Notes Preview")
166
- st.text_area("Preview Content:", raw_text, height=300, disabled=True)
167
- text_chunks = get_chunks(raw_text)
168
- vectorstore = get_vectorstore(text_chunks)
169
- st.session_state.conversation = get_conversationchain(vectorstore)
170
 
171
  # Chat interface
172
  question = st.text_input("Ask a question about your selected subject:")
173
  if question and st.session_state.conversation:
174
- st.write(f"**Subject:** {selected_subject}") # Display subject before chat
175
  handle_question(question)
176
  elif question:
177
  st.warning("Please process a document before asking a question.")
 
11
 
12
  load_dotenv()
13
 
 
 
14
  # Creating custom template to guide LLM model
15
  custom_template = """Given the following conversation and a follow-up question, rephrase the follow-up question to be a standalone question, in its original language.
16
  Chat History:
 
67
 
68
  def conversation_chain(inputs):
69
  question = inputs['question']
 
70
  documents = vectorstore.similarity_search(question, k=5)
71
 
 
72
  if not documents:
73
  answer = "Sorry, I couldn't find relevant information in the document. Please ask a question related to the document."
74
  memory.save_context({"user_input": question}, {"answer": answer})
75
  return {"chat_history": memory.chat_memory.messages, "answer": answer}
76
 
77
+ context = "\n".join([doc.page_content for doc in documents])
78
  answer, score = qa_function(question, context)
79
 
 
80
  if score < 0.5:
81
  answer = "Sorry, I couldn't find relevant information in the document. Please ask a question related to the document."
82
 
 
122
  preview_folders = {subject: os.path.join(preview_folder, subject.replace(' ', '_')) for subject in subjects}
123
  selected_subject = st.sidebar.selectbox("Select a Subject:", subjects)
124
 
125
+ st.sidebar.info(f"You have selected: {selected_subject}")
126
+
127
+ # Load preview content
128
+ preview_folder_path = preview_folders[selected_subject]
129
+ if os.path.exists(preview_folder_path):
130
+ preview_text = get_text_files_content(preview_folder_path)
131
+ st.subheader("Preview of Notes")
132
+ st.text_area("Preview Content:", preview_text, height=300, disabled=True)
133
+ else:
134
+ st.error(f"No preview available for {selected_subject}.")
135
+
136
+ # Process data folder for question answering
137
+ subject_folder_path = subject_folders[selected_subject]
138
+ if os.path.exists(subject_folder_path):
139
+ raw_text = get_text_files_content(subject_folder_path)
140
+ if raw_text:
141
+ text_chunks = get_chunks(raw_text)
142
+ vectorstore = get_vectorstore(text_chunks)
143
+ st.session_state.conversation = get_conversationchain(vectorstore)
 
 
 
 
 
 
144
  else:
145
+ st.error("Could not load the content for question answering.")
146
+ else:
147
+ st.error(f"No data available for {selected_subject}.")
 
 
 
 
 
 
 
 
 
 
 
148
 
149
  # Chat interface
150
  question = st.text_input("Ask a question about your selected subject:")
151
  if question and st.session_state.conversation:
152
+ st.write(f"**Subject:** {selected_subject}")
153
  handle_question(question)
154
  elif question:
155
  st.warning("Please process a document before asking a question.")