Dave67350 commited on
Commit
c3b5b29
·
verified ·
1 Parent(s): 6b6a163

Create dsa_trusted_flaggers_log.py

Browse files
Files changed (1) hide show
  1. tools/dsa_trusted_flaggers_log.py +130 -0
tools/dsa_trusted_flaggers_log.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tools/dsa_trusted_flaggers_log.py
2
+
3
+ import re
4
+ from datetime import datetime
5
+ from fpdf import FPDF
6
+ import gradio as gr
7
+ from langdetect import detect
8
+
9
+ # === PDF Export ===
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_trusted_flaggers_log_{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, "Trusted Flaggers Activity Log", 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
+ line = line.strip()
39
+ if line.startswith("## "):
40
+ section = line.replace("## ", "").strip()
41
+ pdf.set_font("Arial", 'B', 13)
42
+ pdf.set_text_color(30, 30, 120)
43
+ pdf.ln(6)
44
+ pdf.cell(0, 10, section, ln=True)
45
+ pdf.set_font("Arial", '', 12)
46
+ pdf.set_text_color(0, 0, 0)
47
+ elif line.startswith("- **"):
48
+ match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
49
+ if match:
50
+ label, answer = match.groups()
51
+ pdf.set_font("Arial", 'B', 12)
52
+ pdf.cell(0, 10, f"{label}:", ln=True)
53
+ pdf.set_font("Arial", '', 12)
54
+ pdf.multi_cell(0, 10, answer)
55
+ else:
56
+ pdf.multi_cell(0, 10, line)
57
+
58
+ pdf.output(output_path)
59
+ return output_path
60
+
61
+ # === Questions ===
62
+ QUESTIONS = [
63
+ ("organization", "What is the name of your organization?"),
64
+ ("completed_by", "Who is completing this log?"),
65
+ ("role", "What is your role?"),
66
+ ("flagger_name", "What is the name of the trusted flagger?"),
67
+ ("flag_reason", "What content was flagged and why?"),
68
+ ("date_flagged", "When was the flag submitted?"),
69
+ ("response_action", "What action was taken in response?"),
70
+ ("timeline", "What was the timeline for the resolution?"),
71
+ ("escalation", "Was the case escalated to another authority?")
72
+ ]
73
+
74
+ def get_questions():
75
+ return QUESTIONS
76
+
77
+ # === Run Tool ===
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
+ 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
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
94
+
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 "✅ Trusted flagger log completed. Download below.", {"done": True}, pdf_path
109
+
110
+ with gr.Blocks(title="Trusted Flagger Log Tool") as demo:
111
+ chatbot = gr.Chatbot(label="🚩 Trusted Flaggers Log", 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)