Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ocr prompt to instead enhance tesseract hOCR #61

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ RUN adduser -S -G nobody scyllaridae

RUN apk update && \
apk add --no-cache \
curl==8.11.0-r2 \
curl==8.11.1-r0 \
bash==5.2.26-r0 \
ca-certificates==20240705-r0 \
openssl==3.3.2-r1
Expand Down
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.
6 changes: 5 additions & 1 deletion examples/openai-htr/scyllaridae.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
allowedMimeTypes:
- "image/*"
- "text/vnd.hocr+html"
- "text/html"
- "application/octet-stream"
cmdByMimeType:
default:
cmd: /app/cmd.sh
args:
- "%source-uri"
3 changes: 3 additions & 0 deletions internal/config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ func IsAllowedMimeType(mimetype string, allowedFormats []string) bool {
return true
}
}
if format == strings.Split(mimetype, ";")[0] {
return true
}
}
return false
}
Expand Down
Loading