KushwanthK commited on
Commit
7268c9c
·
verified ·
1 Parent(s): e90e956

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -18
app.py CHANGED
@@ -65,7 +65,7 @@ if "value" not in st.session_state:
65
  if "faq" not in st.session_state:
66
  st.session_state["faq"] = None
67
 
68
- st.divider()
69
 
70
  def upload_file():
71
  uploaded_file = st.file_uploader("Upload a file", type=["pdf"])
@@ -114,9 +114,9 @@ def create_pickle_file(filepath):
114
  pickle.dump(faiss_index, f)
115
 
116
 
117
- uploaded_file_name = upload_file()
118
- if uploaded_file_name is not None:
119
- create_pickle_file(uploaded_file_name)
120
 
121
  def highlight_pdf(file_path, text_to_highlight, page_numbers):
122
  # Create a temporary file to save the modified PDF
@@ -136,11 +136,9 @@ def highlight_pdf(file_path, text_to_highlight, page_numbers):
136
  # Tokenize the text into words
137
  words = text_to_highlight.split()
138
 
139
-
140
-
141
  # Remove stopwords
142
- # stop_words = set(stopwords.words("english"))
143
- # words = [word for word in words if word.lower() not in stop_words]
144
 
145
  print(words)
146
 
@@ -213,7 +211,7 @@ def get_faiss_semantic_index():
213
  with open(index_path, "rb") as f:
214
  faiss_index = pickle.load(f)
215
  sleep(0.1)
216
- st.write("Embeddings loaded successfully.")
217
  return faiss_index
218
  except Exception as e:
219
  st.error(f"Error loading embeddings: {e}")
@@ -224,14 +222,17 @@ print(faiss_index)
224
  # def promt_engineer(text):
225
  PROMPT_TEMPLATE = """
226
  Instructions:
227
- --------------------------------------------------------
228
- Answer the question based only on the following context:
229
- Please dont show human instructions in the answer
230
- ----------------------------------------------------------
 
 
 
231
 
232
  {context}
233
 
234
- -----------------------------------------------------------
235
 
236
  Answer the question based on the above context: {question}
237
  """
@@ -259,7 +260,7 @@ def chat_actions():
259
  # query_embedding = model.encode(st.session_state["chat_input"])
260
  query = st.session_state["chat_input"]
261
  if faiss_index is not None:
262
- docs = faiss_index.similarity_search(query, k=2)
263
  else:
264
  st.error("Failed to load embeddings.")
265
  # docs = faiss_index.similarity_search(query, k=2)
@@ -278,7 +279,7 @@ def chat_actions():
278
  result = ""
279
  try:
280
  llm = HuggingFaceHub(
281
- repo_id="meta-llama/Meta-Llama-3-8B-Instruct"
282
  )
283
  response_text = llm.invoke(prompt)
284
  escaped_query = re.escape(query)
@@ -317,11 +318,14 @@ with st.sidebar:
317
 
318
  faq = st.selectbox(
319
  "Select Your Favorite Scriptures",
320
- ("Why does atheism exist even when all questions are answered in Bhagavad Gita?",
 
 
 
321
  "Why don’t all souls surrender to Lord Krishna, although he has demonstrated that everyone is part and parcel of Him, and all can be liberated from all sufferings by surrendering to Him?",
322
  "Why do souls misuse their independence by rebelling against Lord Krishna?",
323
  "How do I put an end to my suffering in this world?",
324
- "What are the key points from Krishna?"),
325
  # index=None,
326
  # placeholder="Select scriptures...",
327
  key="faq",
 
65
  if "faq" not in st.session_state:
66
  st.session_state["faq"] = None
67
 
68
+ # st.divider()
69
 
70
  def upload_file():
71
  uploaded_file = st.file_uploader("Upload a file", type=["pdf"])
 
114
  pickle.dump(faiss_index, f)
115
 
116
 
117
+ # uploaded_file_name = upload_file()
118
+ # if uploaded_file_name is not None:
119
+ # create_pickle_file(uploaded_file_name)
120
 
121
  def highlight_pdf(file_path, text_to_highlight, page_numbers):
122
  # Create a temporary file to save the modified PDF
 
136
  # Tokenize the text into words
137
  words = text_to_highlight.split()
138
 
 
 
139
  # Remove stopwords
140
+ stop_words = set(stopwords.words("english"))
141
+ words = [word for word in words if word.lower() not in stop_words]
142
 
143
  print(words)
144
 
 
211
  with open(index_path, "rb") as f:
212
  faiss_index = pickle.load(f)
213
  sleep(0.1)
214
+ # st.write("Embeddings loaded successfully.")
215
  return faiss_index
216
  except Exception as e:
217
  st.error(f"Error loading embeddings: {e}")
 
222
  # def promt_engineer(text):
223
  PROMPT_TEMPLATE = """
224
  Instructions:
225
+ -----------------------------------------------------------------------------------------
226
+ Answer the question only based on the below context:
227
+ - You're a Vedic AI expert in the Hindu Vedic scriptures.
228
+ - Always try to provide Keep it simple answers in nice format without incomplete sentence.
229
+ - Give the answer atleast 5 seperate lines addition to the title info.
230
+ - provide Title: <title> Chapter: <chapter> Text No: <textnumber> Page No: <pagenumber>
231
+ ------------------------------------------------------------------------------------------
232
 
233
  {context}
234
 
235
+ ----------------------------------------------------------------------------------
236
 
237
  Answer the question based on the above context: {question}
238
  """
 
260
  # query_embedding = model.encode(st.session_state["chat_input"])
261
  query = st.session_state["chat_input"]
262
  if faiss_index is not None:
263
+ docs = faiss_index.similarity_search(query, k=6)
264
  else:
265
  st.error("Failed to load embeddings.")
266
  # docs = faiss_index.similarity_search(query, k=2)
 
279
  result = ""
280
  try:
281
  llm = HuggingFaceHub(
282
+ repo_id="meta-llama/Meta-Llama-3-8B-Instruct", model_kwargs={"temperature": 0.1, "max_new_tokens": 256, "task":"text-generation"}
283
  )
284
  response_text = llm.invoke(prompt)
285
  escaped_query = re.escape(query)
 
318
 
319
  faq = st.selectbox(
320
  "Select Your Favorite Scriptures",
321
+ ("what is jeeva and paramathma?",
322
+ "What are the Krishna told to Arjuna?",
323
+ "What are the key points from Krishna?",
324
+ "Why does atheism exist even when all questions are answered in Bhagavad Gita?",
325
  "Why don’t all souls surrender to Lord Krishna, although he has demonstrated that everyone is part and parcel of Him, and all can be liberated from all sufferings by surrendering to Him?",
326
  "Why do souls misuse their independence by rebelling against Lord Krishna?",
327
  "How do I put an end to my suffering in this world?",
328
+ "what is the reason behind Krishna decided to go far war?"),
329
  # index=None,
330
  # placeholder="Select scriptures...",
331
  key="faq",