Dave67350 commited on
Commit
3c8424f
·
verified ·
1 Parent(s): dd97689

Create nis2_disaster_recovery_plan.py

Browse files
Files changed (1) hide show
  1. tools/nis2_disaster_recovery_plan.py +125 -0
tools/nis2_disaster_recovery_plan.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 Function ===
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"disaster_recovery_plan_{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 = "Disaster Recovery Plan (NIS2)"
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
+ 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 = 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, 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, val = 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, val)
55
+ else:
56
+ pdf.multi_cell(0, 10, line)
57
+
58
+ pdf.output(output_path)
59
+ return output_path
60
+
61
+ # === Questions ===
62
+ QUESTIONS = prepend_metadata_questions([
63
+ ("plan_scope", "What is the scope of the disaster recovery plan?"),
64
+ ("critical_assets", "Which assets/systems are considered critical?"),
65
+ ("recovery_time", "What is the Recovery Time Objective (RTO)?"),
66
+ ("recovery_point", "What is the Recovery Point Objective (RPO)?"),
67
+ ("roles_responsibilities", "Who is responsible for executing the plan?"),
68
+ ("testing_schedule", "How and when is the plan tested?"),
69
+ ("communication_protocol", "What is the communication protocol during incidents?"),
70
+ ("external_contacts", "List external partners or vendors involved."),
71
+ ("last_updated", "When was the plan last reviewed or updated?")
72
+ ])
73
+
74
+ def get_questions():
75
+ return QUESTIONS
76
+
77
+ # === Tool Runner ===
78
+ def run_tool():
79
+ state = {"step": 0, "answers": {}}
80
+
81
+ def step_by_step_agent(user_input, state):
82
+ step = state["step"]
83
+ answers = state["answers"]
84
+
85
+ if step > 0:
86
+ key, _ = QUESTIONS[step - 1]
87
+ answers[key] = user_input
88
+
89
+ if step < len(QUESTIONS):
90
+ next_q = QUESTIONS[step][1]
91
+ state["step"] += 1
92
+ return next_q, state, None
93
+
94
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
95
+ lang = detect(content)
96
+ metadata = {
97
+ "organization_name": answers.get("organization_name", "N/A"),
98
+ "user_name": answers.get("user_name", "N/A"),
99
+ "user_role": answers.get("user_role", "N/A"),
100
+ "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
101
+ }
102
+ pdf_path = export_text_to_pdf(content, metadata=metadata, language=lang)
103
+ return "✅ Disaster Recovery Plan completed. Download your PDF below.", {"done": True}, pdf_path
104
+
105
+ with gr.Blocks(title="Disaster Recovery Plan Tool") as demo:
106
+ chatbot = gr.Chatbot(label="🆘 Disaster Recovery Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
107
+ msg = gr.Textbox(label="Your answer")
108
+ state_var = gr.State(state)
109
+ file_output = gr.File(label="Download PDF")
110
+ reset_btn = gr.Button("🔁 Restart")
111
+
112
+ def chat_logic(msg_in, state_in):
113
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
114
+ messages = [{"role": "user", "content": msg_in}]
115
+ if reply:
116
+ messages.append({"role": "assistant", "content": reply})
117
+ return messages, updated_state, file
118
+
119
+ def reset():
120
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
121
+
122
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
123
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
124
+
125
+ demo.launch(show_api=False)