Dave67350 commited on
Commit
74bb918
·
verified ·
1 Parent(s): 2fe1a52

Create dsa_transparency_report.py

Browse files
Files changed (1) hide show
  1. tools/dsa_transparency_report.py +130 -0
tools/dsa_transparency_report.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tools/dsa_transparency_report.py
2
+
3
+ from datetime import datetime
4
+ from fpdf import FPDF
5
+ import re
6
+ import gradio as gr
7
+ from langdetect import detect
8
+
9
+ # === PDF Export Function ===
10
+ def export_text_to_pdf(text, metadata=None, output_path=None, language="en"):
11
+ if output_path is None:
12
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
13
+ output_path = f"dsa_transparency_report_{timestamp}.pdf"
14
+
15
+ pdf = FPDF()
16
+ pdf.add_page()
17
+ pdf.set_auto_page_break(auto=True, margin=15)
18
+
19
+ # Title
20
+ pdf.set_font("Arial", 'B', 16)
21
+ pdf.set_text_color(0, 51, 102)
22
+ pdf.cell(0, 15, "DSA Transparency Report", ln=True, align='C')
23
+ pdf.ln(8)
24
+
25
+ # Metadata
26
+ if metadata:
27
+ pdf.set_font("Arial", '', 12)
28
+ pdf.set_text_color(90, 90, 90)
29
+ pdf.multi_cell(0, 10, f"Organization: {metadata.get('organization', 'N/A')}")
30
+ pdf.multi_cell(0, 10, f"Completed by: {metadata.get('completed_by', 'N/A')} ({metadata.get('role', 'N/A')})")
31
+ pdf.multi_cell(0, 10, f"Timestamp: {metadata.get('timestamp', 'N/A')}")
32
+ pdf.ln(5)
33
+
34
+ # Body
35
+ pdf.set_font("Arial", '', 12)
36
+ pdf.set_text_color(0, 0, 0)
37
+ for line in text.strip().split('\n'):
38
+ if line.startswith("## "):
39
+ section = line.replace("## ", "").strip()
40
+ pdf.set_font("Arial", 'B', 13)
41
+ pdf.set_text_color(30, 30, 120)
42
+ pdf.ln(6)
43
+ pdf.cell(0, 10, section, ln=True)
44
+ pdf.set_font("Arial", '', 12)
45
+ pdf.set_text_color(0, 0, 0)
46
+ elif line.startswith("- **"):
47
+ match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
48
+ if match:
49
+ label, value = match.groups()
50
+ pdf.set_font("Arial", 'B', 12)
51
+ pdf.cell(0, 10, f"{label}:", ln=True)
52
+ pdf.set_font("Arial", '', 12)
53
+ pdf.multi_cell(0, 10, value)
54
+ else:
55
+ pdf.multi_cell(0, 10, line)
56
+
57
+ pdf.output(output_path)
58
+ return output_path
59
+
60
+ # === Questions ===
61
+ QUESTIONS = [
62
+ ("organization", "What is the name of your organization?"),
63
+ ("completed_by", "Who is completing this report?"),
64
+ ("role", "What is your role?"),
65
+ ("reporting_period", "What is the reporting period (e.g. Q1 2025)?"),
66
+ ("platform", "Which platform/service does this report apply to?"),
67
+ ("moderation_volume", "How many content moderation actions occurred?"),
68
+ ("appeals_count", "How many appeals were received?"),
69
+ ("automated_tools", "What automated tools are used for moderation?"),
70
+ ("government_requests", "How many content removal requests were from authorities?"),
71
+ ("transparency_measures", "What transparency measures were implemented?")
72
+ ]
73
+
74
+ def get_questions():
75
+ return QUESTIONS
76
+
77
+ # === Tool Execution ===
78
+ def run_tool():
79
+ state = {"step": 0, "answers": {}}
80
+
81
+ def step_by_step_agent(user_input, state):
82
+ step = state["step"]
83
+ answers = state["answers"]
84
+
85
+ if step > 0:
86
+ key, _ = QUESTIONS[step - 1]
87
+ answers[key] = user_input
88
+
89
+ if step < len(QUESTIONS):
90
+ next_q = QUESTIONS[step][1]
91
+ state["step"] += 1
92
+ return next_q, state, None
93
+
94
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
95
+ try:
96
+ lang = detect(content) if len(content.strip()) > 3 else "en"
97
+ except:
98
+ lang = "en"
99
+
100
+ metadata = {
101
+ "organization": answers.get("organization"),
102
+ "completed_by": answers.get("completed_by"),
103
+ "role": answers.get("role"),
104
+ "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
105
+ }
106
+
107
+ pdf_path = export_text_to_pdf(content, metadata=metadata, language=lang)
108
+ return "✅ Transparency report completed. Download below.", {"done": True}, pdf_path
109
+
110
+ with gr.Blocks(title="DSA Transparency Report Tool") as demo:
111
+ chatbot = gr.Chatbot(label="📊 Transparency Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
112
+ msg = gr.Textbox(label="Your answer")
113
+ state_var = gr.State(state)
114
+ file_output = gr.File(label="Download PDF")
115
+ reset_btn = gr.Button("🔁 Restart")
116
+
117
+ def chat_logic(msg_in, state_in):
118
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
119
+ messages = [{"role": "user", "content": msg_in}]
120
+ if reply:
121
+ messages.append({"role": "assistant", "content": reply})
122
+ return messages, updated_state, file
123
+
124
+ def reset():
125
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
126
+
127
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
128
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
129
+
130
+ demo.launch(show_api=False)