thesnak commited on
Commit
43f9fa7
1 Parent(s): a3bd89c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -26
app.py CHANGED
@@ -25,12 +25,17 @@ import re
25
 
26
 
27
  # default_persist_directory = './chroma_HF/'
28
- list_llm = ["mistralai/Mistral-7B-Instruct-v0.2", "mistralai/Mixtral-8x7B-Instruct-v0.1", "mistralai/Mistral-7B-Instruct-v0.1", \
29
- "google/gemma-7b-it","google/gemma-2b-it", \
30
- "HuggingFaceH4/zephyr-7b-beta", "HuggingFaceH4/zephyr-7b-gemma-v0.1", \
31
- "meta-llama/Llama-2-7b-chat-hf", "microsoft/phi-2", \
32
- "TinyLlama/TinyLlama-1.1B-Chat-v1.0", "mosaicml/mpt-7b-instruct", "tiiuae/falcon-7b-instruct", \
33
- "google/flan-t5-xxl" , "core42/jais-13b"
 
 
 
 
 
34
  ]
35
  list_llm_simple = [os.path.basename(llm) for llm in list_llm]
36
 
@@ -121,7 +126,7 @@ def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, pr
121
  top_k = top_k,
122
  )
123
  elif llm_model == "microsoft/phi-2":
