Spaces:
Running
Running
Update api_usage.py
Browse files- api_usage.py +45 -4
api_usage.py
CHANGED
@@ -476,7 +476,7 @@ def check_key_aws_availability(key):
|
|
476 |
if not username[0]:
|
477 |
return False, "", "", "", "", "", username[1], "", ""
|
478 |
|
479 |
-
if username[0] == 'root':
|
480 |
root = True
|
481 |
admin = True
|
482 |
|
@@ -507,9 +507,9 @@ def check_username(session):
|
|
507 |
sts = session.client('sts')
|
508 |
sts_iden = sts.get_caller_identity()
|
509 |
if len(sts_iden['Arn'].split('/')) > 1:
|
510 |
-
return sts_iden['Arn'].split('/')[1], "Valid"
|
511 |
|
512 |
-
return sts_iden['Arn'].split(':')[5], "Valid"
|
513 |
except botocore.exceptions.ClientError as error:
|
514 |
return False, error.response['Error']['Code']
|
515 |
|
@@ -575,7 +575,48 @@ def check_aws_billing(session):
|
|
575 |
return ce_cost['ResultsByTime']
|
576 |
except botocore.exceptions.ClientError as error:
|
577 |
return error.response['Error']['Message']
|
578 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
579 |
if __name__ == "__main__":
|
580 |
key = os.getenv("OPENAI_API_KEY")
|
581 |
key_ant = os.getenv("ANTHROPIC_API_KEY")
|
|
|
476 |
if not username[0]:
|
477 |
return False, "", "", "", "", "", username[1], "", ""
|
478 |
|
479 |
+
if username[0] == 'root' and username[2]:
|
480 |
root = True
|
481 |
admin = True
|
482 |
|
|
|
507 |
sts = session.client('sts')
|
508 |
sts_iden = sts.get_caller_identity()
|
509 |
if len(sts_iden['Arn'].split('/')) > 1:
|
510 |
+
return sts_iden['Arn'].split('/')[1], "Valid", False
|
511 |
|
512 |
+
return sts_iden['Arn'].split(':')[5], "Valid", True
|
513 |
except botocore.exceptions.ClientError as error:
|
514 |
return False, error.response['Error']['Code']
|
515 |
|
|
|
575 |
return ce_cost['ResultsByTime']
|
576 |
except botocore.exceptions.ClientError as error:
|
577 |
return error.response['Error']['Message']
|
578 |
+
|
579 |
+
def check_key_or_availability(key):
|
580 |
+
url = "https://openrouter.ai/api/v1/auth/key"
|
581 |
+
headers = {'Authorization': f'Bearer {key}'}
|
582 |
+
|
583 |
+
rq = requests.get(url, headers=headers)
|
584 |
+
res = rq.json()
|
585 |
+
if rq.status_code == 200:
|
586 |
+
data = res['data']
|
587 |
+
rpm = data['rate_limit']['requests'] // int(data['rate_limit']['interval'].replace('s', '')) * 60
|
588 |
+
return True, data, rpm
|
589 |
+
return False, f"{res['error']['code']}: {res['error']['message']}", 0
|
590 |
+
|
591 |
+
def check_key_or_limits(key):
|
592 |
+
url = "https://openrouter.ai/api/v1/models"
|
593 |
+
headers = {"Authorization": f"Bearer {key}"}
|
594 |
+
models = {
|
595 |
+
"openai/gpt-4-turbo-preview": "",
|
596 |
+
"anthropic/claude-3-sonnet:beta": "",
|
597 |
+
"anthropic/claude-3-opus:beta":""
|
598 |
+
}
|
599 |
+
|
600 |
+
rq = requests.get(url, headers=headers)
|
601 |
+
res = rq.json()
|
602 |
+
|
603 |
+
balance = 0.0
|
604 |
+
count = 0
|
605 |
+
|
606 |
+
for model in res['data']:
|
607 |
+
if model['id'] in models.keys():
|
608 |
+
if count == 3:
|
609 |
+
break
|
610 |
+
prompt_tokens_limit = int(model.get("per_request_limits", "").get("prompt_tokens", ""))
|
611 |
+
completion_tokens_limit = int(model.get("per_request_limits", "").get("completion_tokens", ""))
|
612 |
+
models[model['id']] = { "Prompt": prompt_tokens_limit, "Completion": completion_tokens_limit }
|
613 |
+
if model['id'] == "anthropic/claude-3-sonnet:beta":
|
614 |
+
price_prompt = float(model.get("pricing", 0).get("prompt", 0))
|
615 |
+
price_completion = float(model.get("pricing", 0).get("completion", 0))
|
616 |
+
balance = (prompt_tokens_limit * price_prompt) + (completion_tokens_limit * price_completion)
|
617 |
+
count+=1
|
618 |
+
return balance, models
|
619 |
+
|
620 |
if __name__ == "__main__":
|
621 |
key = os.getenv("OPENAI_API_KEY")
|
622 |
key_ant = os.getenv("ANTHROPIC_API_KEY")
|