raj999 commited on
Commit
b3ae10a
·
verified ·
1 Parent(s): eefb4a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -12
app.py CHANGED
@@ -5,7 +5,6 @@ from langchain.embeddings import HuggingFaceEmbeddings
5
  from langchain.vectorstores import FAISS
6
  from langchain.llms import HuggingFaceHub
7
  from langchain.chains import ConversationalRetrievalChain
8
- from pathlib import Path
9
 
10
  # Load the HuggingFace language model and embeddings
11
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
@@ -17,14 +16,29 @@ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-b
17
  vector_store = None
18
  retriever = None
19
 
20
- def update_documents(documents):
21
  global vector_store, retriever
 
 
 
22
  # Update the FAISS vector store with new documents
23
  vector_store = FAISS.from_texts(documents, embeddings)
 
 
24
  retriever = vector_store.as_retriever()
25
  return f"{len(documents)} documents successfully added to the vector store."
26
 
27
- def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
 
 
 
 
 
 
 
 
 
 
28
  global rag_chain, retriever
29
 
30
  if retriever is None:
@@ -54,21 +68,27 @@ def respond(message, history: list[tuple[str, str]], system_message, max_tokens,
54
  # Return the model's response
55
  return response['answer']
56
 
57
- def upload_file(file):
58
- text = file.read().decode("utf-8") # Read file content
59
- documents = text.split("\n") # Split into documents
60
- return update_documents(documents)
 
 
61
 
62
  # Gradio interface setup
63
  demo = gr.Blocks()
64
 
65
  with demo:
66
  with gr.Row():
67
- u = gr.UploadButton("Upload a file (txt)", file_count="single", file_types=[".txt"])
68
-
69
- # Process the uploaded file
70
- u.upload(upload_file, u, gr.Textbox(label="Status", visible=True))
71
 
 
 
 
 
72
  with gr.Row():
73
  # Chat interface for the RAG system
74
  chat = gr.ChatInterface(
@@ -81,5 +101,8 @@ with demo:
81
  ],
82
  )
83
 
 
 
 
84
  if __name__ == "__main__":
85
- demo.launch()
 
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
10
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
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,
36
+ history: list[tuple[str, str]],
37
+ system_message,
38
+ max_tokens,
39
+ temperature,
40
+ top_p,
41
+ ):
42
  global rag_chain, retriever
43
 
44
  if retriever is None:
 
68
  # Return the model's response
69
  return response['answer']
70
 
71
+ def upload_file(filepath):
72
+ name = Path(filepath).name
73
+ return [gr.UploadButton(visible=False), gr.DownloadButton(label=f"Download {name}", value=filepath, visible=True)]
74
+
75
+ def download_file():
76
+ return [gr.UploadButton(visible=True), gr.DownloadButton(visible=False)]
77
 
78
  # Gradio interface setup
79
  demo = gr.Blocks()
80
 
81
  with demo:
82
  with gr.Row():
83
+ # upload_button = gr.Button("Upload Documents")
84
+ with gr.Row():
85
+ u = gr.UploadButton("Upload a file", file_count="single")
86
+ d = gr.DownloadButton("Download the file", visible=False)
87
 
88
+ u.upload(upload_file, u, [u, d])
89
+ d.click(download_file, None, [u, d])
90
+
91
+
92
  with gr.Row():
93
  # Chat interface for the RAG system
94
  chat = gr.ChatInterface(
 
101
  ],
102
  )
103
 
104
+ # Bind button to update the document vector store
105
+ # upload_button.click(update_documents, inputs=[doc_input], outputs=gr.Textbox(label="Status"))
106
+
107
  if __name__ == "__main__":
108
+ demo.launch()