Dave67350 commited on
Commit
6ea1e62
·
verified ·
1 Parent(s): 7bcbd43

Update Gradio_UI.py

Browse files
Files changed (1) hide show
  1. Gradio_UI.py +19 -33
Gradio_UI.py CHANGED
@@ -1,39 +1,22 @@
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()
@@ -56,7 +39,7 @@ def export_pdf(title, answers, lang="en"):
56
  pdf.output(filename)
57
  return filename
58
 
59
- # === App State ===
60
  conversation_state = {
61
  "step": 0,
62
  "questions": [],
@@ -65,13 +48,19 @@ conversation_state = {
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"]):
@@ -87,24 +76,21 @@ def continue_conversation(user_input):
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)
 
1
+ # Gradio_UI.py
2
  import gradio as gr
3
  from fpdf import FPDF
4
  from langdetect import detect
5
  import time
6
+ import importlib
7
 
8
+ # === Map document choices to modules ===
9
+ DOCUMENT_TO_MODULE = {
10
  "AI Act": {
11
+ "Compliance Register": "tools.ai_act_register",
12
+ "High-Risk System Record": "tools.ai_high_risk_system"
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  },
14
  "GDPR": {
15
+ "Data Processing Record": "tools.gdpr_data_record"
 
 
 
 
 
16
  }
17
  }
18
 
19
+ # === PDF Export Function ===
20
  def export_pdf(title, answers, lang="en"):
21
  pdf = FPDF()
22
  pdf.add_page()
 
39
  pdf.output(filename)
40
  return filename
41
 
42
+ # === State Memory ===
43
  conversation_state = {
44
  "step": 0,
45
  "questions": [],
 
48
  "lang": "en"
49
  }
50
 
51
+ # === Load Tool Dynamically ===
52
  def start_conversation(reg, doc):
53
  conversation_state["step"] = 0
54
  conversation_state["answers"] = {}
 
55
  conversation_state["title"] = f"{reg} - {doc}"
56
+
57
+ module_path = DOCUMENT_TO_MODULE[reg][doc]
58
+ module = importlib.import_module(module_path)
59
+ conversation_state["questions"] = module.get_questions()
60
+
61
  return f"📋 {conversation_state['questions'][0][1]}"
62
 
63
+ # === Continue Chat ===
64
  def continue_conversation(user_input):
65
  step = conversation_state["step"]
66
  if step < len(conversation_state["questions"]):
 
76
  pdf_path = export_pdf(conversation_state["title"], conversation_state["answers"], lang=conversation_state["lang"])
77
  return "✅ Done! Your document is ready to download.", pdf_path
78
 
79
+ # === Gradio Interface ===
80
  with gr.Blocks(title="Regulatory Compliance Generator") as demo:
81
  gr.Markdown("## 📑 Regulation Document Generator")
82
 
83
  with gr.Row():
84
+ reg_dropdown = gr.Dropdown(choices=list(DOCUMENT_TO_MODULE.keys()), label="Select Regulation")
85
  doc_dropdown = gr.Dropdown(label="Select Document Type")
86
 
87
+ reg_dropdown.change(lambda r: list(DOCUMENT_TO_MODULE[r].keys()), inputs=reg_dropdown, outputs=doc_dropdown)
88
 
89
  start_btn = gr.Button("🚀 Start")
90
  chatbox = gr.Chatbot(label="🧑‍⚖️ Legalbot")
91
  user_input = gr.Textbox(label="Your answer")
92
  pdf_output = gr.File(label="Download PDF", visible=True)
93
 
 
 
 
94
  start_btn.click(start_conversation, inputs=[reg_dropdown, doc_dropdown], outputs=chatbox)
95
  user_input.submit(continue_conversation, inputs=user_input, outputs=[chatbox, pdf_output])
96
  user_input.submit(lambda: "", None, user_input)