Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# UV
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
#uv.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# Ruff stuff:
.ruff_cache/

# PyPI configuration file
.pypirc

# MacOS files
.DS_Store
.DS_Store/
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# spark-symbiota-ml
Source code for models developed by BU Spark to enable plant specimen recognition for Symbiota

## Directories
1. transcription: Contains the transcription scripts for performing ocr.
2. backend: Contains a simple api implementation with one route to receive the url of an image, downloads and verifies the image, and then calls the transcription scripts to perform ocr on the downloaded image.
3. docker: Contains a docker compose implementation of the backend server.

## Running the ocr service
1. Put the `.env` file containing the required variables (see the [transcription README](/transcription/README.md))in the transcription directory.
2. To do a minimal installation of only the Azure pipeline, install the packages in `transcription/requirements-doc-int.txt` and then `backend/requirements-backend.txt` in order. Otherwise, you can install the root `requirements.txt` and then `backend/requirements-backend.txt`.
3. If the docker network `symbiota-network` has not been created yet, run `docker network create symbiota-network` to create the network. The ocr service and the ocr middleware can be hosted on this network together. Change this in `docker/docker-compose.yaml` according to your hosting preferences.
4. Navigate to the `docker` directory and run `docker compose up -d`.
1 change: 1 addition & 0 deletions backend/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Let Python treat this directory as a package for importing functions
48 changes: 48 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import requests
from PIL import Image
import os
from fastapi import FastAPI, HTTPException, Query
from transcription.doc_intelligence import run_doc_intell_pipeline

app = FastAPI()

@app.get("/")
async def root():
return {"message": "OCR service is running"}

@app.post("/azure")
async def evaluate(url: str = Query(...)):
# Create a temporary file
temp_filename = 'temp.jpg'

# Download the image
def download_image():
response = requests.get(url)
if response.status_code != 200:
raise Exception("Failed to download image")
with open(temp_filename, "wb") as f:
f.write(response.content)

try:
download_image()
except Exception as e:
os.remove(temp_filename)
raise HTTPException(status_code=400, detail=str(e))

# Verify that the downloaded file is a valid image
try:
with Image.open(temp_filename) as img:
img.verify()
except Exception:
os.remove(temp_filename)
raise HTTPException(status_code=400, detail="Downloaded file is not a valid image")

# Downloaded file path
print("Downloaded image path:", temp_filename)

azure_result = run_doc_intell_pipeline(temp_filename)

# Clean up by deleting the temporary file
os.remove(temp_filename)

return azure_result
Binary file added backend/requirements-backend.txt
Binary file not shown.
20 changes: 20 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Dockerfile for running the ocr service
FROM python:3.11.5-slim

# Set working directory
WORKDIR /app

# Install dependencies
COPY transcription/requirements-doc-int.txt transcription/
RUN pip install --no-cache-dir -r transcription/requirements-doc-int.txt
COPY backend/requirements-backend.txt backend/
RUN pip install --no-cache-dir -r backend/requirements-backend.txt

# Copy code into the container
COPY . .

# Expose port 8080 for FastAPI
EXPOSE 8080

# Run FastAPI
CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8080"]
15 changes: 15 additions & 0 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3.8'

services:
ocr_service:
build:
context: ..
dockerfile: docker/Dockerfile
ports:
- "8080:8080"
networks:
- symbiota-network

networks:
symbiota-network:
external: true
Loading