diff --git a/.docker/Dockerfile b/.docker/Dockerfile index 2fff268e..54de9cb5 100644 --- a/.docker/Dockerfile +++ b/.docker/Dockerfile @@ -76,7 +76,6 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ tesseract-ocr-fra \ tesseract-ocr-por \ tesseract-ocr-spa \ - ttyd \ unpaper \ && rm -rf /var/lib/apt/lists/* diff --git a/.docker/Dockerfile.alpine b/.docker/Dockerfile.alpine index 6b83368f..4e7aa3f4 100644 --- a/.docker/Dockerfile.alpine +++ b/.docker/Dockerfile.alpine @@ -64,7 +64,6 @@ RUN apk add --no-cache \ tesseract-ocr-data-por \ tesseract-ocr-data-spa \ ttf-droid \ - ttyd \ unpaper \ && rm -rf /var/cache/apk/* diff --git a/misc/_webservice.py b/misc/_webservice.py index 95106cb0..b006a1ba 100644 --- a/misc/_webservice.py +++ b/misc/_webservice.py @@ -16,7 +16,6 @@ import os import subprocess import sys import time -import urllib from functools import partial from operator import getitem from pathlib import Path @@ -25,7 +24,6 @@ from tempfile import NamedTemporaryFile import pikepdf import streamlit as st -from port_for import get_port from streamlit.components.v1 import iframe from ocrmypdf._defaults import DEFAULT_ROTATE_PAGES_THRESHOLD @@ -43,10 +41,6 @@ def get_host_url_with_port(port: int) -> str: st.title("OCRmyPDF Web Service") -if not which("ttyd"): - st.error("Missing dependency: ttyd. Please install ttyd to the local environment.") - sys.exit(1) - uploaded = st.file_uploader("Upload input PDF or image", type=["pdf"], key="file") mode = st.selectbox("Mode", options=["normal", "skip-text", "force-ocr", "redo-ocr"]) @@ -220,32 +214,28 @@ if uploaded: key='run_button', ): st.session_state['running'] = True - args = [sys.executable, '-m', "ocrmypdf"] + args - cmdline = " ".join(args) - st.code(cmdline, language="bash") + args = [sys.executable, '-u', '-m', "ocrmypdf"] + args - port = get_port((5000, 7000)) - ttyd_args = ['ttyd', '--port', str(port), '--once', '--readonly'] + proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + with st.container(border=True): + while proc.poll() is None: + line = proc.stderr.readline() + if line: + st.html("" + line.decode().strip() + "") - ttyd_proc = subprocess.Popen( - ttyd_args + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE + if proc.returncode != 0: + st.error(f"ocrmypdf failed with exit code {proc.returncode}") + st.session_state['running'] = False + st.stop() + + if Path(output_file.name).stat().st_size == 0: + st.error("No output PDF file was generated") + st.stop() + + st.download_button( + label="Download output PDF", + data=input_file.read(), + file_name=uploaded.name, + mime="application/pdf", ) - iframe(src=get_host_url_with_port(port), height=400) - - while ttyd_proc.poll() is None: - ttyd_proc.poll() - time.sleep(1) - - if ttyd_proc.returncode == 0: - if Path(output_file.name).stat().st_size == 0: - st.error("No output PDF file was generated") - else: - st.download_button( - label="Download output PDF", - data=input_file.read(), - file_name=uploaded.name, - mime="application/pdf", - ) - else: - st.error(f"ttyd failed with exit code {ttyd_proc.returncode}") st.session_state['running'] = False diff --git a/pyproject.toml b/pyproject.toml index 111640ec..34955e9f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -63,10 +63,7 @@ test = [ "types-humanfriendly", ] watcher = ["watchdog>=1.0.2", "typer-slim[standard]", "python-dotenv"] -webservice = [ - "port-for>=0.7.4", - "streamlit>=1.41.0", -] +webservice = ["streamlit>=1.41.0"] [project.scripts] ocrmypdf = "ocrmypdf.__main__:run"