CoolFace
Apppublic

Dave67350/First_agent_template

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
data_governance_record.py143 linesDownload Raw Back to tools
1#!/usr/bin/env python2# coding=utf-83import datetime4import re5from fpdf import FPDF6from langdetect import detect7import gradio as gr8 9from tools.common import prepend_metadata_questions  # ✅ Import helper10 11# === PDF Export Function ===12def export_text_to_pdf(text, metadata=None, output_path=None, language="en"):13    if output_path is None:14        timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")15        output_path = f"data_governance_record_{timestamp}.pdf"16 17    pdf = FPDF()18    pdf.add_page()19    pdf.set_auto_page_break(auto=True, margin=15)20 21    # Title22    pdf.set_font("Arial", 'B', 16)23    pdf.set_text_color(0, 51, 102)24    title = "Data Governance and Quality Record" if language == "en" else "Dossier de Gouvernance et Qualité des Données"25    pdf.cell(0, 15, title, ln=True, align='C')26    pdf.ln(10)27 28    # Metadata block29    if metadata:30        pdf.set_font("Arial", '', 12)31        pdf.set_text_color(90, 90, 90)32        pdf.multi_cell(0, 10, f"Organization: {metadata.get('organization', 'N/A')}")33        pdf.multi_cell(0, 10, f"Completed by: {metadata.get('completed_by', 'N/A')} ({metadata.get('role', 'N/A')})")34        pdf.multi_cell(0, 10, f"Timestamp: {metadata.get('timestamp', 'N/A')}")35        pdf.ln(5)36 37    # Body38    pdf.set_font("Arial", '', 12)39    pdf.set_text_color(0, 0, 0)40    for line in text.strip().split('\n'):41        line = line.strip()42        if line.startswith("## "):43            section_title = line.replace("## ", "").strip()44            pdf.set_font("Arial", 'B', 13)45            pdf.set_text_color(30, 30, 120)46            pdf.ln(8)47            pdf.cell(0, 10, section_title, ln=True)48            pdf.set_font("Arial", '', 12)49            pdf.set_text_color(0, 0, 0)50        elif line.startswith("- **"):51            match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)52            if match:53                label, answer = match.groups()54                pdf.set_font("Arial", 'B', 12)55                pdf.cell(0, 10, f"{label}:", ln=True)56                pdf.set_font("Arial", '', 12)57                pdf.multi_cell(0, 10, answer)58        elif line == "---":59            pdf.line(10, pdf.get_y(), 200, pdf.get_y())60            pdf.ln(5)61        else:62            pdf.multi_cell(0, 10, line)63 64    pdf.output(output_path)65    return output_path66 67 68# === Core Questions ===69BASE_QUESTIONS = [70    ("dataset_description", "Please describe the dataset(s) used."),71    ("data_sources", "What are the sources of the data?"),72    ("data_collection_method", "How was the data collected?"),73    ("preprocessing", "What preprocessing steps were applied?"),74    ("representativeness", "Is the data representative of the use case?"),75    ("bias_handling", "How are biases identified and mitigated?"),76    ("data_split", "How is the data split (training/testing/validation)?"),77    ("missing_data", "How is missing or incomplete data handled?"),78    ("updates", "How is data kept up-to-date or refreshed?"),79    ("access_control", "Who has access to the data and under what conditions?")80]81 82QUESTIONS = prepend_metadata_questions(BASE_QUESTIONS)  # ✅ Prepend metadata83 84def get_questions():85    return QUESTIONS86 87# === Run Tool ===88def run_tool():89    state = {"step": 0, "answers": {}}90 91    def step_by_step_agent(user_input, state):92        step = state["step"]93        answers = state["answers"]94 95        if step > 0:96            key, _ = QUESTIONS[step - 1]97            answers[key] = user_input98 99        if step < len(QUESTIONS):100            next_q = QUESTIONS[step][1]101            state["step"] += 1102            return next_q, state, None103 104        content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])105        detected_lang = detect(content)106 107        # ✅ Extract metadata from prepended fields108        metadata = {109            "organization": answers.get("organization_name", "N/A"),110            "completed_by": answers.get("user_name", "N/A"),111            "role": answers.get("user_role", "N/A"),112            "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")113        }114 115        pdf_path = export_text_to_pdf(content, metadata=metadata, language=detected_lang)116        return "✅ Documentation complete. Download below.", {"done": True}, pdf_path117 118    with gr.Blocks(title="Data Governance Tool") as demo:119        chatbot = gr.Chatbot(120            label="📊 Data Governance Assistant",121            value=[{"role": "assistant", "content": QUESTIONS[0][1]}],122            type="messages"123        )124        msg = gr.Textbox(label="Your answer")125        state_var = gr.State(state)126        file_output = gr.File(label="Download PDF")127        reset_btn = gr.Button("🔁 Restart")128 129        def chat_logic(msg_in, state_in):130            reply, updated_state, file = step_by_step_agent(msg_in, state_in)131            messages = [{"role": "user", "content": msg_in}]132            if reply:133                messages.append({"role": "assistant", "content": reply})134            return messages, updated_state, file135 136        def reset():137            return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None138 139        msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])140        reset_btn.click(reset, outputs=[chatbot, state_var, file_output])141 142    demo.launch(show_api=False)143