Samarth991 commited on
Commit
8da2c81
1 Parent(s): e2b4917

adding chatbot with advanced options

Browse files
Files changed (1) hide show
  1. app.py +29 -27
app.py CHANGED
@@ -34,13 +34,13 @@ def process_documents(documents,data_chunk=1500,chunk_overlap=100):
34
  texts = text_splitter.split_documents(documents)
35
  return texts
36
 
37
- def get_hugging_face_model(model_id,API_key,temperature=0.1):
38
  chat_llm = HuggingFaceHub(huggingfacehub_api_token=API_key,
39
  repo_id=model_id,
40
- model_kwargs={"temperature": temperature, "max_new_tokens": 4096})
41
  return chat_llm
42
 
43
- def chat_application(llm_service,key):
44
  if llm_service == 'HuggingFace':
45
  llm = get_hugging_face_model(model_id='tiiuae/falcon-7b-instruct',API_key=key)
46
  else:
@@ -51,7 +51,7 @@ def summarize_contents():
51
  question = "Generate a summary of the contents. Do not return the response in json format"
52
  return qa.run(question)
53
 
54
- def document_loader(file_path,api_key,doc_type='pdf',llm='Huggingface'):
55
  document = None
56
  if doc_type == 'pdf':
57
  document = process_pdf_document(document_file=file_path)
@@ -69,7 +69,9 @@ def document_loader(file_path,api_key,doc_type='pdf',llm='Huggingface'):
69
  texts = process_documents(documents=document)
70
  vector_db = FAISS.from_documents(documents=texts, embedding= embedding_model)
71
 
72
- qa = RetrievalQA.from_chain_type(llm=chat_application(llm_service=llm,key=api_key),
 
 
73
  chain_type='stuff',
74
  retriever=vector_db.as_retriever(),
75
  # chain_type_kwargs=chain_type_kwargs,
@@ -151,11 +153,28 @@ with gr.Blocks(css=css) as demo:
151
  with gr.Box():
152
  gr.Row()
153
  LLM_option = gr.Dropdown(['HuggingFace','OpenAI'],label='Large Language Model Selection',info='LLM Service')
154
- file_extension = gr.Dropdown(FILE_EXT, label="File Extensions", info="Select your files extensions!")
155
- API_key = gr.Textbox(label="Add API key", type="password")
 
156
  with gr.Column():
157
  with gr.Box():
 
158
  pdf_doc = gr.File(label="Upload File to start QA", file_types=FILE_EXT, type="file")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  with gr.Row():
160
  langchain_status = gr.Textbox(label="Status", placeholder="", interactive = False)
161
  load_pdf = gr.Button("Upload File & Generate Embeddings",).style(full_width = False)
@@ -165,7 +184,7 @@ with gr.Blocks(css=css) as demo:
165
  # submit_button = gr.Button("Send Message")
166
 
167
  load_pdf.click(loading_file, None, langchain_status, queue=False)
168
- load_pdf.click(document_loader, inputs=[pdf_doc,API_key,file_extension,LLM_option], outputs=[langchain_status], queue=False)
169
 
170
  with gr.Group():
171
  chatbot = gr.Chatbot(height=300)
@@ -177,23 +196,6 @@ with gr.Blocks(css=css) as demo:
177
  question.submit(add_text, [chatbot, question], [chatbot, question]).then(bot, chatbot, chatbot)
178
  submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(bot, chatbot, chatbot)
179
 
180
- with gr.Accordion(label='Advanced options', open=False):
181
- system_prompt = gr.Textbox(label='System prompt',
182
- value=DEFAULT_SYSTEM_PROMPT,
183
- lines=6)
184
- max_new_tokens = gr.Slider(
185
- label='Max new tokens',
186
- minimum=1,
187
- maximum=MAX_MAX_NEW_TOKENS,
188
- step=1,
189
- value=DEFAULT_MAX_NEW_TOKENS,
190
- )
191
- temperature = gr.Slider(
192
- label='Temperature',
193
- minimum=0.1,
194
- maximum=4.0,
195
- step=0.1,
196
- value=1.0,
197
- )
198
-
199
  demo.launch()
 
34
  texts = text_splitter.split_documents(documents)
35
  return texts
36
 
37
+ def get_hugging_face_model(model_id,API_key,temperature=0.1,max_tokens=4096):
38
  chat_llm = HuggingFaceHub(huggingfacehub_api_token=API_key,
39
  repo_id=model_id,
40
+ model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens})
41
  return chat_llm
42
 
43
+ def chat_application(llm_service,key,temperature=0.1,max_tokens=1024):
44
  if llm_service == 'HuggingFace':
45
  llm = get_hugging_face_model(model_id='tiiuae/falcon-7b-instruct',API_key=key)
46
  else:
 
51
  question = "Generate a summary of the contents. Do not return the response in json format"
52
  return qa.run(question)
53
 
54
+ def document_loader(file_path,api_key,doc_type='pdf',llm='Huggingface',temperature=0.1,max_tokens=4096):
55
  document = None
56
  if doc_type == 'pdf':
57
  document = process_pdf_document(document_file=file_path)
 
69
  texts = process_documents(documents=document)
70
  vector_db = FAISS.from_documents(documents=texts, embedding= embedding_model)
71
 
72
+ qa = RetrievalQA.from_chain_type(llm=chat_application(llm_service=llm,key=api_key,
73
+ temperature=temperature,
74
+ max_tokens=max_tokens),
75
  chain_type='stuff',
76
  retriever=vector_db.as_retriever(),
77
  # chain_type_kwargs=chain_type_kwargs,
 
153
  with gr.Box():
154
  gr.Row()
155
  LLM_option = gr.Dropdown(['HuggingFace','OpenAI'],label='Large Language Model Selection',info='LLM Service')
156
+ API_key = gr.Textbox(label="Add API key", type="password")
157
+
158
+
159
  with gr.Column():
160
  with gr.Box():
161
+ file_extension = gr.Dropdown(FILE_EXT, label="File Extensions", info="Select your files extensions!")
162
  pdf_doc = gr.File(label="Upload File to start QA", file_types=FILE_EXT, type="file")
163
+ with gr.Accordion(label='Advanced options', open=False):
164
+ max_new_tokens = gr.Slider(
165
+ label='Max new tokens',
166
+ minimum=1,
167
+ maximum=MAX_MAX_NEW_TOKENS,
168
+ step=1,
169
+ value=DEFAULT_MAX_NEW_TOKENS,
170
+ )
171
+ temperature = gr.Slider(
172
+ label='Temperature',
173
+ minimum=0.1,
174
+ maximum=4.0,
175
+ step=0.1,
176
+ value=1.0,
177
+ )
178
  with gr.Row():
179
  langchain_status = gr.Textbox(label="Status", placeholder="", interactive = False)
180
  load_pdf = gr.Button("Upload File & Generate Embeddings",).style(full_width = False)
 
184
  # submit_button = gr.Button("Send Message")
185
 
186
  load_pdf.click(loading_file, None, langchain_status, queue=False)
187
+ load_pdf.click(document_loader, inputs=[pdf_doc,API_key,file_extension,LLM_option,temperature,max_new_tokens], outputs=[langchain_status], queue=False)
188
 
189
  with gr.Group():
190
  chatbot = gr.Chatbot(height=300)
 
196
  question.submit(add_text, [chatbot, question], [chatbot, question]).then(bot, chatbot, chatbot)
197
  submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(bot, chatbot, chatbot)
198
 
199
+
200
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  demo.launch()