Spaces:
Sleeping
Sleeping
Update Gradio_UI.py
Browse files- Gradio_UI.py +28 -152
Gradio_UI.py
CHANGED
|
@@ -1,163 +1,39 @@
|
|
| 1 |
-
|
| 2 |
-
# coding=utf-8
|
| 3 |
-
import csv
|
| 4 |
-
import datetime
|
| 5 |
-
import mimetypes
|
| 6 |
-
import os
|
| 7 |
-
import re
|
| 8 |
-
import shutil
|
| 9 |
-
from typing import Optional
|
| 10 |
-
|
| 11 |
-
from smolagents.agent_types import AgentAudio, AgentImage, AgentText, handle_agent_output_types
|
| 12 |
-
from smolagents.agents import ActionStep, MultiStepAgent
|
| 13 |
-
from smolagents.memory import MemoryStep
|
| 14 |
-
from smolagents.utils import _is_package_available
|
| 15 |
-
|
| 16 |
import gradio as gr
|
| 17 |
from fpdf import FPDF
|
| 18 |
from langdetect import detect
|
| 19 |
|
| 20 |
-
|
| 21 |
-
def
|
| 22 |
-
|
| 23 |
-
pdf.add_page()
|
| 24 |
-
pdf.set_auto_page_break(auto=True, margin=15)
|
| 25 |
-
pdf.set_font("Arial", size=12)
|
| 26 |
-
|
| 27 |
-
title = "AI Act Compliance Register" if language == "en" else "Registre de Conformité AI Act"
|
| 28 |
-
pdf.set_font("Arial", 'B', 14)
|
| 29 |
-
pdf.cell(0, 10, title, ln=True)
|
| 30 |
-
pdf.ln(10)
|
| 31 |
-
pdf.set_font("Arial", size=12)
|
| 32 |
-
|
| 33 |
-
for line in text.split('\n'):
|
| 34 |
-
pdf.multi_cell(0, 10, line)
|
| 35 |
-
|
| 36 |
-
pdf.output(output_path)
|
| 37 |
-
return output_path
|
| 38 |
-
|
| 39 |
-
# === Sequential Questions ===
|
| 40 |
-
QUESTIONS = [
|
| 41 |
-
("organization_name", "What is the name of your organization?"),
|
| 42 |
-
("responsible_person", "Who is responsible for this AI system?"),
|
| 43 |
-
("deployment_date", "When is the AI system scheduled to be deployed?"),
|
| 44 |
-
("ai_type", "What type of AI system is it?"),
|
| 45 |
-
("ai_description", "Please briefly describe what the system does."),
|
| 46 |
-
("risk_level", "What is the risk level of this system (e.g., high, medium)?"),
|
| 47 |
-
("risk_justification", "Why do you consider it this risk level?"),
|
| 48 |
-
("data_evaluation", "How have you evaluated the training data?"),
|
| 49 |
-
("technical_docs", "What technical documentation is available?"),
|
| 50 |
-
("human_oversight", "What kind of human oversight is planned?"),
|
| 51 |
-
("transparency_measures", "What transparency mechanisms are in place?"),
|
| 52 |
-
("audit_frequency", "How often will the system be audited?"),
|
| 53 |
-
("compliance_contact", "Who is the contact person for compliance (email or name)?")
|
| 54 |
-
]
|
| 55 |
-
|
| 56 |
-
RESPONSES = {}
|
| 57 |
-
|
| 58 |
-
# === Interactive Collection Flow ===
|
| 59 |
-
def step_by_step_agent(user_input, state):
|
| 60 |
-
if state is None:
|
| 61 |
-
state = {"step": 0, "answers": {}}
|
| 62 |
-
|
| 63 |
-
step = state["step"]
|
| 64 |
-
answers = state["answers"]
|
| 65 |
-
|
| 66 |
-
if step > 0:
|
| 67 |
-
key, _ = QUESTIONS[step - 1]
|
| 68 |
-
answers[key] = user_input
|
| 69 |
-
|
| 70 |
-
if step < len(QUESTIONS):
|
| 71 |
-
next_question = QUESTIONS[step][1]
|
| 72 |
-
state["step"] += 1
|
| 73 |
-
return next_question, state, None # Ensure return matches expected 3 outputs
|
| 74 |
-
|
| 75 |
-
filled_template = f"""
|
| 76 |
-
# AI Act Compliance Register
|
| 77 |
-
|
| 78 |
-
## General Information
|
| 79 |
-
- **Organization**: {answers['organization_name']}
|
| 80 |
-
- **Responsible Person**: {answers['responsible_person']}
|
| 81 |
-
- **Deployment Date**: {answers['deployment_date']}
|
| 82 |
-
- **System Description**: {answers['ai_description']}
|
| 83 |
-
|
| 84 |
-
## Risk Category
|
| 85 |
-
- **Type**: {answers['ai_type']}
|
| 86 |
-
- **Risk Level**: {answers['risk_level']}
|
| 87 |
-
- **Justification**: {answers['risk_justification']}
|
| 88 |
-
|
| 89 |
-
## Compliance Measures
|
| 90 |
-
- **Data Evaluation**: {answers['data_evaluation']}
|
| 91 |
-
- **Technical Docs**: {answers['technical_docs']}
|
| 92 |
-
- **Human Oversight**: {answers['human_oversight']}
|
| 93 |
-
- **Transparency Measures**: {answers['transparency_measures']}
|
| 94 |
-
|
| 95 |
-
## Audit & Follow-up
|
| 96 |
-
- **Audit Frequency**: {answers['audit_frequency']}
|
| 97 |
-
- **Compliance Contact**: {answers['compliance_contact']}
|
| 98 |
-
|
| 99 |
-
---
|
| 100 |
-
Generated by AI Act Assistant.
|
| 101 |
-
"""
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
try:
|
| 106 |
-
detected_lang = detect(filled_template)
|
| 107 |
-
except:
|
| 108 |
-
detected_lang = "en"
|
| 109 |
-
|
| 110 |
-
flag = "🇬🇧" if detected_lang == "en" else "🇫🇷"
|
| 111 |
-
# Save to CSV
|
| 112 |
-
csv_file = "ai_act_registers.csv"
|
| 113 |
-
fieldnames = [key for key, _ in QUESTIONS] + ["timestamp"]
|
| 114 |
-
row_data = {**answers, "timestamp": datetime.datetime.now().isoformat()}
|
| 115 |
-
|
| 116 |
-
file_exists = os.path.isfile(csv_file)
|
| 117 |
-
with open(csv_file, mode="a", newline="", encoding="utf-8") as csvfile:
|
| 118 |
-
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
|
| 119 |
-
if not file_exists:
|
| 120 |
-
writer.writeheader()
|
| 121 |
-
writer.writerow(row_data)
|
| 122 |
-
|
| 123 |
-
pdf_path = export_text_to_pdf(filled_template, language=detected_lang)
|
| 124 |
-
return f"{flag} Language detected: {detected_lang.upper()}
|
| 125 |
-
✅ All answers received. Download your AI Act compliance PDF below.", {"done": True, "pdf": pdf_path}, pdf_path
|
| 126 |
-
|
| 127 |
-
# === UI for Step-by-Step Form ===
|
| 128 |
-
def launch_step_by_step_ui():
|
| 129 |
-
with gr.Blocks(fill_height=True) as demo:
|
| 130 |
-
initial_question = QUESTIONS[0][1]
|
| 131 |
-
initial_message = gr.ChatMessage(role="assistant", content="""
|
| 132 |
-
👋 Welcome! I will guide you through the AI Act compliance form.
|
| 133 |
-
Let's begin with a few questions to generate your compliance register.
|
| 134 |
-
|
| 135 |
-
What is the name of your organization?
|
| 136 |
-
""")
|
| 137 |
-
stored_messages = gr.State([initial_message])
|
| 138 |
-
chatbot = gr.Chatbot(type="messages", value=stored_messages)
|
| 139 |
-
msg = gr.Textbox(label="Your answer")
|
| 140 |
-
state = gr.State()
|
| 141 |
-
file_output = gr.File(visible=False)
|
| 142 |
-
restart_button = gr.Button("🔁 Restart")
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
-
|
| 158 |
-
restart_button.click(restart_conversation, outputs=[chatbot, state, file_output])
|
| 159 |
|
| 160 |
-
|
| 161 |
|
| 162 |
-
if __name__ == "__main__":
|
| 163 |
-
launch_step_by_step_ui()
|
|
|
|
| 1 |
+
# Gradio_UI.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from fpdf import FPDF
|
| 4 |
from langdetect import detect
|
| 5 |
|
| 6 |
+
class GradioUI:
|
| 7 |
+
def __init__(self, agent=None):
|
| 8 |
+
self.agent = agent # Optionnel si tu veux intégrer un agent SmolAgent plus tard
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def export_text_to_pdf(self, text, lang="en"):
|
| 11 |
+
pdf = FPDF()
|
| 12 |
+
pdf.add_page()
|
| 13 |
+
pdf.set_font("Arial", size=12)
|
| 14 |
+
title = "AI Act Compliance Register" if lang == "en" else "Registre de Conformité AI Act"
|
| 15 |
+
pdf.multi_cell(0, 10, title + "\n\n" + text)
|
| 16 |
+
output_path = "ai_act_register.pdf"
|
| 17 |
+
pdf.output(output_path)
|
| 18 |
+
return output_path
|
| 19 |
|
| 20 |
+
def interact(self, user_input, history):
|
| 21 |
+
history.append((user_input, None))
|
| 22 |
|
| 23 |
+
# Exemple simple de génération de contenu
|
| 24 |
+
fake_response = f"Thank you for your input: **{user_input}**.\n\n📄 Here is your draft..."
|
| 25 |
+
pdf_path = self.export_text_to_pdf(fake_response, lang=detect(user_input))
|
| 26 |
+
history[-1] = (user_input, fake_response)
|
| 27 |
+
return history, pdf_path
|
| 28 |
|
| 29 |
+
def launch(self):
|
| 30 |
+
with gr.Blocks(title="AI Act Assistant") as demo:
|
| 31 |
+
chatbot = gr.Chatbot(label="AI Legal Assistant")
|
| 32 |
+
user_input = gr.Textbox(placeholder="Ask about your AI system...", label="Your message")
|
| 33 |
+
pdf_output = gr.File(label="Download PDF", visible=True)
|
| 34 |
+
state = gr.State([])
|
| 35 |
|
| 36 |
+
user_input.submit(self.interact, [user_input, state], [chatbot, pdf_output])
|
|
|
|
| 37 |
|
| 38 |
+
demo.launch(share=True)
|
| 39 |
|
|
|
|
|
|