Dave67350 commited on
Commit
67191f7
·
verified ·
1 Parent(s): 5a043c6

Create dma_transparency_log.py

Browse files
Files changed (1) hide show
  1. tools/dma_transparency_log.py +126 -0
tools/dma_transparency_log.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tools/dma_transparency_log.py
2
+
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
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"dma_transparency_log_{timestamp}.pdf"
15
+
16
+ pdf = FPDF()
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 = "DMA Transparency Log" if language == "en" else "Journal de Transparence DMA"
24
+ pdf.cell(0, 15, title, ln=True, align='C')
25
+ pdf.ln(10)
26
+
27
+ # Metadata Section
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('name', '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
+ # Main Content
37
+ pdf.set_font("Arial", '', 12)
38
+ pdf.set_text_color(0, 0, 0)
39
+
40
+ for line in text.strip().split('\n'):
41
+ if line.startswith("## "):
42
+ section = line.replace("## ", "").strip()
43
+ pdf.set_font("Arial", 'B', 13)
44
+ pdf.set_text_color(30, 30, 120)
45
+ pdf.ln(8)
46
+ pdf.cell(0, 10, section, ln=True)
47
+ pdf.set_font("Arial", '', 12)
48
+ pdf.set_text_color(0, 0, 0)
49
+ elif line.startswith("- **"):
50
+ match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
51
+ if match:
52
+ label, value = match.groups()
53
+ pdf.set_font("Arial", 'B', 12)
54
+ pdf.cell(0, 10, f"{label}:", ln=True)
55
+ pdf.set_font("Arial", '', 12)
56
+ pdf.multi_cell(0, 10, value)
57
+ else:
58
+ pdf.multi_cell(0, 10, line)
59
+
60
+ pdf.output(output_path)
61
+ return output_path
62
+
63
+ # === Questions ===
64
+ QUESTIONS = prepend_metadata_questions([
65
+ ("purpose", "What was the purpose of the communication or update?"),
66
+ ("audience", "Who was the target audience (e.g. regulators, users, public)?"),
67
+ ("content_summary", "Provide a brief summary of the information disclosed."),
68
+ ("disclosure_date", "When was this information disclosed?"),
69
+ ("channel", "Through what channel was the disclosure made (e.g. website, press release)?"),
70
+ ("legal_reference", "Which DMA article or obligation does it correspond to?")
71
+ ])
72
+
73
+ def get_questions():
74
+ return QUESTIONS
75
+
76
+ # === Run Tool ===
77
+ def run_tool():
78
+ state = {"step": 0, "answers": {}}
79
+
80
+ def step_by_step_agent(user_input, state):
81
+ step = state["step"]
82
+ answers = state["answers"]
83
+
84
+ if step > 0:
85
+ key, _ = QUESTIONS[step - 1]
86
+ answers[key] = user_input
87
+
88
+ if step < len(QUESTIONS):
89
+ next_q = QUESTIONS[step][1]
90
+ state["step"] += 1
91
+ return next_q, state, None
92
+
93
+ # Compile content
94
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
95
+ language = detect(content) if len(content.strip()) > 3 else "en"
96
+ metadata = {
97
+ "organization": answers.get("organization_name", "N/A"),
98
+ "name": answers.get("user_name", "N/A"),
99
+ "role": answers.get("user_role", "N/A"),
100
+ "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
101
+ }
102
+ pdf_path = export_text_to_pdf(content, metadata=metadata, language=language)
103
+ return "✅ Transparency log completed. Download your PDF below.", {"done": True}, pdf_path
104
+
105
+ # Gradio Interface
106
+ with gr.Blocks(title="DMA Transparency Log") as demo:
107
+ chatbot = gr.Chatbot(label="🔍 Transparency Log Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
108
+ msg = gr.Textbox(label="Your answer")
109
+ state_var = gr.State(state)
110
+ file_output = gr.File(label="Download PDF")
111
+ reset_btn = gr.Button("🔁 Restart")
112
+
113
+ def chat_logic(msg_in, state_in):
114
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
115
+ messages = [{"role": "user", "content": msg_in}]
116
+ if reply:
117
+ messages.append({"role": "assistant", "content": reply})
118
+ return messages, updated_state, file
119
+
120
+ def reset():
121
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
122
+
123
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
124
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
125
+
126
+ demo.launch(show_api=False)