Spaces:
Sleeping
Sleeping
Update tools/high_risk_ai_register.py
Browse files- tools/high_risk_ai_register.py +30 -17
tools/high_risk_ai_register.py
CHANGED
|
@@ -8,8 +8,10 @@ from fpdf import FPDF
|
|
| 8 |
import gradio as gr
|
| 9 |
from langdetect import detect
|
| 10 |
|
|
|
|
|
|
|
| 11 |
# === PDF Export Function ===
|
| 12 |
-
def export_text_to_pdf(text, output_path=None, language="en"):
|
| 13 |
if output_path is None:
|
| 14 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 15 |
output_path = f"high_risk_ai_summary_{timestamp}.pdf"
|
|
@@ -25,17 +27,18 @@ def export_text_to_pdf(text, output_path=None, language="en"):
|
|
| 25 |
pdf.cell(0, 15, title, ln=True, align='C')
|
| 26 |
pdf.ln(10)
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
# Content
|
| 36 |
pdf.set_font("Arial", '', 12)
|
| 37 |
pdf.set_text_color(0, 0, 0)
|
| 38 |
-
|
| 39 |
for line in text.strip().split('\n'):
|
| 40 |
line = line.strip()
|
| 41 |
if line.startswith("## "):
|
|
@@ -62,8 +65,8 @@ def export_text_to_pdf(text, output_path=None, language="en"):
|
|
| 62 |
pdf.output(output_path)
|
| 63 |
return output_path
|
| 64 |
|
| 65 |
-
# === Questions
|
| 66 |
-
|
| 67 |
("system_name", "What is the name of your AI system?"),
|
| 68 |
("system_purpose", "What is its intended purpose?"),
|
| 69 |
("category", "Which Annex III category does it fall under?"),
|
|
@@ -73,10 +76,12 @@ QUESTIONS = [
|
|
| 73 |
("output", "What actions does it perform?"),
|
| 74 |
("dependencies", "List any critical dependencies (e.g., APIs, models)."),
|
| 75 |
("context", "What is the intended deployment environment?"),
|
| 76 |
-
("justification", "Why is it high-risk under the AI Act?")
|
| 77 |
]
|
| 78 |
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
def step_by_step(user_input, state):
|
| 81 |
if state is None:
|
| 82 |
state = {"step": 0, "answers": {}}
|
|
@@ -93,9 +98,10 @@ def step_by_step(user_input, state):
|
|
| 93 |
state["step"] += 1
|
| 94 |
return next_q, state, None
|
| 95 |
|
| 96 |
-
# === Format
|
| 97 |
content = f"""
|
| 98 |
# High-Risk AI System Summary
|
|
|
|
| 99 |
## General Info
|
| 100 |
- **System Name**: {answers.get('system_name', '')}
|
| 101 |
- **Purpose**: {answers.get('system_purpose', '')}
|
|
@@ -111,14 +117,21 @@ def step_by_step(user_input, state):
|
|
| 111 |
|
| 112 |
## Risk Classification
|
| 113 |
- **Justification for High-Risk**: {answers.get('justification', '')}
|
|
|
|
| 114 |
---
|
| 115 |
Generated by AI Act Assistant.
|
| 116 |
"""
|
| 117 |
-
|
| 118 |
lang = detect(content)
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
return
|
| 122 |
|
| 123 |
# === Gradio UI ===
|
| 124 |
def launch_ui():
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
from langdetect import detect
|
| 10 |
|
| 11 |
+
from tools.common import prepend_metadata_questions # ✅ Add common metadata helper
|
| 12 |
+
|
| 13 |
# === PDF Export Function ===
|
| 14 |
+
def export_text_to_pdf(text, metadata=None, output_path=None, language="en"):
|
| 15 |
if output_path is None:
|
| 16 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 17 |
output_path = f"high_risk_ai_summary_{timestamp}.pdf"
|
|
|
|
| 27 |
pdf.cell(0, 15, title, ln=True, align='C')
|
| 28 |
pdf.ln(10)
|
| 29 |
|
| 30 |
+
# Metadata
|
| 31 |
+
if metadata:
|
| 32 |
+
pdf.set_font("Arial", '', 12)
|
| 33 |
+
pdf.set_text_color(90, 90, 90)
|
| 34 |
+
pdf.multi_cell(0, 10, f"Organization: {metadata.get('organization', 'N/A')}")
|
| 35 |
+
pdf.multi_cell(0, 10, f"Completed by: {metadata.get('completed_by', 'N/A')} ({metadata.get('role', 'N/A')})")
|
| 36 |
+
pdf.multi_cell(0, 10, f"Timestamp: {metadata.get('timestamp', 'N/A')}")
|
| 37 |
+
pdf.ln(5)
|
| 38 |
|
| 39 |
+
# Content
|
| 40 |
pdf.set_font("Arial", '', 12)
|
| 41 |
pdf.set_text_color(0, 0, 0)
|
|
|
|
| 42 |
for line in text.strip().split('\n'):
|
| 43 |
line = line.strip()
|
| 44 |
if line.startswith("## "):
|
|
|
|
| 65 |
pdf.output(output_path)
|
| 66 |
return output_path
|
| 67 |
|
| 68 |
+
# === Questions (core, then prepend metadata) ===
|
| 69 |
+
BASE_QUESTIONS = [
|
| 70 |
("system_name", "What is the name of your AI system?"),
|
| 71 |
("system_purpose", "What is its intended purpose?"),
|
| 72 |
("category", "Which Annex III category does it fall under?"),
|
|
|
|
| 76 |
("output", "What actions does it perform?"),
|
| 77 |
("dependencies", "List any critical dependencies (e.g., APIs, models)."),
|
| 78 |
("context", "What is the intended deployment environment?"),
|
| 79 |
+
("justification", "Why is it high-risk under the AI Act?")
|
| 80 |
]
|
| 81 |
|
| 82 |
+
QUESTIONS = prepend_metadata_questions(BASE_QUESTIONS)
|
| 83 |
+
|
| 84 |
+
# === Step-by-step Conversation ===
|
| 85 |
def step_by_step(user_input, state):
|
| 86 |
if state is None:
|
| 87 |
state = {"step": 0, "answers": {}}
|
|
|
|
| 98 |
state["step"] += 1
|
| 99 |
return next_q, state, None
|
| 100 |
|
| 101 |
+
# === Format content ===
|
| 102 |
content = f"""
|
| 103 |
# High-Risk AI System Summary
|
| 104 |
+
|
| 105 |
## General Info
|
| 106 |
- **System Name**: {answers.get('system_name', '')}
|
| 107 |
- **Purpose**: {answers.get('system_purpose', '')}
|
|
|
|
| 117 |
|
| 118 |
## Risk Classification
|
| 119 |
- **Justification for High-Risk**: {answers.get('justification', '')}
|
| 120 |
+
|
| 121 |
---
|
| 122 |
Generated by AI Act Assistant.
|
| 123 |
"""
|
|
|
|
| 124 |
lang = detect(content)
|
| 125 |
+
metadata = {
|
| 126 |
+
"organization": answers.get("organization_name", "N/A"),
|
| 127 |
+
"completed_by": answers.get("user_name", "N/A"),
|
| 128 |
+
"role": answers.get("user_role", "N/A"),
|
| 129 |
+
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
pdf_path = export_text_to_pdf(content, metadata=metadata, language=lang)
|
| 133 |
|
| 134 |
+
return "✅ All data collected!\n📝 Your summary is ready. Click below to download your PDF.", {"done": True}, pdf_path
|
| 135 |
|
| 136 |
# === Gradio UI ===
|
| 137 |
def launch_ui():
|