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

Add OpenAI HTR service #58

Merged
merged 5 commits into from
Dec 6, 2024
Merged
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
52 changes: 52 additions & 0 deletions ci/k8s/htr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
apiVersion: v1
kind: Service
metadata:
name: islandora-openai-htr
spec:
selector:
app: islandora-openai-htr
ports:
- protocol: TCP
port: 8001
targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: islandora-openai-htr
spec:
replicas: 3
selector:
matchLabels:
app: islandora-openai-htr
template:
metadata:
labels:
app: islandora-openai-htr
spec:
containers:
- name: scyllaridae-tesseract
image: lehighlts/scyllaridae-openai-htr:main
imagePullPolicy: IfNotPresent
env:
- name: OPENAI_API_KEY
valueFrom:
secretKeyRef:
name: openai
key: api-key
resources:
requests:
memory: "128Mi"
cpu: "250m"
limits:
memory: "1Gi"
ports:
- hostPort: 8001
containerPort: 8080
readinessProbe:
httpGet:
path: /healthcheck
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
7 changes: 7 additions & 0 deletions ci/k8s/ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,10 @@ spec:
name: islandora-ocrpdf
port:
number: 8080
- path: /openai\-htr(/|$)(.*)
pathType: Prefix
backend:
service:
name: islandora-openai-htr
port:
number: 8080
8 changes: 8 additions & 0 deletions examples/openai-htr/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ARG TAG=main
ARG DOCKER_REPOSITORY=lehighlts
FROM ${DOCKER_REPOSITORY}/scyllaridae:${TAG}

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
COPY scyllaridae.yml /app/scyllaridae.yml
14 changes: 14 additions & 0 deletions examples/openai-htr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# openai-htr

Use OpenAI ChatGPT to transcribe handwritten text.

## Secrets

Requires an environment variable `OPENAI_API_KEY`

If deploying this in kubernetes, you can create the secret via

```
kubectl create secret generic openai \
--from-literal=api-key=$OPENAI_API_KEY
```
28 changes: 28 additions & 0 deletions examples/openai-htr/cmd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

set -eou pipefail

curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "'"$OPENAI_MODEL"'",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "'"$PROMPT"'"
},
{
"type": "image_url",
"image_url": {
"url": "'"$1"'"
}
}
]
}
],
"max_tokens": '"$MAX_TOKENS"'
}' | jq -r .choices[0].message.content
7 changes: 7 additions & 0 deletions examples/openai-htr/scyllaridae.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
allowedMimeTypes:
- "image/*"
cmdByMimeType:
default:
cmd: /app/cmd.sh
args:
- "%source-uri"
3 changes: 3 additions & 0 deletions examples/parry/scyllaridae.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ queueMiddlewares:
- queueName: islandora-merge-pdf
url: http://mergepdf:8080
consumers: 3
- queueName: islandora-openai-htr
url: http://openai-htr:8080
consumers: 3
Loading