Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -351,24 +351,19 @@ def create_app():
|
|
| 351 |
"""
|
| 352 |
)
|
| 353 |
|
| 354 |
-
# Selected answer language
|
| 355 |
lang = gr.Dropdown(
|
| 356 |
["auto"] + list(LANG_CODES.keys()),
|
| 357 |
value="auto",
|
| 358 |
label="Answer Language",
|
| 359 |
-
scale=1,
|
| 360 |
)
|
| 361 |
|
| 362 |
-
# Chat history (messages format)
|
| 363 |
chat = gr.Chatbot(label="Chat History", height=350)
|
| 364 |
user_question = gr.Textbox(label="Ask a question", placeholder="Type your question here...", lines=2)
|
| 365 |
ask_btn = gr.Button("Ask", variant="primary")
|
| 366 |
|
| 367 |
-
# Status box
|
| 368 |
status = gr.HTML()
|
| 369 |
|
| 370 |
-
|
| 371 |
-
with gr.Box():
|
| 372 |
gr.Markdown("### Load PDF or URL")
|
| 373 |
|
| 374 |
with gr.Row():
|
|
@@ -386,7 +381,6 @@ def create_app():
|
|
| 386 |
load_pdf_btn = gr.Button("Load PDF", variant="secondary")
|
| 387 |
load_url_btn = gr.Button("Load URL", variant="secondary")
|
| 388 |
|
| 389 |
-
# Example Questions
|
| 390 |
gr.Markdown("### Example Questions")
|
| 391 |
with gr.Row():
|
| 392 |
ex1 = gr.Button("Summarize the document")
|
|
@@ -395,14 +389,12 @@ def create_app():
|
|
| 395 |
ex4 = gr.Button("List the conclusions")
|
| 396 |
ex5 = gr.Button("Explain in simple terms")
|
| 397 |
|
| 398 |
-
# State for tracking which PDF is loaded
|
| 399 |
pdf_id_state = gr.State("")
|
| 400 |
|
| 401 |
-
#
|
| 402 |
def reset_chat_on_new_pdf(message, old_id):
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
return [], new_id
|
| 406 |
return gr.skip(), old_id
|
| 407 |
|
| 408 |
load_pdf_btn.click(load_pdf_ui, [pdf, lang], status).then(
|
|
@@ -413,32 +405,21 @@ def create_app():
|
|
| 413 |
reset_chat_on_new_pdf, [status, pdf_id_state], [chat, pdf_id_state]
|
| 414 |
)
|
| 415 |
|
| 416 |
-
#
|
| 417 |
def chat_wrapper(question, history, lang):
|
| 418 |
-
if question.strip()
|
| 419 |
return "", history
|
| 420 |
-
|
| 421 |
ans = answer_question(question, lang)
|
| 422 |
|
| 423 |
-
# Correct Chatbot message format
|
| 424 |
history.append({"role": "user", "content": question})
|
| 425 |
history.append({"role": "assistant", "content": ans})
|
| 426 |
|
| 427 |
return "", history
|
| 428 |
|
| 429 |
-
ask_btn.click(
|
| 430 |
-
|
| 431 |
-
[user_question, chat, lang],
|
| 432 |
-
[user_question, chat]
|
| 433 |
-
)
|
| 434 |
|
| 435 |
-
|
| 436 |
-
chat_wrapper,
|
| 437 |
-
[user_question, chat, lang],
|
| 438 |
-
[user_question, chat]
|
| 439 |
-
)
|
| 440 |
-
|
| 441 |
-
# ---- EXAMPLES ----
|
| 442 |
ex1.click(lambda: "Summarize the document", None, user_question)
|
| 443 |
ex2.click(lambda: "What are the key findings?", None, user_question)
|
| 444 |
ex3.click(lambda: "Explain the methodology used", None, user_question)
|
|
@@ -449,6 +430,7 @@ def create_app():
|
|
| 449 |
|
| 450 |
|
| 451 |
|
|
|
|
| 452 |
demo = create_app()
|
| 453 |
|
| 454 |
if __name__ == "__main__":
|
|
|
|
| 351 |
"""
|
| 352 |
)
|
| 353 |
|
|
|
|
| 354 |
lang = gr.Dropdown(
|
| 355 |
["auto"] + list(LANG_CODES.keys()),
|
| 356 |
value="auto",
|
| 357 |
label="Answer Language",
|
|
|
|
| 358 |
)
|
| 359 |
|
|
|
|
| 360 |
chat = gr.Chatbot(label="Chat History", height=350)
|
| 361 |
user_question = gr.Textbox(label="Ask a question", placeholder="Type your question here...", lines=2)
|
| 362 |
ask_btn = gr.Button("Ask", variant="primary")
|
| 363 |
|
|
|
|
| 364 |
status = gr.HTML()
|
| 365 |
|
| 366 |
+
with gr.Group():
|
|
|
|
| 367 |
gr.Markdown("### Load PDF or URL")
|
| 368 |
|
| 369 |
with gr.Row():
|
|
|
|
| 381 |
load_pdf_btn = gr.Button("Load PDF", variant="secondary")
|
| 382 |
load_url_btn = gr.Button("Load URL", variant="secondary")
|
| 383 |
|
|
|
|
| 384 |
gr.Markdown("### Example Questions")
|
| 385 |
with gr.Row():
|
| 386 |
ex1 = gr.Button("Summarize the document")
|
|
|
|
| 389 |
ex4 = gr.Button("List the conclusions")
|
| 390 |
ex5 = gr.Button("Explain in simple terms")
|
| 391 |
|
|
|
|
| 392 |
pdf_id_state = gr.State("")
|
| 393 |
|
| 394 |
+
# --- Reset chat when new PDF is loaded ---
|
| 395 |
def reset_chat_on_new_pdf(message, old_id):
|
| 396 |
+
if message != old_id:
|
| 397 |
+
return [], message
|
|
|
|
| 398 |
return gr.skip(), old_id
|
| 399 |
|
| 400 |
load_pdf_btn.click(load_pdf_ui, [pdf, lang], status).then(
|
|
|
|
| 405 |
reset_chat_on_new_pdf, [status, pdf_id_state], [chat, pdf_id_state]
|
| 406 |
)
|
| 407 |
|
| 408 |
+
# --- ASK ---
|
| 409 |
def chat_wrapper(question, history, lang):
|
| 410 |
+
if not question.strip():
|
| 411 |
return "", history
|
|
|
|
| 412 |
ans = answer_question(question, lang)
|
| 413 |
|
|
|
|
| 414 |
history.append({"role": "user", "content": question})
|
| 415 |
history.append({"role": "assistant", "content": ans})
|
| 416 |
|
| 417 |
return "", history
|
| 418 |
|
| 419 |
+
ask_btn.click(chat_wrapper, [user_question, chat, lang], [user_question, chat])
|
| 420 |
+
user_question.submit(chat_wrapper, [user_question, chat, lang], [user_question, chat])
|
|
|
|
|
|
|
|
|
|
| 421 |
|
| 422 |
+
# Examples
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
ex1.click(lambda: "Summarize the document", None, user_question)
|
| 424 |
ex2.click(lambda: "What are the key findings?", None, user_question)
|
| 425 |
ex3.click(lambda: "Explain the methodology used", None, user_question)
|
|
|
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
+
|
| 434 |
demo = create_app()
|
| 435 |
|
| 436 |
if __name__ == "__main__":
|