Dave67350 commited on
Commit
3aa0340
·
verified ·
1 Parent(s): bd8d91b

Create corrective_action_log.py

Browse files
Files changed (1) hide show
  1. tools/corrective_action_log.py +98 -0
tools/corrective_action_log.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"
11
+
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(10)
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()
26
+ pdf.set_font("Arial", 'B', 13)
27
+ pdf.set_text_color(30, 30, 120)
28
+ pdf.ln(8)
29
+ pdf.cell(0, 10, section, ln=True)
30
+ pdf.set_font("Arial", '', 12)
31
+ pdf.set_text_color(0, 0, 0)
32
+ elif line.startswith("- **"):
33
+ match = re.match(r"- \*\*(.+?)\*\*: (.+)", line)
34
+ if match:
35
+ label, value = match.groups()
36
+ pdf.set_font("Arial", 'B', 12)
37
+ pdf.cell(0, 10, f"{label}:", ln=True)
38
+ pdf.set_font("Arial", '', 12)
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
+ QUESTIONS = [
46
+ ("incident_date", "When was the issue detected?"),
47
+ ("system_affected", "Which system/component was affected?"),
48
+ ("issue_description", "Briefly describe the issue."),
49
+ ("risk_level", "What was the risk level?"),
50
+ ("corrective_action", "What corrective action was taken?"),
51
+ ("person_responsible", "Who implemented the fix?"),
52
+ ("timeline", "What was the resolution timeline?"),
53
+ ("follow_up", "What follow-up was planned or done?")
54
+ ]
55
+
56
+ def get_questions():
57
+ return QUESTIONS
58
+
59
+ def run_tool():
60
+ state = {"step": 0, "answers": {}}
61
+
62
+ def step_by_step_agent(user_input, state):
63
+ step = state["step"]
64
+ answers = state["answers"]
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([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
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(label="🛠️ Corrective Log Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
80
+ msg = gr.Textbox(label="Your answer")
81
+ state_var = gr.State(state)
82
+ file_output = gr.File(label="Download PDF")
83
+ reset_btn = gr.Button("🔁 Restart")
84
+
85
+ def chat_logic(msg_in, state_in):
86
+ reply, updated_state, file = step_by_step_agent(msg_in, state_in)
87
+ messages = [{"role": "user", "content": msg_in}]
88
+ if reply:
89
+ messages.append({"role": "assistant", "content": reply})
90
+ return messages, updated_state, file
91
+
92
+ def reset():
93
+ return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
94
+
95
+ msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
96
+ reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
97
+
98
+ demo.launch()