|
| 1 | +import os |
| 2 | +import shutil |
| 3 | + |
| 4 | +import jsons |
| 5 | +import pytest |
| 6 | +from fastapi.testclient import TestClient |
| 7 | + |
| 8 | +from unstructured_inference import api |
| 9 | +from unstructured_inference.inference.layout import DocumentLayout |
| 10 | +from unstructured_inference.models.yolox import yolox_local_inference, get_model_loading_info |
| 11 | +from unstructured_inference.models.base import UnknownModelException |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.slow |
| 15 | +def test_layout_v02_api_parsing_image(): |
| 16 | + filename = os.path.join("sample-docs", "test-image.jpg") |
| 17 | + |
| 18 | + client = TestClient(api.app) |
| 19 | + response = client.post( |
| 20 | + "/layout/yolox/image", |
| 21 | + headers={"Accept": "multipart/mixed"}, |
| 22 | + files=[("file", (filename, open(filename, "rb"), "image/png"))], |
| 23 | + data={"version": "yolox"}, |
| 24 | + ) |
| 25 | + doc_layout = jsons.load(response.json(), DocumentLayout) |
| 26 | + assert len(doc_layout.pages) == 1 |
| 27 | + # NOTE(benjamin) The example sent to the test contains 13 detections |
| 28 | + assert len(doc_layout.pages[0]["layout"]) == 13 |
| 29 | + assert response.status_code == 200 |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.slow |
| 33 | +def test_layout_v02_api_parsing_pdf(): |
| 34 | + filename = os.path.join("sample-docs", "loremipsum.pdf") |
| 35 | + |
| 36 | + client = TestClient(api.app) |
| 37 | + response = client.post( |
| 38 | + "/layout/yolox/pdf", |
| 39 | + files={"file": (filename, open(filename, "rb"))}, |
| 40 | + data={"version": "yolox"}, |
| 41 | + ) |
| 42 | + doc_layout = jsons.load(response.json(), DocumentLayout) |
| 43 | + assert len(doc_layout.pages) == 1 |
| 44 | + # NOTE(benjamin) The example sent to the test contains 5 detections |
| 45 | + assert len(doc_layout.pages[0]["layout"]) == 5 |
| 46 | + assert response.status_code == 200 |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.slow |
| 50 | +def test_layout_v02_api_parsing_pdf_ocr(): |
| 51 | + filename = os.path.join("sample-docs", "non-embedded.pdf") |
| 52 | + |
| 53 | + client = TestClient(api.app) |
| 54 | + response = client.post( |
| 55 | + "/layout/yolox/pdf", |
| 56 | + files={"file": (filename, open(filename, "rb"))}, |
| 57 | + data={"force_ocr": True, "version": "yolox"}, |
| 58 | + ) |
| 59 | + doc_layout = jsons.load(response.json(), DocumentLayout) |
| 60 | + assert len(doc_layout.pages) == 10 |
| 61 | + assert len(doc_layout.pages[0]["layout"]) > 1 |
| 62 | + assert response.status_code == 200 |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.slow |
| 66 | +def test_layout_v02_local_parsing_image(): |
| 67 | + filename = os.path.join("sample-docs", "test-image.jpg") |
| 68 | + OUTPUT_DIR = "yolox_output" |
| 69 | + # NOTE(benjamin) keep_output = True create a file for each image in |
| 70 | + # localstorage for visualization of the result |
| 71 | + if os.path.exists(OUTPUT_DIR): |
| 72 | + # NOTE(benjamin): should delete the default output folder on test? |
| 73 | + shutil.rmtree(OUTPUT_DIR) |
| 74 | + document_layout_1 = yolox_local_inference( |
| 75 | + filename, type="image", output_directory=OUTPUT_DIR, version="yolox" |
| 76 | + ) |
| 77 | + assert len(document_layout_1.pages) == 1 |
| 78 | + document_layout_2 = yolox_local_inference(filename, type="image", version="yolox") |
| 79 | + # NOTE(benjamin) The example image should result in one page result |
| 80 | + assert len(document_layout_2.pages) == 1 |
| 81 | + # NOTE(benjamin) The example sent to the test contains 13 detections |
| 82 | + assert len(document_layout_2.pages[0].layout) == 13 |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.slow |
| 86 | +def test_layout_v02_local_parsing_pdf(): |
| 87 | + filename = os.path.join("sample-docs", "loremipsum.pdf") |
| 88 | + document_layout = yolox_local_inference(filename, type="pdf", version="yolox") |
| 89 | + content = document_layout.to_string() |
| 90 | + assert "Lorem ipsum" in content |
| 91 | + assert len(document_layout.pages) == 1 |
| 92 | + # NOTE(benjamin) The example sent to the test contains 5 detections |
| 93 | + assert len(document_layout.pages[0].layout) == 5 |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.slow |
| 97 | +def test_layout_v02_local_parsing_empty_pdf(): |
| 98 | + filename = os.path.join("sample-docs", "empty-document.pdf") |
| 99 | + document_layout = yolox_local_inference(filename, type="pdf", version="yolox") |
| 100 | + assert len(document_layout.pages) == 1 |
| 101 | + # NOTE(benjamin) The example sent to the test contains 5 detections |
| 102 | + assert len(document_layout.pages[0].layout) == 0 |
| 103 | + |
| 104 | + |
| 105 | +def test_invalid_model(): |
| 106 | + with pytest.raises(UnknownModelException): |
| 107 | + get_model_loading_info("invalidmodel") |
| 108 | + |
| 109 | + |
| 110 | +######################## |
| 111 | +# ONLY SHORT TESTS BELOW |
| 112 | +######################## |
| 113 | + |
| 114 | + |
| 115 | +def test_layout_v02_api_parsing_image_soft(): |
| 116 | + filename = os.path.join("sample-docs", "test-image.jpg") |
| 117 | + |
| 118 | + client = TestClient(api.app) |
| 119 | + response = client.post( |
| 120 | + "/layout/yolox/image", |
| 121 | + headers={"Accept": "multipart/mixed"}, |
| 122 | + files=[("file", (filename, open(filename, "rb"), "image/png"))], |
| 123 | + data={"version": "yolox_tiny"}, |
| 124 | + ) |
| 125 | + doc_layout = jsons.load(response.json(), DocumentLayout) |
| 126 | + assert len(doc_layout.pages) == 1 |
| 127 | + # NOTE(benjamin) Soft version of the test, run make test-long in order to run with full model |
| 128 | + assert len(doc_layout.pages[0]["layout"]) > 0 |
| 129 | + assert response.status_code == 200 |
| 130 | + |
| 131 | + |
| 132 | +def test_layout_v02_api_parsing_pdf_soft(): |
| 133 | + filename = os.path.join("sample-docs", "loremipsum.pdf") |
| 134 | + |
| 135 | + client = TestClient(api.app) |
| 136 | + response = client.post( |
| 137 | + "/layout/yolox/pdf", |
| 138 | + files={"file": (filename, open(filename, "rb"))}, |
| 139 | + data={"version": "yolox_tiny"}, |
| 140 | + ) |
| 141 | + doc_layout = jsons.load(response.json(), DocumentLayout) |
| 142 | + assert len(doc_layout.pages) == 1 |
| 143 | + # NOTE(benjamin) Soft version of the test, run make test-long in order to run with full model |
| 144 | + assert len(doc_layout.pages[0]["layout"]) > 0 |
| 145 | + assert response.status_code == 200 |
| 146 | + |
| 147 | + |
| 148 | +def test_layout_v02_api_parsing_pdf_ocr_soft(): |
| 149 | + filename = os.path.join("sample-docs", "non-embedded.pdf") |
| 150 | + |
| 151 | + client = TestClient(api.app) |
| 152 | + response = client.post( |
| 153 | + "/layout/yolox/pdf", |
| 154 | + files={"file": (filename, open(filename, "rb"))}, |
| 155 | + data={"force_ocr": True, "version": "yolox_tiny"}, |
| 156 | + ) |
| 157 | + doc_layout = jsons.load(response.json(), DocumentLayout) |
| 158 | + assert len(doc_layout.pages) == 10 |
| 159 | + assert len(doc_layout.pages[0]["layout"]) > 1 |
| 160 | + assert response.status_code == 200 |
| 161 | + |
| 162 | + |
| 163 | +def test_layout_v02_local_parsing_image_soft(): |
| 164 | + filename = os.path.join("sample-docs", "test-image.jpg") |
| 165 | + OUTPUT_DIR = "yolox_output" |
| 166 | + # NOTE(benjamin) keep_output = True create a file for each image in |
| 167 | + # localstorage for visualization of the result |
| 168 | + if os.path.exists(OUTPUT_DIR): |
| 169 | + # NOTE(benjamin): should delete the default output folder on test? |
| 170 | + shutil.rmtree(OUTPUT_DIR) |
| 171 | + document_layout_1 = yolox_local_inference( |
| 172 | + filename, type="image", output_directory=OUTPUT_DIR, version="yolox_tiny" |
| 173 | + ) |
| 174 | + assert len(document_layout_1.pages) == 1 |
| 175 | + document_layout_2 = yolox_local_inference(filename, type="image", version="yolox_tiny") |
| 176 | + # NOTE(benjamin) The example image should result in one page result |
| 177 | + assert len(document_layout_2.pages) == 1 |
| 178 | + # NOTE(benjamin) Soft version of the test, run make test-long in order to run with full model |
| 179 | + assert len(document_layout_2.pages[0].layout) > 0 |
| 180 | + |
| 181 | + |
| 182 | +def test_layout_v02_local_parsing_pdf_soft(): |
| 183 | + filename = os.path.join("sample-docs", "loremipsum.pdf") |
| 184 | + document_layout = yolox_local_inference(filename, type="pdf", version="yolox_tiny") |
| 185 | + content = document_layout.to_string() |
| 186 | + assert "Lorem ipsum" in content |
| 187 | + assert len(document_layout.pages) == 1 |
| 188 | + # NOTE(benjamin) Soft version of the test, run make test-long in order to run with full model |
| 189 | + assert len(document_layout.pages[0].layout) > 0 |
| 190 | + |
| 191 | + |
| 192 | +def test_layout_v02_local_parsing_empty_pdf_soft(): |
| 193 | + filename = os.path.join("sample-docs", "empty-document.pdf") |
| 194 | + document_layout = yolox_local_inference(filename, type="pdf", version="yolox_tiny") |
| 195 | + assert len(document_layout.pages) == 1 |
| 196 | + # NOTE(benjamin) The example sent to the test contains 5 detections |
| 197 | + assert len(document_layout.pages[0].layout) == 0 |
0 commit comments