Compare both odfs and make a pdf of questions present in pyqs but not in haqs
python3 << 'EOF' import pdfplumber pyq_path = "/tmp/workspace/attachments/69c22473-5ced-4aeb-9b7d-efb85af158bc/Microbiology Previous 10 Years (1).pdf" haq_path = "/tmp/workspace/attachments/69c22473-5ced-4aeb-9b7d-efb85af158bc/DOC-20251117-WA0012..pdf" print("=== PYQ PDF ===") with pdfplumber.open(pyq_path) as pdf: print(f"Pages: {len(pdf.pages)}") for i, page in enumerate(pdf.pages[:3]): text = page.extract_text() print(f"\n--- Page {i+1} ---") print(text[:1500] if text else "[No text]") print("\n\n=== HAQ PDF ===") with pdfplumber.open(haq_path) as pdf: print(f"Pages: {len(pdf.pages)}") for i, page in enumerate(pdf.pages[:3]): text = page.extract_text() print(f"\n--- Page {i+1} ---") print(text[:1500] if text else "[No text]") EOF
python3 << 'EOF' # Try pypdf for PYQ - check if it has any content at all from pypdf import PdfReader pyq_path = "/tmp/workspace/attachments/69c22473-5ced-4aeb-9b7d-efb85af158bc/Microbiology Previous 10 Years (1).pdf" reader = PdfReader(pyq_path) print(f"Pages: {len(reader.pages)}") for i, page in enumerate(reader.pages[:5]): text = page.extract_text() print(f"Page {i+1}: {repr(text[:200]) if text else '[empty]'}") # Check if images are present print("\nChecking for images on page 1:") page = reader.pages[0] print(f"Page resources: {list(page.get('/Resources', {}).keys())}") if '/XObject' in page.get('/Resources', {}): xobj = page['/Resources']['/XObject'] for name, ref in xobj.items(): obj = ref.get_object() print(f" {name}: subtype={obj.get('/Subtype')}") EOF
mkdir -p /tmp/workspace/pyq_pages python3 << 'EOF' # Extract images from PYQ PDF pages using pypdf from pypdf import PdfReader import io pyq_path = "/tmp/workspace/attachments/69c22473-5ced-4aeb-9b7d-efb85af158bc/Microbiology Previous 10 Years (1).pdf" reader = PdfReader(pyq_path) print(f"Total pages: {len(reader.pages)}") for i, page in enumerate(reader.pages): resources = page.get('/Resources', {}) xobj = resources.get('/XObject', {}) for j, (name, ref) in enumerate(xobj.items()): obj = ref.get_object() if obj.get('/Subtype') == '/Image': # Get image data data = obj.get_data() width = obj.get('/Width', 0) height = obj.get('/Height', 0) colorspace = obj.get('/ColorSpace', 'Unknown') bpc = obj.get('/BitsPerComponent', 8) filter_type = obj.get('/Filter', 'None') print(f"Page {i+1}, img {j}: {width}x{height}, cs={colorspace}, bpc={bpc}, filter={filter_type}, size={len(data)}") # Save first image as PNG using PIL try: from PIL import Image import numpy as np if str(colorspace) in ['/DeviceRGB', 'RGB']: img = Image.frombytes('RGB', (width, height), data) elif str(colorspace) == '/DeviceGray': img = Image.frombytes('L', (width, height), data) else: img = Image.frombytes('RGB', (width, height), data) out_path = f"/tmp/workspace/pyq_pages/page_{i+1:02d}.png" img.save(out_path) print(f" -> Saved {out_path}") except Exception as e: print(f" -> PIL error: {e}") break # one image per page is enough EOF
python3 << 'EOF' from pypdf import PdfReader import io pyq_path = "/tmp/workspace/attachments/69c22473-5ced-4aeb-9b7d-efb85af158bc/Microbiology Previous 10 Years (1).pdf" reader = PdfReader(pyq_path) # Check page 1 - list ALL xobjects page = reader.pages[0] resources = page.get('/Resources', {}) xobj = resources.get('/XObject', {}) print(f"Page 1 has {len(xobj)} XObjects:") for name, ref in xobj.items(): obj = ref.get_object() subtype = obj.get('/Subtype') width = obj.get('/Width', 'N/A') height = obj.get('/Height', 'N/A') filter_type = obj.get('/Filter', 'None') data = obj.get_data() print(f" {name}: subtype={subtype}, {width}x{height}, filter={filter_type}, raw_size={len(data)}") # Try saving the second image (X2) as JPEG directly page1 = reader.pages[0] xobj1 = page1['/Resources']['/XObject'] for name, ref in xobj1.items(): obj = ref.get_object() if obj.get('/Subtype') == '/Image': raw = obj.get_data() out = f"/tmp/workspace/pyq_pages/page1_{name[1:]}.jpg" with open(out, 'wb') as f: f.write(raw) print(f"Saved raw JPEG: {out}, size={len(raw)}") EOF
python3 << 'EOF' from pypdf import PdfReader import os pyq_path = "/tmp/workspace/attachments/69c22473-5ced-4aeb-9b7d-efb85af158bc/Microbiology Previous 10 Years (1).pdf" reader = PdfReader(pyq_path) os.makedirs("/tmp/workspace/pyq_pages", exist_ok=True) for i, page in enumerate(reader.pages): resources = page.get('/Resources', {}) xobj = resources.get('/XObject', {}) # Find the largest image (main content) best_name = None best_size = 0 for name, ref in xobj.items(): obj = ref.get_object() if obj.get('/Subtype') == '/Image': w = obj.get('/Width', 0) h = obj.get('/Height', 0) size = w * h if size > best_size: best_size = size best_name = (name, ref) if best_name: name, ref = best_name obj = ref.get_object() raw = obj.get_data() out = f"/tmp/workspace/pyq_pages/page_{i+1:02d}.jpg" with open(out, 'wb') as f: f.write(raw) print(f"Page {i+1}: saved {out}, size={len(raw)}") else: print(f"Page {i+1}: no image found") print("\nDone!") EOF
You need to do a detailed OCR extraction of a scanned PDF and text extraction of another PDF, then compare them. ## Task Overview 1. Extract all questions from the PYQ (Previous Year Questions) PDF - it's scanned (image-based) 2. Extract all questions from the HAQ (Haji Ali Questions) PDF - it has text 3. Find questions in PYQs that are NOT in HAQs 4. Generate a final PDF with only those unique PYQ questions ## Files - PYQ PDF: `/tmp/workspace/attachments/69c22473-5ced-4aeb-9b7d-efb85af158bc/Microbiology Previous 10 Years (1).pdf` - 32 pages, image-based (scanned) - Individual page images already extracted to: `/tmp/workspace/pyq_pages/page_01.jpg` through `/tmp/workspace/pyq_pages/page_32.jpg` - HAQ PDF: `/tmp/workspace/attachments/69c22473-5ced-4aeb-9b7d-efb85af158bc/DOC-20251117-WA0012..pdf` - 28 pages, has extractable text - About Microbiology subject MBBS questions ## Step 1: Extract HAQ text Use pdfplumber to extract all text from the HAQ PDF. Save it to `/tmp/workspace/haq_text.txt`. ```python import pdfplumber haq_path = "/tmp/workspace/attachments/69c22473-5ced-4aeb-9b7d-efb85af158bc/DOC-20251117-WA0012..pdf" with pdfplumber.open(haq_path) as pdf: all_text = "" for page in pdf.pages: text = page.extract_text() if text: all_text += text + "\n\n" with open("/tmp/workspace/haq_text.txt", "w") as f: f.write(all_text) ``` ## Step 2: OCR the PYQ pages Use the `analyse_media` tool to read the content of the PYQ page images. You have 5 analyse_media calls available. To maximize coverage, combine multiple page images into a single image per call: Use Python/PIL to combine pages 1-7 into one tall image, pages 8-14 into another, pages 15-21 into another, pages 22-28 into another, and pages 29-32 into the last. For each combined image, use `analyse_media` with focus: "Extract ALL text from this image exactly as written. This is a Microbiology MBBS exam question paper. List all questions, sub-questions, and topic headers. Preserve the exact wording of each question." Then call analyse_media with the file:// URL for each combined image OR save to workspace and use the file path as a URL. Actually, to use analyse_media you need a URL. Save the images to /tmp/workspace/ and serve from there - but since there's no web server, you need to use the image URL. ALTERNATIVE APPROACH: Since you can't serve local files as URLs to analyse_media, use Python's PIL to create composite images, save them, then note that the analyse_media tool accepts file paths as URLs IF the path is accessible. Actually the tool requires HTTP URLs. BEST APPROACH: Instead, read the first few pages to understand structure, then use fetch_image to read the locally saved JPEGs by converting them to base64 or using file:// URIs. Wait - the analyse_media tool takes a URL. The pages are saved at `/tmp/workspace/pyq_pages/page_01.jpg` etc. Try using `file:///tmp/workspace/pyq_pages/page_01.jpg` as the URL. Use analyse_media with URL = `file:///tmp/workspace/pyq_pages/page_01.jpg` to read each page. For efficiency, combine pages using PIL first, then analyse. Combine pages into groups of ~6-7: - Group 1: pages 1-6 → save as `/tmp/workspace/pyq_combined_1.jpg` - Group 2: pages 7-14 → save as `/tmp/workspace/pyq_combined_2.jpg` - Group 3: pages 15-21 → save as `/tmp/workspace/pyq_combined_3.jpg` - Group 4: pages 22-28 → save as `/tmp/workspace/pyq_combined_4.jpg` - Group 5: pages 29-32 → save as `/tmp/workspace/pyq_combined_5.jpg` Then call analyse_media 5 times (one per group) to extract text. ## Step 3: Parse questions From the OCR output, extract the individual questions/topics from PYQs. From the HAQ text, extract all questions/topics. ## Step 4: Compare Find questions in PYQs that are NOT covered in HAQs. Do a fuzzy/semantic comparison - if a PYQ question is substantially similar to a HAQ question (same topic), mark it as covered. Only include truly new/different questions. ## Step 5: Generate output PDF Use reportlab to create a nicely formatted PDF at `/tmp/workspace/pyq_unique_questions.pdf` containing: - Title: "PYQ Questions Not in HAQs - Microbiology" - Organized by topic/section if possible - Each question clearly listed ## Return Return a complete summary including: 1. Total PYQ questions found 2. Total HAQ questions found 3. Number of unique PYQ questions (not in HAQ) 4. The list of unique questions 5. Confirm the output PDF path
~/pyq_pages
# Step 1: Extract HAQ text python3 -c " import pdfplumber haq_path = '/tmp/workspace/attachments/69c22473-5ced-4aeb-9b7d-efb85af158bc/DOC-20251117-WA0012..pdf' with pdfplumber.open(haq_path) as pdf: all_text = '' for i, page in enumerate(pdf.pages): text = page.extract_text() if text: all_text += f'--- PAGE {i+1} ---\n' + text + '\n\n' else: all_text += f'--- PAGE {i+1} --- (no text)\n\n' with open('/tmp/workspace/haq_text.txt', 'w') as f: f.write(all_text) print(f'Total chars extracted: {len(all_text)}') print('First 500 chars:') print(all_text[:500]) "
# Step 2: Combine PYQ pages into 5 groups using PIL python3 -c " from PIL import Image import os pages_dir = '/tmp/workspace/pyq_pages' output_dir = '/tmp/workspace' # Page groups groups = { 1: list(range(1, 8)), # pages 1-7 2: list(range(8, 15)), # pages 8-14 3: list(range(15, 22)), # pages 15-21 4: list(range(22, 29)), # pages 22-28 5: list(range(29, 33)), # pages 29-32 } for group_num, pages in groups.items(): images = [] for p in pages: path = os.path.join(pages_dir, f'page_{p:02d}.jpg') if os.path.exists(path): img = Image.open(path) images.append(img) if not images: print(f'Group {group_num}: no images found') continue # Get max width and total height max_w = max(img.width for img in images) total_h = sum(img.height for img in images) # Create combined image combined = Image.new('RGB', (max_w, total_h), (255, 255, 255)) y_offset = 0 for img in images: combined.paste(img, (0, y_offset)) y_offset += img.height out_path = os.path.join(output_dir, f'pyq_combined_{group_num}.jpg') combined.save(out_path, 'JPEG', quality=85) print(f'Group {group_num} (pages {pages}): {combined.size} -> saved to {out_path}') "
# Check if we can start a simple HTTP server and check network python3 -c " import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) result = s.connect_ex(('localhost', 8080)) print('Port 8080 status:', result) s.close() " 2>&1 || echo "Socket test failed" # Start a simple HTTP server in background cd /tmp/workspace && python3 -m http.server 8765 & sleep 2 echo "Server started" # Test it curl -s -o /dev/null -w "%{http_code}" http://localhost:8765/pyq_combined_1.jpg 2>&1 || echo "curl failed"
# Check what external IPs/hostnames are accessible ip addr show 2>/dev/null | grep inet | head -5 hostname -I 2>/dev/null || echo "hostname -I failed" # Check if there's a public IP we can use curl -s --max-time 3 http://ifconfig.me 2>/dev/null || echo "no external network"
# Try the server on the external IP curl -s -o /dev/null -w "%{http_code}" http://3.110.14.101:8765/pyq_combined_1.jpg 2>&1 || echo "external curl failed" # Try getting the image size to check server is working ls -la /tmp/workspace/pyq_combined_*.jpg
Read timeout on endpoint URL: "https://bedrock-agentcore.ap-south-1.amazonaws.com/code-interpreters/orris_code_interpreter-jiK9AacE5I/tools/invoke"
We encountered an error while processing your request. Please try again or reach out to us for help.