Spaces:
Sleeping
Sleeping
Download src/modules/execution/scenarios/pdf.py from dellweb3/VibeCoding: direct link, hf CLI and curl.
- Browser
- Download file 8 kB
-
https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/modules/execution/scenarios/pdf.py
- Command line
-
hf download hf://spaces/dellweb3/VibeCoding/src/modules/execution/scenarios/pdf.py
-
curl -L -o pdf.py https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/modules/execution/scenarios/pdf.py
8 kB
| """Public PDF parsing contract and platform-owned fixtures, never supplied by the model.""" | |
| import base64 | |
| import json | |
| import zlib | |
| from pathlib import Path | |
| from src.modules.execution.contracts import SandboxConfig, HttpCase | |
| from src.modules.execution.pdf_dependency import FRAMEWORK, PROFILE, VERSION, COORDINATE, SHA512 | |
| from src.modules.skill.domain.models import Framework | |
| SKILL_ID = 'pdf-java-development' | |
| PDF_PROFILE = FRAMEWORK | |
| PDF_VERSION = '0.1.0' | |
| DEPENDENCIES = {COORDINATE: VERSION} | |
| MAX_PDF_BYTES = 131072 | |
| MAX_BODY_BYTES = 180000 | |
| MAX_PAGES = 20 | |
| MAX_TEXT_BYTES = 24000 | |
| CONTRACT = '''Implement demo.App using Java17 HttpServer on port8080 and the provided Apache PDFBox3.0.8 jar (org.apache.pdfbox.Loader, PDDocument, PDFTextStripper). No other dependency/framework/download. | |
| GET /health =>200 {"status":"ok"}. POST /api/pdf/analyze requires Authorization: Bearer demo-pdf, otherwise401 {"error":"UNAUTHORIZED"}. | |
| Wrong method on either known endpoint =>405 {"error":"METHOD_NOT_ALLOWED"} before auth; unknown exact path=>404 {"error":"NOT_FOUND"}. | |
| Analyze accepts UTF-8 JSON with exactly one field: {"base64":"<canonical padded standard Base64 PDF>"}, optional JSON whitespace. No URL/path/password fields or duplicate keys. Invalid/missing/extra fields, empty/noncanonical/invalid Base64 or query parameters=>400 {"error":"INVALID_INPUT"}. | |
| Read at most180001 request bytes. Raw JSON>180000 or decoded PDF>131072 bytes=>413 {"error":"PDF_TOO_LARGE"}. Content-Type must start application/json (otherwise400 INVALID_INPUT). | |
| PDF must begin %PDF- and parse with PDFBox; malformed=>400 {"error":"INVALID_PDF"}. Password-protected OR any encrypted PDF=>422 {"error":"ENCRYPTED_PDF"}, never decrypt/bypass. Zero pages=>400 INVALID_PDF; >20 pages=>422 {"error":"PAGE_LIMIT"}. | |
| Use try-with-resources Loader.loadPDF(byte[]), reject document.isEncrypted(), PDFTextStripper with sortByPosition=true and start/end page, getText per page. Normalize CRLF/CR to LF and strip() surrounding whitespace; preserve internal line breaks. Count Unicode code points, not UTF16 units. | |
| Success200 exactly {"pageCount":N,"pages":[{"page":1,"text":"..."},...],"totalCharacters":sumCodePoints,"status":"TEXT_EXTRACTED" or "NO_TEXT"}. NO_TEXT if every extracted page text is empty; do not pretend OCR. Keep blank pages. UTF8 text total>24000 or serialized response>60000 bytes=>413 {"error":"TEXT_LIMIT"}. Do not return partial success. | |
| Use default single-thread executor or bounded executor, bounded memory input; never execute attachments, fetch URLs, use host filesystem, or log uploaded content. No external OCR. Close exchanges; JSON escaping must be valid. Files only src/main/java/demo/App.java and optional docs/README.md.''' | |
| def framework_candidate(): | |
| return Framework(profile_id=PDF_PROFILE, version=PDF_VERSION, dependencies=DEPENDENCIES, state='draft') | |
| def fixture_pdf(texts, *, image=False, compressed=False): | |
| """Small deterministic authored PDFs with exact xref offsets; not harvested customer data.""" | |
| objects = [b'<< /Type /Catalog /Pages 2 0 R >>', b''] | |
| objects.append(b'<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>') | |
| pages = [] | |
| for text in texts: | |
| page_id = len(objects) + 1 | |
| content_id = page_id + 1 | |
| pages.append(page_id) | |
| content = b'' | |
| if text: | |
| escaped = text.replace('\\', '\\\\').replace('(', '\\(').replace(')', '\\)') | |
| content = ('BT /F1 12 Tf 40 750 Td (' + escaped + ') Tj ET').encode('ascii') | |
| if image: | |
| # A genuine image-only page: raw one-pixel RGB image, with no text objects. | |
| content = b'q 100 0 0 100 40 600 cm /Im1 Do Q' | |
| resources = b'/Font << /F1 3 0 R >>' | |
| if image: | |
| resources += f' /XObject << /Im1 {content_id + 1} 0 R >>'.encode() | |
| objects.append(f'<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << '.encode()+resources+f' >> /Contents {content_id} 0 R >>'.encode()) | |
| payload = zlib.compress(content) if compressed else content | |
| objects.append(f'<< /Length {len(payload)}'.encode() + (b' /Filter /FlateDecode' if compressed else b'') + b' >>\nstream\n' + payload + b'\nendstream') | |
| if image: | |
| objects.append(b'<< /Type /XObject /Subtype /Image /Width 1 /Height 1 /ColorSpace /DeviceRGB /BitsPerComponent 8 /Length 3 >>\nstream\n\x00\x80\xff\nendstream') | |
| objects[1] = ('<< /Type /Pages /Count ' + str(len(pages)) + ' /Kids [' + ' '.join(str(p)+' 0 R' for p in pages) + '] >>').encode() | |
| data = bytearray(b'%PDF-1.4\n%\xe2\xe3\xcf\xd3\n') | |
| offsets = [0] | |
| for index, obj in enumerate(objects, 1): | |
| offsets.append(len(data)); data.extend(f'{index} 0 obj\n'.encode()+obj+b'\nendobj\n') | |
| xref = len(data) | |
| data.extend(f'xref\n0 {len(offsets)}\n0000000000 65535 f \n'.encode()) | |
| for offset in offsets[1:]: data.extend(f'{offset:010} 00000 n \n'.encode()) | |
| data.extend(f'trailer\n<< /Size {len(offsets)} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n'.encode()) | |
| return bytes(data) | |
| def sample_pdf(): | |
| return fixture_pdf(['Purchase order PO-2026-1003', 'Received 40 of 80 units.']) | |
| def payload(pdf): | |
| return {'base64': base64.b64encode(pdf).decode()} | |
| def scenario(image): | |
| health = HttpCase(path='/health', status=200, expected_json={'status':'ok'}) | |
| cases = [health] | |
| def add(body, expected, status=200, method='POST', auth=True, path='/api/pdf/analyze'): | |
| cases.append(HttpCase(path=path, method=method, body=body, status=status, expected_json=expected, | |
| headers={'Authorization':'Bearer demo-pdf'} if auth else {})) | |
| texts = ['Purchase order PO-2026-1003', 'Received 40 of 80 units.'] | |
| add(payload(sample_pdf()), {'pageCount':2,'pages':[{'page':i+1,'text':text} for i,text in enumerate(texts)], | |
| 'totalCharacters':sum(map(len,texts)), 'status':'TEXT_EXTRACTED'}) | |
| add(payload(fixture_pdf([''])), {'pageCount':1,'pages':[{'page':1,'text':''}],'totalCharacters':0,'status':'NO_TEXT'}) | |
| add(payload(fixture_pdf([''],image=True)), {'pageCount':1,'pages':[{'page':1,'text':''}],'totalCharacters':0,'status':'NO_TEXT'}) | |
| add(payload(fixture_pdf(['Compressed PDF text.'],compressed=True)), {'pageCount':1,'pages':[{'page':1,'text':'Compressed PDF text.'}],'totalCharacters':20,'status':'TEXT_EXTRACTED'}) | |
| encrypted = json.loads(Path(__file__).with_name('pdf_fixtures.json').read_text()) | |
| for key in ('password_required', 'empty_user_password'): | |
| add({'base64':encrypted[key]}, {'error':'ENCRYPTED_PDF'},422) | |
| for body in ({}, {'base64':''}, {'base64':'***'}, {'base64':42}, {'base64':'Zg'}, | |
| dict(payload(sample_pdf()), url='https://example.com/remote.pdf')): | |
| add(body, {'error':'INVALID_INPUT'},400) | |
| add(payload(b'not a pdf'), {'error':'INVALID_PDF'},400) | |
| add(payload(b'%PDF-1.4\nnot valid\n%%EOF'), {'error':'INVALID_PDF'},400) | |
| add(payload(fixture_pdf([''] * 21)), {'error':'PAGE_LIMIT'},422) | |
| add(payload(b'%PDF-' + b'x' * MAX_PDF_BYTES), {'error':'PDF_TOO_LARGE'},413) | |
| add(payload(fixture_pdf(['A'*24001],compressed=True)), {'error':'TEXT_LIMIT'},413) | |
| add(payload(sample_pdf()), {'error':'UNAUTHORIZED'},401,auth=False) | |
| add(None, {'error':'METHOD_NOT_ALLOWED'},405,method='GET') | |
| add({}, {'error':'NOT_FOUND'},404,path='/api/pdf/other') | |
| add(payload(sample_pdf()), {'error':'INVALID_INPUT'},400,path='/api/pdf/analyze?url=x') | |
| # Long upload proves HTTP-body handling independently of the preview UART transport. | |
| padded = sample_pdf() + b'\n%' + b' ' * 12000 | |
| add(payload(padded), {'pageCount':2,'pages':[{'page':i+1,'text':text} for i,text in enumerate(texts)], | |
| 'totalCharacters':sum(map(len,texts)), 'status':'TEXT_EXTRACTED'}) | |
| return SandboxConfig(image=image, skill_ids=[SKILL_ID], api_contract=CONTRACT, command_seconds=120, | |
| dependency_profile=PROFILE, dependency_sha512=SHA512, tests=cases, health=health) | |