Dave67350 commited on
Commit
8996dc4
·
verified ·
1 Parent(s): 8550003

Update tools/ai_technical_doc.py

Browse files
Files changed (1) hide show
  1. tools/ai_technical_doc.py +29 -9
tools/ai_technical_doc.py CHANGED
@@ -1,13 +1,15 @@
1
  # tools/ai_technical_doc.py
2
  import datetime
 
3
  import re
4
  from fpdf import FPDF
5
  from langdetect import detect
6
  import gradio as gr
7
- import os
 
8
 
9
  # === PDF Export Function ===
10
- def export_text_to_pdf(text, output_path=None, language="en"):
11
  if output_path is None:
12
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
13
  output_path = f"technical_documentation_{timestamp}.pdf"
@@ -20,8 +22,19 @@ def export_text_to_pdf(text, output_path=None, language="en"):
20
  pdf.set_text_color(0, 51, 102)
21
  title = "Technical Documentation - AI Act (Art. 11)" if language == "en" else "Documentation Technique - AI Act"
22
  pdf.cell(0, 15, title, ln=True, align='C')
23
- pdf.ln(10)
24
-
 
 
 
 
 
 
 
 
 
 
 
25
  pdf.set_font("Arial", '', 12)
26
  pdf.set_text_color(0, 0, 0)
27
  for line in text.strip().split('\n'):
@@ -47,11 +60,12 @@ def export_text_to_pdf(text, output_path=None, language="en"):
47
  pdf.ln(5)
48
  else:
49
  pdf.multi_cell(0, 10, line)
 
50
  pdf.output(output_path)
51
  return output_path
52
 
53
  # === Questions ===
54
- QUESTIONS = [
55
  ("system_name", "What is the name of your AI system?"),
56
  ("provider", "Who is the provider or developer of the system?"),
57
  ("intended_purpose", "What is the intended purpose of the system?"),
@@ -66,11 +80,12 @@ QUESTIONS = [
66
  ("recordkeeping", "How are logs and records maintained?")
67
  ]
68
 
 
 
69
  def get_questions():
70
  return QUESTIONS
71
 
72
  def run_tool():
73
- import gradio as gr
74
  state = {"step": 0, "answers": {}}
75
 
76
  def step_by_step_agent(user_input, state):
@@ -86,13 +101,18 @@ def run_tool():
86
  state["step"] += 1
87
  return next_question, state, None
88
 
89
- content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
 
90
  detected_lang = detect(content)
91
- pdf_path = export_text_to_pdf(content, language=detected_lang)
92
  return "✅ Completed. Download your documentation below.", {"done": True}, pdf_path
93
 
94
  with gr.Blocks(title="AI Technical Documentation Tool") as demo:
95
- chatbot = gr.Chatbot(label="🧠 Technical Doc Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
 
 
 
 
96
  msg = gr.Textbox(label="Your answer")
97
  state_var = gr.State(state)
98
  file_output = gr.File(label="Download PDF", visible=True)
 
1
  # tools/ai_technical_doc.py
2
  import datetime
3
+ import os
4
  import re
5
  from fpdf import FPDF
6
  from langdetect import detect
7
  import gradio as gr
8
+
9
+ from tools.common import prepend_metadata_questions # Shared metadata question helper
10
 
11
  # === PDF Export Function ===
12
+ def export_text_to_pdf(text, answers, 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"technical_documentation_{timestamp}.pdf"
 
22
  pdf.set_text_color(0, 51, 102)
23
  title = "Technical Documentation - AI Act (Art. 11)" if language == "en" else "Documentation Technique - AI Act"
24
  pdf.cell(0, 15, title, ln=True, align='C')
25
+ pdf.ln(5)
26
+
27
+ # Metadata under title
28
+ pdf.set_font("Arial", 'I', 11)
29
+ pdf.set_text_color(80, 80, 80)
30
+ name = answers.get("user_name", "N/A")
31
+ role = answers.get("user_role", "N/A")
32
+ org = answers.get("organization_name", "N/A")
33
+ timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
34
+ pdf.multi_cell(0, 10, f"Completed by {name} ({role}) at {org} on {timestamp}", align="C")
35
+ pdf.ln(5)
36
+
37
+ # Main body
38
  pdf.set_font("Arial", '', 12)
39
  pdf.set_text_color(0, 0, 0)
40
  for line in text.strip().split('\n'):
 
60
  pdf.ln(5)
61
  else:
62
  pdf.multi_cell(0, 10, line)
63
+
64
  pdf.output(output_path)
65
  return output_path
66
 
67
  # === Questions ===
68
+ CORE_QUESTIONS = [
69
  ("system_name", "What is the name of your AI system?"),
70
  ("provider", "Who is the provider or developer of the system?"),
71
  ("intended_purpose", "What is the intended purpose of the system?"),
 
80
  ("recordkeeping", "How are logs and records maintained?")
81
  ]
82
 
83
+ QUESTIONS = prepend_metadata_questions(CORE_QUESTIONS)
84
+
85
  def get_questions():
86
  return QUESTIONS
87
 
88
  def run_tool():
 
89
  state = {"step": 0, "answers": {}}
90
 
91
  def step_by_step_agent(user_input, state):
 
101
  state["step"] += 1
102
  return next_question, state, None
103
 
104
+ # Final content for PDF
105
+ content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS if key not in ["user_name", "user_role", "organization_name"]])
106
  detected_lang = detect(content)
107
+ pdf_path = export_text_to_pdf(content, answers, language=detected_lang)
108
  return "✅ Completed. Download your documentation below.", {"done": True}, pdf_path
109
 
110
  with gr.Blocks(title="AI Technical Documentation Tool") as demo:
111
+ chatbot = gr.Chatbot(
112
+ label="🧠 Technical Doc Assistant",
113
+ value=[{"role": "assistant", "content": QUESTIONS[0][1]}],
114
+ type="messages"
115
+ )
116
  msg = gr.Textbox(label="Your answer")
117
  state_var = gr.State(state)
118
  file_output = gr.File(label="Download PDF", visible=True)