Spaces:
Sleeping
Sleeping
Update tools/corrective_action_log.py
Browse files
tools/corrective_action_log.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
# tools/corrective_action_log.py
|
| 2 |
-
import datetime, re
|
| 3 |
from fpdf import FPDF
|
| 4 |
from langdetect import detect
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
|
| 7 |
-
def export_text_to_pdf(text, output_path=None, language="en"):
|
| 8 |
if output_path is None:
|
| 9 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 10 |
output_path = f"corrective_action_log_{timestamp}.pdf"
|
|
@@ -12,14 +13,25 @@ def export_text_to_pdf(text, output_path=None, language="en"):
|
|
| 12 |
pdf = FPDF()
|
| 13 |
pdf.add_page()
|
| 14 |
pdf.set_auto_page_break(auto=True, margin=15)
|
|
|
|
| 15 |
pdf.set_font("Arial", 'B', 16)
|
| 16 |
pdf.set_text_color(0, 51, 102)
|
| 17 |
title = "Corrective Action Log" if language == "en" else "Journal des Mesures Correctives"
|
| 18 |
pdf.cell(0, 15, title, ln=True, align='C')
|
| 19 |
-
pdf.ln(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
pdf.set_font("Arial", '', 12)
|
| 21 |
pdf.set_text_color(0, 0, 0)
|
| 22 |
-
|
| 23 |
for line in text.strip().split('\n'):
|
| 24 |
if line.startswith("## "):
|
| 25 |
section = line.replace("## ", "").strip()
|
|
@@ -39,10 +51,12 @@ def export_text_to_pdf(text, output_path=None, language="en"):
|
|
| 39 |
pdf.multi_cell(0, 10, value)
|
| 40 |
else:
|
| 41 |
pdf.multi_cell(0, 10, line)
|
|
|
|
| 42 |
pdf.output(output_path)
|
| 43 |
return output_path
|
| 44 |
|
| 45 |
-
|
|
|
|
| 46 |
("incident_date", "When was the issue detected?"),
|
| 47 |
("system_affected", "Which system/component was affected?"),
|
| 48 |
("issue_description", "Briefly describe the issue."),
|
|
@@ -53,6 +67,8 @@ QUESTIONS = [
|
|
| 53 |
("follow_up", "What follow-up was planned or done?")
|
| 54 |
]
|
| 55 |
|
|
|
|
|
|
|
| 56 |
def get_questions():
|
| 57 |
return QUESTIONS
|
| 58 |
|
|
@@ -65,18 +81,27 @@ def run_tool():
|
|
| 65 |
if step > 0:
|
| 66 |
key, _ = QUESTIONS[step - 1]
|
| 67 |
answers[key] = user_input
|
|
|
|
| 68 |
if step < len(QUESTIONS):
|
| 69 |
question = QUESTIONS[step][1]
|
| 70 |
state["step"] += 1
|
| 71 |
return question, state, None
|
| 72 |
|
| 73 |
-
content = "\n".join([
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
lang = detect(content)
|
| 75 |
-
pdf_path = export_text_to_pdf(content, language=lang)
|
| 76 |
return "✅ Log complete. Download your corrective action record below.", {"done": True}, pdf_path
|
| 77 |
|
| 78 |
-
with gr.Blocks() as demo:
|
| 79 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
msg = gr.Textbox(label="Your answer")
|
| 81 |
state_var = gr.State(state)
|
| 82 |
file_output = gr.File(label="Download PDF")
|
|
|
|
| 1 |
# tools/corrective_action_log.py
|
| 2 |
+
import datetime, re, os
|
| 3 |
from fpdf import FPDF
|
| 4 |
from langdetect import detect
|
| 5 |
import gradio as gr
|
| 6 |
+
from tools.common import prepend_metadata_questions # import shared metadata logic
|
| 7 |
|
| 8 |
+
def export_text_to_pdf(text, answers, output_path=None, language="en"):
|
| 9 |
if output_path is None:
|
| 10 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 11 |
output_path = f"corrective_action_log_{timestamp}.pdf"
|
|
|
|
| 13 |
pdf = FPDF()
|
| 14 |
pdf.add_page()
|
| 15 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 16 |
+
|
| 17 |
pdf.set_font("Arial", 'B', 16)
|
| 18 |
pdf.set_text_color(0, 51, 102)
|
| 19 |
title = "Corrective Action Log" if language == "en" else "Journal des Mesures Correctives"
|
| 20 |
pdf.cell(0, 15, title, ln=True, align='C')
|
| 21 |
+
pdf.ln(5)
|
| 22 |
+
|
| 23 |
+
# Metadata
|
| 24 |
+
pdf.set_font("Arial", 'I', 11)
|
| 25 |
+
pdf.set_text_color(80, 80, 80)
|
| 26 |
+
name = answers.get("user_name", "N/A")
|
| 27 |
+
role = answers.get("user_role", "N/A")
|
| 28 |
+
org = answers.get("organization_name", "N/A")
|
| 29 |
+
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 30 |
+
pdf.multi_cell(0, 10, f"Completed by {name} ({role}) at {org} on {timestamp}", align="C")
|
| 31 |
+
pdf.ln(5)
|
| 32 |
+
|
| 33 |
pdf.set_font("Arial", '', 12)
|
| 34 |
pdf.set_text_color(0, 0, 0)
|
|
|
|
| 35 |
for line in text.strip().split('\n'):
|
| 36 |
if line.startswith("## "):
|
| 37 |
section = line.replace("## ", "").strip()
|
|
|
|
| 51 |
pdf.multi_cell(0, 10, value)
|
| 52 |
else:
|
| 53 |
pdf.multi_cell(0, 10, line)
|
| 54 |
+
|
| 55 |
pdf.output(output_path)
|
| 56 |
return output_path
|
| 57 |
|
| 58 |
+
# === Questions ===
|
| 59 |
+
CORE_QUESTIONS = [
|
| 60 |
("incident_date", "When was the issue detected?"),
|
| 61 |
("system_affected", "Which system/component was affected?"),
|
| 62 |
("issue_description", "Briefly describe the issue."),
|
|
|
|
| 67 |
("follow_up", "What follow-up was planned or done?")
|
| 68 |
]
|
| 69 |
|
| 70 |
+
QUESTIONS = prepend_metadata_questions(CORE_QUESTIONS)
|
| 71 |
+
|
| 72 |
def get_questions():
|
| 73 |
return QUESTIONS
|
| 74 |
|
|
|
|
| 81 |
if step > 0:
|
| 82 |
key, _ = QUESTIONS[step - 1]
|
| 83 |
answers[key] = user_input
|
| 84 |
+
|
| 85 |
if step < len(QUESTIONS):
|
| 86 |
question = QUESTIONS[step][1]
|
| 87 |
state["step"] += 1
|
| 88 |
return question, state, None
|
| 89 |
|
| 90 |
+
content = "\n".join([
|
| 91 |
+
f"- **{label}**: {answers.get(key, '')}"
|
| 92 |
+
for key, label in QUESTIONS
|
| 93 |
+
if key not in ["user_name", "user_role", "organization_name"]
|
| 94 |
+
])
|
| 95 |
lang = detect(content)
|
| 96 |
+
pdf_path = export_text_to_pdf(content, answers, language=lang)
|
| 97 |
return "✅ Log complete. Download your corrective action record below.", {"done": True}, pdf_path
|
| 98 |
|
| 99 |
+
with gr.Blocks(title="Corrective Action Log Tool") as demo:
|
| 100 |
+
chatbot = gr.Chatbot(
|
| 101 |
+
label="🛠️ Corrective Log Assistant",
|
| 102 |
+
value=[{"role": "assistant", "content": QUESTIONS[0][1]}],
|
| 103 |
+
type="messages"
|
| 104 |
+
)
|
| 105 |
msg = gr.Textbox(label="Your answer")
|
| 106 |
state_var = gr.State(state)
|
| 107 |
file_output = gr.File(label="Download PDF")
|