Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -276,7 +276,17 @@ Answer in English:
|
|
| 276 |
""".strip()
|
| 277 |
return prompt
|
| 278 |
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
def answer_question(question: str, language: str):
|
| 281 |
if question.strip() == "":
|
| 282 |
return "Please enter a question.", ""
|
|
@@ -340,93 +350,77 @@ def load_url_ui(url: str, language: str):
|
|
| 340 |
|
| 341 |
def create_app():
|
| 342 |
with gr.Blocks() as demo:
|
| 343 |
-
|
| 344 |
gr.Markdown(
|
| 345 |
-
""
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
Load a PDF or Article and ask questions. The app retrieves relevant chunks
|
| 349 |
-
and answers using a local LLM with multilingual support.
|
| 350 |
-
</p>
|
| 351 |
-
"""
|
| 352 |
)
|
| 353 |
|
|
|
|
| 354 |
lang = gr.Dropdown(
|
| 355 |
["auto"] + list(LANG_CODES.keys()),
|
| 356 |
value="auto",
|
| 357 |
label="Answer Language",
|
| 358 |
)
|
| 359 |
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
status = gr.HTML()
|
| 365 |
-
|
| 366 |
-
with gr.Group():
|
| 367 |
-
gr.Markdown("### Load PDF or URL")
|
| 368 |
-
|
| 369 |
-
with gr.Row():
|
| 370 |
-
pdf = gr.File(
|
| 371 |
-
label="Upload PDF",
|
| 372 |
-
file_types=[".pdf"],
|
| 373 |
-
height=70,
|
| 374 |
-
interactive=True
|
| 375 |
-
)
|
| 376 |
-
url = gr.Textbox(
|
| 377 |
-
label="Or paste URL",
|
| 378 |
-
placeholder="https://arxiv.org/pdf/... or research article",
|
| 379 |
-
)
|
| 380 |
-
|
| 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 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
)
|
|
|
|
| 403 |
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
return "", history
|
| 412 |
-
ans = answer_question(question, lang)
|
| 413 |
|
| 414 |
-
|
| 415 |
-
|
| 416 |
|
| 417 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
|
| 419 |
-
|
| 420 |
-
|
|
|
|
|
|
|
|
|
|
| 421 |
|
| 422 |
-
|
| 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)
|
| 426 |
-
ex4.click(lambda: "List the main conclusions", None, user_question)
|
| 427 |
-
ex5.click(lambda: "Explain in simple terms", None, user_question)
|
| 428 |
|
| 429 |
-
return demo
|
| 430 |
|
| 431 |
|
| 432 |
|
|
|
|
| 276 |
""".strip()
|
| 277 |
return prompt
|
| 278 |
|
| 279 |
+
def highlight_sources(retrieved):
|
| 280 |
+
html = "<h4>📚 Source Passages</h4>"
|
| 281 |
+
for i, (chunk, score) in enumerate(retrieved):
|
| 282 |
+
html += f"""
|
| 283 |
+
<div style='padding:10px; background:#eef6ff; margin:8px 0;'>
|
| 284 |
+
<b>[{i+1}] Score = {1-score:.3f}</b><br>
|
| 285 |
+
{chunk[:400]}...
|
| 286 |
+
</div>
|
| 287 |
+
"""
|
| 288 |
+
return html
|
| 289 |
+
|
| 290 |
def answer_question(question: str, language: str):
|
| 291 |
if question.strip() == "":
|
| 292 |
return "Please enter a question.", ""
|
|
|
|
| 350 |
|
| 351 |
def create_app():
|
| 352 |
with gr.Blocks() as demo:
|
| 353 |
+
gr.Markdown("<h1 style='text-align:center;'> Multilingual Chat with PDF / URL</h1>")
|
| 354 |
gr.Markdown(
|
| 355 |
+
"Upload a PDF or paste a URL (PDF or article). "
|
| 356 |
+
"The app creates embeddings, retrieves the most relevant chunks, "
|
| 357 |
+
"and answers using a small local LLM."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
)
|
| 359 |
|
| 360 |
+
# Language selector
|
| 361 |
lang = gr.Dropdown(
|
| 362 |
["auto"] + list(LANG_CODES.keys()),
|
| 363 |
value="auto",
|
| 364 |
label="Answer Language",
|
| 365 |
)
|
| 366 |
|
| 367 |
+
# ---------------------------
|
| 368 |
+
# DOCUMENT LOADING AREA
|
| 369 |
+
# ---------------------------
|
| 370 |
+
gr.Markdown("### Load PDF or Article")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
|
|
|
|
| 372 |
with gr.Row():
|
| 373 |
+
pdf = gr.File(
|
| 374 |
+
label="Upload PDF",
|
| 375 |
+
file_types=[".pdf"],
|
| 376 |
+
height=70 # smaller box
|
| 377 |
+
)
|
| 378 |
+
pdf_status = gr.HTML()
|
| 379 |
+
|
| 380 |
+
# Auto load PDF on upload
|
| 381 |
+
pdf.upload(load_pdf_ui, [pdf, lang], pdf_status)
|
| 382 |
+
|
| 383 |
+
gr.Markdown("---")
|
| 384 |
+
|
| 385 |
+
# URL input
|
| 386 |
+
url = gr.Textbox(
|
| 387 |
+
label="Enter URL",
|
| 388 |
+
placeholder="https://example.com/article.pdf",
|
| 389 |
)
|
| 390 |
+
url_status = gr.HTML()
|
| 391 |
|
| 392 |
+
# Auto load URL on pressing Enter
|
| 393 |
+
url.submit(load_url_ui, [url, lang], url_status)
|
| 394 |
+
|
| 395 |
+
# ---------------------------
|
| 396 |
+
# CHAT AREA
|
| 397 |
+
# ---------------------------
|
| 398 |
+
gr.Markdown("### Ask Questions About the Loaded Document")
|
| 399 |
|
| 400 |
+
q = gr.Textbox(label="Your Question")
|
| 401 |
+
a = gr.HTML(label="Answer")
|
| 402 |
+
cits = gr.HTML(label="Source Passages")
|
|
|
|
|
|
|
| 403 |
|
| 404 |
+
ask_btn = gr.Button("Ask", variant="primary")
|
| 405 |
+
ask_btn.click(answer_question, [q, lang], [a, cits])
|
| 406 |
|
| 407 |
+
# Pre-set example question buttons
|
| 408 |
+
gr.Markdown("### Example Questions")
|
| 409 |
+
with gr.Row():
|
| 410 |
+
b1 = gr.Button("Summarize the document")
|
| 411 |
+
b2 = gr.Button("What are the key findings?")
|
| 412 |
+
b3 = gr.Button("Explain the methodology used")
|
| 413 |
+
b4 = gr.Button("What are the main limitations?")
|
| 414 |
+
b5 = gr.Button("What is the conclusion of this paper?")
|
| 415 |
|
| 416 |
+
b1.click(lambda: "Summarize the document.", None, q)
|
| 417 |
+
b2.click(lambda: "What are the key findings?", None, q)
|
| 418 |
+
b3.click(lambda: "Explain the methodology used in this study.", None, q)
|
| 419 |
+
b4.click(lambda: "What are the main limitations of this study?", None, q)
|
| 420 |
+
b5.click(lambda: "What is the conclusion of this paper?", None, q)
|
| 421 |
|
| 422 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
|
|
|
|
| 424 |
|
| 425 |
|
| 426 |
|