jarif commited on
Commit
96d0b2f
·
verified ·
1 Parent(s): ada8299

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -22
app.py CHANGED
@@ -1,30 +1,18 @@
1
  import os
2
- import faiss
3
  import logging
4
  import streamlit as st
5
  from langchain.embeddings import HuggingFaceEmbeddings
6
- from langchain.vectorstores import FAISS
7
  from langchain.chains import RetrievalQA
8
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
9
 
10
  # Configure logging
11
  logging.basicConfig(level=logging.DEBUG)
12
 
13
- def load_faiss_index(index_path):
14
- if not os.path.exists(index_path):
15
- logging.error(f"FAISS index not found at {index_path}. Please create the index first.")
16
- st.error(f"FAISS index not found at {index_path}. Please create the index first.")
17
- raise FileNotFoundError(f"FAISS index not found at {index_path}.")
18
- try:
19
- logging.info(f"Attempting to load FAISS index from {index_path}.")
20
- index = faiss.read_index(index_path)
21
- logging.info("FAISS index loaded successfully.")
22
- st.success("FAISS index loaded successfully.")
23
- return index
24
- except Exception as e:
25
- logging.error(f"Failed to load FAISS index: {e}")
26
- st.error(f"Failed to load FAISS index: {e}")
27
- raise
28
 
29
  def load_llm():
30
  checkpoint = "LaMini-T5-738M"
@@ -42,16 +30,13 @@ def load_llm():
42
  return pipe
43
 
44
  def process_answer(question):
45
- index_path = 'faiss_index/index.faiss'
46
- embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
47
  try:
48
- faiss_index = load_faiss_index(index_path)
49
- retriever = FAISS(index=faiss_index, embeddings=embeddings)
50
  llm = load_llm()
51
  qa = RetrievalQA.from_chain_type(
52
  llm=llm,
53
  chain_type="stuff",
54
- retriever=retriever,
55
  return_source_documents=True
56
  )
57
  result = qa.invoke(question)
 
1
  import os
 
2
  import logging
3
  import streamlit as st
4
  from langchain.embeddings import HuggingFaceEmbeddings
5
+ from langchain.vectorstores import Chroma
6
  from langchain.chains import RetrievalQA
7
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
8
 
9
  # Configure logging
10
  logging.basicConfig(level=logging.DEBUG)
11
 
12
+ def load_vector_store():
13
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
14
+ vector_store = Chroma(persist_directory="./chroma_db", embedding_function=embeddings)
15
+ return vector_store
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  def load_llm():
18
  checkpoint = "LaMini-T5-738M"
 
30
  return pipe
31
 
32
  def process_answer(question):
 
 
33
  try:
34
+ vector_store = load_vector_store()
 
35
  llm = load_llm()
36
  qa = RetrievalQA.from_chain_type(
37
  llm=llm,
38
  chain_type="stuff",
39
+ retriever=vector_store.as_retriever(),
40
  return_source_documents=True
41
  )
42
  result = qa.invoke(question)