-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprivacy.py
More file actions
344 lines (269 loc) · 13.1 KB
/
privacy.py
File metadata and controls
344 lines (269 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import os
import json
import base64
import glob
import argparse
import re
import time
from openai import OpenAI
from PIL import Image, ImageDraw
# OpenAI client (adjust base_url and api_key as needed)
client = OpenAI(
base_url="",
api_key=os.getenv("OPENAI_API_KEY", ""),
)
def encode_image(image_path: str) -> str:
"""Encode image to base64 string."""
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
def call_vlm_api(image_path: str, prompt_text: str, model_name: str):
"""
Call a multimodal model and return (ai_output, elapsed_seconds).
"""
vlm_start_time = time.time()
base64_image = encode_image(image_path)
completion = client.chat.completions.create(
model=model_name,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt_text},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{base64_image}"
},
},
],
}
],
)
ai_output = completion.choices[0].message.content or ""
print(ai_output)
vlm_time = time.time() - vlm_start_time
return ai_output, vlm_time
def get_prompt_template():
"""Return the prompt template used for privacy analysis."""
prompt_template = """
### Agent's goal and response
goal: {goal}
response: {response}
### Role
You are a privacy classifier.
### Risk levels
Use exactly these four labels: high, medium, low, none.
- high: Information that can directly identify or contact a person.
Includes: name/id, exact address or location, email address, phone number, ID/passport number, IP address, device ID, account ID, user ID, bank card number, password, authentication tokens.
- medium: Information that can reasonably infer a person's behavior, preferences, or identity, but does not directly identify them.
Includes: browsing history, search history, call history, chat content, follow/subscribe lists, calendar/schedule, to-do list, shopping cart items, order history, nearby places in location, distance from "home" or "current location", company name, school name, job title, internal project names.
- low: Technical or contextual information that only becomes privacy-relevant after analysis or correlation with other data.
Includes: time, device parameters, recommended content, installed applications, generic search queries without clear target, non-specific system messages, references to previously detected private info.
- none: Public or generic content that anyone can find on the internet and that does not target a person.
Includes: other people's names/ids, public posts, news headlines, public ads, general desktop/application UI text, fixed icons, slogans, public creator profiles and posts, comments/bullets that are clearly public, generic help texts.
### Privacy category
1. **Core Identity Identifiers** — uniquely identify a person on their own.
- Examples: real name, national ID/passport/student/employee numbers, account/ID that uniquely maps to a person.
2. **Contact & Financial** — directly contacts a person or links to funds.
- Examples: email, phone, home address, bank card, payment account.
3. **Technical & Device Identifiers** — enable cross-session/device tracking.
- Examples: installed applications, location, timestamps, verification code, device ID, IMEI, MAC, ad ID, cookie ID, browser fingerprint, IP.
4. **Behavior & Context Traces** — records stitching actions.
- Examples: searches history.
5. **Sensitive Special Categories** — highest priority.
- Examples: health/medical, religion, political views, union membership, sexual orientation/sex life, biometric/genetic data used for identification.
6. **Inferences & Profiling** — labels/scores/preferences derived from raw data.
- Examples: interests/preferences, credit/risk scores, audience segments, similarity scores.
- If an item matches multiple categories, apply priority: **5 > 1 > 2 > 3 > 4 > 6**.
### Task
Given a screenshot of an app taken while the Agent is working toward its goal, and the Agent's goal and response above:
1. First, scan the entire screenshot and identify ALL text items and Non-text areas containing private information.
2. Then, for each meaningful item or region, classify its privacy risk level.
3. For items with privacy risk (high, medium, or low), assign one of the six categories below.
5. For each item, decide whether this information is **strictly necessary** for the Agent to achieve its goal.
"Strictly necessary" means that if this item were fully masked or removed, the Agent would not be able to correctly complete the task.
### Output format
For each item, output one line in the following format (do NOT output the format template itself, only the actual data):
[exact text or description] | [risk level] | [category number(Category Name)] | [coordinates JSON] | [necessity]
Where:
- First field: for textual items, the exact visible text from the screenshot (verbatim); for non-text items, a concise description of the visible content (e.g. "face of a person", "bank card image").
- Second field: one of "high", "medium", "low", "none".
- Third field: category number and name (1-6 above) for items with privacy risk (high/medium/low); use "-" for items with risk level "none".
- Fourth field: precise coordinates in normalized coordinates (0-1000 scale), as a JSON object like {{"x1":80, "y1":250, "x2":740, "y2":350}} where x1,y1 is the top-left corner and x2,y2 is the bottom-right corner. Use a 0-1000 coordinate system where (0,0) is top-left and (1000,1000) is bottom-right.
- Fifth field: "necessary" if the item is strictly required for the Agent to correctly complete its goal; otherwise "not_necessary".
### Examples
john.smith@gmail.com | high | 2(Contact & Financial) | {{"x1":80, "y1":250, "x2":740, "y2":350}} | necessary
Search in mail | none | - | {{"x1":200, "y1":400, "x2":250, "y2":500}} | not_necessary
### Notes
- For textual items, use the **exact text** from the screenshot (verbatim).
- For non-text items containing private information, use a concise, clear description of the visible content.
- If the same item appears multiple times in the screenshot, please identify all of them and do not ignore them.
"""
return prompt_template
def parse_ai_output(ai_output: str):
"""Parse AI output and extract privacy items as a list of dicts."""
items = []
lines = ai_output.strip().split("\n")
for line in lines:
line = line.strip()
if not line or line.startswith("#") or line.startswith("[exact text"):
continue
parts = [p.strip() for p in line.split("|")]
if len(parts) != 5:
continue
text, risk_level, category, coords_str, necessity = parts
# 解析坐标 JSON
coords_match = re.search(r"\{.*?\}", coords_str)
if not coords_match:
continue
try:
coords = json.loads(coords_match.group())
except Exception:
continue
required_keys = {"x1", "y1", "x2", "y2"}
if not required_keys.issubset(coords):
continue
items.append(
{
"text": text,
"risk_level": risk_level,
"category": category,
"coordinates": coords,
"necessary": necessity == "necessary",
}
)
return items
def convert_normalized_coords_to_pixels(coords: dict, width: int, height: int):
"""
Convert 0–1000 normalized coordinates to integer pixel coordinates.
"""
x1 = coords["x1"] * width / 1000
y1 = coords["y1"] * height / 1000
x2 = coords["x2"] * width / 1000
y2 = coords["y2"] * height / 1000
return int(x1), int(y1), int(x2), int(y2)
RISK_COLORS = {
"high": "red",
"medium": "orange",
"low": "yellow",
"none": "green",
}
RISK_MAP_ZH = {
"high": "High risk",
"medium": "Medium risk",
"low": "Low risk",
"none": "No risk",
}
def draw_boxes_on_image(image_path: str, items, output_path: str):
"""Draw bounding boxes on the image and save to output_path."""
img = Image.open(image_path)
draw = ImageDraw.Draw(img)
width, height = img.size
for item in items:
coords = item["coordinates"]
x1, y1, x2, y2 = convert_normalized_coords_to_pixels(coords, width, height)
color = RISK_COLORS.get(item["risk_level"], "green")
draw.rectangle([x1, y1, x2, y2], outline=color, width=3)
img.save(output_path)
print(f"Annotated image saved: {output_path}")
def convert_to_json_format(folder_name: str, image_files, all_results):
"""Convert recognition results to JSON structure."""
batch_id = os.path.basename(folder_name)
json_data = []
for idx, (image_file, items) in enumerate(zip(image_files, all_results)):
img = Image.open(image_file)
width, height = img.size
labels = []
for label_id, item in enumerate(items, start=1):
coords = item["coordinates"]
x1, y1, x2, y2 = convert_normalized_coords_to_pixels(coords, width, height)
category = item["category"]
category_num = category.split("(")[0] if "(" in category else "-"
labels.append(
{
"id": label_id,
"risk": RISK_MAP_ZH.get(item["risk_level"], "unknown"),
"category": category_num,
"points": [x1, y1, x2, y2],
"text": item["text"],
"necessary": item["necessary"],
}
)
json_data.append(
{
"batchId": batch_id,
"index": idx,
"file": os.path.basename(image_file),
"width": width,
"height": height,
"labels": labels,
}
)
return json_data
def process_folder(folder_path: str, model_name: str):
"""Process a single task folder."""
print(f"\nProcessing folder: {folder_path}")
# Read goal
task_result_file = os.path.join(folder_path, "task_result.json")
with open(task_result_file, "r", encoding="utf-8") as f:
task_result = json.load(f)
goal = str(task_result.get("goal", "")).strip()
# Read manager.response sequence
traj_file = os.path.join(folder_path, "traj.jsonl")
plans = []
with open(traj_file, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
data = json.loads(line)
manager = data.get("manager")
if isinstance(manager, dict) and manager.get("response"):
plans.append(manager["response"])
# Image list
image_files = sorted(glob.glob(os.path.join(folder_path, "images", "*.png")))
# Align counts
count = min(len(image_files), len(plans))
image_files = image_files[:count]
plans = plans[:count]
print(f"Processing {len(image_files)} images")
# Output folder: annotations/<model_name>/
output_folder = os.path.join(
folder_path, "annotations", model_name.replace("/", "_")
)
os.makedirs(output_folder, exist_ok=True)
prompt_template = get_prompt_template()
all_results = []
for idx, (image_file, plan) in enumerate(zip(image_files, plans)):
print(f"\nProcessing image {idx + 1}/{len(image_files)}: {os.path.basename(image_file)}")
prompt = prompt_template.format(goal=goal, response=plan)
ai_output, _ = call_vlm_api(image_file, prompt, model_name)
items = parse_ai_output(ai_output)
all_results.append(items)
# Draw annotations on the image
output_image = os.path.join(
output_folder,
os.path.basename(image_file).replace(".png", "_annotated.png"),
)
draw_boxes_on_image(image_file, items, output_image)
# Generate JSON results
json_output = convert_to_json_format(folder_path, image_files, all_results)
json_file = os.path.join(output_folder, "ai_results.json")
with open(json_file, "w", encoding="utf-8") as f:
json.dump(json_output, f, indent=2, ensure_ascii=False)
print(f"\nJSON results saved: {json_file}")
print(f"Finished processing folder: {folder_path}")
def main():
parser = argparse.ArgumentParser(description="PC privacy detection tool")
parser.add_argument("folder", type=str, help="Input folder path")
parser.add_argument(
"--model",
"-m",
type=str,
default="google/gemini-3-pro-preview",
help="Model name",
)
args = parser.parse_args()
process_folder(args.folder, args.model)
if __name__ == "__main__":
main()