Spaces:
Sleeping
Sleeping
Create risk_management_plan.py
Browse files- tools/risk_management_plan.py +108 -0
tools/risk_management_plan.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# tools/risk_management_plan.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, 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"risk_management_plan_{timestamp}.pdf"
|
| 13 |
+
|
| 14 |
+
pdf = FPDF()
|
| 15 |
+
pdf.add_page()
|
| 16 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 17 |
+
|
| 18 |
+
pdf.set_font("Arial", 'B', 16)
|
| 19 |
+
pdf.set_text_color(0, 51, 102)
|
| 20 |
+
title = "Risk Management Plan - AI Act (Annex VII)" if language == "en" else "Plan de Gestion des Risques - AI Act"
|
| 21 |
+
pdf.cell(0, 15, title, ln=True, align='C')
|
| 22 |
+
pdf.ln(10)
|
| 23 |
+
|
| 24 |
+
pdf.set_font("Arial", '', 12)
|
| 25 |
+
pdf.set_text_color(0, 0, 0)
|
| 26 |
+
for line in text.strip().split('\n'):
|
| 27 |
+
line = line.strip()
|
| 28 |
+
if line.startswith("## "):
|
| 29 |
+
section = line.replace("## ", "").strip()
|
| 30 |
+
pdf.set_font("Arial", 'B', 13)
|
| 31 |
+
pdf.set_text_color(30, 30, 120)
|
| 32 |
+
pdf.ln(8)
|
| 33 |
+
pdf.cell(0, 10, section, ln=True)
|
| 34 |
+
pdf.set_font("Arial", '', 12)
|
| 35 |
+
pdf.set_text_color(0, 0, 0)
|
| 36 |
+
elif line.startswith("- **"):
|
| 37 |
+
match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
|
| 38 |
+
if match:
|
| 39 |
+
label, value = match.groups()
|
| 40 |
+
pdf.set_font("Arial", 'B', 12)
|
| 41 |
+
pdf.cell(0, 10, f"{label}:", ln=True)
|
| 42 |
+
pdf.set_font("Arial", '', 12)
|
| 43 |
+
pdf.multi_cell(0, 10, value)
|
| 44 |
+
elif line == "---":
|
| 45 |
+
pdf.line(10, pdf.get_y(), 200, pdf.get_y())
|
| 46 |
+
pdf.ln(5)
|
| 47 |
+
else:
|
| 48 |
+
pdf.multi_cell(0, 10, line)
|
| 49 |
+
pdf.output(output_path)
|
| 50 |
+
return output_path
|
| 51 |
+
|
| 52 |
+
# === Questions ===
|
| 53 |
+
QUESTIONS = [
|
| 54 |
+
("system_name", "What is the name of the AI system?"),
|
| 55 |
+
("risk_identification", "How are risks identified throughout development and use?"),
|
| 56 |
+
("risk_assessment", "Describe your risk assessment methodology."),
|
| 57 |
+
("risk_mitigation", "What risk mitigation techniques are applied?"),
|
| 58 |
+
("lifecycle_management", "How are risks managed across the system lifecycle?"),
|
| 59 |
+
("incident_response", "What procedures are in place for incident handling?"),
|
| 60 |
+
("monitoring_measures", "How is ongoing risk monitored post-deployment?"),
|
| 61 |
+
("responsibility", "Who is responsible for risk management actions?")
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
def get_questions():
|
| 65 |
+
return QUESTIONS
|
| 66 |
+
|
| 67 |
+
def run_tool():
|
| 68 |
+
state = {"step": 0, "answers": {}}
|
| 69 |
+
|
| 70 |
+
def step_by_step_agent(user_input, state):
|
| 71 |
+
step = state["step"]
|
| 72 |
+
answers = state["answers"]
|
| 73 |
+
|
| 74 |
+
if step > 0:
|
| 75 |
+
key, _ = QUESTIONS[step - 1]
|
| 76 |
+
answers[key] = user_input
|
| 77 |
+
|
| 78 |
+
if step < len(QUESTIONS):
|
| 79 |
+
next_question = QUESTIONS[step][1]
|
| 80 |
+
state["step"] += 1
|
| 81 |
+
return next_question, state, None
|
| 82 |
+
|
| 83 |
+
content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
|
| 84 |
+
detected_lang = detect(content)
|
| 85 |
+
pdf_path = export_text_to_pdf(content, language=detected_lang)
|
| 86 |
+
return "✅ Risk Management Plan completed. Download your PDF below.", {"done": True}, pdf_path
|
| 87 |
+
|
| 88 |
+
with gr.Blocks() as demo:
|
| 89 |
+
chatbot = gr.Chatbot(label="🛡️ Risk Management Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
|
| 90 |
+
msg = gr.Textbox(label="Your answer")
|
| 91 |
+
state_var = gr.State(state)
|
| 92 |
+
file_output = gr.File(label="Download PDF")
|
| 93 |
+
reset_btn = gr.Button("🔁 Restart")
|
| 94 |
+
|
| 95 |
+
def chat_logic(msg_in, state_in):
|
| 96 |
+
reply, updated_state, file = step_by_step_agent(msg_in, state_in)
|
| 97 |
+
messages = [{"role": "user", "content": msg_in}]
|
| 98 |
+
if reply:
|
| 99 |
+
messages.append({"role": "assistant", "content": reply})
|
| 100 |
+
return messages, updated_state, file
|
| 101 |
+
|
| 102 |
+
def reset():
|
| 103 |
+
return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
|
| 104 |
+
|
| 105 |
+
msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
|
| 106 |
+
reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
|
| 107 |
+
|
| 108 |
+
demo.launch()
|