124
- # raise gr.Error("phi-2 model requires 'trust_remote_code=True', currently not supported by langchain HuggingFaceHub...")
125
  llm = HuggingFaceEndpoint(
126
  repo_id=llm_model,
127
  # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "trust_remote_code": True, "torch_dtype": "auto"}
@@ -286,21 +291,16 @@ def demo():
286
  collection_name = gr.State()
287
 
288
  gr.Markdown(
289
- """<center><h2>PDF-based chatbot</center></h2>
290
- <h3>Ask any questions about your PDF documents</h3>""")
291
- gr.Markdown(
292
- """<b>Note:</b> This AI assistant, using Langchain and open-source LLMs, performs retrieval-augmented generation (RAG) from your PDF documents. \
293
- The user interface explicitely shows multiple steps to help understand the RAG workflow.
294
- This chatbot takes past questions into account when generating answers (via conversational memory), and includes document references for clarity purposes.<br>
295
- <br><b>Warning:</b> This space uses the free CPU Basic hardware from Hugging Face. Some steps and LLM models used below (free inference endpoints) can take some time to generate a reply.
296
  """)
297
-
298
- with gr.Tab("Step 1 - Upload PDF"):
299
  with gr.Row():
300
  document = gr.Files(height=100, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload your PDF documents (single or multiple)")
301
  # upload_btn = gr.UploadButton("Loading document...", height=100, file_count="multiple", file_types=["pdf"], scale=1)
302
-
303
- with gr.Tab("Step 2 - Process document"):
304
  with gr.Row():
305
  db_btn = gr.Radio(["ChromaDB"], label="Vector database type", value = "ChromaDB", type="index", info="Choose your vector database")
306
  with gr.Accordion("Advanced options - Document text splitter", open=False):
@@ -311,15 +311,15 @@ def demo():
311
  with gr.Row():
312
  db_progress = gr.Textbox(label="Vector database initialization", value="None")
313
  with gr.Row():
314
- db_btn = gr.Button("Generate vector database")
315
 
316
- with gr.Tab("Step 3 - Initialize QA chain"):
317
  with gr.Row():
318
  llm_btn = gr.Radio(list_llm_simple, \
319
  label="LLM models", value = list_llm_simple[0], type="index", info="Choose your LLM model")
320
  with gr.Accordion("Advanced options - LLM model", open=False):
321
  with gr.Row():
322
- slider_temperature = gr.Slider(minimum = 0.01, maximum = 1.0, value=0.7, step=0.1, label="Temperature", info="Model temperature", interactive=True)
323
  with gr.Row():
324
  slider_maxtokens = gr.Slider(minimum = 224, maximum = 4096, value=1024, step=32, label="Max Tokens", info="Model max tokens", interactive=True)
325
  with gr.Row():
@@ -327,9 +327,9 @@ def demo():
327
  with gr.Row():
328
  llm_progress = gr.Textbox(value="None",label="QA chain initialization")
329
  with gr.Row():
330
- qachain_btn = gr.Button("Initialize Question Answering chain")
331
 
332
- with gr.Tab("Step 4 - Chatbot"):
333
  chatbot = gr.Chatbot(height=300)
334
  with gr.Accordion("Advanced - Document references", open=False):
335
  with gr.Row():
@@ -342,10 +342,10 @@ def demo():
342
  doc_source3 = gr.Textbox(label="Reference 3", lines=2, container=True, scale=20)
343
  source3_page = gr.Number(label="Page", scale=1)
344
  with gr.Row():
345
- msg = gr.Textbox(placeholder="Type message (e.g. 'What is this document about?')", container=True)
346
  with gr.Row():
347
- submit_btn = gr.Button("Submit message")
348
- clear_btn = gr.ClearButton([msg, chatbot], value="Clear conversation")
349
 
350
  # Preprocessing events
351
  #upload_btn.upload(upload_file, inputs=[upload_btn], outputs=[document])
 
25
 
26
 
27
  # default_persist_directory = './chroma_HF/'
28
+ # list_llm = ["mistralai/Mistral-7B-Instruct-v0.2", "mistralai/Mixtral-8x7B-Instruct-v0.1", "mistralai/Mistral-7B-Instruct-v0.1", \
29
+ # "google/gemma-7b-it","google/gemma-2b-it", \
30
+ # "HuggingFaceH4/zephyr-7b-beta", "HuggingFaceH4/zephyr-7b-gemma-v0.1", \
31
+ # "meta-llama/Llama-2-7b-chat-hf", "microsoft/phi-2", \
32
+ # "TinyLlama/TinyLlama-1.1B-Chat-v1.0", "mosaicml/mpt-7b-instruct", "tiiuae/falcon-7b-instruct", \
33
+ # "google/flan-t5-xxl" , "core42/jais-13b"
34
+ # ]
35
+
36
+ list_llm = [
37
+ "HuggingFaceH4/zephyr-7b-beta",
38
+
39
  ]
40
  list_llm_simple = [os.path.basename(llm) for llm in list_llm]
41
 
 
126
  top_k = top_k,
127
  )
128
  elif llm_model == "microsoft/phi-2":
129
+ raise gr.Error("phi-2 model requires 'trust_remote_code=True', currently not supported by langchain HuggingFaceHub...")
130
  llm = HuggingFaceEndpoint(
131
  repo_id=llm_model,
132
  # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "trust_remote_code": True, "torch_dtype": "auto"}
 
291
  collection_name = gr.State()
292
 
293
  gr.Markdown(
294
+ """<center><h2>PDF-based chatbot (powered by LangChain and open-source LLMs)</center></h2>
295
+ <h3>Ask any questions about your PDF documents, along with follow-ups</h3>
296
+ <b>Note:</b> This AI assistant performs retrieval-augmented generation from your PDF documents. \
297
+ When generating answers, it takes past questions into account (via conversational memory), and includes document references for clarity purposes.</i>
298
+ <br><b>Warning:</b> This space uses the free CPU Basic hardware from Hugging Face. Some steps and LLM models used below (free inference endpoints) can take some time to generate an output.<br>
 
 
299
  """)
300
+ with gr.Tab("Step 1 - Document pre-processing"):
 
301
  with gr.Row():
302
  document = gr.Files(height=100, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload your PDF documents (single or multiple)")
303
  # upload_btn = gr.UploadButton("Loading document...", height=100, file_count="multiple", file_types=["pdf"], scale=1)
 
 
304
  with gr.Row():
305
  db_btn = gr.Radio(["ChromaDB"], label="Vector database type", value = "ChromaDB", type="index", info="Choose your vector database")
306
  with gr.Accordion("Advanced options - Document text splitter", open=False):
 
311
  with gr.Row():
312
  db_progress = gr.Textbox(label="Vector database initialization", value="None")
313
  with gr.Row():
314
+ db_btn = gr.Button("Generate vector database...")
315
 
316
+ with gr.Tab("Step 2 - QA chain initialization"):
317
  with gr.Row():
318
  llm_btn = gr.Radio(list_llm_simple, \
319
  label="LLM models", value = list_llm_simple[0], type="index", info="Choose your LLM model")
320
  with gr.Accordion("Advanced options - LLM model", open=False):
321
  with gr.Row():
322
+ slider_temperature = gr.Slider(minimum = 0.0, maximum = 1.0, value=0.7, step=0.1, label="Temperature", info="Model temperature", interactive=True)
323
  with gr.Row():
324
  slider_maxtokens = gr.Slider(minimum = 224, maximum = 4096, value=1024, step=32, label="Max Tokens", info="Model max tokens", interactive=True)
325
  with gr.Row():
 
327
  with gr.Row():
328
  llm_progress = gr.Textbox(value="None",label="QA chain initialization")
329
  with gr.Row():
330
+ qachain_btn = gr.Button("Initialize question-answering chain...")
331
 
332
+ with gr.Tab("Step 3 - Conversation with chatbot"):
333
  chatbot = gr.Chatbot(height=300)
334
  with gr.Accordion("Advanced - Document references", open=False):
335
  with gr.Row():
 
342
  doc_source3 = gr.Textbox(label="Reference 3", lines=2, container=True, scale=20)
343
  source3_page = gr.Number(label="Page", scale=1)
344
  with gr.Row():
345
+ msg = gr.Textbox(placeholder="Type message", container=True)
346
  with gr.Row():
347
+ submit_btn = gr.Button("Submit")
348
+ clear_btn = gr.ClearButton([msg, chatbot])
349
 
350
  # Preprocessing events
351
  #upload_btn.upload(upload_file, inputs=[upload_btn], outputs=[document])