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

Create gdpr_breach_log.py

Browse files
Files changed (1) hide show
  1. tools/gdpr_breach_log.py +123 -0
tools/gdpr_breach_log.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tools/gdpr_breach_log.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_breach_log_{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 Breach Notification Log (GDPR)" if language == "en" else "Journal des Violations de 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_name', 'N/A')}")
30
+ pdf.multi_cell(0, 10, f"Completed by: {metadata.get('user_name', 'N/A')} ({metadata.get('user_role', 'N/A')})")
31
+ pdf.multi_cell(0, 10, f"Timestamp: {metadata.get('timestamp', 'N/A')}")
32
+ pdf.ln(5)
33
+
34
+ pdf.set_font("Arial", '', 12)
35
+ pdf.set_text_color(0, 0, 0)
36
+ for line in text.strip().split('\n'):
37
+ line = line.strip()
38
+ if line.startswith("## "):
39
+ section_title = line.replace("## ", "").strip()
40
+ pdf.set_font("Arial", 'B', 13)
41
+ pdf.set_text_color(30, 30, 120)
42
+ pdf.ln(8)
43
+ pdf.cell(0, 10, section_title, ln=True)
44
+ pdf.set_font("Arial", '', 12)
45
+ pdf.set_text_color(0, 0, 0)
46
+ elif line.startswith("- **"):
47
+ match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
48
+ if match:
49
+ label, value = match.groups()
50
+ pdf.set_font("Arial", 'B', 12)
51
+ pdf.cell(0, 10, f"{label}:", ln=True)
52
+ pdf.set_font("Arial", '', 12)
53
+ pdf.multi_cell(0, 10, value)
54
+ else:
55
+ pdf.multi_cell(0, 10, line)
56
+
57
+ pdf.output(output_path)
58
+ return output_path
59
+
60
+ QUESTIONS = prepend_metadata_questions([
61
+ ("breach_date", "When was the data breach detected?"),
62
+ ("breach_nature", "What is the nature of the breach? (e.g., unauthorized access, loss of data, etc.)"),
63
+ ("data_types", "What types of personal data were involved?"),
64
+ ("affected_subjects", "How many data subjects are affected?"),
65
+ ("risk_consequences", "What are the potential consequences or risks to the data subjects?"),
66
+ ("mitigation_measures", "What measures have been taken to mitigate the breach?"),
67
+ ("authority_notified", "Was the supervisory authority notified? If yes, when?"),
68
+ ("data_subjects_notified", "Were the data subjects informed? If yes, when and how?")
69
+ ])
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_question = QUESTIONS[step][1]
87
+ state["step"] += 1
88
+ return next_question, state, None
89
+
90
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
91
+ detected_lang = detect(content)
92
+
93
+ metadata = {
94
+ "organization_name": answers.get("organization_name", "N/A"),
95
+ "user_name": answers.get("user_name", "N/A"),
96
+ "user_role": answers.get("user_role", "N/A"),
97
+ "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
98
+ }
99
+
100
+ pdf_path = export_text_to_pdf(content, metadata=metadata, language=detected_lang)
101
+ return "✅ Breach log complete. Download your PDF below:", {"done": True}, pdf_path
102
+
103
+ with gr.Blocks(title="GDPR - Data Breach Log") as demo:
104
+ chatbot = gr.Chatbot(label="⚠️ Data Breach Log Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
105
+ msg = gr.Textbox(label="Your answer")
106
+ state_var = gr.State(state)
107
+ file_output = gr.File(label="Download PDF")
108
+ reset_btn = gr.Button("🔁 Restart")
109
+
110
+ def chat_logic(msg_in, state_in):
111
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
112
+ messages = [{"role": "user", "content": msg_in}]
113
+ if reply:
114
+ messages.append({"role": "assistant", "content": reply})
115
+ return messages, updated_state, file
116
+
117
+ def reset():
118
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
119
+
120
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
121
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
122
+
123
+ demo.launch(show_api=False)