Dave67350 commited on
Commit
47eda3a
·
verified ·
1 Parent(s): 19f9d91

Create data_governance_record.py

Browse files
Files changed (1) hide show
  1. tools/data_governance_record.py +114 -0
tools/data_governance_record.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # === PDF Export Function ===
10
+ def export_text_to_pdf(text, output_path=None, language="en"):
11
+ if output_path is None:
12
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
13
+ output_path = f"data_governance_record_{timestamp}.pdf"
14
+
15
+ pdf = FPDF()
16
+ pdf.add_page()
17
+ pdf.set_auto_page_break(auto=True, margin=15)
18
+
19
+ pdf.set_font("Arial", 'B', 16)
20
+ pdf.set_text_color(0, 51, 102)
21
+ title = "Data Governance and Quality Record" if language == "en" else "Dossier de Gouvernance et Qualité des Données"
22
+ pdf.cell(0, 15, title, ln=True, align='C')
23
+ pdf.ln(10)
24
+
25
+ pdf.set_font("Arial", '', 12)
26
+ pdf.set_text_color(0, 0, 0)
27
+
28
+ for line in text.strip().split('\n'):
29
+ line = line.strip()
30
+ if line.startswith("## "):
31
+ section_title = line.replace("## ", "").strip()
32
+ pdf.set_font("Arial", 'B', 13)
33
+ pdf.set_text_color(30, 30, 120)
34
+ pdf.ln(8)
35
+ pdf.cell(0, 10, section_title, ln=True)
36
+ pdf.set_font("Arial", '', 12)
37
+ pdf.set_text_color(0, 0, 0)
38
+ elif line.startswith("- **"):
39
+ match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
40
+ if match:
41
+ label, answer = match.groups()
42
+ pdf.set_font("Arial", 'B', 12)
43
+ pdf.cell(0, 10, f"{label}:", ln=True)
44
+ pdf.set_font("Arial", '', 12)
45
+ pdf.multi_cell(0, 10, answer)
46
+ elif line == "---":
47
+ pdf.line(10, pdf.get_y(), 200, pdf.get_y())
48
+ pdf.ln(5)
49
+ else:
50
+ pdf.multi_cell(0, 10, line)
51
+
52
+ pdf.output(output_path)
53
+ return output_path
54
+
55
+ # === Questions ===
56
+ QUESTIONS = [
57
+ ("dataset_description", "Please describe the dataset(s) used."),
58
+ ("data_sources", "What are the sources of the data?"),
59
+ ("data_collection_method", "How was the data collected?"),
60
+ ("preprocessing", "What preprocessing steps were applied?"),
61
+ ("representativeness", "Is the data representative of the use case?"),
62
+ ("bias_handling", "How are biases identified and mitigated?"),
63
+ ("data_split", "How is the data split (training/testing/validation)?"),
64
+ ("missing_data", "How is missing or incomplete data handled?"),
65
+ ("updates", "How is data kept up-to-date or refreshed?"),
66
+ ("access_control", "Who has access to the data and under what conditions?")
67
+ ]
68
+
69
+ def get_questions():
70
+ return QUESTIONS
71
+
72
+ # === Standalone Execution ===
73
+ def run_tool():
74
+ state = {"step": 0, "answers": {}}
75
+
76
+ def step_by_step_agent(user_input, state):
77
+ step = state["step"]
78
+ answers = state["answers"]
79
+
80
+ if step > 0:
81
+ key, _ = QUESTIONS[step - 1]
82
+ answers[key] = user_input
83
+
84
+ if step < len(QUESTIONS):
85
+ next_q = QUESTIONS[step][1]
86
+ state["step"] += 1
87
+ return next_q, state, None
88
+
89
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
90
+ detected_lang = detect(content)
91
+ pdf_path = export_text_to_pdf(content, language=detected_lang)
92
+ return "✅ Documentation complete. Download below.", {"done": True}, pdf_path
93
+
94
+ with gr.Blocks(title="Data Governance Tool") as demo:
95
+ chatbot = gr.Chatbot(label="📊 Data Governance Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
96
+ msg = gr.Textbox(label="Your answer")
97
+ state_var = gr.State(state)
98
+ file_output = gr.File(label="Download PDF")
99
+ reset_btn = gr.Button("🔁 Restart")
100
+
101
+ def chat_logic(msg_in, state_in):
102
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
103
+ messages = [{"role": "user", "content": msg_in}]
104
+ if reply:
105
+ messages.append({"role": "assistant", "content": reply})
106
+ return messages, updated_state, file
107
+
108
+ def reset():
109
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
110
+
111
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
112
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
113
+
114
+ demo.launch()