Dave67350 commited on
Commit
a43b019
·
verified ·
1 Parent(s): 30f2036

Create dora_risk_management_policy.py

Browse files
Files changed (1) hide show
  1. tools/dora_risk_management_policy.py +127 -0
tools/dora_risk_management_policy.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tools/dora_risk_management_policy.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"dora_risk_management_policy_{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 = "ICT Risk Management Policy - DORA" if language == "en" else "Politique de Gestion des Risques TIC - DORA"
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_name', 'N/A')}")
29
+ pdf.multi_cell(0, 10, f"Completed by: {metadata.get('user_name', 'N/A')} ({metadata.get('user_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
+ elif line == "---":
54
+ pdf.line(10, pdf.get_y(), 200, pdf.get_y())
55
+ pdf.ln(5)
56
+ else:
57
+ pdf.multi_cell(0, 10, line)
58
+ pdf.output(output_path)
59
+ return output_path
60
+
61
+ # === Questions ===
62
+ QUESTIONS = prepend_metadata_questions([
63
+ ("policy_scope", "What areas of ICT risk are covered by this policy?"),
64
+ ("objectives", "What are the main objectives of the policy?"),
65
+ ("risk_appetite", "Describe your organization's ICT risk appetite."),
66
+ ("responsibilities", "Who is responsible for ICT risk management?"),
67
+ ("assessment_process", "How are ICT risks identified and assessed?"),
68
+ ("mitigation_strategy", "What mitigation strategies are in place?"),
69
+ ("monitoring_mechanisms", "How is risk monitored on an ongoing basis?"),
70
+ ("review_frequency", "How often is this policy reviewed or updated?"),
71
+ ("compliance_links", "How does this policy align with legal/regulatory obligations?")
72
+ ])
73
+
74
+
75
+ def get_questions():
76
+ return QUESTIONS
77
+
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_question = QUESTIONS[step][1]
92
+ state["step"] += 1
93
+ return next_question, state, None
94
+
95
+ metadata = {
96
+ "user_name": answers.get("user_name", "N/A"),
97
+ "user_role": answers.get("user_role", "N/A"),
98
+ "organization_name": answers.get("organization_name", "N/A"),
99
+ "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
100
+ }
101
+
102
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
103
+ lang = detect(content) if len(content.strip()) > 3 else "en"
104
+ pdf_path = export_text_to_pdf(content, metadata=metadata, language=lang)
105
+ return "✅ Policy complete. Download your PDF below.", {"done": True}, pdf_path
106
+
107
+ with gr.Blocks(title="DORA - ICT Risk Management Policy") as demo:
108
+ chatbot = gr.Chatbot(label="📋 DORA Policy Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
109
+ msg = gr.Textbox(label="Your answer")
110
+ state_var = gr.State(state)
111
+ file_output = gr.File(label="Download PDF")
112
+ reset_btn = gr.Button("🔁 Restart")
113
+
114
+ def chat_logic(msg_in, state_in):
115
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
116
+ messages = [{"role": "user", "content": msg_in}]
117
+ if reply:
118
+ messages.append({"role": "assistant", "content": reply})
119
+ return messages, updated_state, file
120
+
121
+ def reset():
122
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
123
+
124
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
125
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
126
+
127
+ demo.launch(show_api=False)