Dave67350 commited on
Commit
bd8d91b
·
verified ·
1 Parent(s): 33943d3

Create human_oversight_strategy.py

Browse files
Files changed (1) hide show
  1. tools/human_oversight_strategy.py +98 -0
tools/human_oversight_strategy.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"
11
+
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)
27
+ pdf.set_text_color(30, 30, 120)
28
+ pdf.ln(8)
29
+ pdf.cell(0, 10, section, ln=True)
30
+ pdf.set_font("Arial", '', 12)
31
+ pdf.set_text_color(0, 0, 0)
32
+ elif line.startswith("- **"):
33
+ match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
34
+ if match:
35
+ label, value = match.groups()
36
+ pdf.set_font("Arial", 'B', 12)
37
+ pdf.cell(0, 10, f"{label}:", ln=True)
38
+ pdf.set_font("Arial", '', 12)
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?"),
49
+ ("intervention_methods", "How can they intervene in the system?"),
50
+ ("training", "What training is provided to oversight personnel?"),
51
+ ("monitoring_tools", "What tools help humans supervise the system?"),
52
+ ("limitations", "What are known limitations of human control?"),
53
+ ("escalation", "What is the escalation protocol for risks?")
54
+ ]
55
+
56
+ def get_questions():
57
+ return QUESTIONS
58
+
59
+ def run_tool():
60
+ state = {"step": 0, "answers": {}}
61
+
62
+ def step_by_step_agent(user_input, state):
63
+ step = state["step"]
64
+ answers = state["answers"]
65
+ if step > 0:
66
+ key, _ = QUESTIONS[step - 1]
67
+ answers[key] = user_input
68
+ if step < len(QUESTIONS):
69
+ question = QUESTIONS[step][1]
70
+ state["step"] += 1
71
+ return question, state, None
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")
83
+ reset_btn = gr.Button("🔁 Restart")
84
+
85
+ def chat_logic(msg_in, state_in):
86
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
87
+ messages = [{"role": "user", "content": msg_in}]
88
+ if reply:
89
+ messages.append({"role": "assistant", "content": reply})
90
+ return messages, updated_state, file
91
+
92
+ def reset():
93
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
94
+
95
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
96
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
97
+
98
+ demo.launch()