Spaces:
Sleeping
Sleeping
Update Gradio_UI.py
Browse files- Gradio_UI.py +105 -31
Gradio_UI.py
CHANGED
|
@@ -1,38 +1,112 @@
|
|
| 1 |
-
#
|
| 2 |
import gradio as gr
|
| 3 |
from fpdf import FPDF
|
| 4 |
from langdetect import detect
|
| 5 |
import time
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
pdf = FPDF()
|
| 9 |
pdf.add_page()
|
| 10 |
-
pdf.
|
| 11 |
-
|
| 12 |
-
pdf.
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
pdf.
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
],
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# gradio_UI.py
|
| 2 |
import gradio as gr
|
| 3 |
from fpdf import FPDF
|
| 4 |
from langdetect import detect
|
| 5 |
import time
|
| 6 |
|
| 7 |
+
# === Define documents/questions structure ===
|
| 8 |
+
DOCUMENTS = {
|
| 9 |
+
"AI Act": {
|
| 10 |
+
"Compliance Register": [
|
| 11 |
+
("organization_name", "What is the name of your organization?"),
|
| 12 |
+
("responsible_person", "Who is responsible for this AI system?"),
|
| 13 |
+
("deployment_date", "When is the AI system scheduled to be deployed?"),
|
| 14 |
+
("ai_type", "What type of AI system is it?"),
|
| 15 |
+
("ai_description", "What does the system do?"),
|
| 16 |
+
("risk_level", "What is the risk level?"),
|
| 17 |
+
("risk_justification", "Why this level?"),
|
| 18 |
+
],
|
| 19 |
+
"High-Risk System Record": [
|
| 20 |
+
("organization_name", "Organization name?"),
|
| 21 |
+
("system_purpose", "Purpose of the high-risk AI system?"),
|
| 22 |
+
("annex_category", "Which Annex III category applies?"),
|
| 23 |
+
("risk_controls", "Risk mitigation measures?"),
|
| 24 |
+
]
|
| 25 |
+
},
|
| 26 |
+
"GDPR": {
|
| 27 |
+
"Data Processing Record": [
|
| 28 |
+
("data_controller", "Who is the data controller?"),
|
| 29 |
+
("legal_basis", "Legal basis for processing?"),
|
| 30 |
+
("data_subjects", "Categories of data subjects?"),
|
| 31 |
+
("data_retention", "Retention period for data?"),
|
| 32 |
+
]
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# === PDF Export ===
|
| 37 |
+
def export_pdf(title, answers, lang="en"):
|
| 38 |
pdf = FPDF()
|
| 39 |
pdf.add_page()
|
| 40 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 41 |
+
pdf.set_font("Arial", 'B', 16)
|
| 42 |
+
pdf.set_text_color(0, 51, 102)
|
| 43 |
+
pdf.cell(0, 10, title, ln=True, align='C')
|
| 44 |
+
pdf.ln(10)
|
| 45 |
+
|
| 46 |
+
pdf.set_font("Arial", '', 12)
|
| 47 |
+
pdf.set_text_color(0, 0, 0)
|
| 48 |
+
for label, value in answers.items():
|
| 49 |
+
pdf.set_font("Arial", 'B', 12)
|
| 50 |
+
pdf.multi_cell(0, 10, f"{label}:", align='L')
|
| 51 |
+
pdf.set_font("Arial", '', 12)
|
| 52 |
+
pdf.multi_cell(0, 10, value)
|
| 53 |
+
pdf.ln(5)
|
| 54 |
+
|
| 55 |
+
filename = f"{title.replace(' ', '_')}_{int(time.time())}.pdf"
|
| 56 |
+
pdf.output(filename)
|
| 57 |
+
return filename
|
| 58 |
+
|
| 59 |
+
# === App State ===
|
| 60 |
+
conversation_state = {
|
| 61 |
+
"step": 0,
|
| 62 |
+
"questions": [],
|
| 63 |
+
"answers": {},
|
| 64 |
+
"title": "",
|
| 65 |
+
"lang": "en"
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
def start_conversation(reg, doc):
|
| 69 |
+
conversation_state["step"] = 0
|
| 70 |
+
conversation_state["answers"] = {}
|
| 71 |
+
conversation_state["questions"] = DOCUMENTS[reg][doc]
|
| 72 |
+
conversation_state["title"] = f"{reg} - {doc}"
|
| 73 |
+
return f"📋 {conversation_state['questions'][0][1]}"
|
| 74 |
+
|
| 75 |
+
def continue_conversation(user_input):
|
| 76 |
+
step = conversation_state["step"]
|
| 77 |
+
if step < len(conversation_state["questions"]):
|
| 78 |
+
key, question = conversation_state["questions"][step]
|
| 79 |
+
conversation_state["answers"][question] = user_input
|
| 80 |
+
conversation_state["step"] += 1
|
| 81 |
+
conversation_state["lang"] = detect(user_input)
|
| 82 |
+
|
| 83 |
+
if conversation_state["step"] < len(conversation_state["questions"]):
|
| 84 |
+
next_question = conversation_state["questions"][conversation_state["step"]][1]
|
| 85 |
+
return f"🧠 {next_question}", None
|
| 86 |
+
else:
|
| 87 |
+
pdf_path = export_pdf(conversation_state["title"], conversation_state["answers"], lang=conversation_state["lang"])
|
| 88 |
+
return "✅ Done! Your document is ready to download.", pdf_path
|
| 89 |
+
|
| 90 |
+
# === Gradio UI ===
|
| 91 |
+
with gr.Blocks(title="Regulatory Compliance Generator") as demo:
|
| 92 |
+
gr.Markdown("## 📑 Regulation Document Generator")
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
reg_dropdown = gr.Dropdown(choices=list(DOCUMENTS.keys()), label="Select Regulation")
|
| 96 |
+
doc_dropdown = gr.Dropdown(label="Select Document Type")
|
| 97 |
+
|
| 98 |
+
reg_dropdown.change(lambda r: list(DOCUMENTS[r].keys()), inputs=reg_dropdown, outputs=doc_dropdown)
|
| 99 |
+
|
| 100 |
+
start_btn = gr.Button("🚀 Start")
|
| 101 |
+
chatbox = gr.Chatbot(label="🧑⚖️ Legalbot")
|
| 102 |
+
user_input = gr.Textbox(label="Your answer")
|
| 103 |
+
pdf_output = gr.File(label="Download PDF", visible=True)
|
| 104 |
+
|
| 105 |
+
def reset():
|
| 106 |
+
return "", "", "", [], None
|
| 107 |
+
|
| 108 |
+
start_btn.click(start_conversation, inputs=[reg_dropdown, doc_dropdown], outputs=chatbox)
|
| 109 |
+
user_input.submit(continue_conversation, inputs=user_input, outputs=[chatbox, pdf_output])
|
| 110 |
+
user_input.submit(lambda: "", None, user_input)
|
| 111 |
+
|
| 112 |
+
demo.launch()
|