Dave67350 commited on
Commit
b831aaa
·
verified ·
1 Parent(s): 30d0686

Create gdpr_data_record.py

Browse files
Files changed (1) hide show
  1. tools/gdpr_data_record.py +130 -0
tools/gdpr_data_record.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tools/gdpr_data_record.py
2
+ import datetime
3
+ import re
4
+ from fpdf import FPDF
5
+ from langdetect import detect
6
+ import gradio as gr
7
+
8
+ # === PDF Export Function ===
9
+ def export_text_to_pdf(text, metadata=None, output_path=None, language="en"):
10
+ if output_path is None:
11
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
12
+ output_path = f"gdpr_data_processing_record_{timestamp}.pdf"
13
+
14
+ pdf = FPDF()
15
+ pdf.add_page()
16
+ pdf.set_auto_page_break(auto=True, margin=15)
17
+
18
+ # Title
19
+ pdf.set_font("Arial", 'B', 16)
20
+ pdf.set_text_color(0, 51, 102)
21
+ title = "GDPR Data Processing Record" if language == "en" else "Registre de Traitement des Données (RGPD)"
22
+ pdf.cell(0, 15, title, ln=True, align='C')
23
+ pdf.ln(10)
24
+
25
+ # Metadata
26
+ if metadata:
27
+ pdf.set_font("Arial", '', 12)
28
+ pdf.set_text_color(90, 90, 90)
29
+ pdf.multi_cell(0, 10, f"Organization: {metadata.get('organization', 'N/A')}")
30
+ pdf.multi_cell(0, 10, f"Completed by: {metadata.get('completed_by', 'N/A')} ({metadata.get('role', 'N/A')})")
31
+ pdf.multi_cell(0, 10, f"Timestamp: {metadata.get('timestamp', 'N/A')}")
32
+ pdf.ln(5)
33
+
34
+ # Content
35
+ pdf.set_font("Arial", '', 12)
36
+ pdf.set_text_color(0, 0, 0)
37
+ for line in text.strip().split('\n'):
38
+ line = line.strip()
39
+ if line.startswith("## "):
40
+ section_title = line.replace("## ", "").strip()
41
+ pdf.set_font("Arial", 'B', 13)
42
+ pdf.set_text_color(30, 30, 120)
43
+ pdf.ln(8)
44
+ pdf.cell(0, 10, section_title, ln=True)
45
+ pdf.set_font("Arial", '', 12)
46
+ pdf.set_text_color(0, 0, 0)
47
+ elif line.startswith("- **"):
48
+ match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
49
+ if match:
50
+ label, value = match.groups()
51
+ pdf.set_font("Arial", 'B', 12)
52
+ pdf.cell(0, 10, f"{label}:", ln=True)
53
+ pdf.set_font("Arial", '', 12)
54
+ pdf.multi_cell(0, 10, value)
55
+ else:
56
+ pdf.multi_cell(0, 10, line)
57
+
58
+ pdf.output(output_path)
59
+ return output_path
60
+
61
+ # === Questions (with metadata) ===
62
+ QUESTIONS = [
63
+ ("organization", "What is the name of your organization?"),
64
+ ("completed_by", "What is your full name?"),
65
+ ("role", "What is your role in the organization?"),
66
+ ("purpose", "What is the purpose of the data processing activity?"),
67
+ ("data_categories", "What categories of personal data are processed?"),
68
+ ("data_subjects", "What types of data subjects are affected?"),
69
+ ("recipients", "Who receives or processes the data?"),
70
+ ("transfers", "Are there any international data transfers involved?"),
71
+ ("retention", "What is the data retention period?"),
72
+ ("security", "What security measures are in place?"),
73
+ ("dpo", "Who is the Data Protection Officer (if any)?"),
74
+ ]
75
+
76
+ def get_questions():
77
+ return QUESTIONS
78
+
79
+ def run_tool():
80
+ state = {"step": 0, "answers": {}}
81
+
82
+ def step_by_step_agent(user_input, state):
83
+ step = state["step"]
84
+ answers = state["answers"]
85
+
86
+ if step > 0:
87
+ key, _ = QUESTIONS[step - 1]
88
+ answers[key] = user_input
89
+
90
+ if step < len(QUESTIONS):
91
+ next_q = QUESTIONS[step][1]
92
+ state["step"] += 1
93
+ return next_q, state, None
94
+
95
+ # Compile content and metadata
96
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
97
+ detected_lang = detect(content)
98
+
99
+ metadata = {
100
+ "organization": answers.get("organization", "N/A"),
101
+ "completed_by": answers.get("completed_by", "N/A"),
102
+ "role": answers.get("role", "N/A"),
103
+ "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
104
+ }
105
+
106
+ pdf_path = export_text_to_pdf(content, metadata=metadata, language=detected_lang)
107
+ return "✅ Record completed. Download your GDPR data processing record below.", {"done": True}, pdf_path
108
+
109
+ # === Gradio UI ===
110
+ with gr.Blocks(title="GDPR Data Processing Record Tool") as demo:
111
+ chatbot = gr.Chatbot(label="🔐 GDPR Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
112
+ msg = gr.Textbox(label="Your answer")
113
+ state_var = gr.State(state)
114
+ file_output = gr.File(label="Download PDF", visible=True)
115
+ reset_btn = gr.Button("🔁 Restart")
116
+
117
+ def chat_logic(msg_in, state_in):
118
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
119
+ messages = [{"role": "user", "content": msg_in}]
120
+ if reply:
121
+ messages.append({"role": "assistant", "content": reply})
122
+ return messages, updated_state, file
123
+
124
+ def reset():
125
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
126
+
127
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
128
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
129
+
130
+ demo.launch(show_api=False)