httpdaniel commited on
Commit
9531160
·
1 Parent(s): 90231c1
Files changed (1) hide show
  1. app.py +33 -18
app.py CHANGED
@@ -8,6 +8,7 @@ from langchain_core.prompts import ChatPromptTemplate
8
  from langchain.chains.combine_documents import create_stuff_documents_chain
9
  from langchain.chains import create_retrieval_chain
10
 
 
11
  def initialise_chatbot(pdf, llm, progress=gr.Progress()):
12
  progress(0, desc="Reading PDF")
13
 
@@ -18,10 +19,7 @@ def initialise_chatbot(pdf, llm, progress=gr.Progress()):
18
 
19
  progress(0.25, desc="Initialising Vectorstore")
20
 
21
- vectorstore = Chroma.from_documents(
22
- splits,
23
- embedding=HuggingFaceEmbeddings()
24
- )
25
 
26
  progress(0.85, desc="Initialising LLM")
27
 
@@ -30,13 +28,10 @@ def initialise_chatbot(pdf, llm, progress=gr.Progress()):
30
  task="text-generation",
31
  max_new_tokens=512,
32
  top_k=4,
33
- temperature=0.05
34
  )
35
 
36
- chat = ChatHuggingFace(
37
- llm=llm,
38
- verbose=True
39
- )
40
 
41
  retriever = vectorstore.as_retriever(search_kwargs={"k": 8})
42
 
@@ -62,35 +57,55 @@ def initialise_chatbot(pdf, llm, progress=gr.Progress()):
62
 
63
  return rag_chain, "Complete!"
64
 
 
65
  def send(message, rag_chain, chat_history):
66
  response = rag_chain.invoke({"input": message})
67
  chat_history.append((message, response["answer"]))
68
 
69
  return "", chat_history
70
 
71
- with gr.Blocks() as demo:
72
 
 
73
  vectorstore = gr.State()
74
  rag_chain = gr.State()
75
-
76
  gr.Markdown("<H1>Talk to Documents</H1>")
77
  gr.Markdown("<H3>Upload and ask questions about your PDF files</H3>")
78
- gr.Markdown("<H6>Note: This project uses LangChain to perform RAG (Retrieval Augmented Generation) on PDF files, allowing users to ask any questions related to their contents. When a PDF file is uploaded, it is embedded and stored in an in-memory Chroma vectorstore, which the chatbot uses as a source of knowledge when aswering user questions.</H6>")
 
 
79
 
80
  with gr.Row():
81
  with gr.Column(scale=1):
82
  input_pdf = gr.File(label="1. Upload PDF")
83
- language_model = gr.Radio(label="2. Choose LLM", choices=["microsoft/Phi-3-mini-4k-instruct", "mistralai/Mistral-7B-Instruct-v0.2", "HuggingFaceH4/zephyr-7b-beta", "mistralai/Mixtral-8x7B-Instruct-v0.1"])
84
- initialise_chatbot_btn = gr.Button(value="3. Initialise Chatbot", variant='primary')
85
- chatbot_initialisation_progress = gr.Textbox(value="Not Started", label="Initialization Progress")
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  with gr.Column(scale=4):
88
  chatbot = gr.Chatbot(scale=1)
89
  message = gr.Textbox(label="4. Ask questions about your PDF")
90
 
91
  initialise_chatbot_btn.click(
92
- fn=initialise_chatbot, inputs=[input_pdf, language_model], outputs=[rag_chain, chatbot_initialisation_progress]
 
 
93
  )
94
- message.submit(fn=send, inputs=[message, rag_chain, chatbot], outputs=[message, chatbot])
 
 
 
 
95
 
96
- demo.launch()
 
8
  from langchain.chains.combine_documents import create_stuff_documents_chain
9
  from langchain.chains import create_retrieval_chain
10
 
11
+
12
  def initialise_chatbot(pdf, llm, progress=gr.Progress()):
13
  progress(0, desc="Reading PDF")
14
 
 
19
 
20
  progress(0.25, desc="Initialising Vectorstore")
21
 
22
+ vectorstore = Chroma.from_documents(splits, embedding=HuggingFaceEmbeddings())
 
 
 
23
 
24
  progress(0.85, desc="Initialising LLM")
25
 
 
28
  task="text-generation",
29
  max_new_tokens=512,
30
  top_k=4,
31
+ temperature=0.05,
32
  )
33
 
34
+ chat = ChatHuggingFace(llm=llm, verbose=True)
 
 
 
35
 
36
  retriever = vectorstore.as_retriever(search_kwargs={"k": 8})
37
 
 
57
 
58
  return rag_chain, "Complete!"
59
 
60
+
61
  def send(message, rag_chain, chat_history):
62
  response = rag_chain.invoke({"input": message})
63
  chat_history.append((message, response["answer"]))
64
 
65
  return "", chat_history
66
 
 
67
 
68
+ with gr.Blocks() as demo:
69
  vectorstore = gr.State()
70
  rag_chain = gr.State()
71
+
72
  gr.Markdown("<H1>Talk to Documents</H1>")
73
  gr.Markdown("<H3>Upload and ask questions about your PDF files</H3>")
74
+ gr.Markdown(
75
+ "<H6>Note: This project uses LangChain to perform RAG (Retrieval Augmented Generation) on PDF files, allowing users to ask any questions related to their contents. When a PDF file is uploaded, it is embedded and stored in an in-memory Chroma vectorstore, which the chatbot uses as a source of knowledge when aswering user questions.</H6>"
76
+ )
77
 
78
  with gr.Row():
79
  with gr.Column(scale=1):
80
  input_pdf = gr.File(label="1. Upload PDF")
81
+ language_model = gr.Radio(
82
+ label="2. Choose LLM",
83
+ choices=[
84
+ "microsoft/Phi-3-mini-4k-instruct",
85
+ "mistralai/Mistral-7B-Instruct-v0.2",
86
+ "HuggingFaceH4/zephyr-7b-beta",
87
+ "mistralai/Mixtral-8x7B-Instruct-v0.1",
88
+ ],
89
+ )
90
+ initialise_chatbot_btn = gr.Button(
91
+ value="3. Initialise Chatbot", variant="primary"
92
+ )
93
+ chatbot_initialisation_progress = gr.Textbox(
94
+ value="Not Started", label="Initialization Progress"
95
+ )
96
 
97
  with gr.Column(scale=4):
98
  chatbot = gr.Chatbot(scale=1)
99
  message = gr.Textbox(label="4. Ask questions about your PDF")
100
 
101
  initialise_chatbot_btn.click(
102
+ fn=initialise_chatbot,
103
+ inputs=[input_pdf, language_model],
104
+ outputs=[rag_chain, chatbot_initialisation_progress],
105
  )
106
+ message.submit(
107
+ fn=send, inputs=[message, rag_chain, chatbot], outputs=[message, chatbot]
108
+ )
109
+
110
+ demo.launch()
111