Spaces:
Sleeping
Sleeping
Enhance Book Learning with text extraction for accurate analysis
Browse files
app.py
CHANGED
|
@@ -374,14 +374,14 @@ def process_anatomy_query(query: str) -> tuple:
|
|
| 374 |
# Book Learning Mode Functions
|
| 375 |
def process_uploaded_book(pdf_file):
|
| 376 |
"""
|
| 377 |
-
Process uploaded PDF book and extract first 20 pages with images.
|
| 378 |
-
Returns (
|
| 379 |
"""
|
| 380 |
if pdf_file is None:
|
| 381 |
return [], "Please upload a PDF file."
|
| 382 |
|
| 383 |
try:
|
| 384 |
-
|
| 385 |
|
| 386 |
# Save uploaded file temporarily
|
| 387 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
|
|
@@ -390,13 +390,28 @@ def process_uploaded_book(pdf_file):
|
|
| 390 |
|
| 391 |
try:
|
| 392 |
# Convert first 20 pages to images
|
| 393 |
-
|
| 394 |
|
| 395 |
-
|
| 396 |
-
|
| 397 |
|
| 398 |
-
|
| 399 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
|
| 401 |
finally:
|
| 402 |
# Clean up temp file
|
|
@@ -407,7 +422,7 @@ def process_uploaded_book(pdf_file):
|
|
| 407 |
return [], f"β οΈ Error processing PDF: {str(e)}"
|
| 408 |
|
| 409 |
|
| 410 |
-
def analyze_book_image(image, page_info):
|
| 411 |
"""
|
| 412 |
Analyze selected image from book using AI to extract anatomical information.
|
| 413 |
Returns formatted explanation text.
|
|
@@ -416,53 +431,45 @@ def analyze_book_image(image, page_info):
|
|
| 416 |
return "Please select an image from the book."
|
| 417 |
|
| 418 |
try:
|
| 419 |
-
# For now, we'll use the Hyperbolic API to analyze what might be in the image
|
| 420 |
-
# In a production version, you'd use vision models or OCR
|
| 421 |
-
|
| 422 |
headers = {
|
| 423 |
"Content-Type": "application/json",
|
| 424 |
"Authorization": f"Bearer {HYPERBOLIC_API_KEY}"
|
| 425 |
}
|
| 426 |
|
|
|
|
|
|
|
|
|
|
| 427 |
prompt = f"""You are an anatomy professor helping MBBS students learn from their textbook.
|
| 428 |
|
| 429 |
-
A student is looking at {page_info}
|
|
|
|
| 430 |
|
| 431 |
-
|
| 432 |
|
| 433 |
-
## π
|
|
|
|
| 434 |
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
## π Key Features to Observe
|
| 438 |
-
|
| 439 |
-
- [Feature 1 - what students should look for in the diagram]
|
| 440 |
-
- [Feature 2]
|
| 441 |
-
- [Feature 3]
|
| 442 |
-
- [Feature 4]
|
| 443 |
|
| 444 |
## π₯ Clinical Relevance
|
| 445 |
-
|
| 446 |
-
- [Clinical point 1]
|
| 447 |
-
- [Clinical point 2]
|
| 448 |
|
| 449 |
## π‘ Study Tips
|
| 450 |
-
|
| 451 |
-
[How to effectively study this diagram/structure]
|
| 452 |
|
| 453 |
## β Self-Test Questions
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
-
|
| 456 |
-
2. [Question about function/relationship]
|
| 457 |
-
3. [Question about clinical application]
|
| 458 |
-
|
| 459 |
-
Be thorough and educational, as if explaining a textbook figure."""
|
| 460 |
|
| 461 |
payload = {
|
| 462 |
"model": HYPERBOLIC_MODEL,
|
| 463 |
"messages": [{"role": "user", "content": prompt}],
|
| 464 |
"max_tokens": 800,
|
| 465 |
-
"temperature": 0.
|
| 466 |
}
|
| 467 |
|
| 468 |
response = requests.post(HYPERBOLIC_API_URL, headers=headers, json=payload, timeout=25)
|
|
@@ -471,13 +478,13 @@ Be thorough and educational, as if explaining a textbook figure."""
|
|
| 471 |
result = response.json()
|
| 472 |
explanation = result["choices"][0]["message"]["content"]
|
| 473 |
|
| 474 |
-
formatted_output = f"""# π Textbook
|
| 475 |
|
| 476 |
{explanation}
|
| 477 |
|
| 478 |
---
|
| 479 |
|
| 480 |
-
πͺ **Next Steps:**
|
| 481 |
|
| 482 |
return formatted_output
|
| 483 |
|
|
@@ -624,9 +631,10 @@ with gr.Blocks(title="AnatomyBot - MBBS Anatomy Tutor") as demo:
|
|
| 624 |
pdf_upload = gr.File(label="Upload Anatomy Textbook (PDF)", file_types=[".pdf"], type="binary")
|
| 625 |
upload_status = gr.Markdown()
|
| 626 |
|
| 627 |
-
# State to hold extracted images and
|
| 628 |
book_images_state = gr.State([])
|
| 629 |
page_captions_state = gr.State([])
|
|
|
|
| 630 |
|
| 631 |
# Dropdown to select a page after processing
|
| 632 |
page_dropdown = gr.Dropdown(label="Select Page", choices=[], interactive=False)
|
|
@@ -639,25 +647,28 @@ with gr.Blocks(title="AnatomyBot - MBBS Anatomy Tutor") as demo:
|
|
| 639 |
|
| 640 |
# Process upload
|
| 641 |
def handle_book_upload(pdf_bytes):
|
| 642 |
-
|
| 643 |
-
if not
|
| 644 |
-
# No
|
| 645 |
-
return [], status_msg, [], gr.update(choices=[], interactive=False), None, ""
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
|
|
|
|
|
|
|
|
|
| 649 |
# Update dropdown with captions and enable it
|
| 650 |
dropdown_update = gr.update(choices=caps, interactive=True)
|
| 651 |
-
return img_list, status_msg, caps, dropdown_update, None, ""
|
| 652 |
|
| 653 |
pdf_upload.upload(
|
| 654 |
fn=handle_book_upload,
|
| 655 |
inputs=[pdf_upload],
|
| 656 |
-
outputs=[book_images_state, upload_status, page_captions_state, page_dropdown, selected_page_image, analysis_output]
|
| 657 |
)
|
| 658 |
|
| 659 |
# When a page is selected, show image and analysis
|
| 660 |
-
def show_page_analysis(selected_caption, images, captions):
|
| 661 |
if not selected_caption:
|
| 662 |
return None, ""
|
| 663 |
# Find index
|
|
@@ -665,11 +676,14 @@ with gr.Blocks(title="AnatomyBot - MBBS Anatomy Tutor") as demo:
|
|
| 665 |
idx = captions.index(selected_caption)
|
| 666 |
except ValueError:
|
| 667 |
return None, ""
|
|
|
|
| 668 |
img = images[idx]
|
| 669 |
-
|
|
|
|
|
|
|
| 670 |
return img, analysis
|
| 671 |
|
| 672 |
-
page_dropdown.change(fn=show_page_analysis, inputs=[page_dropdown, book_images_state, page_captions_state], outputs=[selected_page_image, analysis_output])
|
| 673 |
|
| 674 |
# VIVA MODE TAB
|
| 675 |
with gr.Tab("π― VIVA Training Mode") as viva_tab:
|
|
|
|
| 374 |
# Book Learning Mode Functions
|
| 375 |
def process_uploaded_book(pdf_file):
|
| 376 |
"""
|
| 377 |
+
Process uploaded PDF book and extract first 20 pages with images and text.
|
| 378 |
+
Returns (list_of_tuples, status_message) where tuple is (image, caption, text)
|
| 379 |
"""
|
| 380 |
if pdf_file is None:
|
| 381 |
return [], "Please upload a PDF file."
|
| 382 |
|
| 383 |
try:
|
| 384 |
+
extracted_data = []
|
| 385 |
|
| 386 |
# Save uploaded file temporarily
|
| 387 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
|
|
|
|
| 390 |
|
| 391 |
try:
|
| 392 |
# Convert first 20 pages to images
|
| 393 |
+
images = convert_from_path(tmp_path, first_page=1, last_page=20, dpi=150)
|
| 394 |
|
| 395 |
+
# Extract text from pages
|
| 396 |
+
reader = PyPDF2.PdfReader(tmp_path)
|
| 397 |
|
| 398 |
+
for i, image in enumerate(images):
|
| 399 |
+
# Get text for this page if available
|
| 400 |
+
text_content = ""
|
| 401 |
+
if i < len(reader.pages):
|
| 402 |
+
try:
|
| 403 |
+
text_content = reader.pages[i].extract_text()
|
| 404 |
+
except:
|
| 405 |
+
text_content = "Could not extract text from this page."
|
| 406 |
+
|
| 407 |
+
# Limit text length to avoid token limits
|
| 408 |
+
if len(text_content) > 2000:
|
| 409 |
+
text_content = text_content[:2000] + "..."
|
| 410 |
+
|
| 411 |
+
extracted_data.append((image, f"Page {i+1}", text_content))
|
| 412 |
+
|
| 413 |
+
status = f"β
Successfully processed {len(extracted_data)} pages from your anatomy textbook!"
|
| 414 |
+
return extracted_data, status
|
| 415 |
|
| 416 |
finally:
|
| 417 |
# Clean up temp file
|
|
|
|
| 422 |
return [], f"β οΈ Error processing PDF: {str(e)}"
|
| 423 |
|
| 424 |
|
| 425 |
+
def analyze_book_image(image, page_info, page_text=""):
|
| 426 |
"""
|
| 427 |
Analyze selected image from book using AI to extract anatomical information.
|
| 428 |
Returns formatted explanation text.
|
|
|
|
| 431 |
return "Please select an image from the book."
|
| 432 |
|
| 433 |
try:
|
|
|
|
|
|
|
|
|
|
| 434 |
headers = {
|
| 435 |
"Content-Type": "application/json",
|
| 436 |
"Authorization": f"Bearer {HYPERBOLIC_API_KEY}"
|
| 437 |
}
|
| 438 |
|
| 439 |
+
# Include extracted text in the prompt context
|
| 440 |
+
context_text = f"Page Content:\n{page_text}" if page_text else "No text extracted from this page."
|
| 441 |
+
|
| 442 |
prompt = f"""You are an anatomy professor helping MBBS students learn from their textbook.
|
| 443 |
|
| 444 |
+
A student is looking at {page_info} of their anatomy textbook.
|
| 445 |
+
{context_text}
|
| 446 |
|
| 447 |
+
Based on the text content above (and typical anatomical diagrams found in such contexts), provide a comprehensive explanation:
|
| 448 |
|
| 449 |
+
## π Page Overview
|
| 450 |
+
[Summarize the key anatomical topic discussed on this page based on the text]
|
| 451 |
|
| 452 |
+
## π Key Concepts Explained
|
| 453 |
+
[Explain the main concepts covered in the text in simple terms]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
## π₯ Clinical Relevance
|
| 456 |
+
[Extract or infer clinical points mentioned or relevant to this topic]
|
|
|
|
|
|
|
| 457 |
|
| 458 |
## π‘ Study Tips
|
| 459 |
+
[How to remember this specific information]
|
|
|
|
| 460 |
|
| 461 |
## β Self-Test Questions
|
| 462 |
+
1. [Question based on the page text]
|
| 463 |
+
2. [Question based on the page text]
|
| 464 |
+
3. [Question based on the page text]
|
| 465 |
|
| 466 |
+
Be educational and specific to the provided text content."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
|
| 468 |
payload = {
|
| 469 |
"model": HYPERBOLIC_MODEL,
|
| 470 |
"messages": [{"role": "user", "content": prompt}],
|
| 471 |
"max_tokens": 800,
|
| 472 |
+
"temperature": 0.5
|
| 473 |
}
|
| 474 |
|
| 475 |
response = requests.post(HYPERBOLIC_API_URL, headers=headers, json=payload, timeout=25)
|
|
|
|
| 478 |
result = response.json()
|
| 479 |
explanation = result["choices"][0]["message"]["content"]
|
| 480 |
|
| 481 |
+
formatted_output = f"""# π Textbook Analysis: {page_info}
|
| 482 |
|
| 483 |
{explanation}
|
| 484 |
|
| 485 |
---
|
| 486 |
|
| 487 |
+
πͺ **Next Steps:** Mastered this page? Try the VIVA mode to test yourself!"""
|
| 488 |
|
| 489 |
return formatted_output
|
| 490 |
|
|
|
|
| 631 |
pdf_upload = gr.File(label="Upload Anatomy Textbook (PDF)", file_types=[".pdf"], type="binary")
|
| 632 |
upload_status = gr.Markdown()
|
| 633 |
|
| 634 |
+
# State to hold extracted images, captions, and text
|
| 635 |
book_images_state = gr.State([])
|
| 636 |
page_captions_state = gr.State([])
|
| 637 |
+
page_texts_state = gr.State([])
|
| 638 |
|
| 639 |
# Dropdown to select a page after processing
|
| 640 |
page_dropdown = gr.Dropdown(label="Select Page", choices=[], interactive=False)
|
|
|
|
| 647 |
|
| 648 |
# Process upload
|
| 649 |
def handle_book_upload(pdf_bytes):
|
| 650 |
+
extracted_data, status_msg = process_uploaded_book(pdf_bytes)
|
| 651 |
+
if not extracted_data:
|
| 652 |
+
# No data extracted
|
| 653 |
+
return [], status_msg, [], [], gr.update(choices=[], interactive=False), None, ""
|
| 654 |
+
|
| 655 |
+
# Separate images, captions, and text
|
| 656 |
+
img_list = [item[0] for item in extracted_data]
|
| 657 |
+
caps = [item[1] for item in extracted_data]
|
| 658 |
+
texts = [item[2] for item in extracted_data]
|
| 659 |
+
|
| 660 |
# Update dropdown with captions and enable it
|
| 661 |
dropdown_update = gr.update(choices=caps, interactive=True)
|
| 662 |
+
return img_list, status_msg, caps, texts, dropdown_update, None, ""
|
| 663 |
|
| 664 |
pdf_upload.upload(
|
| 665 |
fn=handle_book_upload,
|
| 666 |
inputs=[pdf_upload],
|
| 667 |
+
outputs=[book_images_state, upload_status, page_captions_state, page_texts_state, page_dropdown, selected_page_image, analysis_output]
|
| 668 |
)
|
| 669 |
|
| 670 |
# When a page is selected, show image and analysis
|
| 671 |
+
def show_page_analysis(selected_caption, images, captions, texts):
|
| 672 |
if not selected_caption:
|
| 673 |
return None, ""
|
| 674 |
# Find index
|
|
|
|
| 676 |
idx = captions.index(selected_caption)
|
| 677 |
except ValueError:
|
| 678 |
return None, ""
|
| 679 |
+
|
| 680 |
img = images[idx]
|
| 681 |
+
text = texts[idx] if idx < len(texts) else ""
|
| 682 |
+
|
| 683 |
+
analysis = analyze_book_image(img, selected_caption, text)
|
| 684 |
return img, analysis
|
| 685 |
|
| 686 |
+
page_dropdown.change(fn=show_page_analysis, inputs=[page_dropdown, book_images_state, page_captions_state, page_texts_state], outputs=[selected_page_image, analysis_output])
|
| 687 |
|
| 688 |
# VIVA MODE TAB
|
| 689 |
with gr.Tab("π― VIVA Training Mode") as viva_tab:
|