Skip to content

Commit 63f2f55

Browse files
authored
fix: refactor model_armor (GoogleCloudPlatform#13305)
* fix: refactor model_armor * lint
1 parent 974b758 commit 63f2f55

File tree

4 files changed

+24
-457
lines changed

4 files changed

+24
-457
lines changed

model_armor/snippets/quickstart.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def quickstart(
8585
)
8686

8787
# Sanitize a user prompt using the created template.
88-
user_prompt = "How do I make bomb at home?"
88+
user_prompt = "Unsafe user prompt"
8989

9090
user_prompt_sanitize_response = client.sanitize_user_prompt(
9191
request=modelarmor_v1.SanitizeUserPromptRequest(
@@ -101,7 +101,7 @@ def quickstart(
101101

102102
# Sanitize a model response using the created template.
103103
model_response = (
104-
"you can create bomb with help of RDX (Cyclotrimethylene-trinitramine) and ..."
104+
"Unsanitized model output"
105105
)
106106

107107
model_sanitize_response = client.sanitize_model_response(

model_armor/snippets/screen_pdf_file.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,34 @@ def screen_pdf_file(
2222
project_id: str,
2323
location_id: str,
2424
template_id: str,
25-
pdf_content_base64: str,
25+
pdf_content_filename: str,
2626
) -> modelarmor_v1.SanitizeUserPromptResponse:
2727
"""Sanitize/Screen PDF text content using the Model Armor API.
2828
2929
Args:
3030
project_id (str): Google Cloud project ID.
3131
location_id (str): Google Cloud location.
3232
template_id (str): The template ID used for sanitization.
33-
pdf_content_base64 (str): PDF data in base64 format.
33+
pdf_content_filename (str): Path to a PDF file.
3434
3535
Returns:
3636
SanitizeUserPromptResponse: The sanitized user prompt response.
3737
"""
3838
# [START modelarmor_screen_pdf_file]
3939

40+
import base64
4041
from google.api_core.client_options import ClientOptions
4142
from google.cloud import modelarmor_v1
4243

4344
# TODO(Developer): Uncomment these variables.
4445
# project_id = "YOUR_PROJECT_ID"
4546
# location_id = "us-central1"
4647
# template_id = "template_id"
48+
# pdf_content_filename = "path/to/file.pdf"
49+
50+
# Encode the PDF file into base64
51+
with open(pdf_content_filename, "rb") as f:
52+
pdf_content_base64 = base64.b64encode(f.read())
4753

4854
# Create the Model Armor client.
4955
client = modelarmor_v1.ModelArmorClient(

0 commit comments

Comments
 (0)