Dave67350 commited on
Commit
8faecf2
·
verified ·
1 Parent(s): 55ebae5

Update Gradio_UI.py

Browse files
Files changed (1) hide show
  1. Gradio_UI.py +16 -14
Gradio_UI.py CHANGED
@@ -12,8 +12,6 @@ from smolagents.memory import MemoryStep
12
  from smolagents.utils import _is_package_available
13
 
14
  import gradio as gr
15
- import csv
16
- import datetime
17
  from fpdf import FPDF
18
  from langdetect import detect
19
 
@@ -53,6 +51,8 @@ QUESTIONS = [
53
  ("compliance_contact", "Who is the contact person for compliance (email or name)?")
54
  ]
55
 
 
 
56
  # === Interactive Collection Flow ===
57
  def step_by_step_agent(user_input, state):
58
  if state is None:
@@ -98,13 +98,15 @@ def step_by_step_agent(user_input, state):
98
  Generated by AI Act Assistant.
99
  """
100
 
 
 
 
101
  try:
102
  detected_lang = detect(filled_template)
103
  except:
104
  detected_lang = "en"
105
 
106
  flag = "🇬🇧" if detected_lang == "en" else "🇫🇷"
107
-
108
  # Save to CSV
109
  csv_file = "ai_act_registers.csv"
110
  fieldnames = [key for key, _ in QUESTIONS] + ["timestamp"]
@@ -118,18 +120,24 @@ Generated by AI Act Assistant.
118
  writer.writerow(row_data)
119
 
120
  pdf_path = export_text_to_pdf(filled_template, language=detected_lang)
121
- return f"{flag} Language detected: {detected_lang.upper()}\n✅ All answers received. Download your AI Act compliance PDF below.", {"done": True, "pdf": pdf_path}
 
122
 
123
  # === UI for Step-by-Step Form ===
124
  def launch_step_by_step_ui():
125
  with gr.Blocks(fill_height=True) as demo:
126
- reset_csv = gr.Button("🗑️ Reset CSV File")
127
- csv_download = gr.File(value="ai_act_registers.csv", label="📥 Download All Registers (.csv)", visible=os.path.exists("ai_act_registers.csv"))
128
-
129
  initial_message = gr.ChatMessage(role="assistant", content=(
 
 
130
  "👋 Welcome! I will guide you through the AI Act compliance form.\n"
131
  "Let's begin with a few questions to generate your compliance register.\n"
132
- "Please answer the following:"
 
 
 
 
 
133
  ))
134
  stored_messages = gr.State([initial_message])
135
  chatbot = gr.Chatbot(type="messages", value=stored_messages)
@@ -150,14 +158,8 @@ def launch_step_by_step_ui():
150
  def restart_conversation():
151
  return [initial_message], {"step": 0, "answers": {}}, None
152
 
153
- def delete_csv():
154
- if os.path.exists("ai_act_registers.csv"):
155
- os.remove("ai_act_registers.csv")
156
- return None
157
-
158
  msg.submit(chat_logic, [msg, state], [chatbot, state, file_output])
159
  restart_button.click(restart_conversation, outputs=[chatbot, state, file_output])
160
- reset_csv.click(delete_csv, outputs=[])
161
 
162
  demo.launch()
163
 
 
12
  from smolagents.utils import _is_package_available
13
 
14
  import gradio as gr
 
 
15
  from fpdf import FPDF
16
  from langdetect import detect
17
 
 
51
  ("compliance_contact", "Who is the contact person for compliance (email or name)?")
52
  ]
53
 
54
+ RESPONSES = {}
55
+
56
  # === Interactive Collection Flow ===
57
  def step_by_step_agent(user_input, state):
58
  if state is None:
 
98
  Generated by AI Act Assistant.
99
  """
100
 
101
+ import csv
102
+ import datetime
103
+
104
  try:
105
  detected_lang = detect(filled_template)
106
  except:
107
  detected_lang = "en"
108
 
109
  flag = "🇬🇧" if detected_lang == "en" else "🇫🇷"
 
110
  # Save to CSV
111
  csv_file = "ai_act_registers.csv"
112
  fieldnames = [key for key, _ in QUESTIONS] + ["timestamp"]
 
120
  writer.writerow(row_data)
121
 
122
  pdf_path = export_text_to_pdf(filled_template, language=detected_lang)
123
+ return f"{flag} Language detected: {detected_lang.upper()}
124
+ ✅ All answers received. Download your AI Act compliance PDF below.", {"done": True, "pdf": pdf_path}
125
 
126
  # === UI for Step-by-Step Form ===
127
  def launch_step_by_step_ui():
128
  with gr.Blocks(fill_height=True) as demo:
129
+ initial_question = QUESTIONS[0][1]
 
 
130
  initial_message = gr.ChatMessage(role="assistant", content=(
131
+ "👋 Welcome! I will guide you through the AI Act compliance form.
132
+ "
133
  "👋 Welcome! I will guide you through the AI Act compliance form.\n"
134
  "Let's begin with a few questions to generate your compliance register.\n"
135
+ "Please answer the following:
136
+
137
+ " + QUESTIONS[0][1]
138
+ }
139
+ ]
140
+ }"
141
  ))
142
  stored_messages = gr.State([initial_message])
143
  chatbot = gr.Chatbot(type="messages", value=stored_messages)
 
158
  def restart_conversation():
159
  return [initial_message], {"step": 0, "answers": {}}, None
160
 
 
 
 
 
 
161
  msg.submit(chat_logic, [msg, state], [chatbot, state, file_output])
162
  restart_button.click(restart_conversation, outputs=[chatbot, state, file_output])
 
163
 
164
  demo.launch()
165