akshay33 commited on
Commit
ae146c8
·
verified ·
1 Parent(s): c256377

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -1,21 +1,23 @@
1
  import os
2
- import pickle
3
  from langchain.chains import RetrievalQA
4
  from langchain_community.embeddings import HuggingFaceEmbeddings
5
  from langchain_openai import ChatOpenAI
 
6
  import gradio as gr
 
7
 
8
- # Load API key from environment
9
  openai_api_key = os.environ.get("OPENAI_API_KEY")
10
-
11
- # Ensure key is available
12
  if not openai_api_key:
13
- raise ValueError("OPENAI_API_KEY is not set in the environment variables.")
14
 
15
- # Load vectorstore
16
  def load_vectorstore():
17
- with open("vectorstore.pkl", "rb") as f:
18
- return pickle.load(f)
 
 
 
19
 
20
  vectorstore = load_vectorstore()
21
 
@@ -34,9 +36,9 @@ def chat_with_pdf(query):
34
  if not query.strip():
35
  return "❗ Please enter a question."
36
 
37
- result = qa_chain.invoke(query)
38
- answer = result['result']
39
- sources = result.get('source_documents', [])
40
 
41
  formatted_sources = ""
42
  for i, doc in enumerate(sources):
@@ -51,7 +53,7 @@ def chat_with_pdf(query):
51
  with gr.Blocks() as demo:
52
  gr.Markdown("# 📚 PDF Chatbot using RAG")
53
  gr.Markdown("Ask a question based on pre-embedded PDF documents.")
54
-
55
  with gr.Row():
56
  query_input = gr.Textbox(lines=2, placeholder="Type your question here...", label="Question")
57
  submit_button = gr.Button("Submit")
@@ -62,4 +64,4 @@ with gr.Blocks() as demo:
62
 
63
  # Launch app
64
  if __name__ == "__main__":
65
- demo.launch(share=True)
 
1
  import os
 
2
  from langchain.chains import RetrievalQA
3
  from langchain_community.embeddings import HuggingFaceEmbeddings
4
  from langchain_openai import ChatOpenAI
5
+ from langchain_community.vectorstores import FAISS
6
  import gradio as gr
7
+ from secret_key import huggingface_api_key # if you use a separate secret file
8
 
9
+ # Load OpenAI API key from environment
10
  openai_api_key = os.environ.get("OPENAI_API_KEY")
 
 
11
  if not openai_api_key:
12
+ raise ValueError("OPENAI_API_KEY is not set in environment variables.")
13
 
14
+ # Load vectorstore from saved FAISS directory
15
  def load_vectorstore():
16
+ embeddings = HuggingFaceEmbeddings(
17
+ model_name="sentence-transformers/all-MiniLM-L6-v2",
18
+ huggingfacehub_api_token=huggingface_api_key
19
+ )
20
+ return FAISS.load_local("faiss_index", embeddings)
21
 
22
  vectorstore = load_vectorstore()
23
 
 
36
  if not query.strip():
37
  return "❗ Please enter a question."
38
 
39
+ result = qa_chain.invoke({"query": query})
40
+ answer = result["result"]
41
+ sources = result.get("source_documents", [])
42
 
43
  formatted_sources = ""
44
  for i, doc in enumerate(sources):
 
53
  with gr.Blocks() as demo:
54
  gr.Markdown("# 📚 PDF Chatbot using RAG")
55
  gr.Markdown("Ask a question based on pre-embedded PDF documents.")
56
+
57
  with gr.Row():
58
  query_input = gr.Textbox(lines=2, placeholder="Type your question here...", label="Question")
59
  submit_button = gr.Button("Submit")
 
64
 
65
  # Launch app
66
  if __name__ == "__main__":
67
+ demo.launch(share=True)