Dave67350 commited on
Commit
c76ad79
·
verified ·
1 Parent(s): 039746d

Create dma_interoperability_report.py

Browse files
Files changed (1) hide show
  1. tools/dma_interoperability_report.py +124 -0
tools/dma_interoperability_report.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tools/dma_interoperability_report.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"interoperability_report_{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 = "Interoperability Report (DMA)" if language == "en" else "Rapport d'Interopérabilité (DMA)"
24
+ pdf.cell(0, 15, title, ln=True, align='C')
25
+ pdf.ln(10)
26
+
27
+ # Metadata
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
+ # Body content
37
+ pdf.set_font("Arial", '', 12)
38
+ pdf.set_text_color(0, 0, 0)
39
+ for line in text.strip().split('\n'):
40
+ if line.startswith("## "):
41
+ section = line.replace("## ", "").strip()
42
+ pdf.set_font("Arial", 'B', 13)
43
+ pdf.set_text_color(30, 30, 120)
44
+ pdf.ln(8)
45
+ pdf.cell(0, 10, section, ln=True)
46
+ pdf.set_font("Arial", '', 12)
47
+ pdf.set_text_color(0, 0, 0)
48
+ elif line.startswith("- **"):
49
+ match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
50
+ if match:
51
+ label, value = match.groups()
52
+ pdf.set_font("Arial", 'B', 12)
53
+ pdf.cell(0, 10, f"{label}:", ln=True)
54
+ pdf.set_font("Arial", '', 12)
55
+ pdf.multi_cell(0, 10, value)
56
+ elif line == "---":
57
+ pdf.line(10, pdf.get_y(), 200, pdf.get_y())
58
+ pdf.ln(5)
59
+ else:
60
+ pdf.multi_cell(0, 10, line)
61
+
62
+ pdf.output(output_path)
63
+ return output_path
64
+
65
+ # === Questions ===
66
+ QUESTIONS = prepend_metadata_questions([
67
+ ("interoperability_approach", "Describe your general approach to ensuring interoperability."),
68
+ ("standards_used", "Which open standards or APIs are supported or implemented?"),
69
+ ("third_party_support", "How do third parties connect and interact with your services?"),
70
+ ("testing_frameworks", "What testing frameworks or tools are used to validate interoperability?"),
71
+ ("known_issues", "Are there any known issues or limitations in interoperability?"),
72
+ ("update_process", "How often is the interoperability mechanism reviewed or updated?"),
73
+ ])
74
+
75
+ def get_questions():
76
+ return QUESTIONS
77
+
78
+ # === Tool Execution ===
79
+ def run_tool():
80
+ state = {"step": 0, "answers": {}}
81
+
82
+ def step_by_step_agent(user_input, state):
83
+ step = state["step"]
84
+ answers = state["answers"]
85
+ if step > 0:
86
+ key, _ = QUESTIONS[step - 1]
87
+ answers[key] = user_input
88
+ if step < len(QUESTIONS):
89
+ next_question = QUESTIONS[step][1]
90
+ state["step"] += 1
91
+ return next_question, state, None
92
+
93
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
94
+ lang = detect(content) if len(content.strip()) > 3 else "en"
95
+ metadata = {
96
+ "organization": answers.get("organization_name", "N/A"),
97
+ "name": answers.get("user_name", "N/A"),
98
+ "role": answers.get("user_role", "N/A"),
99
+ "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
100
+ }
101
+ pdf_path = export_text_to_pdf(content, metadata=metadata, language=lang)
102
+ return "✅ Interoperability Report completed. Download your PDF below.", {"done": True}, pdf_path
103
+
104
+ with gr.Blocks(title="DMA Interoperability Tool") as demo:
105
+ chatbot = gr.Chatbot(label="🔗 DMA Interoperability Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
106
+ msg = gr.Textbox(label="Your answer")
107
+ state_var = gr.State(state)
108
+ file_output = gr.File(label="Download PDF")
109
+ reset_btn = gr.Button("🔁 Restart")
110
+
111
+ def chat_logic(msg_in, state_in):
112
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
113
+ messages = [{"role": "user", "content": msg_in}]
114
+ if reply:
115
+ messages.append({"role": "assistant", "content": reply})
116
+ return messages, updated_state, file
117
+
118
+ def reset():
119
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
120
+
121
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
122
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
123
+
124
+ demo.launch(show_api=False)