Update app.py
Browse files
app.py
CHANGED
|
@@ -133,40 +133,34 @@ def upload_file(file_obj):
|
|
| 133 |
|
| 134 |
|
| 135 |
def demo():
|
| 136 |
-
|
| 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 |
-
|
| 143 |
-
""")
|
| 144 |
with gr.Row():
|
| 145 |
-
|
|
|
|
| 146 |
gr.Markdown("<b>Step 1 - Upload PDF documents and Initialize RAG pipeline</b>")
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 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 |
-
|
| 155 |
-
|
| 156 |
-
with gr.
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 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 QA Chatbot")
|
| 166 |
-
with gr.Row():
|
| 167 |
-
llm_progress = gr.Textbox(value="Not initialized", show_label=False) # label="Chatbot status",
|
| 168 |
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
gr.Markdown("<b>Step 2 - Chat with your Document</b>")
|
| 171 |
chatbot = gr.Chatbot(height=480)
|
| 172 |
with gr.Accordion("Relevent context from the source document", open=False):
|
|
@@ -179,38 +173,27 @@ def demo():
|
|
| 179 |
with gr.Row():
|
| 180 |
doc_source3 = gr.Textbox(label="Reference 3", lines=2, container=True, scale=20)
|
| 181 |
source3_page = gr.Number(label="Page", scale=1)
|
| 182 |
-
|
| 183 |
-
msg = gr.Textbox(placeholder="Ask a question", container=True)
|
| 184 |
with gr.Row():
|
| 185 |
submit_btn = gr.Button("Submit")
|
| 186 |
clear_btn = gr.ClearButton([msg, chatbot], value="Clear")
|
| 187 |
-
|
| 188 |
# Preprocessing events
|
| 189 |
-
db_btn.click(initialize_database,
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], \
|
| 197 |
-
queue=False)
|
| 198 |
|
| 199 |
# Chatbot events
|
| 200 |
-
msg.submit(conversation,
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
submit_btn.click(conversation, \
|
| 205 |
-
inputs=[qa_chain, msg, chatbot], \
|
| 206 |
-
outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], \
|
| 207 |
-
queue=False)
|
| 208 |
-
clear_btn.click(lambda:[None,"",0,"",0,"",0], \
|
| 209 |
-
inputs=None, \
|
| 210 |
-
outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], \
|
| 211 |
-
queue=False)
|
| 212 |
demo.queue().launch(debug=True)
|
| 213 |
-
|
| 214 |
|
| 215 |
|
| 216 |
if __name__ == "__main__":
|
|
|
|
| 133 |
|
| 134 |
|
| 135 |
def demo():
|
| 136 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="red", secondary_hue="pink", neutral_hue="sky")) as demo:
|
|
|
|
| 137 |
vector_db = gr.State()
|
| 138 |
qa_chain = gr.State()
|
| 139 |
+
|
| 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. <b>Please do not upload confidential documents.</b>""")
|
| 142 |
+
|
|
|
|
| 143 |
with gr.Row():
|
| 144 |
+
# Left Column (thin)
|
| 145 |
+
with gr.Column(scale=1):
|
| 146 |
gr.Markdown("<b>Step 1 - Upload PDF documents and Initialize RAG pipeline</b>")
|
| 147 |
+
document = gr.Files(height=140, file_count="multiple", file_types=[".pdf"], interactive=True, label="Upload PDF documents")
|
| 148 |
+
db_btn = gr.Button("Create vector DB")
|
| 149 |
+
db_progress = gr.Textbox(value="Not initialized", show_label=False)
|
| 150 |
+
|
|
|
|
|
|
|
| 151 |
gr.Markdown("<style>body { font-size: 16px; }</style><b>Select Large Language Model (LLM) and input parameters</b>")
|
| 152 |
+
llm_btn = gr.Radio(list_llm_simple, label="Available LLMs", value=list_llm_simple[0], type="index")
|
| 153 |
+
|
| 154 |
+
with gr.Accordion("LLM input parameters", open=False):
|
| 155 |
+
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)
|
| 156 |
+
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)
|
| 157 |
+
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
qachain_btn = gr.Button("Initialize QA Chatbot")
|
| 160 |
+
llm_progress = gr.Textbox(value="Not initialized", show_label=False)
|
| 161 |
+
|
| 162 |
+
# Right Column (wide)
|
| 163 |
+
with gr.Column(scale=5):
|
| 164 |
gr.Markdown("<b>Step 2 - Chat with your Document</b>")
|
| 165 |
chatbot = gr.Chatbot(height=480)
|
| 166 |
with gr.Accordion("Relevent context from the source document", open=False):
|
|
|
|
| 173 |
with gr.Row():
|
| 174 |
doc_source3 = gr.Textbox(label="Reference 3", lines=2, container=True, scale=20)
|
| 175 |
source3_page = gr.Number(label="Page", scale=1)
|
| 176 |
+
msg = gr.Textbox(placeholder="Ask a question", container=True)
|
|
|
|
| 177 |
with gr.Row():
|
| 178 |
submit_btn = gr.Button("Submit")
|
| 179 |
clear_btn = gr.ClearButton([msg, chatbot], value="Clear")
|
| 180 |
+
|
| 181 |
# Preprocessing events
|
| 182 |
+
db_btn.click(initialize_database, inputs=[document], outputs=[vector_db, db_progress])
|
| 183 |
+
qachain_btn.click(initialize_LLM, inputs=[llm_btn, slider_temperature, slider_maxtokens, slider_topk, vector_db], outputs=[qa_chain, llm_progress]).then(
|
| 184 |
+
lambda: [None, "", 0, "", 0, "", 0],
|
| 185 |
+
inputs=None,
|
| 186 |
+
outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page],
|
| 187 |
+
queue=False
|
| 188 |
+
)
|
|
|
|
|
|
|
| 189 |
|
| 190 |
# Chatbot events
|
| 191 |
+
msg.submit(conversation, inputs=[qa_chain, msg, chatbot], outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], queue=False)
|
| 192 |
+
submit_btn.click(conversation, inputs=[qa_chain, msg, chatbot], outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], queue=False)
|
| 193 |
+
clear_btn.click(lambda: [None, "", 0, "", 0, "", 0], inputs=None, outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], queue=False)
|
| 194 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
demo.queue().launch(debug=True)
|
| 196 |
+
|
| 197 |
|
| 198 |
|
| 199 |
if __name__ == "__main__":
|