superdup95 commited on
Commit
a32637d
1 Parent(s): 054c5e2

Update api_usage.py

Browse files
Files changed (1) hide show
  1. api_usage.py +34 -27
api_usage.py CHANGED
@@ -512,8 +512,9 @@ def check_key_aws_availability(key):
512
  quarantine = False
513
  iam_full_access = False
514
  iam_policies_perm = False
515
- enable_region_v2 = 'None'
516
- enable_region_sonnet = 'None'
 
517
  session = boto3.Session(
518
  aws_access_key_id=access_id,
519
  aws_secret_access_key=access_secret
@@ -524,7 +525,7 @@ def check_key_aws_availability(key):
524
  username = check_username(session)
525
  #print(username)
526
  if not username[0]:
527
- return False, "", "", "", "", "", username[1], "", ""
528
 
529
  if username[0] == 'root' and username[2]:
530
  root = True
@@ -540,17 +541,15 @@ def check_key_aws_availability(key):
540
  iam_full_access = True
541
  if policy['PolicyName'] == 'AWSCompromisedKeyQuarantineV2':
542
  quarantine = True
 
 
 
 
543
 
544
  enable_region = check_bedrock_invoke(session)
545
-
546
- if enable_region[0]:
547
- enable_region_v2 = enable_region[0]
548
- if enable_region[1]:
549
- enable_region_sonnet = enable_region[1]
550
  cost = check_aws_billing(session)
551
- if enable_region[0]:
552
- return True, username[0], root, admin, iam_full_access, quarantine, enable_region_v2, enable_region_sonnet, cost
553
- return True, username[0], root, admin, iam_full_access, quarantine, enable_region_v2, enable_region_sonnet, cost
554
 
555
  def check_username(session):
556
  try:
@@ -592,24 +591,32 @@ def invoke_claude(session, region, modelId):
592
  #print(e)
593
  return
594
 
 
 
 
 
 
595
  def check_bedrock_invoke(session):
596
- regions = ['us-east-1', 'us-west-2', 'eu-central-1', 'ap-southeast-1', 'ap-northeast-1']
597
- enable_region = []
598
- enable_region_sonnet = []
599
- with concurrent.futures.ThreadPoolExecutor() as executer:
600
- futures = [executer.submit(invoke_claude, session, region, "anthropic.claude-v2") for region in regions]
601
- for future in concurrent.futures.as_completed(futures):
602
- if future.result() and (future.result() not in enable_region):
603
- enable_region.append(future.result())
604
- futures = [executer.submit(invoke_claude, session, region, "anthropic.claude-v2:1") for region in regions]
605
- for future in concurrent.futures.as_completed(futures):
606
- if future.result() and (future.result() not in enable_region):
607
- enable_region.append(future.result())
608
- futures = [executer.submit(invoke_claude, session, region, "anthropic.claude-3-sonnet-20240229-v1:0") for region in regions]
 
609
  for future in concurrent.futures.as_completed(futures):
610
- if future.result() and (future.result() not in enable_region_sonnet):
611
- enable_region_sonnet.append(future.result())
612
- return enable_region, enable_region_sonnet
 
 
613
 
614
  def check_aws_billing(session):
615
  try:
 
512
  quarantine = False
513
  iam_full_access = False
514
  iam_policies_perm = False
515
+ iam_user_change_password = False
516
+ aws_bedrock_full_access = False
517
+
518
  session = boto3.Session(
519
  aws_access_key_id=access_id,
520
  aws_secret_access_key=access_secret
 
525
  username = check_username(session)
526
  #print(username)
527
  if not username[0]:
528
+ return False, username[1]
529
 
530
  if username[0] == 'root' and username[2]:
531
  root = True
 
541
  iam_full_access = True
542
  if policy['PolicyName'] == 'AWSCompromisedKeyQuarantineV2':
543
  quarantine = True
544
+ if policy['PolicyName'] == 'IAMUserChangePassword':
545
+ iam_user_change_password = True
546
+ if policy['PolicyName'] == 'AmazonBedrockFullAccess':
547
+ aws_bedrock_full_access = True
548
 
549
  enable_region = check_bedrock_invoke(session)
 
 
 
 
 
550
  cost = check_aws_billing(session)
551
+
552
+ return True, username[0], root, admin, quarantine, iam_full_access, iam_user_change_password, aws_bedrock_full_access, enable_region, cost
 
553
 
554
  def check_username(session):
555
  try:
 
591
  #print(e)
592
  return
593
 
594
+ def invoke_and_collect(session, model_name, region):
595
+ result = invoke_claude(session, region, f"anthropic.{model_name}")
596
+ if result:
597
+ return model_name, result
598
+
599
  def check_bedrock_invoke(session):
600
+ regions = ['us-east-1', 'us-west-2', 'eu-central-1', 'eu-west-3', 'ap-southeast-1', 'ap-northeast-1']
601
+ models = {
602
+ "claude-v2": [],
603
+ "claude-3-haiku-20240307-v1:0": [],
604
+ "claude-3-sonnet-20240229-v1:0": [],
605
+ "claude-3-opus-20240229-v1:0": []
606
+ }
607
+
608
+ with concurrent.futures.ThreadPoolExecutor() as executor:
609
+ futures = []
610
+ for region in regions:
611
+ for model in models:
612
+ futures.append(executor.submit(invoke_and_collect, session, model, region))
613
+
614
  for future in concurrent.futures.as_completed(futures):
615
+ if future.result():
616
+ model_name, region = future.result()
617
+ models[model_name].append(region)
618
+
619
+ return models
620
 
621
  def check_aws_billing(session):
622
  try: