Dave67350 commited on
Commit
c3518a6
·
verified ·
1 Parent(s): c6d3213

Create dma_gatekeeper_service_log.py

Browse files
Files changed (1) hide show
  1. tools/dma_gatekeeper_service_log.py +121 -0
tools/dma_gatekeeper_service_log.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tools/dma_gatekeeper_service_log.py
2
+ import datetime
3
+ import re
4
+ from fpdf import FPDF
5
+ from langdetect import detect
6
+ import gradio as gr
7
+ from tools.common import prepend_metadata_questions
8
+
9
+
10
+ def export_text_to_pdf(text, metadata=None, 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"dma_gatekeeper_service_log_{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 = "Gatekeeper Service Log - DMA" if language == "en" else "Journal des Services Gatekeeper - DMA"
22
+ pdf.cell(0, 15, title, ln=True, align='C')
23
+ pdf.ln(10)
24
+
25
+ if metadata:
26
+ pdf.set_font("Arial", '', 12)
27
+ pdf.set_text_color(90, 90, 90)
28
+ pdf.multi_cell(0, 10, f"Organization: {metadata.get('organization_name', 'N/A')}")
29
+ pdf.multi_cell(0, 10, f"Completed by: {metadata.get('user_name', 'N/A')} ({metadata.get('user_role', 'N/A')})")
30
+ pdf.multi_cell(0, 10, f"Timestamp: {metadata.get('timestamp', 'N/A')}")
31
+ pdf.ln(5)
32
+
33
+ pdf.set_font("Arial", '', 12)
34
+ pdf.set_text_color(0, 0, 0)
35
+ for line in text.strip().split('\n'):
36
+ line = line.strip()
37
+ if line.startswith("## "):
38
+ section_title = line.replace("## ", "").strip()
39
+ pdf.set_font("Arial", 'B', 13)
40
+ pdf.set_text_color(30, 30, 120)
41
+ pdf.ln(8)
42
+ pdf.cell(0, 10, section_title, ln=True)
43
+ pdf.set_font("Arial", '', 12)
44
+ pdf.set_text_color(0, 0, 0)
45
+ elif line.startswith("- **"):
46
+ match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
47
+ if match:
48
+ label, value = match.groups()
49
+ pdf.set_font("Arial", 'B', 12)
50
+ pdf.cell(0, 10, f"{label}:", ln=True)
51
+ pdf.set_font("Arial", '', 12)
52
+ pdf.multi_cell(0, 10, value)
53
+ else:
54
+ pdf.multi_cell(0, 10, line)
55
+
56
+ pdf.output(output_path)
57
+ return output_path
58
+
59
+
60
+ QUESTIONS = prepend_metadata_questions([
61
+ ("gatekeeper_name", "What is the name of the gatekeeper platform/service?"),
62
+ ("service_type", "What type of service is offered (e.g., marketplace, OS, chat)?"),
63
+ ("access_conditions", "What are the conditions for business users to access the platform?"),
64
+ ("user_metrics", "How many active users (monthly/yearly)?"),
65
+ ("core_platform_features", "List the core platform services provided."),
66
+ ("data_usage", "Describe how user or business data is processed."),
67
+ ("fairness_measures", "What measures are taken to ensure fair access and transparency?")
68
+ ])
69
+
70
+ def get_questions():
71
+ return QUESTIONS
72
+
73
+
74
+ def run_tool():
75
+ state = {"step": 0, "answers": {}}
76
+
77
+ def step_by_step_agent(user_input, state):
78
+ step = state["step"]
79
+ answers = state["answers"]
80
+
81
+ if step > 0:
82
+ key, _ = QUESTIONS[step - 1]
83
+ answers[key] = user_input
84
+
85
+ if step < len(QUESTIONS):
86
+ next_q = QUESTIONS[step][1]
87
+ state["step"] += 1
88
+ return next_q, state, None
89
+
90
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
91
+ lang = detect(content) if len(content.strip()) > 3 else "en"
92
+ metadata = {
93
+ "organization_name": answers.get("organization_name"),
94
+ "user_name": answers.get("user_name"),
95
+ "user_role": answers.get("user_role"),
96
+ "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
97
+ }
98
+ pdf_path = export_text_to_pdf(content, metadata=metadata, language=lang)
99
+ return "✅ Log completed. Download your Gatekeeper Service Log below.", {"done": True}, pdf_path
100
+
101
+ with gr.Blocks(title="DMA - Gatekeeper Service Log") as demo:
102
+ chatbot = gr.Chatbot(label="🧭 DMA Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
103
+ msg = gr.Textbox(label="Your answer")
104
+ state_var = gr.State(state)
105
+ file_output = gr.File(label="Download PDF")
106
+ reset_btn = gr.Button("🔁 Restart")
107
+
108
+ def chat_logic(msg_in, state_in):
109
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
110
+ messages = [{"role": "user", "content": msg_in}]
111
+ if reply:
112
+ messages.append({"role": "assistant", "content": reply})
113
+ return messages, updated_state, file
114
+
115
+ def reset():
116
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
117
+
118
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
119
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
120
+
121
+ demo.launch(show_api=False)