humanizetech commited on
Commit
615fd07
Β·
1 Parent(s): 2a5a030

feat: enable and disable Gradio buttons during security tests and tutor interactions

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -51,11 +51,12 @@ def run_security_test(
51
  api_endpoint: str,
52
  http_method: str,
53
  api_key: Optional[str] = None,
54
- ) -> Generator[Tuple[str, str, str], None, None]:
55
- """Yield progress, report markdown and report file path for Gradio.
56
 
57
  The function validates inputs, starts the VAPT agent in a background thread,
58
- and periodically yields any new progress messages.
 
59
  """
60
  # ---------- Validation ----------
61
  if not api_endpoint or not api_endpoint.strip():
@@ -63,6 +64,7 @@ def run_security_test(
63
  "❌ Error: API endpoint is required",
64
  "## Error\n\nPlease provide a valid API endpoint URL.",
65
  None,
 
66
  )
67
  return
68
  if not api_endpoint.startswith(("http://", "https://")):
@@ -70,6 +72,7 @@ def run_security_test(
70
  "❌ Error: Invalid URL format",
71
  "## Error\n\nAPI endpoint must start with `http://` or `https://`.",
72
  None,
 
73
  )
74
  return
75
 
@@ -91,6 +94,7 @@ def run_security_test(
91
  add_progress("πŸš€ Initializing VAPT Agent..."),
92
  "## Starting Security Test\n\nPlease wait while we assess your API endpoint...",
93
  None,
 
94
  )
95
 
96
  # Prepare request headers
@@ -101,6 +105,7 @@ def run_security_test(
101
  add_progress("πŸ”‘ API key provided – will test authenticated endpoints"),
102
  "## Starting Security Test\n\nPreparing to test with authentication...",
103
  None,
 
104
  )
105
 
106
  # ---------- Run agent in background thread ----------
@@ -138,6 +143,7 @@ def run_security_test(
138
  add_progress("πŸ”Œ Connecting to security engine..."),
139
  "## Starting Security Test\n\nConnecting...",
140
  None,
 
141
  )
142
  threading.Thread(target=agent_worker, daemon=True).start()
143
 
@@ -151,6 +157,7 @@ def run_security_test(
151
  "\n".join(progress_messages),
152
  "## Security Test in Progress\n\nPlease wait while the agent performs testing...",
153
  None,
 
154
  )
155
  last_len = len(progress_messages)
156
 
@@ -162,12 +169,14 @@ def run_security_test(
162
  add_progress(f"⏱️ {err}"),
163
  "## Error\n\n**Timeout Error**\n\nThe assessment exceeded the allowed time.",
164
  None,
 
165
  )
166
  else:
167
  yield (
168
  add_progress(f"❌ Error: {err}"),
169
  f"## Error\n\n**Exception Occurred**\n\n```\n{err}\n```\n\nPlease check configuration and retry.",
170
  None,
 
171
  )
172
  else:
173
  # Success – return report and file path
@@ -175,6 +184,7 @@ def run_security_test(
175
  add_progress("βœ… Security assessment completed successfully!"),
176
  result["report_content"] or "## Error\n\nNo report was generated.",
177
  result["report_file_path"],
 
178
  )
179
 
180
 
@@ -352,7 +362,7 @@ def create_gradio_interface() -> gr.Blocks:
352
  submit_btn.click(
353
  fn=run_security_test,
354
  inputs=[api_endpoint, http_method, api_key],
355
- outputs=[progress_output, report_output, report_file],
356
  show_progress=True,
357
  )
358
 
@@ -433,13 +443,13 @@ def create_gradio_interface() -> gr.Blocks:
433
  new_history.append({"role": "user", "content": question})
434
  new_history.append({"role": "assistant", "content": answer})
435
 
436
- # Clear the input textbox
437
- return new_history, ""
438
 
439
  tutor_btn.click(
440
  fn=tutor_respond,
441
  inputs=[tutor_input, chatbot, report_output],
442
- outputs=[chatbot, tutor_input],
443
  )
444
 
445
  return iface
@@ -472,6 +482,5 @@ def main():
472
  #iface.launch(server_name="0.0.0.0", server_port=7861, share=True, inbrowser=True)
