Spaces:
Sleeping
Sleeping
Update tools/risk_management_plan.py
Browse files
tools/risk_management_plan.py
CHANGED
|
@@ -1,12 +1,14 @@
|
|
| 1 |
-
#
|
|
|
|
| 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"
|
|
@@ -15,12 +17,23 @@ def export_text_to_pdf(text, output_path=None, language="en"):
|
|
| 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'):
|
|
@@ -46,11 +59,12 @@ def export_text_to_pdf(text, output_path=None, language="en"):
|
|
| 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 |
-
|
| 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."),
|
|
@@ -61,6 +75,8 @@ QUESTIONS = [
|
|
| 61 |
("responsibility", "Who is responsible for risk management actions?")
|
| 62 |
]
|
| 63 |
|
|
|
|
|
|
|
| 64 |
def get_questions():
|
| 65 |
return QUESTIONS
|
| 66 |
|
|
@@ -80,12 +96,21 @@ def run_tool():
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
|
|
| 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 # ✅ Reuse 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"risk_management_plan_{timestamp}.pdf"
|
|
|
|
| 17 |
pdf.add_page()
|
| 18 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 19 |
|
| 20 |
+
# Title
|
| 21 |
pdf.set_font("Arial", 'B', 16)
|
| 22 |
pdf.set_text_color(0, 51, 102)
|
| 23 |
title = "Risk Management Plan - AI Act (Annex VII)" if language == "en" else "Plan de Gestion des Risques - AI Act"
|
| 24 |
pdf.cell(0, 15, title, ln=True, align='C')
|
| 25 |
pdf.ln(10)
|
| 26 |
|
| 27 |
+
# Metadata block
|
| 28 |
+
if metadata:
|
| 29 |
+
pdf.set_font("Arial", '', 12)
|
| 30 |
+
pdf.set_text_color(90, 90, 90)
|
| 31 |
+
pdf.multi_cell(0, 10, f"Organization: {metadata.get('organization', 'N/A')}")
|
| 32 |
+
pdf.multi_cell(0, 10, f"Completed by: {metadata.get('completed_by', 'N/A')} ({metadata.get('role', 'N/A')})")
|
| 33 |
+
pdf.multi_cell(0, 10, f"Timestamp: {metadata.get('timestamp', 'N/A')}")
|
| 34 |
+
pdf.ln(5)
|
| 35 |
+
|
| 36 |
+
# Body
|
| 37 |
pdf.set_font("Arial", '', 12)
|
| 38 |
pdf.set_text_color(0, 0, 0)
|
| 39 |
for line in text.strip().split('\n'):
|
|
|
|
| 59 |
pdf.ln(5)
|
| 60 |
else:
|
| 61 |
pdf.multi_cell(0, 10, line)
|
| 62 |
+
|
| 63 |
pdf.output(output_path)
|
| 64 |
return output_path
|
| 65 |
|
| 66 |
+
# === Core Questions ===
|
| 67 |
+
BASE_QUESTIONS = [
|
| 68 |
("system_name", "What is the name of the AI system?"),
|
| 69 |
("risk_identification", "How are risks identified throughout development and use?"),
|
| 70 |
("risk_assessment", "Describe your risk assessment methodology."),
|
|
|
|
| 75 |
("responsibility", "Who is responsible for risk management actions?")
|
| 76 |
]
|
| 77 |
|
| 78 |
+
QUESTIONS = prepend_metadata_questions(BASE_QUESTIONS)
|
| 79 |
+
|
| 80 |
def get_questions():
|
| 81 |
return QUESTIONS
|
| 82 |
|
|
|
|
| 96 |
state["step"] += 1
|
| 97 |
return next_question, state, None
|
| 98 |
|
| 99 |
+
# Final document content
|
| 100 |
content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
|
| 101 |
detected_lang = detect(content)
|
| 102 |
+
|
| 103 |
+
metadata = {
|
| 104 |
+
"organization": answers.get("organization_name", "N/A"),
|
| 105 |
+
"completed_by": answers.get("user_name", "N/A"),
|
| 106 |
+
"role": answers.get("user_role", "N/A"),
|
| 107 |
+
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
pdf_path = export_text_to_pdf(content, metadata=metadata, language=detected_lang)
|
| 111 |
return "✅ Risk Management Plan completed. Download your PDF below.", {"done": True}, pdf_path
|
| 112 |
|
| 113 |
+
with gr.Blocks(title="Risk Management Tool") as demo:
|
| 114 |
chatbot = gr.Chatbot(label="🛡️ Risk Management Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
|
| 115 |
msg = gr.Textbox(label="Your answer")
|
| 116 |
state_var = gr.State(state)
|