sairamn commited on
Commit
c165793
1 Parent(s): 5368614

Added v2 Code to tree main

Browse files
Files changed (2) hide show
  1. app.py +18 -29
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,12 +1,9 @@
1
  import gradio as gr
2
- import PyPDF2
3
- from langchain_community.vectorstores import FAISS # Updated import
4
- from langchain_huggingface import HuggingFaceEmbeddings # Updated import
5
  from langchain.prompts import PromptTemplate
6
  from langchain_together import Together
7
- from huggingface_hub import hf_hub_download
8
- import faiss
9
- import pickle
10
 
11
  # Load embeddings
12
  embeddings = HuggingFaceEmbeddings(
@@ -14,22 +11,12 @@ embeddings = HuggingFaceEmbeddings(
14
  model_kwargs={"trust_remote_code": True, "revision": "289f532e14dbbbd5a04753fa58739e9ba766f3c7"}
15
  )
16
 
17
- # Download and load the FAISS index from the Hugging Face repo
18
- faiss_index_path = hf_hub_download(repo_id="sairamn/LawGPT", filename="index.faiss")
19
- db = faiss.read_index(faiss_index_path)
20
-
21
- # Download and load the pickle file
22
- pkl_index_path = hf_hub_download(repo_id="sairamn/LawGPT", filename="index.pkl")
23
- with open(pkl_index_path, 'rb') as f:
24
- data = pickle.load(f)
25
 
26
- # Create a FAISS vector store from the loaded data
27
- # Extracting the document store from the loaded data
28
- docstore = data['docstore'] # Ensure 'docstore' key exists in your pickle file
29
- index_to_docstore_id = data['index_to_docstore_id'] # Ensure 'index_to_docstore_id' key exists in your pickle file
30
-
31
- # Now initializing the FAISS with required arguments
32
- db = FAISS(embedding_function=embeddings.embed_documents, index=db, docstore=docstore, index_to_docstore_id=index_to_docstore_id)
33
  db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 4})
34
 
35
  # Define the prompt template for the chatbot
@@ -53,7 +40,7 @@ llm = Together(
53
  together_api_key=TOGETHER_AI_API
54
  )
55
 
56
- # Create a function to answer questions
57
  def ask_question(user_question):
58
  # Retrieve relevant documents
59
  context_docs = db_retriever.get_relevant_documents(user_question)
@@ -70,16 +57,18 @@ def ask_question(user_question):
70
 
71
  # Generate an answer
72
  response = llm(prompt.format(**input_data))
 
73
  return response
74
 
75
- # Gradio interface
76
  iface = gr.Interface(
77
  fn=ask_question,
78
- inputs=gr.inputs.Textbox(label="Ask a question"),
79
- outputs=gr.outputs.Textbox(label="Answer"),
80
- title="IPC Legal Chatbot",
81
- description="Ask questions related to the Indian Penal Code and get concise answers."
82
  )
83
 
84
- # Launch the app
85
- iface.launch()
 
 
1
  import gradio as gr
2
+ from huggingface_hub import hf_hub_download
3
+ from langchain_community.vectorstores import FAISS
4
+ from langchain_community.embeddings import HuggingFaceEmbeddings
5
  from langchain.prompts import PromptTemplate
6
  from langchain_together import Together
 
 
 
7
 
8
  # Load embeddings
9
  embeddings = HuggingFaceEmbeddings(
 
11
  model_kwargs={"trust_remote_code": True, "revision": "289f532e14dbbbd5a04753fa58739e9ba766f3c7"}
12
  )
13
 
14
+ # Download FAISS index files from Hugging Face Hub
15
+ index_file = hf_hub_download(repo_id="sairamn/LawGPT", filename="index.faiss")
16
+ pkl_file = hf_hub_download(repo_id="sairamn/LawGPT", filename="index.pkl")
 
 
 
 
 
17
 
18
+ # Load FAISS index from the downloaded files
19
+ db = FAISS.load_local(index_file, embeddings, allow_dangerous_deserialization=True)
 
 
 
 
 
20
  db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 4})
21
 
22
  # Define the prompt template for the chatbot
 
40
  together_api_key=TOGETHER_AI_API
41
  )
42
 
43
+ # Create a function to process user input and generate responses
44
  def ask_question(user_question):
45
  # Retrieve relevant documents
46
  context_docs = db_retriever.get_relevant_documents(user_question)
 
57
 
58
  # Generate an answer
59
  response = llm(prompt.format(**input_data))
60
+
61
  return response
62
 
63
+ # Set up Gradio interface
64
  iface = gr.Interface(
65
  fn=ask_question,
66
+ inputs=gr.inputs.Textbox(label="Ask a Question", placeholder="Type your question here..."),
67
+ outputs="text",
68
+ title="Legal Chatbot",
69
+ description="Ask questions about the Indian Penal Code."
70
  )
71
 
72
+ # Launch the Gradio app
73
+ if __name__ == "__main__":
74
+ iface.launch()
requirements.txt CHANGED
@@ -2,8 +2,9 @@ langchain
2
  faiss-cpu
3
  together
4
  transformers
5
- sentence-transformers
6
  langchain-community
7
  langchain-together
8
  einops
9
  PyPDF2
 
 
 
2
  faiss-cpu
3
  together
4
  transformers
 
5
  langchain-community
6
  langchain-together
7
  einops
8
  PyPDF2
9
+ huggingface-hub
10
+ gradio