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

Create dsa_complaint_handling_log.py

Browse files
Files changed (1) hide show
  1. tools/dsa_complaint_handling_log.py +127 -0
tools/dsa_complaint_handling_log.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tools/dsa_complaint_handling_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
+ # === 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_complaint_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
+ pdf.cell(0, 15, "DSA Complaint Handling Log", 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
+ else:
52
+ pdf.multi_cell(0, 10, line)
53
+
54
+ pdf.output(output_path)
55
+ return output_path
56
+
57
+ # === Questions ===
58
+ QUESTIONS = [
59
+ ("organization", "What is the name of your organization?"),
60
+ ("completed_by", "Who is completing this log?"),
61
+ ("role", "What is your role?"),
62
+ ("date_received", "Date the complaint was received?"),
63
+ ("user_identifier", "Who submitted the complaint (user ID, anonymized ID, etc.)?"),
64
+ ("complaint_type", "What is the type of complaint (e.g., content takedown, restriction)?"),
65
+ ("complaint_details", "Brief description of the complaint."),
66
+ ("resolution_status", "What was the resolution status (e.g., resolved, pending)?"),
67
+ ("response_time", "How long did it take to respond to the user?"),
68
+ ("notes", "Any additional notes or actions taken?")
69
+ ]
70
+
71
+ def get_questions():
72
+ return QUESTIONS
73
+
74
+ # === Run Tool ===
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
+ try:
93
+ lang = detect(content) if len(content.strip()) > 3 else "en"
94
+ except:
95
+ lang = "en"
96
+
97
+ metadata = {
98
+ "organization": answers.get("organization"),
99
+ "completed_by": answers.get("completed_by"),
100
+ "role": answers.get("role"),
101
+ "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
102
+ }
103
+
104
+ pdf_path = export_text_to_pdf(content, metadata=metadata, language=lang)
105
+ return "✅ Complaint handling log completed. Download below.", {"done": True}, pdf_path
106
+
107
+ with gr.Blocks(title="DSA Complaint Log Tool") as demo:
108
+ chatbot = gr.Chatbot(label="📨 Complaint Handling Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
109
+ msg = gr.Textbox(label="Your answer")
110
+ state_var = gr.State(state)
111
+ file_output = gr.File(label="Download PDF")
112
+ reset_btn = gr.Button("🔁 Restart")
113
+
114
+ def chat_logic(msg_in, state_in):
115
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
116
+ messages = [{"role": "user", "content": msg_in}]
117
+ if reply:
118
+ messages.append({"role": "assistant", "content": reply})
119
+ return messages, updated_state, file
120
+
121
+ def reset():
122
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
123
+
124
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
125
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
126
+
127
+ demo.launch(show_api=False)