gladguy commited on
Commit
23cc7e7
Β·
1 Parent(s): 6390e94

Enhance Book Learning with text extraction for accurate analysis

Browse files
Files changed (1) hide show
  1. app.py +63 -49
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 (list_of_images, status_message)
379
  """
380
  if pdf_file is None:
381
  return [], "Please upload a PDF file."
382
 
383
  try:
384
- extracted_images = []
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
- pages = convert_from_path(tmp_path, first_page=1, last_page=20, dpi=150)
394
 
395
- for i, page in enumerate(pages):
396
- extracted_images.append((page, f"Page {i+1}"))
397
 
398
- status = f"βœ… Successfully processed {len(extracted_images)} pages from your anatomy textbook!"
399
- return extracted_images, status
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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} which contains an anatomical diagram or illustration.
 
430
 
431
- Provide a comprehensive explanation that would typically accompany anatomical images in medical textbooks:
432
 
433
- ## πŸ“– Anatomical Structure Overview
 
434
 
435
- [Explain what anatomical structure is likely shown]
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
- 1. [Question about identification]
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.7
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 Page Analysis: {page_info}
475
 
476
  {explanation}
477
 
478
  ---
479
 
480
- πŸ’ͺ **Next Steps:** After studying this page, you can test your knowledge in VIVA mode!"""
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 captions
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
- images, status_msg = process_uploaded_book(pdf_bytes)
643
- if not images:
644
- # No images extracted
645
- return [], status_msg, [], gr.update(choices=[], interactive=False), None, ""
646
- # Separate images and captions
647
- img_list = [img for img, cap in images]
648
- caps = [cap for img, cap in images]
 
 
 
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
- analysis = analyze_book_image(img, selected_caption)
 
 
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: