Dave67350 commited on
Commit
72e320d
·
verified ·
1 Parent(s): 3f4ba42

Create gdpr_dpia.py

Browse files
Files changed (1) hide show
  1. tools/gdpr_dpia.py +121 -0
tools/gdpr_dpia.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tools/gdpr_dpia_template.py
2
+ import datetime
3
+ import re
4
+ from fpdf import FPDF
5
+ from langdetect import detect
6
+ import gradio as gr
7
+ from tools.common import prepend_metadata_questions
8
+
9
+ # === PDF Export Function ===
10
+ def export_text_to_pdf(text, metadata=None, output_path=None, language="en"):
11
+ if output_path is None:
12
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
13
+ output_path = f"gdpr_dpia_{timestamp}.pdf"
14
+
15
+ pdf = FPDF()
16
+ pdf.add_page()
17
+ pdf.set_auto_page_break(auto=True, margin=15)
18
+
19
+ pdf.set_font("Arial", 'B', 16)
20
+ pdf.set_text_color(0, 51, 102)
21
+ title = "Data Protection Impact Assessment (DPIA)" if language == "en" else "Analyse d'Impact sur la Protection des Données (AIPD)"
22
+ pdf.cell(0, 15, title, ln=True, align='C')
23
+ pdf.ln(10)
24
+
25
+ if metadata:
26
+ pdf.set_font("Arial", '', 12)
27
+ pdf.set_text_color(90, 90, 90)
28
+ pdf.multi_cell(0, 10, f"Organization: {metadata.get('organization', 'N/A')}")
29
+ pdf.multi_cell(0, 10, f"Completed by: {metadata.get('completed_by', 'N/A')} ({metadata.get('role', 'N/A')})")
30
+ pdf.multi_cell(0, 10, f"Timestamp: {metadata.get('timestamp', 'N/A')}")
31
+ pdf.ln(5)
32
+
33
+ pdf.set_font("Arial", '', 12)
34
+ pdf.set_text_color(0, 0, 0)
35
+ for line in text.strip().split('\n'):
36
+ line = line.strip()
37
+ if line.startswith("## "):
38
+ section = line.replace("## ", "").strip()
39
+ pdf.set_font("Arial", 'B', 13)
40
+ pdf.set_text_color(30, 30, 120)
41
+ pdf.ln(8)
42
+ pdf.cell(0, 10, section, ln=True)
43
+ pdf.set_font("Arial", '', 12)
44
+ pdf.set_text_color(0, 0, 0)
45
+ elif line.startswith("- **"):
46
+ match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
47
+ if match:
48
+ label, value = match.groups()
49
+ pdf.set_font("Arial", 'B', 12)
50
+ pdf.cell(0, 10, f"{label}:", ln=True)
51
+ pdf.set_font("Arial", '', 12)
52
+ pdf.multi_cell(0, 10, value)
53
+ else:
54
+ pdf.multi_cell(0, 10, line)
55
+
56
+ pdf.output(output_path)
57
+ return output_path
58
+
59
+ # === DPIA Questions ===
60
+ BASE_QUESTIONS = [
61
+ ("processing_description", "Describe the processing activity and purpose."),
62
+ ("necessity_proportionality", "Why is the processing necessary and proportionate?"),
63
+ ("risks", "What are the data protection risks?"),
64
+ ("measures", "What safeguards are implemented to mitigate risks?"),
65
+ ("consultation", "Was the DPO or public consulted?"),
66
+ ("outcome", "Summary of the assessment's outcome.")
67
+ ]
68
+
69
+ QUESTIONS = prepend_metadata_questions(BASE_QUESTIONS)
70
+
71
+ def get_questions():
72
+ return QUESTIONS
73
+
74
+ def run_tool():
75
+ state = {"step": 0, "answers": {}}
76
+
77
+ def step_by_step_agent(user_input, state):
78
+ step = state["step"]
79
+ answers = state["answers"]
80
+
81
+ if step > 0:
82
+ key, _ = QUESTIONS[step - 1]
83
+ answers[key] = user_input
84
+
85
+ if step < len(QUESTIONS):
86
+ next_q = QUESTIONS[step][1]
87
+ state["step"] += 1
88
+ return next_q, state, None
89
+
90
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
91
+ lang = detect(content)
92
+ metadata = {
93
+ "organization": answers.get("organization", "N/A"),
94
+ "completed_by": answers.get("completed_by", "N/A"),
95
+ "role": answers.get("role", "N/A"),
96
+ "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
97
+ }
98
+ pdf_path = export_text_to_pdf(content, metadata=metadata, language=lang)
99
+ return "✅ DPIA completed. Download below:", {"done": True}, pdf_path
100
+
101
+ with gr.Blocks(title="GDPR DPIA Tool") as demo:
102
+ chatbot = gr.Chatbot(label="🔍 GDPR DPIA Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
103
+ msg = gr.Textbox(label="Your answer")
104
+ state_var = gr.State(state)
105
+ file_output = gr.File(label="Download PDF")
106
+ reset_btn = gr.Button("🔁 Restart")
107
+
108
+ def chat_logic(msg_in, state_in):
109
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
110
+ messages = [{"role": "user", "content": msg_in}]
111
+ if reply:
112
+ messages.append({"role": "assistant", "content": reply})
113
+ return messages, updated_state, file
114
+
115
+ def reset():
116
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
117
+
118
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
119
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
120
+
121
+ demo.launch(show_api=False)