Skip to content

Commit

Permalink
Add option to save results locally
Browse files Browse the repository at this point in the history
  • Loading branch information
ali6parmak committed Jan 15, 2025
1 parent d1aa7e9 commit a54e6a0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ RUN pip --default-timeout=1000 install -r requirements.txt
WORKDIR /app
COPY --chown=python:python ./src/. ./src
COPY --chown=python:python ./models/. ./models/
COPY --chown=python:python ./data/. ./data/
RUN python src/download_models.py

ENV PYTHONPATH "${PYTHONPATH}:/app/src"

3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ remove_docker_images:
start:
ifeq ($(OS), Windows_NT)
if not exist models mkdir models
if not exist data mkdir data
else
mkdir -p ./models
mkdir -p ./data
endif
ifeq ($(HAS_GPU), 1)
@echo "NVIDIA GPU detected, using docker-compose-gpu.yml"
Expand All @@ -38,6 +40,7 @@ else
endif

start_no_gpu:
mkdir -p ./data
mkdir -p ./models
docker compose up --build

Expand Down
23 changes: 22 additions & 1 deletion docker-compose-gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,31 @@ services:
extends:
file: docker-compose.yml
service: ner
volumes:
- ./data:/app/data
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: 1
capabilities: [ gpu ]
capabilities: [ gpu ]

worker-pdf-layout-gpu:
extends:
file: docker-compose.yml
service: worker-pdf-layout-gpu
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: 1
capabilities: [ gpu ]

networks:
network-ner:
driver: bridge

volumes:
data:
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ services:
build:
context: .
dockerfile: Dockerfile
volumes:
- ./data:/app/data
networks:
- network-ner
ports:
Expand All @@ -26,3 +28,6 @@ services:
networks:
network-ner:
driver: bridge

volumes:
data:
12 changes: 10 additions & 2 deletions src/drivers/rest/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import sys
import tempfile
import uuid
Expand Down Expand Up @@ -34,7 +35,14 @@ async def get_named_entities(text: str = Form("")):


@app.post("/pdf")
async def get_pdf_named_entities(file: UploadFile = File(...)):
async def get_pdf_named_entities(file: UploadFile = File(...), save_locally: bool = Form(False)):
repository = PDFLayoutAnalysisRepository()
pdf_path: Path = pdf_content_to_pdf_path(file.file.read())
return [entity for entity in NamedEntitiesFromPDFUseCase(repository).get_entities(pdf_path)]
entities = [entity for entity in NamedEntitiesFromPDFUseCase(repository).get_entities(pdf_path)]

if save_locally:
entities_json = [entity.model_dump() for entity in entities]
save_path: Path = Path("/app/data", pdf_path.name.replace(".pdf", ".json"))
save_path.write_text(json.dumps(entities_json, indent=2))

return entities

0 comments on commit a54e6a0

Please sign in to comment.