473
  iface.launch(server_name="0.0.0.0")
474
 
475
-
476
  if __name__ == "__main__":
477
  main()
 
51
  api_endpoint: str,
52
  http_method: str,
53
  api_key: Optional[str] = None,
54
+ ) -> Generator[Tuple[str, str, str, gr.Button], None, None]:
55
+ """Yield progress, report markdown, report file path, and button state for Gradio.
56
 
57
  The function validates inputs, starts the VAPT agent in a background thread,
58
+ and periodically yields any new progress messages. The button is disabled during
59
+ the test and re-enabled when complete.
60
  """
61
  # ---------- Validation ----------
62
  if not api_endpoint or not api_endpoint.strip():
 
64
  "❌ Error: API endpoint is required",
65
  "## Error\n\nPlease provide a valid API endpoint URL.",
66
  None,
67
+ gr.Button(interactive=True),
68
  )
69
  return
70
  if not api_endpoint.startswith(("http://", "https://")):
 
72
  "❌ Error: Invalid URL format",
73
  "## Error\n\nAPI endpoint must start with `http://` or `https://`.",
74
  None,
75
+ gr.Button(interactive=True),
76
  )
77
  return
78
 
 
94
  add_progress("πŸš€ Initializing VAPT Agent..."),
95
  "## Starting Security Test\n\nPlease wait while we assess your API endpoint...",
96
  None,
97
+ gr.Button(interactive=False),
98
  )
99
 
100
  # Prepare request headers
 
105
  add_progress("πŸ”‘ API key provided – will test authenticated endpoints"),
106
  "## Starting Security Test\n\nPreparing to test with authentication...",
107
  None,
108
+ gr.Button(interactive=False),
109
  )
110
 
111
  # ---------- Run agent in background thread ----------
 
143
  add_progress("πŸ”Œ Connecting to security engine..."),
144
  "## Starting Security Test\n\nConnecting...",
145
  None,
146
+ gr.Button(interactive=False),
147
  )
148
  threading.Thread(target=agent_worker, daemon=True).start()
149
 
 
157
  "\n".join(progress_messages),
158
  "## Security Test in Progress\n\nPlease wait while the agent performs testing...",
159
  None,
160
+ gr.Button(interactive=False),
161
  )
162
  last_len = len(progress_messages)
163
 
 
169
  add_progress(f"⏱️ {err}"),
170
  "## Error\n\n**Timeout Error**\n\nThe assessment exceeded the allowed time.",
171
  None,
172
+ gr.Button(interactive=True),
173
  )
174
  else:
175
  yield (
176
  add_progress(f"❌ Error: {err}"),
177
  f"## Error\n\n**Exception Occurred**\n\n```\n{err}\n```\n\nPlease check configuration and retry.",
178
  None,
179
+ gr.Button(interactive=True),
180
  )
181
  else:
182
  # Success – return report and file path
 
184
  add_progress("βœ… Security assessment completed successfully!"),
185
  result["report_content"] or "## Error\n\nNo report was generated.",
186
  result["report_file_path"],
187
+ gr.Button(interactive=True),
188
  )
189
 
190
 
 
362
  submit_btn.click(
363
  fn=run_security_test,
364
  inputs=[api_endpoint, http_method, api_key],
365
+ outputs=[progress_output, report_output, report_file, submit_btn],
366
  show_progress=True,
367
  )
368
 
 
443
  new_history.append({"role": "user", "content": question})
444
  new_history.append({"role": "assistant", "content": answer})
445
 
446
+ # Clear the input textbox and re-enable the button
447
+ return new_history, "", gr.Button(interactive=True)
448
 
449
  tutor_btn.click(
450
  fn=tutor_respond,
451
  inputs=[tutor_input, chatbot, report_output],
452
+ outputs=[chatbot, tutor_input, tutor_btn],
453
  )
454
 
455
  return iface
 
482
  #iface.launch(server_name="0.0.0.0", server_port=7861, share=True, inbrowser=True)
483
  iface.launch(server_name="0.0.0.0")
484
 
 
485
  if __name__ == "__main__":
486
  main()