raj999 commited on
Commit
64e4082
·
verified ·
1 Parent(s): 902fe41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -23
app.py CHANGED
@@ -4,7 +4,6 @@ from langchain.chains import RetrievalQA
4
  from langchain.embeddings import HuggingFaceEmbeddings
5
  from langchain.vectorstores import FAISS
6
  from langchain.llms import HuggingFaceHub
7
- from langchain.prompts import PromptTemplate
8
  from langchain.chains import ConversationalRetrievalChain
9
 
10
  # Load the HuggingFace language model and embeddings
@@ -13,19 +12,24 @@ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
13
  # Initialize the embeddings model for document retrieval
14
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
15
 
16
- # Set up FAISS as the vector store for document retrieval
17
- # (Replace 'documents' with your actual document list or corpus)
18
- texts = ["Document 1", "Document 2", "Document 3"]
19
- vector_store = FAISS.from_texts(texts, embeddings)
20
 
21
- # Initialize the LangChain RAG system
22
- retriever = vector_store.as_retriever()
 
 
 
 
 
 
 
 
 
23
 
24
- # Set up ConversationalRetrievalChain using LangChain's tools
25
- rag_chain = ConversationalRetrievalChain.from_llm(
26
- HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta"),
27
- retriever=retriever
28
- )
29
 
30
  def respond(
31
  message,
@@ -35,6 +39,18 @@ def respond(
35
  temperature,
36
  top_p,
37
  ):
 
 
 
 
 
 
 
 
 
 
 
 
38
  # Combine history with the user message
39
  conversation_history = [{"role": "system", "content": system_message}]
40
 
@@ -46,22 +62,38 @@ def respond(
46
 
47
  conversation_history.append({"role": "user", "content": message})
48
 
49
- # Retrieve documents using the retriever
50
  response = rag_chain({"question": message, "chat_history": history})
51
 
52
- # Format and return the response
53
  return response['answer']
54
 
 
55
  # Gradio interface setup
56
- demo = gr.ChatInterface(
57
- respond,
58
- additional_inputs=[
59
- gr.Textbox(value="You are a helpful assistant.", label="System message"),
60
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
61
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
62
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
63
- ],
64
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  if __name__ == "__main__":
67
  demo.launch()
 
4
  from langchain.embeddings import HuggingFaceEmbeddings
5
  from langchain.vectorstores import FAISS
6
  from langchain.llms import HuggingFaceHub
 
7
  from langchain.chains import ConversationalRetrievalChain
8
 
9
  # Load the HuggingFace language model and embeddings
 
12
  # Initialize the embeddings model for document retrieval
13
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
14
 
15
+ # Initialize vector_store and retriever as None initially
16
+ vector_store = None
17
+ retriever = None
 
18
 
19
+ def update_documents(text_input):
20
+ global vector_store, retriever
21
+ # Split the input text into individual documents based on newlines or other delimiters
22
+ documents = text_input.split("\n")
23
+
24
+ # Update the FAISS vector store with new documents
25
+ vector_store = FAISS.from_texts(documents, embeddings)
26
+
27
+ # Set the retriever to use the new vector store
28
+ retriever = vector_store.as_retriever()
29
+ return f"{len(documents)} documents successfully added to the vector store."
30
 
31
+ # Set up ConversationalRetrievalChain
32
+ rag_chain = None
 
 
 
33
 
34
  def respond(
35
  message,
 
39
  temperature,
40
  top_p,
41
  ):
42
+ global rag_chain, retriever
43
+
44
+ if retriever is None:
45
+ return "Please upload or enter documents before asking a question."
46
+
47
+ # Create the chain if it hasn't been initialized
48
+ if rag_chain is None:
49
+ rag_chain = ConversationalRetrievalChain.from_llm(
50
+ HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta"),
51
+ retriever=retriever
52
+ )
53
+
54
  # Combine history with the user message
55
  conversation_history = [{"role": "system", "content": system_message}]
56
 
 
62
 
63
  conversation_history.append({"role": "user", "content": message})
64
 
65
+ # Retrieve documents and generate response
66
  response = rag_chain({"question": message, "chat_history": history})
67
 
68
+ # Return the model's response
69
  return response['answer']
70
 
71
+
72
  # Gradio interface setup
73
+ demo = gr.Blocks()
74
+
75
+ with demo:
76
+ with gr.Row():
77
+ # Input box for user to add documents
78
+ doc_input = gr.Textbox(
79
+ lines=10, placeholder="Enter your documents here, one per line.", label="Input Documents"
80
+ )
81
+ upload_button = gr.Button("Upload Documents")
82
+
83
+ with gr.Row():
84
+ # Chat interface for the RAG system
85
+ chat = gr.ChatInterface(
86
+ respond,
87
+ additional_inputs=[
88
+ gr.Textbox(value="You are a helpful assistant.", label="System message"),
89
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
90
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
91
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
92
+ ],
93
+ )
94
+
95
+ # Bind button to update the document vector store
96
+ upload_button.click(update_documents, inputs=[doc_input], outputs=gr.Textbox(label="Status"))
97
 
98
  if __name__ == "__main__":
99
  demo.launch()