kodetr commited on
Commit
37ab34b
·
verified ·
1 Parent(s): e50d6ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -15,7 +15,7 @@ from langchain.memory import ConversationBufferMemory
15
  from langchain_community.llms import HuggingFaceEndpoint
16
  import torch
17
 
18
- list_llm = ["meta-llama/Meta-Llama-3-8B-Instruct", "mistralai/Mistral-7B-Instruct-v0.2"]
19
  list_llm_simple = [os.path.basename(llm) for llm in list_llm]
20
 
21
  # Load and split PDF document
@@ -43,7 +43,7 @@ def create_db(splits):
43
 
44
  # Initialize langchain LLM chain
45
  def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
46
- if llm_model == "meta-llama/Meta-Llama-3-8B-Instruct":
47
  llm = HuggingFaceEndpoint(
48
  repo_id=llm_model,
49
  huggingfacehub_api_token = api_token,
@@ -93,7 +93,7 @@ def initialize_LLM(llm_option, llm_temperature, max_tokens, top_k, vector_db, pr
93
  llm_name = list_llm[llm_option]
94
  print("llm_name: ",llm_name)
95
  qa_chain = initialize_llmchain(llm_name, llm_temperature, max_tokens, top_k, vector_db, progress)
96
- return qa_chain, "QA chain initialized. Chatbot is ready!"
97
 
98
 
99
  def format_chat_history(message, chat_history):
@@ -137,39 +137,39 @@ def demo():
137
  with gr.Blocks(theme=gr.themes.Default(primary_hue="red", secondary_hue="pink", neutral_hue = "sky")) as demo:
138
  vector_db = gr.State()
139
  qa_chain = gr.State()
140
- gr.HTML("<center><h1>RAG PDF chatbot</h1><center>")
141
- gr.Markdown("""<b>Query your PDF documents!</b> This AI agent is designed to perform retrieval augmented generation (RAG) on PDF documents. The app is hosted on Hugging Face Hub for the sole purpose of demonstration. \
142
- <b>Please do not upload confidential documents.</b>
143
  """)
144
  with gr.Row():
145
  with gr.Column(scale = 86):
146
- gr.Markdown("<b>Step 1 - Upload PDF documents and Initialize RAG pipeline</b>")
147
  with gr.Row():
148
- document = gr.Files(height=300, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload PDF documents")
149
  with gr.Row():
150
- db_btn = gr.Button("Create vector database")
151
  with gr.Row():
152
  db_progress = gr.Textbox(value="Not initialized", show_label=False) # label="Vector database status",
153
- gr.Markdown("<style>body { font-size: 16px; }</style><b>Select Large Language Model (LLM) and input parameters</b>")
154
  with gr.Row():
155
  llm_btn = gr.Radio(list_llm_simple, label="Available LLMs", value = list_llm_simple[0], type="index") # info="Select LLM", show_label=False
156
  with gr.Row():
157
- with gr.Accordion("LLM input parameters", open=False):
158
  with gr.Row():
159
- slider_temperature = gr.Slider(minimum = 0.01, maximum = 1.0, value=0.5, step=0.1, label="Temperature", info="Controls randomness in token generation", interactive=True)
160
  with gr.Row():
161
- slider_maxtokens = gr.Slider(minimum = 128, maximum = 9192, value=4096, step=128, label="Max New Tokens", info="Maximum number of tokens to be generated",interactive=True)
162
  with gr.Row():
163
- slider_topk = gr.Slider(minimum = 1, maximum = 10, value=3, step=1, label="top-k", info="Number of tokens to select the next token from", interactive=True)
164
  with gr.Row():
165
- qachain_btn = gr.Button("Initialize Question Answering Chatbot")
166
  with gr.Row():
167
  llm_progress = gr.Textbox(value="Not initialized", show_label=False) # label="Chatbot status",
168
 
169
  with gr.Column(scale = 200):
170
- gr.Markdown("<b>Step 2 - Chat with your Document</b>")
171
  chatbot = gr.Chatbot(height=505)
172
- with gr.Accordion("Relevent context from the source document", open=False):
173
  with gr.Row():
174
  doc_source1 = gr.Textbox(label="Reference 1", lines=2, container=True, scale=20)
175
  source1_page = gr.Number(label="Page", scale=1)
 
15
  from langchain_community.llms import HuggingFaceEndpoint
16
  import torch
17
 
18
+ list_llm = ["kodetr/stunting-qa-v3", "meta-llama/Meta-Llama-3-8B-Instruct"]
19
  list_llm_simple = [os.path.basename(llm) for llm in list_llm]
20
 
21
  # Load and split PDF document
 
43
 
44
  # Initialize langchain LLM chain
45
  def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
46
+ if llm_model == "kodetr/stunting-qa-v3":
47
  llm = HuggingFaceEndpoint(
48
  repo_id=llm_model,
49
  huggingfacehub_api_token = api_token,
 
93
  llm_name = list_llm[llm_option]
94
  print("llm_name: ",llm_name)
95
  qa_chain = initialize_llmchain(llm_name, llm_temperature, max_tokens, top_k, vector_db, progress)
96
+ return qa_chain, "QA chain initialized. Chatbot sudah siap!"
97
 
98
 
99
  def format_chat_history(message, chat_history):
 
137
  with gr.Blocks(theme=gr.themes.Default(primary_hue="red", secondary_hue="pink", neutral_hue = "sky")) as demo:
138
  vector_db = gr.State()
139
  qa_chain = gr.State()
140
+ gr.HTML("<center><h1>RAG PDF Stunting</h1><center>")
141
+ gr.Markdown("""<b>Kueri dokumen PDF Anda!</b> Agen AI ini dirancang untuk melakukan pengambilan augmented generation (RAG) pada dokumen PDF. Aplikasi ini dihosting di Hugging Face Hub hanya untuk tujuan demonstrasi. \
142
+ <b>Harap jangan mengunggah dokumen rahasia.</b>
143
  """)
144
  with gr.Row():
145
  with gr.Column(scale = 86):
146
+ gr.Markdown("<b>Langkah 1 - Unggah dokumen PDF dan Inisialisasi pipeline RAG</b>")
147
  with gr.Row():
148
+ document = gr.Files(height=300, file_count="multiple", file_types=["pdf"], interactive=True, label="Unggah dokumen PDF")
149
  with gr.Row():
150
+ db_btn = gr.Button("Buat database vektor")
151
  with gr.Row():
152
  db_progress = gr.Textbox(value="Not initialized", show_label=False) # label="Vector database status",
153
+ gr.Markdown("<style>body { font-size: 16px; }</style><b>Pilih Model Bahasa Besar (LLM) dan parameter masukan</b>")
154
  with gr.Row():
155
  llm_btn = gr.Radio(list_llm_simple, label="Available LLMs", value = list_llm_simple[0], type="index") # info="Select LLM", show_label=False
156
  with gr.Row():
157
+ with gr.Accordion("Parameter masukan LLM", open=False):
158
  with gr.Row():
159
+ slider_temperature = gr.Slider(minimum = 0.01, maximum = 1.0, value=0.5, step=0.1, label="Temperature", info="Mengontrol keacakan dalam pembuatan token", interactive=True)
160
  with gr.Row():
161
+ slider_maxtokens = gr.Slider(minimum = 128, maximum = 9192, value=4096, step=128, label="Maksimum Token Baru", info="Jumlah maksimum token yang akan dihasilkan",interactive=True)
162
  with gr.Row():
163
+ slider_topk = gr.Slider(minimum = 1, maximum = 10, value=3, step=1, label="top-k", info="Jumlah token untuk memilih token berikutnya", interactive=True)
164
  with gr.Row():
165
+ qachain_btn = gr.Button("Inisialisasi Chatbot Penjawab Pertanyaan")
166
  with gr.Row():
167
  llm_progress = gr.Textbox(value="Not initialized", show_label=False) # label="Chatbot status",
168
 
169
  with gr.Column(scale = 200):
170
+ gr.Markdown("<b>Langkah 2 - Ngobrol dengan Dokumen Anda</b>")
171
  chatbot = gr.Chatbot(height=505)
172
+ with gr.Accordion("Konteks yang relevan dari dokumen sumber", open=False):
173
  with gr.Row():
174
  doc_source1 = gr.Textbox(label="Reference 1", lines=2, container=True, scale=20)
175
  source1_page = gr.Number(label="Page", scale=1)