Dave67350 commited on
Commit
2fe1a52
·
verified ·
1 Parent(s): 56a9609

Create dsa_content_moderation_log.py

Browse files
Files changed (1) hide show
  1. tools/dsa_content_moderation_log.py +129 -0
tools/dsa_content_moderation_log.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tools/dsa_content_moderation_log.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
+ def export_text_to_pdf(text, metadata=None, output_path=None, language="en"):
10
+ if output_path is None:
11
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
12
+ output_path = f"dsa_content_moderation_log_{timestamp}.pdf"
13
+
14
+ pdf = FPDF()
15
+ pdf.add_page()
16
+ pdf.set_auto_page_break(auto=True, margin=15)
17
+
18
+ pdf.set_font("Arial", 'B', 16)
19
+ pdf.set_text_color(0, 51, 102)
20
+ title = "Content Moderation Log (DSA)"
21
+ pdf.cell(0, 15, title, ln=True, align='C')
22
+ pdf.ln(8)
23
+
24
+ if metadata:
25
+ pdf.set_font("Arial", '', 12)
26
+ pdf.set_text_color(90, 90, 90)
27
+ pdf.multi_cell(0, 10, f"Organization: {metadata.get('organization', 'N/A')}")
28
+ pdf.multi_cell(0, 10, f"Completed by: {metadata.get('completed_by', 'N/A')} ({metadata.get('role', 'N/A')})")
29
+ pdf.multi_cell(0, 10, f"Timestamp: {metadata.get('timestamp', 'N/A')}")
30
+ pdf.ln(5)
31
+
32
+ pdf.set_font("Arial", '', 12)
33
+ pdf.set_text_color(0, 0, 0)
34
+ for line in text.strip().split('\n'):
35
+ if line.startswith("## "):
36
+ section = line.replace("## ", "").strip()
37
+ pdf.set_font("Arial", 'B', 13)
38
+ pdf.set_text_color(30, 30, 120)
39
+ pdf.ln(6)
40
+ pdf.cell(0, 10, section, ln=True)
41
+ pdf.set_font("Arial", '', 12)
42
+ pdf.set_text_color(0, 0, 0)
43
+ elif line.startswith("- **"):
44
+ match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
45
+ if match:
46
+ label, value = match.groups()
47
+ pdf.set_font("Arial", 'B', 12)
48
+ pdf.cell(0, 10, f"{label}:", ln=True)
49
+ pdf.set_font("Arial", '', 12)
50
+ pdf.multi_cell(0, 10, value)
51
+ elif line == "---":
52
+ pdf.line(10, pdf.get_y(), 200, pdf.get_y())
53
+ pdf.ln(5)
54
+ else:
55
+ pdf.multi_cell(0, 10, line)
56
+ pdf.output(output_path)
57
+ return output_path
58
+
59
+ QUESTIONS = [
60
+ ("organization", "What is the name of your organization?"),
61
+ ("completed_by", "Who is completing this log?"),
62
+ ("role", "What is your role?"),
63
+ ("platform", "What platform or service does this apply to?"),
64
+ ("date", "What is the date of moderation?"),
65
+ ("type_of_content", "What type of content was moderated?"),
66
+ ("moderation_action", "What moderation action was taken (e.g. removal, warning)?"),
67
+ ("reason", "What was the reason for moderation?"),
68
+ ("notified_user", "Was the user notified? If yes, how?"),
69
+ ("appeal_possibility", "Was the possibility of appeal offered?")
70
+ ]
71
+
72
+ def get_questions():
73
+ return QUESTIONS
74
+
75
+ def run_tool():
76
+ state = {"step": 0, "answers": {}}
77
+
78
+ def step_by_step_agent(user_input, state):
79
+ step = state["step"]
80
+ answers = state["answers"]
81
+
82
+ if step > 0:
83
+ key, _ = QUESTIONS[step - 1]
84
+ answers[key] = user_input
85
+
86
+ if step < len(QUESTIONS):
87
+ next_q = QUESTIONS[step][1]
88
+ state["step"] += 1
89
+ return next_q, state, None
90
+
91
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
92
+ lang = "en"
93
+ try:
94
+ if len(content.strip()) > 3:
95
+ lang = detect(content)
96
+ except:
97
+ lang = "en"
98
+
99
+ metadata = {
100
+ "organization": answers.get("organization"),
101
+ "completed_by": answers.get("completed_by"),
102
+ "role": answers.get("role"),
103
+ "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
104
+ }
105
+
106
+ pdf_path = export_text_to_pdf(content, metadata=metadata, language=lang)
107
+ return "✅ Log completed. Download below.", {"done": True}, pdf_path
108
+
109
+ with gr.Blocks(title="DSA Content Moderation Log") as demo:
110
+ chatbot = gr.Chatbot(label="🛡️ DSA Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
111
+ msg = gr.Textbox(label="Your answer")
112
+ state_var = gr.State(state)
113
+ file_output = gr.File(label="Download PDF")
114
+ reset_btn = gr.Button("🔁 Restart")
115
+
116
+ def chat_logic(msg_in, state_in):
117
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
118
+ messages = [{"role": "user", "content": msg_in}]
119
+ if reply:
120
+ messages.append({"role": "assistant", "content": reply})
121
+ return messages, updated_state, file
122
+
123
+ def reset():
124
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
125
+
126
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
127
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
128
+
129
+ demo.launch(show_api=False)