Dave67350 commited on
Commit
ac087c2
·
verified ·
1 Parent(s): 4227e21

Create gdpr_dpa_log.py

Browse files
Files changed (1) hide show
  1. tools/gdpr_dpa_log.py +126 -0
tools/gdpr_dpa_log.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding=utf-8
3
+ import datetime
4
+ import re
5
+ from fpdf import FPDF
6
+ from langdetect import detect
7
+ import gradio as gr
8
+ from tools.common import prepend_metadata_questions
9
+
10
+ # === PDF Export ===
11
+ def export_text_to_pdf(text, metadata=None, output_path=None, language="en"):
12
+ if output_path is None:
13
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
14
+ output_path = f"data_processor_agreements_{timestamp}.pdf"
15
+
16
+ pdf = FPDF()
17
+ pdf.add_page()
18
+ pdf.set_auto_page_break(auto=True, margin=15)
19
+
20
+ pdf.set_font("Arial", 'B', 16)
21
+ pdf.set_text_color(0, 51, 102)
22
+ title = "Data Processor Agreements Log"
23
+ pdf.cell(0, 15, title, ln=True, align='C')
24
+ pdf.ln(10)
25
+
26
+ # Metadata
27
+ if metadata:
28
+ pdf.set_font("Arial", '', 12)
29
+ pdf.set_text_color(90, 90, 90)
30
+ pdf.multi_cell(0, 10, f"Organization: {metadata.get('organization_name', 'N/A')}")
31
+ pdf.multi_cell(0, 10, f"Completed by: {metadata.get('user_name', 'N/A')} ({metadata.get('user_role', 'N/A')})")
32
+ pdf.multi_cell(0, 10, f"Timestamp: {metadata.get('timestamp', 'N/A')}")
33
+ pdf.ln(5)
34
+
35
+ # Body
36
+ pdf.set_font("Arial", '', 12)
37
+ pdf.set_text_color(0, 0, 0)
38
+ for line in text.strip().split('\n'):
39
+ line = line.strip()
40
+ if line.startswith("## "):
41
+ section = line.replace("## ", "").strip()
42
+ pdf.set_font("Arial", 'B', 13)
43
+ pdf.set_text_color(30, 30, 120)
44
+ pdf.ln(8)
45
+ pdf.cell(0, 10, section, ln=True)
46
+ pdf.set_font("Arial", '', 12)
47
+ pdf.set_text_color(0, 0, 0)
48
+ elif line.startswith("- **"):
49
+ match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
50
+ if match:
51
+ label, value = match.groups()
52
+ pdf.set_font("Arial", 'B', 12)
53
+ pdf.cell(0, 10, f"{label}:", ln=True)
54
+ pdf.set_font("Arial", '', 12)
55
+ pdf.multi_cell(0, 10, value)
56
+ else:
57
+ pdf.multi_cell(0, 10, line)
58
+
59
+ pdf.output(output_path)
60
+ return output_path
61
+
62
+ # === Questions ===
63
+ QUESTIONS = prepend_metadata_questions([
64
+ ("processor_name", "What is the name of the data processor?"),
65
+ ("purpose", "What is the purpose of the data processing agreement?"),
66
+ ("data_types", "What categories of data are involved?"),
67
+ ("safeguards", "What safeguards are in place (e.g., encryption, access control)?"),
68
+ ("contract_date", "When was the agreement signed?"),
69
+ ("duration", "What is the duration of the agreement?"),
70
+ ("termination", "What are the termination conditions?")
71
+ ])
72
+
73
+ def get_questions():
74
+ return QUESTIONS
75
+
76
+ # === Run Tool ===
77
+ def run_tool():
78
+ state = {"step": 0, "answers": {}}
79
+
80
+ def step_by_step_agent(user_input, state):
81
+ step = state["step"]
82
+ answers = state["answers"]
83
+
84
+ if step > 0:
85
+ key, _ = QUESTIONS[step - 1]
86
+ answers[key] = user_input
87
+
88
+ if step < len(QUESTIONS):
89
+ next_question = QUESTIONS[step][1]
90
+ state["step"] += 1
91
+ return next_question, state, None
92
+
93
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
94
+ detected_lang = detect(content)
95
+
96
+ metadata = {
97
+ "user_name": answers.get("user_name", "N/A"),
98
+ "user_role": answers.get("user_role", "N/A"),
99
+ "organization_name": answers.get("organization_name", "N/A"),
100
+ "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
101
+ }
102
+
103
+ pdf_path = export_text_to_pdf(content, metadata=metadata, language=detected_lang)
104
+ return "✅ Agreement log completed. Download your PDF below.", {"done": True}, pdf_path
105
+
106
+ with gr.Blocks(title="Data Processor Agreement Tool") as demo:
107
+ chatbot = gr.Chatbot(label="📄 DPA Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
108
+ msg = gr.Textbox(label="Your answer")
109
+ state_var = gr.State(state)
110
+ file_output = gr.File(label="Download PDF")
111
+ reset_btn = gr.Button("🔁 Restart")
112
+
113
+ def chat_logic(msg_in, state_in):
114
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
115
+ messages = [{"role": "user", "content": msg_in}]
116
+ if reply:
117
+ messages.append({"role": "assistant", "content": reply})
118
+ return messages, updated_state, file
119
+
120
+ def reset():
121
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
122
+
123
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
124
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
125
+
126
+ demo.launch(show_api=False)