Package: django-ocr_translate
OCR_translateThis is a Django app for creating back-end server aimed at performing OCR and translation of images received via a POST request.The OCR and translation is performed using freely available machine learning models and packages (see below for what is currently implemented).The server is designed to be used together withthis browser extension, acting as a front-end providing the images and controlling the model languages and models being used.For more information, please consult theFull DocumentationRunning the serverIf you plan to use a different settings (eg. database, or model location), you can either:manually edit thesettings.pyUse the providedEnvironment variablesSee below for alist of supported databasesYou will also have to modify theALLOWED_HOSTSin case you plan to access the server from somewhere other thanlocalhost.All the different way to run the server may provide different set of default values (each of them is targeted for a different level of usage).From Release file (Windows)(Tested on Windows 11)
From thegithub releases pageyou can download either:TheCPU only versionThe GPU version split infile 1andfile 2(The CUDA dependencies makes it take much more space), which can be restored using tools like7zipandNanaZip.Usage:
Unzip the file and from inside the folder, run therun_server-XXX.exefile (XXX=cpu/gpu)The server will run with sensible defaults. Most notably the models files and database will be downloaded/created under%userprofile%/.ocr_translate.
Also the gpu version will attempt to run on GPU by default, and fall-back to CPU if the former is not available.For customization, you can set theenvironment variableyourself:Windows: either via powershell or by searching forenvironment variablein the settings menu.SuperUser: By default the server will be created with a admin useradmin:password(release only).
You can login to the admin interface athttp://127.0.0.1:4000/admin/in order to manage the database.
The main idea is to use this to manually add new models (with the appropriate entrypoint) if you want to test out,
model that are not yet available. (If you find good models, please do share).NOTE: The server by default is open only to localhost. If you ever plan to change this, make sure to change the admin password.From GithubThe Github repo provides not only the Django app files, but also the already configured project files used to start the server.Clone or download the repositorygit clone https://github.com/Crivella/ocr_translate.gitInstall the project dependencies (choose the appropriate files depending if you wanna run on GPU or CPU only):pip install -r requirements-torch-[cpu/cuda].txt
pip install -r requirements.txtCreate/Initialize your database by runningpython manage.py migrateinside your project folder.NOTE: From version 0.3.X model installation and functionalities have been moved to plugins that need to bepip installedseparately.
Seethis documentation pagefor a list of validated plugins.Run the server using for example one of the following options:Django development server. This is more oriented for developing than deploying, but is fine for a self-hosted single-user server accepting connections only onlocalhostFrom inside the project directory:python manage.py runserver PORTThe suggested PORT would be 4000 as it is the one set by default in the extensionNginx+Gunicorn:Check theDockerfile, as this is what the provided image makes use of.At least for the first time, it is suggested to run the server with theEnvironment variablesAUTOCREATE_LANGUAGESandAUTOCREATE_VALIDATED_MODELSset to"true"to automatically load the validated languages and models provided by the project.Notes:Gunicorn workers will each spawn a separate instance of the loaded models, each taking its own space in the memory. This can quickly fill up the memory especially if running on GPU. Ideally set this to 1.Django development server will spawn new threads for handling incoming requests (if no currently existing thread is free), which share the same memory. Running more than one worker per loaded model concurrently might slow down the actual computation and in some case also block the execution.SuperUser: You can create a superuser for managing the database via the django-admin interface by runningpython manage.py createsuperuserFrom PyPI installationRun the commandpip install django-ocr_translateBy default torch 2.x will come in its CUDA enabled version. While this works also for CPU, it will also install ~1 GB of cuda dependencies.
If you wish to run on CPU only, download the filerequirements-torch-cpu.txtfirst and runpip install -r requirements-torch-cpu.txtbefore installing the python package.When installing the project from PyPI, only the app is available.
This will need to be integrated in a Django project in order to be used.
These are the minimal instruction for creating a project and start running the server:Rundjango-admin startproject mysiteto create a django projectConfigure the server by replacing the automatically created files (strongly recommended):settings.pywith the one available on the repo.urls.pywith the one available on the repo.or by manually editing the files:settings.py: Add theocr_translatedapp to theINSTALLED_APPSurls.py: Include the'ocr_translate.urls'into your project urls.From here follow the same instructions as when startingfrom GithubFrom docker imageThis section assumes you have docker installed!!!CPU and CUDA specific images are available onDockerHUB:Download the image from DockerHUB:CPU:docker pull crivella1/ocr_translate:latest-cpuGPU:docker pull crivella1/ocr_translate:latest-gpuor create it manually create your image:Create a .pip-cache-[cpu/gpu] directory inside your project.Optional: re-install the project dependencies pointing this as the cache folder for pip (will make the build process much faster, by reusing the cached dependencies)Rundocker build -t IMAGE_TAG -f Dockerfile-[cpu/gpu] .Run the command:docker run --name CONTAINER_NAME -v PATH_TO_YOUR_MODEL_DIRECTORY:/models -v PATH_TO_DIR_WITH_SQLITE_FILE:/data --env-file=PATH_TO_AND_ENV_VARIABLE_FILE -p SERVER_PORT:4000 -d ocr_translateSee theEnvironment variablessection for configuring your environment variable file. Additionally the docker image defines several other variables to automatically create an admin user for managing the database via the django-admin interface:UID: UID of the user owning the files in /models and /dataGID: GID of the user owning the files in /models and /dataNUM_WEB_WORKERS: Number of gunicorn workers for the serverDJANGO_SUPERUSER_USERNAME: The username of the admin user to be created.DJANGO_SUPERUSER_PASSWORD: The password of the admin user to be created.