Spaces:
Sleeping
Sleeping
Update tools/ai_act_generator.py
Browse files- tools/ai_act_generator.py +44 -14
tools/ai_act_generator.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
# NEW TOOL: Generate AI Act Compliance Register
|
| 2 |
from smolagents import tool
|
| 3 |
-
|
| 4 |
-
#!/usr/bin/env python
|
| 5 |
# coding=utf-8
|
|
|
|
|
|
|
| 6 |
import mimetypes
|
| 7 |
import os
|
| 8 |
import re
|
|
@@ -64,18 +65,15 @@ def step_by_step_agent(user_input, state):
|
|
| 64 |
step = state["step"]
|
| 65 |
answers = state["answers"]
|
| 66 |
|
| 67 |
-
# Save answer from previous step
|
| 68 |
if step > 0:
|
| 69 |
key, _ = QUESTIONS[step - 1]
|
| 70 |
answers[key] = user_input
|
| 71 |
|
| 72 |
-
# If more questions to ask
|
| 73 |
if step < len(QUESTIONS):
|
| 74 |
next_question = QUESTIONS[step][1]
|
| 75 |
state["step"] += 1
|
| 76 |
-
return next_question, state
|
| 77 |
|
| 78 |
-
# All questions answered, generate document
|
| 79 |
filled_template = f"""
|
| 80 |
# AI Act Compliance Register
|
| 81 |
|
|
@@ -104,30 +102,62 @@ def step_by_step_agent(user_input, state):
|
|
| 104 |
Generated by AI Act Assistant.
|
| 105 |
"""
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
# === UI for Step-by-Step Form ===
|
| 111 |
def launch_step_by_step_ui():
|
| 112 |
with gr.Blocks(fill_height=True) as demo:
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
msg = gr.Textbox(label="Your answer")
|
| 115 |
state = gr.State()
|
| 116 |
file_output = gr.File(visible=False)
|
|
|
|
| 117 |
|
| 118 |
def chat_logic(user_msg, state):
|
| 119 |
-
reply,
|
| 120 |
messages = [gr.ChatMessage(role="user", content=user_msg)]
|
| 121 |
|
| 122 |
if isinstance(reply, str):
|
| 123 |
messages.append(gr.ChatMessage(role="assistant", content=reply))
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
| 129 |
|
| 130 |
msg.submit(chat_logic, [msg, state], [chatbot, state, file_output])
|
|
|
|
| 131 |
|
| 132 |
demo.launch()
|
| 133 |
|
|
|
|
| 1 |
# NEW TOOL: Generate AI Act Compliance Register
|
| 2 |
from smolagents import tool
|
| 3 |
+
c#!/usr/bin/env python
|
|
|
|
| 4 |
# coding=utf-8
|
| 5 |
+
import csv
|
| 6 |
+
import datetime
|
| 7 |
import mimetypes
|
| 8 |
import os
|
| 9 |
import re
|
|
|
|
| 65 |
step = state["step"]
|
| 66 |
answers = state["answers"]
|
| 67 |
|
|
|
|
| 68 |
if step > 0:
|
| 69 |
key, _ = QUESTIONS[step - 1]
|
| 70 |
answers[key] = user_input
|
| 71 |
|
|
|
|
| 72 |
if step < len(QUESTIONS):
|
| 73 |
next_question = QUESTIONS[step][1]
|
| 74 |
state["step"] += 1
|
| 75 |
+
return next_question, state, None # Ensure return matches expected 3 outputs
|
| 76 |
|
|
|
|
| 77 |
filled_template = f"""
|
| 78 |
# AI Act Compliance Register
|
| 79 |
|
|
|
|
| 102 |
Generated by AI Act Assistant.
|
| 103 |
"""
|
| 104 |
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
try:
|
| 108 |
+
detected_lang = detect(filled_template)
|
| 109 |
+
except:
|
| 110 |
+
detected_lang = "en"
|
| 111 |
+
|
| 112 |
+
flag = "🇬🇧" if detected_lang == "en" else "🇫🇷"
|
| 113 |
+
# Save to CSV
|
| 114 |
+
csv_file = "ai_act_registers.csv"
|
| 115 |
+
fieldnames = [key for key, _ in QUESTIONS] + ["timestamp"]
|
| 116 |
+
row_data = {**answers, "timestamp": datetime.datetime.now().isoformat()}
|
| 117 |
+
|
| 118 |
+
file_exists = os.path.isfile(csv_file)
|
| 119 |
+
with open(csv_file, mode="a", newline="", encoding="utf-8") as csvfile:
|
| 120 |
+
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
|
| 121 |
+
if not file_exists:
|
| 122 |
+
writer.writeheader()
|
| 123 |
+
writer.writerow(row_data)
|
| 124 |
+
|
| 125 |
+
pdf_path = export_text_to_pdf(filled_template, language=detected_lang)
|
| 126 |
+
return f"{flag} Language detected: {detected_lang.upper()}
|
| 127 |
+
✅ All answers received. Download your AI Act compliance PDF below.", {"done": True, "pdf": pdf_path}, pdf_path
|
| 128 |
|
| 129 |
# === UI for Step-by-Step Form ===
|
| 130 |
def launch_step_by_step_ui():
|
| 131 |
with gr.Blocks(fill_height=True) as demo:
|
| 132 |
+
initial_question = QUESTIONS[0][1]
|
| 133 |
+
initial_message = gr.ChatMessage(role="assistant", content="""
|
| 134 |
+
👋 Welcome! I will guide you through the AI Act compliance form.
|
| 135 |
+
Let's begin with a few questions to generate your compliance register.
|
| 136 |
+
|
| 137 |
+
What is the name of your organization?
|
| 138 |
+
""")
|
| 139 |
+
stored_messages = gr.State([initial_message])
|
| 140 |
+
chatbot = gr.Chatbot(type="messages", value=stored_messages)
|
| 141 |
msg = gr.Textbox(label="Your answer")
|
| 142 |
state = gr.State()
|
| 143 |
file_output = gr.File(visible=False)
|
| 144 |
+
restart_button = gr.Button("🔁 Restart")
|
| 145 |
|
| 146 |
def chat_logic(user_msg, state):
|
| 147 |
+
reply, updated_state, file_path = step_by_step_agent(user_msg, state)
|
| 148 |
messages = [gr.ChatMessage(role="user", content=user_msg)]
|
| 149 |
|
| 150 |
if isinstance(reply, str):
|
| 151 |
messages.append(gr.ChatMessage(role="assistant", content=reply))
|
| 152 |
|
| 153 |
+
file_path = file_path or ""
|
| 154 |
+
return messages, updated_state, file_path
|
| 155 |
+
|
| 156 |
+
def restart_conversation():
|
| 157 |
+
return [initial_message], {"step": 0, "answers": {}}, None
|
| 158 |
|
| 159 |
msg.submit(chat_logic, [msg, state], [chatbot, state, file_output])
|
| 160 |
+
restart_button.click(restart_conversation, outputs=[chatbot, state, file_output])
|
| 161 |
|
| 162 |
demo.launch()
|
| 163 |
|