in demo_app/demo_app.py [0:0]
def pii_redaction_pdf_send_request(file: Optional[str]):
processing_start_time = time.time()
redacted_text_output = ""
redacted_pdf_page_imgs = None
input_doc_pdf_page_imgs = None
try:
if file is None:
gr.Warning(
"Please select or upload a PDF file, then click 'Process PDF File'."
)
return ("", "", {})
# Get response from the API
mime_type = mimetypes.guess_type(file)[0]
with open(file, "rb") as f:
data = f.read()
headers = {"Content-Type": mime_type}
status_code, processing_time_taken, response = send_request(
route="pii_redaction_pdf",
data=data,
headers=headers,
force_json_content_type=True,
)
if isinstance(response, dict) and response.get("redacted_text"):
redacted_text_output = response["redacted_text"]
if isinstance(response, dict) and response.get(
"input_doc_pdf_page_imgs"
):
input_doc_pdf_page_imgs = [
Image.open(BytesIO(base64.b64decode(b64_img)))
for b64_img in response.pop("input_doc_pdf_page_imgs")
]
if isinstance(response, dict) and response.get(
"redacted_pdf_page_imgs"
):
redacted_pdf_page_imgs = [
Image.open(BytesIO(base64.b64decode(b64_img)))
for b64_img in response.pop("redacted_pdf_page_imgs")
]
except Exception as e:
logging.exception("Error processing file")
status_code = 500
processing_time_taken = (
f"{round(time.time() - processing_start_time, 1)} seconds"
if processing_start_time is not None
else "N/A"
)
response = {"Error": str(e)}
return (
status_code,
processing_time_taken,
gr.JSON(value=response, label="Full Function API Response", visible=True),
gr.Textbox(
label="Redacted Text",
value=redacted_text_output,
interactive=False,
visible=True,
),
gr.Gallery(
label="Input PDF Pages",
value=input_doc_pdf_page_imgs,
type="pil",
object_fit="contain",
columns=min(len(input_doc_pdf_page_imgs), 2),
show_label=True,
interactive=False,
visible=True,
),
gr.Gallery(
label="Redacted PDF Pages",
value=redacted_pdf_page_imgs,
type="pil",
object_fit="contain",
columns=min(len(redacted_pdf_page_imgs), 2),
show_label=True,
interactive=False,
visible=True,
),
)