Update app.py
Browse files
app.py
CHANGED
|
@@ -278,36 +278,37 @@ def upload_file(file_obj):
|
|
| 278 |
return list_file_path
|
| 279 |
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
| 283 |
def demo():
|
| 284 |
with gr.Blocks(theme="base") as demo:
|
| 285 |
vector_db = gr.State()
|
| 286 |
qa_chain = gr.State()
|
| 287 |
collection_name = gr.State()
|
| 288 |
-
|
| 289 |
-
gr.Markdown(
|
| 290 |
-
"""<center><h2>BookMyDarshan: Your Personalized Spiritual Assistant</h2></center>
|
| 291 |
-
<h3>Explore Sacred Texts and Enhance Your Spiritual Journey</h3>""")
|
| 292 |
-
|
| 293 |
-
gr.Markdown(
|
| 294 |
-
"""<b>About BookMyDarshan.in:</b> We are dedicated to providing pilgrims with exceptional temple darshan experiences.
|
| 295 |
-
Our platform offers a comprehensive suite of spiritual and religious services, tailored to meet your devotional needs.<br><br>
|
| 296 |
-
<b>Note:</b> This spiritual assistant uses state-of-the-art AI to help you explore and understand your uploaded spiritual documents.
|
| 297 |
-
Combining technology with tradition, this tool assists in deepening your connection with your faith.<br>"""
|
| 298 |
-
)
|
| 299 |
-
|
| 300 |
-
with gr.Tab("Step 1: Upload PDF"):
|
| 301 |
-
document = gr.Files(label="Upload your PDF documents", file_count="multiple", file_types=["pdf"], interactive=True)
|
| 302 |
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
slider_chunk_size = gr.Slider(minimum=100, maximum=1000, value=600, step=20, label="Chunk Size", info="Adjust chunk size for text splitting")
|
| 307 |
-
slider_chunk_overlap = gr.Slider(minimum=10, maximum=200, value=40, step=10, label="Chunk Overlap", info="Adjust overlap between chunks")
|
| 308 |
-
db_progress = gr.Textbox(label="Vector Database Initialization Status", value="None", interactive=False)
|
| 309 |
-
generate_db_btn = gr.Button("Generate Vector Database")
|
| 310 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
with gr.Tab("Step 3 - Initialize QA chain"):
|
| 312 |
with gr.Row():
|
| 313 |
llm_btn = gr.Radio(list_llm_simple, \
|
|
@@ -324,60 +325,51 @@ def demo():
|
|
| 324 |
with gr.Row():
|
| 325 |
qachain_btn = gr.Button("Initialize Question Answering chain")
|
| 326 |
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
with gr.Tab("Step 4: Chatbot"):
|
| 331 |
-
chatbot = gr.Chatbot(label="Chat with your PDF", height=300)
|
| 332 |
-
with gr.Accordion("Advanced: Document References", open=False):
|
| 333 |
with gr.Row():
|
| 334 |
-
doc_source1 = gr.Textbox(label="Reference 1", lines=2)
|
| 335 |
-
source1_page = gr.Number(label="Page",
|
| 336 |
with gr.Row():
|
| 337 |
-
doc_source2 = gr.Textbox(label="Reference 2", lines=2)
|
| 338 |
-
source2_page = gr.Number(label="Page",
|
| 339 |
with gr.Row():
|
| 340 |
-
doc_source3 = gr.Textbox(label="Reference 3", lines=2)
|
| 341 |
-
source3_page = gr.Number(label="Page",
|
| 342 |
-
msg = gr.Textbox(placeholder="Type your question here...", label="Ask a Question", container=True)
|
| 343 |
with gr.Row():
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
|
|
|
|
|
|
| 347 |
# Preprocessing events
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
lambda:
|
| 355 |
-
inputs=None,
|
| 356 |
-
outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page],
|
| 357 |
-
queue=False
|
| 358 |
-
)
|
| 359 |
|
| 360 |
# Chatbot events
|
| 361 |
-
msg.submit(
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
clear_btn.click(
|
| 374 |
-
lambda: [None, "", 0, "", 0, "", 0],
|
| 375 |
-
inputs=None,
|
| 376 |
-
outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page],
|
| 377 |
-
queue=False
|
| 378 |
-
)
|
| 379 |
demo.queue().launch(debug=True)
|
| 380 |
|
| 381 |
|
| 382 |
if __name__ == "__main__":
|
| 383 |
-
demo()
|
|
|
|
| 278 |
return list_file_path
|
| 279 |
|
| 280 |
|
|
|
|
|
|
|
| 281 |
def demo():
|
| 282 |
with gr.Blocks(theme="base") as demo:
|
| 283 |
vector_db = gr.State()
|
| 284 |
qa_chain = gr.State()
|
| 285 |
collection_name = gr.State()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
+
gr.Markdown(
|
| 288 |
+
"""<center><h2>Book My Darshan Chatbot</center></h2>
|
| 289 |
+
<h3>Plan and book your spiritual journeys effortlessly</h3>""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
+
gr.Markdown(
|
| 292 |
+
"""<b>Note:</b> This chatbot helps you book darshan services through BookMyDarshan.in. Simply provide the temple name, date, and time, and the chatbot will assist you with the booking process.""")
|
| 293 |
+
|
| 294 |
+
with gr.Tab("Step 1 - Upload PDF"):
|
| 295 |
+
with gr.Row():
|
| 296 |
+
document = gr.Files(height=100, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload your PDF documents (single or multiple)")
|
| 297 |
+
# upload_btn = gr.UploadButton("Loading document...", height=100, file_count="multiple", file_types=["pdf"], scale=1)
|
| 298 |
+
|
| 299 |
+
with gr.Tab("Step 2 - Process document"):
|
| 300 |
+
with gr.Row():
|
| 301 |
+
db_btn = gr.Radio(["ChromaDB"], label="Vector database type", value = "ChromaDB", type="index", info="Choose your vector database")
|
| 302 |
+
with gr.Accordion("Advanced options - Document text splitter", open=False):
|
| 303 |
+
with gr.Row():
|
| 304 |
+
slider_chunk_size = gr.Slider(minimum = 100, maximum = 1000, value=600, step=20, label="Chunk size", info="Chunk size", interactive=True)
|
| 305 |
+
with gr.Row():
|
| 306 |
+
slider_chunk_overlap = gr.Slider(minimum = 10, maximum = 200, value=40, step=10, label="Chunk overlap", info="Chunk overlap", interactive=True)
|
| 307 |
+
with gr.Row():
|
| 308 |
+
db_progress = gr.Textbox(label="Vector database initialization", value="None")
|
| 309 |
+
with gr.Row():
|
| 310 |
+
db_btn = gr.Button("Generate vector database")
|
| 311 |
+
|
| 312 |
with gr.Tab("Step 3 - Initialize QA chain"):
|
| 313 |
with gr.Row():
|
| 314 |
llm_btn = gr.Radio(list_llm_simple, \
|
|
|
|
| 325 |
with gr.Row():
|
| 326 |
qachain_btn = gr.Button("Initialize Question Answering chain")
|
| 327 |
|
| 328 |
+
with gr.Tab("Step 4 - Chatbot"):
|
| 329 |
+
chatbot = gr.Chatbot(height=300)
|
| 330 |
+
with gr.Accordion("Advanced - Document references", open=False):
|
|
|
|
|
|
|
|
|
|
| 331 |
with gr.Row():
|
| 332 |
+
doc_source1 = gr.Textbox(label="Reference 1", lines=2, container=True, scale=20)
|
| 333 |
+
source1_page = gr.Number(label="Page", scale=1)
|
| 334 |
with gr.Row():
|
| 335 |
+
doc_source2 = gr.Textbox(label="Reference 2", lines=2, container=True, scale=20)
|
| 336 |
+
source2_page = gr.Number(label="Page", scale=1)
|
| 337 |
with gr.Row():
|
| 338 |
+
doc_source3 = gr.Textbox(label="Reference 3", lines=2, container=True, scale=20)
|
| 339 |
+
source3_page = gr.Number(label="Page", scale=1)
|
|
|
|
| 340 |
with gr.Row():
|
| 341 |
+
msg = gr.Textbox(placeholder="Type message (e.g. 'What is this document about?')", container=True)
|
| 342 |
+
with gr.Row():
|
| 343 |
+
submit_btn = gr.Button("Submit message")
|
| 344 |
+
clear_btn = gr.ClearButton([msg, chatbot], value="Clear conversation")
|
| 345 |
+
|
| 346 |
# Preprocessing events
|
| 347 |
+
#upload_btn.upload(upload_file, inputs=[upload_btn], outputs=[document])
|
| 348 |
+
db_btn.click(initialize_database, \
|
| 349 |
+
inputs=[document, slider_chunk_size, slider_chunk_overlap], \
|
| 350 |
+
outputs=[vector_db, collection_name, db_progress])
|
| 351 |
+
qachain_btn.click(initialize_LLM, \
|
| 352 |
+
inputs=[llm_btn, slider_temperature, slider_maxtokens, slider_topk, vector_db], \
|
| 353 |
+
outputs=[qa_chain, llm_progress]).then(lambda:[None,"",0,"",0,"",0], \
|
| 354 |
+
inputs=None, \
|
| 355 |
+
outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], \
|
| 356 |
+
queue=False)
|
|
|
|
| 357 |
|
| 358 |
# Chatbot events
|
| 359 |
+
msg.submit(conversation, \
|
| 360 |
+
inputs=[qa_chain, msg, chatbot], \
|
| 361 |
+
outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], \
|
| 362 |
+
queue=False)
|
| 363 |
+
submit_btn.click(conversation, \
|
| 364 |
+
inputs=[qa_chain, msg, chatbot], \
|
| 365 |
+
outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], \
|
| 366 |
+
queue=False)
|
| 367 |
+
clear_btn.click(lambda:[None,"",0,"",0,"",0], \
|
| 368 |
+
inputs=None, \
|
| 369 |
+
outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], \
|
| 370 |
+
queue=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
demo.queue().launch(debug=True)
|
| 372 |
|
| 373 |
|
| 374 |
if __name__ == "__main__":
|
| 375 |
+
demo()
|