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

Create dsa_ad_targeting_log.py

Browse files
Files changed (1) hide show
  1. tools/dsa_ad_targeting_log.py +128 -0
tools/dsa_ad_targeting_log.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tools/dsa_ad_targeting_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_ad_targeting_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, "DSA Advertising & Targeting Log", ln=True, align='C')
23
+ pdf.ln(8)
24
+
25
+ # Metadata block
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
+ # Content
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 log?"),
64
+ ("role", "What is your role?"),
65
+ ("ad_type", "What type of advertisement was delivered?"),
66
+ ("targeting_criteria", "What targeting criteria were used (e.g., demographics, interests)?"),
67
+ ("user_consent", "Was user consent obtained? If yes, how was it recorded?"),
68
+ ("ad_platform", "Which platform or system delivered the ad?"),
69
+ ("time_period", "What was the time period for the ad campaign?"),
70
+ ("evaluation", "How effective or compliant was the targeting strategy?")
71
+ ]
72
+
73
+ def get_questions():
74
+ return QUESTIONS
75
+
76
+ # === Run Tool ===
77
+ def run_tool():
78
+ state = {"step": 0, "answers": {}}
79
+
80
+ def step_by_step_agent(user_input, state):
81
+ step = state["step"]
82
+ answers = state["answers"]
83
+ if step > 0:
84
+ key, _ = QUESTIONS[step - 1]
85
+ answers[key] = user_input
86
+
87
+ if step < len(QUESTIONS):
88
+ next_q = QUESTIONS[step][1]
89
+ state["step"] += 1
90
+ return next_q, state, None
91
+
92
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
93
+ try:
94
+ lang = detect(content) if len(content.strip()) > 3 else "en"
95
+ except:
96
+ lang = "en"
97
+
98
+ metadata = {
99
+ "organization": answers.get("organization"),
100
+ "completed_by": answers.get("completed_by"),
101
+ "role": answers.get("role"),
102
+ "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
103
+ }
104
+
105
+ pdf_path = export_text_to_pdf(content, metadata=metadata, language=lang)
106
+ return "✅ Ad & targeting log completed. Download below.", {"done": True}, pdf_path
107
+
108
+ with gr.Blocks(title="DSA Ad Targeting Log Tool") as demo:
109
+ chatbot = gr.Chatbot(label="📣 Ad Targeting Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
110
+ msg = gr.Textbox(label="Your answer")
111
+ state_var = gr.State(state)
112
+ file_output = gr.File(label="Download PDF")
113
+ reset_btn = gr.Button("🔁 Restart")
114
+
115
+ def chat_logic(msg_in, state_in):
116
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
117
+ messages = [{"role": "user", "content": msg_in}]
118
+ if reply:
119
+ messages.append({"role": "assistant", "content": reply})
120
+ return messages, updated_state, file
121
+
122
+ def reset():
123
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
124
+
125
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
126
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
127
+
128
+ demo.launch(show_api=False)