Dave67350/First_agent_template
0
1# tools/dsa_content_moderation_log.py2 3from datetime import datetime4from fpdf import FPDF5import re6import gradio as gr7from langdetect import detect8 9def 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_path58 59QUESTIONS = [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 72def get_questions():73 return QUESTIONS74 75def 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_input85 86 if step < len(QUESTIONS):87 next_q = QUESTIONS[step][1]88 state["step"] += 189 return next_q, state, None90 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_path108 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, file122 123 def reset():124 return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None125 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)130 