danishjameel003 commited on
Commit
5991ba4
·
verified ·
1 Parent(s): 51fcb96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -26
app.py CHANGED
@@ -118,45 +118,52 @@ def main():
118
  if "vectorstore" not in st.session_state:
119
  st.session_state.vectorstore = None
120
 
121
- # Root folder for content
122
- root_folder = "data"
123
- content_types = {
124
- "Current Affairs": os.path.join(root_folder, "current_affairs"),
125
- "Essays": os.path.join(root_folder, "essays")
126
- }
127
 
128
  # Content type selection
129
- content_type = st.sidebar.radio("Select Content Type:", list(content_types.keys()))
130
- selected_folder = content_types[content_type]
131
 
132
- # Subject selection
133
- if os.path.exists(selected_folder):
134
- subjects = [f.replace("_", " ").replace(".txt", "") for f in os.listdir(selected_folder) if f.endswith('.txt')]
135
- else:
136
- subjects = []
 
 
 
 
 
 
 
137
 
 
138
  selected_subject = st.sidebar.selectbox("Select a Subject:", subjects)
139
 
140
- # Process data folder for vectorstore
141
  raw_text = ""
142
- if selected_subject:
143
- subject_path = os.path.join(selected_folder, selected_subject.replace(" ", "_") + ".txt")
144
- if os.path.exists(subject_path):
145
- raw_text = get_text_files_content(selected_folder)
146
- if raw_text:
147
- text_chunks = get_chunks(raw_text)
148
- vectorstore = get_vectorstore(text_chunks)
149
- st.session_state.vectorstore = vectorstore
150
- else:
151
- st.warning("No content found for the selected subject.")
152
- else:
153
- st.error(f"File not found for {selected_subject}.")
154
 
155
  # Display preview of notes
156
  if raw_text:
157
  st.subheader("Preview of Notes")
158
  st.text_area("Preview Content:", value=raw_text[:2000], height=300, disabled=True) # Show a snippet of the notes
159
 
 
 
 
 
 
 
 
160
  # Chat interface
161
  st.subheader("Ask Your Question")
162
  question = st.text_input("Ask a question about your selected subject:")
 
118
  if "vectorstore" not in st.session_state:
119
  st.session_state.vectorstore = None
120
 
121
+ # Define folders for Current Affairs and Essays
122
+ data_folder = "data"
123
+ essay_folder = "essay"
 
 
 
124
 
125
  # Content type selection
126
+ content_type = st.sidebar.radio("Select Content Type:", ["Current Affairs", "Essays"])
 
127
 
128
+ # Handle Current Affairs (each subject has its own folder)
129
+ if content_type == "Current Affairs":
130
+ if os.path.exists(data_folder):
131
+ subjects = [f for f in os.listdir(data_folder) if os.path.isdir(os.path.join(data_folder, f))]
132
+ else:
133
+ subjects = []
134
+ # Handle Essays (all essays are in a single folder)
135
+ elif content_type == "Essays":
136
+ if os.path.exists(essay_folder):
137
+ subjects = [f.replace(".txt", "") for f in os.listdir(essay_folder) if f.endswith('.txt')]
138
+ else:
139
+ subjects = []
140
 
141
+ # Subject selection
142
  selected_subject = st.sidebar.selectbox("Select a Subject:", subjects)
143
 
144
+ # Process selected subject
145
  raw_text = ""
146
+ if content_type == "Current Affairs" and selected_subject:
147
+ subject_folder = os.path.join(data_folder, selected_subject)
148
+ raw_text = get_text_files_content(subject_folder)
149
+ elif content_type == "Essays" and selected_subject:
150
+ subject_file = os.path.join(essay_folder, selected_subject + ".txt")
151
+ if os.path.exists(subject_file):
152
+ with open(subject_file, "r", encoding="utf-8") as file:
153
+ raw_text = file.read()
 
 
 
 
154
 
155
  # Display preview of notes
156
  if raw_text:
157
  st.subheader("Preview of Notes")
158
  st.text_area("Preview Content:", value=raw_text[:2000], height=300, disabled=True) # Show a snippet of the notes
159
 
160
+ # Create vectorstore for Current Affairs or Essays
161
+ text_chunks = get_chunks(raw_text)
162
+ vectorstore = get_vectorstore(text_chunks)
163
+ st.session_state.vectorstore = vectorstore
164
+ else:
165
+ st.warning("No content available for the selected subject.")
166
+
167
  # Chat interface
168
  st.subheader("Ask Your Question")
169
  question = st.text_input("Ask a question about your selected subject:")