danishjameel003 commited on
Commit
0401909
·
verified ·
1 Parent(s): ecf2148

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -9
app.py CHANGED
@@ -1,12 +1,11 @@
1
  import os
2
  import torch
3
  import streamlit as st
4
- from langchain.text_splitter import CharacterTextSplitter
5
- from langchain.embeddings import HuggingFaceEmbeddings
6
- from langchain.vectorstores import FAISS
7
- from langchain.memory import ConversationBufferMemory
8
- from langchain import PromptTemplate, LLMChain
9
- from langchain.llms import HuggingFacePipeline
10
  from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
11
  from dotenv import load_dotenv
12
  from htmlTemplates import css
@@ -74,6 +73,7 @@ def get_text_files_content(folder):
74
 
75
  # Converting text to chunks
76
  def get_chunks(raw_text):
 
77
  text_splitter = CharacterTextSplitter(
78
  separator="\n",
79
  chunk_size=2000,
@@ -98,9 +98,9 @@ def handle_question(question, vectorstore=None):
98
  documents = vectorstore.similarity_search(question, k=3)
99
  context = "\n".join([doc.page_content for doc in documents])
100
  if context:
101
- result_with_context = llm_context_chain.run(instruction=question, context=context)
102
  return result_with_context
103
- return llm_chain.run(instruction=question)
104
 
105
  def main():
106
  st.write(css, unsafe_allow_html=True)
@@ -126,7 +126,7 @@ def main():
126
 
127
  st.sidebar.info(f"You have selected: {selected_subject}")
128
 
129
- # Process data folder for question answering
130
  subject_folder_path = subject_folders[selected_subject]
131
  if os.path.exists(subject_folder_path):
132
  raw_text = get_text_files_content(subject_folder_path)
@@ -134,12 +134,17 @@ def main():
134
  text_chunks = get_chunks(raw_text)
135
  vectorstore = get_vectorstore(text_chunks)
136
  st.session_state.vectorstore = vectorstore
 
 
 
 
137
  else:
138
  st.error("No content found for the selected subject.")
139
  else:
140
  st.error(f"Folder not found for {selected_subject}.")
141
 
142
  # Chat interface
 
143
  question = st.text_input("Ask a question about your selected subject:")
144
  if question:
145
  if st.session_state.vectorstore:
 
1
  import os
2
  import torch
3
  import streamlit as st
4
+ from langchain_community.embeddings import HuggingFaceEmbeddings
5
+ from langchain_community.vectorstores import FAISS
6
+ from langchain_core.prompts import PromptTemplate
7
+ from langchain.chains import LLMChain
8
+ from langchain_community.llms import HuggingFacePipeline
 
9
  from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
10
  from dotenv import load_dotenv
11
  from htmlTemplates import css
 
73
 
74
  # Converting text to chunks
75
  def get_chunks(raw_text):
76
+ from langchain.text_splitter import CharacterTextSplitter
77
  text_splitter = CharacterTextSplitter(
78
  separator="\n",
79
  chunk_size=2000,
 
98
  documents = vectorstore.similarity_search(question, k=3)
99
  context = "\n".join([doc.page_content for doc in documents])
100
  if context:
101
+ result_with_context = llm_context_chain.invoke({"instruction": question, "context": context})
102
  return result_with_context
103
+ return llm_chain.invoke({"instruction": question})
104
 
105
  def main():
106
  st.write(css, unsafe_allow_html=True)
 
126
 
127
  st.sidebar.info(f"You have selected: {selected_subject}")
128
 
129
+ # Process data folder for notes preview and question answering
130
  subject_folder_path = subject_folders[selected_subject]
131
  if os.path.exists(subject_folder_path):
132
  raw_text = get_text_files_content(subject_folder_path)
 
134
  text_chunks = get_chunks(raw_text)
135
  vectorstore = get_vectorstore(text_chunks)
136
  st.session_state.vectorstore = vectorstore
137
+
138
+ # Display preview of notes
139
+ st.subheader("Preview of Notes")
140
+ st.text_area("Preview Content:", value=raw_text[:2000], height=300, disabled=True) # Show a snippet of the text
141
  else:
142
  st.error("No content found for the selected subject.")
143
  else:
144
  st.error(f"Folder not found for {selected_subject}.")
145
 
146
  # Chat interface
147
+ st.subheader("Ask Your Question")
148
  question = st.text_input("Ask a question about your selected subject:")
149
  if question:
150
  if st.session_state.vectorstore: