Dave67350 commited on
Commit
653c010
·
verified ·
1 Parent(s): fafbb71

Update tools/human_oversight_strategy.py

Browse files
Files changed (1) hide show
  1. tools/human_oversight_strategy.py +42 -7
tools/human_oversight_strategy.py CHANGED
@@ -1,10 +1,15 @@
1
- # tools/human_oversight_strategy.py
2
- import datetime, re
 
 
3
  from fpdf import FPDF
4
  from langdetect import detect
5
  import gradio as gr
6
 
7
- def export_text_to_pdf(text, output_path=None, language="en"):
 
 
 
8
  if output_path is None:
9
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
10
  output_path = f"human_oversight_strategy_{timestamp}.pdf"
@@ -12,15 +17,29 @@ def export_text_to_pdf(text, output_path=None, language="en"):
12
  pdf = FPDF()
13
  pdf.add_page()
14
  pdf.set_auto_page_break(auto=True, margin=15)
 
 
15
  pdf.set_font("Arial", 'B', 16)
16
  pdf.set_text_color(0, 51, 102)
17
  title = "Human Oversight Strategy (Art. 14)" if language == "en" else "Stratégie de Supervision Humaine"
18
  pdf.cell(0, 15, title, ln=True, align='C')
19
  pdf.ln(10)
 
 
 
 
 
 
 
 
 
 
 
20
  pdf.set_font("Arial", '', 12)
21
  pdf.set_text_color(0, 0, 0)
22
 
23
  for line in text.strip().split('\n'):
 
24
  if line.startswith("## "):
25
  section = line.replace("## ", "").strip()
26
  pdf.set_font("Arial", 'B', 13)
@@ -39,10 +58,12 @@ def export_text_to_pdf(text, output_path=None, language="en"):
39
  pdf.multi_cell(0, 10, value)
40
  else:
41
  pdf.multi_cell(0, 10, line)
 
42
  pdf.output(output_path)
43
  return output_path
44
 
45
- QUESTIONS = [
 
46
  ("system_name", "What is the name of the AI system?"),
47
  ("oversight_roles", "Who is responsible for human oversight?"),
48
  ("oversight_tasks", "What are their oversight responsibilities?"),
@@ -53,6 +74,8 @@ QUESTIONS = [
53
  ("escalation", "What is the escalation protocol for risks?")
54
  ]
55
 
 
 
56
  def get_questions():
57
  return QUESTIONS
58
 
@@ -72,11 +95,23 @@ def run_tool():
72
 
73
  content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
74
  lang = detect(content)
75
- pdf_path = export_text_to_pdf(content, language=lang)
 
 
 
 
 
 
 
 
76
  return "✅ Strategy ready below!", {"done": True}, pdf_path
77
 
78
- with gr.Blocks() as demo:
79
- chatbot = gr.Chatbot(label="👁️ Human Oversight Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
 
 
 
 
80
  msg = gr.Textbox(label="Your answer")
81
  state_var = gr.State(state)
82
  file_output = gr.File(label="Download PDF")
 
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
 
9
+ from tools.common import prepend_metadata_questions # ✅ Import shared metadata logic
10
+
11
+ # === PDF Export Function ===
12
+ def export_text_to_pdf(text, metadata=None, 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"human_oversight_strategy_{timestamp}.pdf"
 
17
  pdf = FPDF()
18
  pdf.add_page()
19
  pdf.set_auto_page_break(auto=True, margin=15)
20
+
21
+ # Title
22
  pdf.set_font("Arial", 'B', 16)
23
  pdf.set_text_color(0, 51, 102)
24
  title = "Human Oversight Strategy (Art. 14)" if language == "en" else "Stratégie de Supervision Humaine"
25
  pdf.cell(0, 15, title, ln=True, align='C')
26
  pdf.ln(10)
27
+
28
+ # Metadata block
29
+ if metadata:
30
+ pdf.set_font("Arial", '', 12)
31
+ pdf.set_text_color(90, 90, 90)
32
+ pdf.multi_cell(0, 10, f"Organization: {metadata.get('organization', 'N/A')}")
33
+ pdf.multi_cell(0, 10, f"Completed by: {metadata.get('completed_by', 'N/A')} ({metadata.get('role', 'N/A')})")
34
+ pdf.multi_cell(0, 10, f"Timestamp: {metadata.get('timestamp', 'N/A')}")
35
+ pdf.ln(5)
36
+
37
+ # Content body
38
  pdf.set_font("Arial", '', 12)
39
  pdf.set_text_color(0, 0, 0)
40
 
41
  for line in text.strip().split('\n'):
42
+ line = line.strip()
43
  if line.startswith("## "):
44
  section = line.replace("## ", "").strip()
45
  pdf.set_font("Arial", 'B', 13)
 
58
  pdf.multi_cell(0, 10, value)
59
  else:
60
  pdf.multi_cell(0, 10, line)
61
+
62
  pdf.output(output_path)
63
  return output_path
64
 
65
+ # === Base Questions ===
66
+ BASE_QUESTIONS = [
67
  ("system_name", "What is the name of the AI system?"),
68
  ("oversight_roles", "Who is responsible for human oversight?"),
69
  ("oversight_tasks", "What are their oversight responsibilities?"),
 
74
  ("escalation", "What is the escalation protocol for risks?")
75
  ]
76
 
77
+ QUESTIONS = prepend_metadata_questions(BASE_QUESTIONS)
78
+
79
  def get_questions():
80
  return QUESTIONS
81
 
 
95
 
96
  content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
97
  lang = detect(content)
98
+
99
+ metadata = {
100
+ "organization": answers.get("organization_name", "N/A"),
101
+ "completed_by": answers.get("user_name", "N/A"),
102
+ "role": answers.get("user_role", "N/A"),
103
+ "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
104
+ }
105
+
106
+ pdf_path = export_text_to_pdf(content, metadata=metadata, language=lang)
107
  return "✅ Strategy ready below!", {"done": True}, pdf_path
108
 
109
+ with gr.Blocks(title="Human Oversight Strategy Tool") as demo:
110
+ chatbot = gr.Chatbot(
111
+ label="👁️ Human Oversight Assistant",
112
+ value=[{"role": "assistant", "content": QUESTIONS[0][1]}],
113
+ type="messages"
114
+ )
115
  msg = gr.Textbox(label="Your answer")
116
  state_var = gr.State(state)
117
  file_output = gr.File(label="Download PDF")