Adibvafa
commited on
Commit
·
31c8268
1
Parent(s):
e6e3048
Make auth safer
Browse files
main.py
CHANGED
|
@@ -67,6 +67,48 @@ def resolve_medgemma_api_url(args) -> str:
|
|
| 67 |
return resolve_medgemma_api_url_from_value(getattr(args, "medgemma_api_url", None))
|
| 68 |
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
def initialize_agent(
|
| 71 |
prompt_file: str,
|
| 72 |
tools_to_use: Optional[List[str]] = None,
|
|
@@ -265,8 +307,8 @@ def parse_arguments():
|
|
| 265 |
parser.add_argument("--gradio-host", default="0.0.0.0", help="Gradio host address")
|
| 266 |
parser.add_argument("--gradio-port", type=int, default=8686, help="Gradio port")
|
| 267 |
parser.add_argument("--auth", nargs=2, metavar=("USERNAME", "PASSWORD"),
|
| 268 |
-
default=
|
| 269 |
-
help="Enable password authentication
|
| 270 |
parser.add_argument("--no-auth", action="store_true",
|
| 271 |
help="Disable authentication (public access)")
|
| 272 |
parser.add_argument("--share", action="store_true",
|
|
@@ -394,13 +436,8 @@ if __name__ == "__main__":
|
|
| 394 |
print(f"Selected tools: {selected_tools}")
|
| 395 |
print(f"Using system prompt: {args.system_prompt}")
|
| 396 |
|
| 397 |
-
# Set up authentication (
|
| 398 |
-
|
| 399 |
-
auth_credentials = None
|
| 400 |
-
print("⚠️ Authentication disabled (public access)")
|
| 401 |
-
else:
|
| 402 |
-
auth_credentials = tuple(args.auth) # Uses default ["admin", "adibjun"] if not specified
|
| 403 |
-
print(f"✅ Authentication enabled for user: {auth_credentials[0]}")
|
| 404 |
|
| 405 |
# Setup the MedGemma environment if the MedGemmaVQATool is selected
|
| 406 |
medgemma_base_url_from_setup: Optional[str] = None
|
|
|
|
| 67 |
return resolve_medgemma_api_url_from_value(getattr(args, "medgemma_api_url", None))
|
| 68 |
|
| 69 |
|
| 70 |
+
def resolve_auth_credentials(args) -> Optional[tuple]:
|
| 71 |
+
"""Resolve authentication credentials from CLI args or environment variables.
|
| 72 |
+
|
| 73 |
+
Resolution order:
|
| 74 |
+
1) Explicit --no-auth flag (returns None, no warnings)
|
| 75 |
+
2) Explicit --auth USERNAME PASSWORD (returns credentials tuple)
|
| 76 |
+
3) MEDRAX_AUTH_USERNAME and MEDRAX_AUTH_PASSWORD environment variables
|
| 77 |
+
4) Default to None with warning messages
|
| 78 |
+
|
| 79 |
+
Args:
|
| 80 |
+
args: Parsed command-line arguments
|
| 81 |
+
|
| 82 |
+
Returns:
|
| 83 |
+
Optional[tuple]: (username, password) tuple if auth is enabled, None otherwise
|
| 84 |
+
"""
|
| 85 |
+
if args.no_auth:
|
| 86 |
+
print("⚠️ Authentication disabled (public access)")
|
| 87 |
+
return None
|
| 88 |
+
|
| 89 |
+
if args.auth:
|
| 90 |
+
username, password = args.auth
|
| 91 |
+
print(f"✅ Authentication enabled for user: {username}")
|
| 92 |
+
return (username, password)
|
| 93 |
+
|
| 94 |
+
# Try to read from environment variables
|
| 95 |
+
auth_username = os.getenv("MEDRAX_AUTH_USERNAME")
|
| 96 |
+
auth_password = os.getenv("MEDRAX_AUTH_PASSWORD")
|
| 97 |
+
|
| 98 |
+
if auth_username and auth_password:
|
| 99 |
+
print(f"✅ Authentication enabled from environment for user: {auth_username}")
|
| 100 |
+
return (auth_username, auth_password)
|
| 101 |
+
|
| 102 |
+
# No auth specified anywhere - default to no auth with warning
|
| 103 |
+
print("⚠️ No authentication configured!")
|
| 104 |
+
print("⚠️ Running without authentication (public access)")
|
| 105 |
+
print("⚠️ To enable auth, either:")
|
| 106 |
+
print(" - Use --auth USERNAME PASSWORD")
|
| 107 |
+
print(" - Set MEDRAX_AUTH_USERNAME and MEDRAX_AUTH_PASSWORD in .env")
|
| 108 |
+
print(" - Or explicitly use --no-auth to suppress this warning")
|
| 109 |
+
return None
|
| 110 |
+
|
| 111 |
+
|
| 112 |
def initialize_agent(
|
| 113 |
prompt_file: str,
|
| 114 |
tools_to_use: Optional[List[str]] = None,
|
|
|
|
| 307 |
parser.add_argument("--gradio-host", default="0.0.0.0", help="Gradio host address")
|
| 308 |
parser.add_argument("--gradio-port", type=int, default=8686, help="Gradio port")
|
| 309 |
parser.add_argument("--auth", nargs=2, metavar=("USERNAME", "PASSWORD"),
|
| 310 |
+
default=None,
|
| 311 |
+
help="Enable password authentication with specified username and password")
|
| 312 |
parser.add_argument("--no-auth", action="store_true",
|
| 313 |
help="Disable authentication (public access)")
|
| 314 |
parser.add_argument("--share", action="store_true",
|
|
|
|
| 436 |
print(f"Selected tools: {selected_tools}")
|
| 437 |
print(f"Using system prompt: {args.system_prompt}")
|
| 438 |
|
| 439 |
+
# Set up authentication (reads from CLI, env vars, or requires explicit choice)
|
| 440 |
+
auth_credentials = resolve_auth_credentials(args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
|
| 442 |
# Setup the MedGemma environment if the MedGemmaVQATool is selected
|
| 443 |
medgemma_base_url_from_setup: Optional[str] = None
|