Skip to content

Commit

Permalink
tag names support and language fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jozefbaranec committed Nov 6, 2024
1 parent 2b74d8d commit 867b8ba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
31 changes: 14 additions & 17 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,14 @@
"category": "Tags",
"configurations": [
{
"program": "docker run --platform linux/amd64 -v \"${working_directory}:/data\" -w /data --rm pdfix/alt-text-openai:latest --name \"${license_name}\" --key \"${license_key}\" detect -i \"${input_pdf}\" -o \"${output_pdf}\" --openai \"${openai_key}\" --lang \"${lang}\"",
"name": "Alt Text with OpenAI",
"desc": "Generate and applies alternative image descriptions to PDF files using PDFix SDK and OpenAI"
"program": "docker run --platform linux/amd64 -v \"${working_directory}:/data\" -w /data --rm pdfix/alt-text-openai:latest --name \"${license_name}\" --key \"${license_key}\" detect -i \"${input_pdf}\" -o \"${output_pdf}\" --tags \"$(tag_name)\" --openai \"${openai_key}\" --lang \"${lang}\"",
"name": "Generate Alt Text for all specified Tags with missing alternate description",
"desc": "Generate and set the alternate descriptions to listed tags in PDF files using PDFix SDK and OpenAI"
},
{
"program": "docker run --platform linux/amd64 -v \"${working_directory}:/data\" -w /data --rm pdfix/alt-text-openai:latest --name \"${license_name}\" --key \"${license_key}\" detect -i \"${input_pdf}\" -o \"${output_pdf}\" --openai \"${openai_key}\" --lang \"${lang}\" --overwrite",
"name": "Alt Text with OpenAI",
"desc": "Generate and applies alternative image descriptions to PDF files using PDFix SDK and OpenAI"
},
{
"program": "docker run --platform linux/amd64 -v \"${working_directory}:/data\" -w /data --rm pdfix/alt-text-openai:latest --name \"${license_name}\" --key \"${license_key}\" detect -i \"${input_pdf}\" -o \"${output_pdf}\" --openai \"${openai_key}\"",
"name": "Alt Text with OpenAI",
"desc": "Generate and applies alternative image descriptions to PDF files using PDFix SDK and OpenAI"
},
{
"program": "docker run --platform linux/amd64 -v \"${working_directory}:/data\" -w /data --rm pdfix/alt-text-openai:latest --name \"${license_name}\" --key \"${license_key}\" detect -i \"${input_pdf}\" -o \"${output_pdf}\" --openai \"${openai_key}\" --overwrite",
"name": "Alt Text with OpenAI",
"desc": "Generate and applies alternative image descriptions to PDF files using PDFix SDK and OpenAI"
"program": "docker run --platform linux/amd64 -v \"${working_directory}:/data\" -w /data --rm pdfix/alt-text-openai:latest --name \"${license_name}\" --key \"${license_key}\" detect -i \"${input_pdf}\" -o \"${output_pdf}\" --tags \"$(tag_name)\" --openai \"${openai_key}\" --lang \"${lang}\" --overwrite",
"name": "Generate Alt Text for all specified Tags",
"desc": "Generate and set the alternate descriptions to listed tags in PDF files using PDFix SDK and OpenAI"
}
],
"args": [
Expand Down Expand Up @@ -56,7 +46,14 @@
"value": ""
},
{
"title": "Alternate description language",
"title": "Tag Name",
"name": "tag_name",
"desc": "Tag name defined by a regular expression",
"type": "string",
"value": "Figure|Formula"
},
{
"title": "Language",
"name": "lang",
"desc": "Alternate description language",
"type": "string",
Expand Down
2 changes: 2 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def main():
)

pars_detect.add_argument("--openai", type=str, required=True, help="OpenAI API key")
pars_detect.add_argument("--tags", type=str, required=True, help="Regular expression defining the tag name")
pars_detect.add_argument(
"--overwrite",
action="store_true",
Expand Down Expand Up @@ -103,6 +104,7 @@ def main():
alt_text(
input_file,
output_file,
args.tags,
args.name,
args.key,
args.openai,
Expand Down
14 changes: 9 additions & 5 deletions src/process_pdf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ctypes
import re

from pdfixsdk.Pdfix import (
GetPdfix,
Expand Down Expand Up @@ -59,6 +60,7 @@ def update_image_alt(
doc: PdfDoc,
api_key: str,
overwrite: bool,
lang: str
) -> None:
img = "image_" + str(elem.GetObject().GetId()) + ".jpg"

Expand Down Expand Up @@ -95,7 +97,7 @@ def update_image_alt(
with open(img, "wb") as bf:
bf.write(data)

response = alt_description(img, api_key)
response = alt_description(img, api_key, lang)

# print(response.message.content)
alt = response.message.content
Expand All @@ -109,6 +111,7 @@ def update_image_alt(
def browse_figure_tags(
parent: PdsStructElement,
doc: PdfDoc,
tags: str,
api_key: str,
overwrite: bool,
lang: str,
Expand All @@ -119,19 +122,20 @@ def browse_figure_tags(
if parent.GetChildType(i) != kPdsStructChildElement:
continue
child_elem = struct_tree.GetStructElementFromObject(parent.GetChildObject(i))
if child_elem.GetType(True) == "Figure":
if re.match(tags, child_elem.GetType(True)):
# process figure element
update_image_alt(child_elem, doc, api_key, overwrite, lang)
else:
browse_figure_tags(child_elem, doc, api_key, overwrite, lang)
browse_figure_tags(child_elem, doc, tags, api_key, overwrite, lang)


def alt_text(
input_path: str,
output_path: str,
tags: str,
license_name: str,
license_key: str,
api_key: str,
api_key: str,
overwrite: bool,
lang: str,
) -> None:
Expand Down Expand Up @@ -176,7 +180,7 @@ def alt_text(

child_elem = struct_tree.GetStructElementFromObject(struct_tree.GetChildObject(0))
try:
browse_figure_tags(child_elem, doc, api_key, overwrite, lang)
browse_figure_tags(child_elem, doc, tags, api_key, overwrite, lang)
except Exception as e:
raise e

Expand Down

0 comments on commit 867b8ba

Please sign in to comment.