Skip to content

Commit

Permalink
Change ocr prompt to instead enhance tesseract hOCR
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Dec 12, 2024
1 parent c466d7a commit 8ea171c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
9 changes: 4 additions & 5 deletions examples/openai-htr/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ RUN --mount=type=bind,from=imagemagick,source=/packages,target=/packages \

RUN magick -list format | grep "JPEG-2000"

ENV OPENAI_MODEL=gpt-4o-mini \
PROMPT="Transcribe this image that contains handwritten text. Include all text you see in the image. In your response, say absolutely nothing except the text from the image." \
MAX_TOKENS=300
ENV OPENAI_MODEL=gpt-4o \
CHAT_PROMPT="changeme" \
MAX_TOKENS=16384

COPY scyllaridae.yml /app/scyllaridae.yml
COPY cmd.sh /app/cmd.sh
COPY . /app/
20 changes: 18 additions & 2 deletions examples/openai-htr/cmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,26 @@
set -eou pipefail

TMP_DIR=$(mktemp -d)
HOCR_URL="$1"
DOMAIN=$(echo "$HOCR_URL"| awk -F/ '{print $1"//"$3}')
# our hOCR filenames are the node ID
NID=$(echo "$HOCR_URL" | xargs basename | awk -F '.hocr' '{print $1}')

# take the base prompt and move it into place
cp /app/prompt.txt "$TMP_DIR/prompt.txt"

# the hOCR document is being streamed into this script
# append the hOCR document into the prompt
# since we're asking the LLM to improve the hOCR doc
cat >> "$TMP_DIR/prompt.txt"
CHAT_PROMPT=$(jq --null-input --rawfile rawstring "$TMP_DIR/prompt.txt" '$rawstring')

# find the service file
SERVICE_FILE_PATH=$(curl -s "$DOMAIN/node/$NID/service-file" | jq -r '.[0].file')
# convert service file to jpg
magick - "$TMP_DIR/img.jpg"
curl -s "${DOMAIN}${SERVICE_FILE_PATH}" | magick - "$TMP_DIR/img.jpg"

# chatgpt needs it base64 encoded
BASE64_IMAGE=$(base64 -w 0 "$TMP_DIR/img.jpg")

cat <<EOF > "$TMP_DIR/payload.json"
Expand All @@ -18,7 +34,7 @@ cat <<EOF > "$TMP_DIR/payload.json"
"content": [
{
"type": "text",
"text": "$PROMPT"
"text": $CHAT_PROMPT
},
{
"type": "image_url",
Expand Down
8 changes: 8 additions & 0 deletions examples/openai-htr/prompt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Transcribe the text in the provided image.
Place the transcribed text into the appropriate ocrx_word span in the hOCR template provided. Ensure the following:
Never use tesseract to aid you.
Include all text in the transcription.
Never return an incomplete document. Always finish the complete job, with every word wrapped in a span, and the words accurately transcribed from the image.
Never edit the hOCR dimensions provided. Only replace the text within <span>'s.
Do not wrap your response in backticks. Your response should only be the hOCR document provided below with your transcribed text placed in the appropriate spots in the hOCR document.
Do not add any additional spans or attributes. You only should be adding plain text within the hocr HTML document.
2 changes: 2 additions & 0 deletions examples/openai-htr/scyllaridae.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ allowedMimeTypes:
cmdByMimeType:
default:
cmd: /app/cmd.sh
args:
- "%source-uri"

0 comments on commit 8ea171c

Please sign in to comment.