danishjameel003 commited on
Commit
e0d1f66
·
verified ·
1 Parent(s): 70a55b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -25
app.py CHANGED
@@ -9,14 +9,16 @@ from dotenv import load_dotenv
9
  # Set Streamlit page configuration
10
  st.set_page_config(page_title="Chat with Notes and AI", page_icon=":books:", layout="wide")
11
 
12
- # Load environment variables
13
  load_dotenv()
14
 
15
- # OpenAI API Key (set in .env or directly in your environment)
16
- OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "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")
 
 
17
  openai.api_key = OPENAI_API_KEY
18
 
19
- # Template for instruction-only prompts
20
  def generate_openai_response(instruction, context=None):
21
  try:
22
  messages = [
@@ -35,7 +37,7 @@ def generate_openai_response(instruction, context=None):
35
  except Exception as e:
36
  return f"Error: {str(e)}"
37
 
38
- # Extracting text from .txt files
39
  def get_text_files_content(folder):
40
  text = ""
41
  for filename in os.listdir(folder):
@@ -44,7 +46,7 @@ def get_text_files_content(folder):
44
  text += file.read() + "\n"
45
  return text
46
 
47
- # Converting text to chunks
48
  def get_chunks(raw_text):
49
  text_splitter = CharacterTextSplitter(
50
  separator="\n",
@@ -54,13 +56,13 @@ def get_chunks(raw_text):
54
  )
55
  return text_splitter.split_text(raw_text)
56
 
57
- # Using OpenAI embeddings model and FAISS to create vectorstore
58
  def get_vectorstore(chunks):
59
  embeddings = OpenAIEmbeddings() # Uses OpenAI Embeddings
60
  vectorstore = FAISS.from_texts(texts=chunks, embedding=embeddings)
61
  return vectorstore
62
 
63
- # Generating response from user queries
64
  def handle_question(question, vectorstore=None):
65
  if vectorstore:
66
  # Retrieve relevant chunks using similarity search
@@ -72,37 +74,31 @@ def handle_question(question, vectorstore=None):
72
  # Fallback to instruction-only prompt if no context is found
73
  return generate_openai_response(question)
74
 
 
75
  def main():
76
  st.title("Chat with Notes :books:")
77
 
78
- # Initialize session state
79
  if "vectorstore" not in st.session_state:
80
  st.session_state.vectorstore = None
81
 
82
  # Define folders for Current Affairs and Essays
83
- data_folder = "data" # Current Affairs folders
84
- essay_folder = "essays" # Essays folder
85
 
86
  # Content type selection
87
  content_type = st.sidebar.radio("Select Content Type:", ["Current Affairs", "Essays"])
88
 
89
- # Handle Current Affairs (each subject has its own folder)
90
  if content_type == "Current Affairs":
91
- if os.path.exists(data_folder):
92
- subjects = [f for f in os.listdir(data_folder) if os.path.isdir(os.path.join(data_folder, f))]
93
- else:
94
- subjects = []
95
- # Handle Essays (all essays are in a single folder)
96
  elif content_type == "Essays":
97
- if os.path.exists(essay_folder):
98
- subjects = [f.replace(".txt", "") for f in os.listdir(essay_folder) if f.endswith('.txt')]
99
- else:
100
- subjects = []
101
 
102
  # Subject selection
103
  selected_subject = st.sidebar.selectbox("Select a Subject:", subjects)
104
 
105
- # Process selected subject
106
  raw_text = ""
107
  if content_type == "Current Affairs" and selected_subject:
108
  subject_folder = os.path.join(data_folder, selected_subject)
@@ -113,12 +109,12 @@ def main():
113
  with open(subject_file, "r", encoding="utf-8") as file:
114
  raw_text = file.read()
115
 
116
- # Display preview of notes
117
  if raw_text:
118
  st.subheader("Preview of Notes")
119
- st.text_area("Preview Content:", value=raw_text[:2000], height=300, disabled=True) # Show a snippet of the notes
120
 
121
- # Create vectorstore for Current Affairs or Essays
122
  text_chunks = get_chunks(raw_text)
123
  vectorstore = get_vectorstore(text_chunks)
124
  st.session_state.vectorstore = vectorstore
@@ -136,5 +132,6 @@ def main():
136
  else:
137
  st.warning("Please load the content for the selected subject before asking a question.")
138
 
 
139
  if __name__ == '__main__':
140
  main()
 
9
  # Set Streamlit page configuration
10
  st.set_page_config(page_title="Chat with Notes and AI", page_icon=":books:", layout="wide")
11
 
12
+ # Load environment variables from .env file
13
  load_dotenv()
14
 
15
+ # Retrieve OpenAI API key from environment
16
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
17
+ if not OPENAI_API_KEY:
18
+ raise ValueError("OpenAI API key not found. Set it in the .env file or environment variables.")
19
  openai.api_key = OPENAI_API_KEY
20
 
21
+ # Function to generate response from OpenAI API
22
  def generate_openai_response(instruction, context=None):
23
  try:
24
  messages = [
 
37
  except Exception as e:
38
  return f"Error: {str(e)}"
39
 
40
+ # Extract text from .txt files in a folder
41
  def get_text_files_content(folder):
42
  text = ""
43
  for filename in os.listdir(folder):
 
46
  text += file.read() + "\n"
47
  return text
48
 
49
+ # Convert raw text into manageable chunks
50
  def get_chunks(raw_text):
51
  text_splitter = CharacterTextSplitter(
52
  separator="\n",
 
56
  )
57
  return text_splitter.split_text(raw_text)
58
 
59
+ # Create a FAISS vectorstore using OpenAI embeddings
60
  def get_vectorstore(chunks):
61
  embeddings = OpenAIEmbeddings() # Uses OpenAI Embeddings
62
  vectorstore = FAISS.from_texts(texts=chunks, embedding=embeddings)
63
  return vectorstore
64
 
65
+ # Handle user queries by fetching relevant context and generating responses
66
  def handle_question(question, vectorstore=None):
67
  if vectorstore:
68
  # Retrieve relevant chunks using similarity search
 
74
  # Fallback to instruction-only prompt if no context is found
75
  return generate_openai_response(question)
76
 
77
+ # Main function for the Streamlit app
78
  def main():
79
  st.title("Chat with Notes :books:")
80
 
81
+ # Initialize session state for vectorstore
82
  if "vectorstore" not in st.session_state:
83
  st.session_state.vectorstore = None
84
 
85
  # Define folders for Current Affairs and Essays
86
+ data_folder = "data" # Folder for Current Affairs notes
87
+ essay_folder = "essays" # Folder for Essays
88
 
89
  # Content type selection
90
  content_type = st.sidebar.radio("Select Content Type:", ["Current Affairs", "Essays"])
91
 
92
+ # Populate subject list based on selected content type
93
  if content_type == "Current Affairs":
94
+ subjects = [f for f in os.listdir(data_folder) if os.path.isdir(os.path.join(data_folder, f))] if os.path.exists(data_folder) else []
 
 
 
 
95
  elif content_type == "Essays":
96
+ subjects = [f.replace(".txt", "") for f in os.listdir(essay_folder) if f.endswith('.txt')] if os.path.exists(essay_folder) else []
 
 
 
97
 
98
  # Subject selection
99
  selected_subject = st.sidebar.selectbox("Select a Subject:", subjects)
100
 
101
+ # Load and process the selected subject
102
  raw_text = ""
103
  if content_type == "Current Affairs" and selected_subject:
104
  subject_folder = os.path.join(data_folder, selected_subject)
 
109
  with open(subject_file, "r", encoding="utf-8") as file:
110
  raw_text = file.read()
111
 
112
+ # Display notes preview
113
  if raw_text:
114
  st.subheader("Preview of Notes")
115
+ st.text_area("Preview Content:", value=raw_text[:2000], height=300, disabled=True)
116
 
117
+ # Generate vectorstore for the selected notes
118
  text_chunks = get_chunks(raw_text)
119
  vectorstore = get_vectorstore(text_chunks)
120
  st.session_state.vectorstore = vectorstore
 
132
  else:
133
  st.warning("Please load the content for the selected subject before asking a question.")
134
 
135
+ # Run the app
136
  if __name__ == '__main__':
137
  main()