superdup95
commited on
Commit
•
339fa5d
1
Parent(s):
1dcf9a8
Create awsLib.py
Browse files
awsLib.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json, asyncio
|
2 |
+
import requests
|
3 |
+
from requests_auth_aws_sigv4 import AWSSigV4
|
4 |
+
|
5 |
+
# lib by drago
|
6 |
+
# For getting form data | foundation-model-availability
|
7 |
+
async def bedrock_model_available(session, key_id="", secret_key="", region="us-east-1", model="anthropic.claude-3-sonnet-20240229-v1%3A"):
|
8 |
+
try:
|
9 |
+
model = model.replace(":","%3A") # issue with urlencode just this so should be fine just doint it with replace
|
10 |
+
except Exception as e:
|
11 |
+
pass
|
12 |
+
endpoint = f'https://bedrock.{region}.amazonaws.com/foundation-model-availability/'+model
|
13 |
+
|
14 |
+
headers = {
|
15 |
+
'authority': 'bedrock.us-east-1.amazonaws.com',
|
16 |
+
'accept': '*/*',
|
17 |
+
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8,pl;q=0.7',
|
18 |
+
'amz-sdk-invocation-id': '17a6e1df-4d2a-41c8-87fd-991da34b2a92',
|
19 |
+
'amz-sdk-request': 'attempt=1; max=3',
|
20 |
+
'origin': 'https://us-east-1.console.aws.amazon.com',
|
21 |
+
'referer': 'https://us-east-1.console.aws.amazon.com/',
|
22 |
+
'sec-ch-ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
|
23 |
+
'sec-ch-ua-mobile': '?0',
|
24 |
+
'sec-ch-ua-platform': '"Windows"',
|
25 |
+
'sec-fetch-dest': 'empty',
|
26 |
+
'sec-fetch-mode': 'cors',
|
27 |
+
'sec-fetch-site': 'cross-site',
|
28 |
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
|
29 |
+
}
|
30 |
+
aws_auth = AWSSigV4("bedrock",
|
31 |
+
aws_access_key_id=key_id,
|
32 |
+
aws_secret_access_key=secret_key,
|
33 |
+
region=region
|
34 |
+
)
|
35 |
+
|
36 |
+
loop = asyncio.get_event_loop()
|
37 |
+
req = requests.Request('GET', endpoint, headers=headers)
|
38 |
+
prepared_req = req.prepare()
|
39 |
+
signed_req = await loop.run_in_executor(None, aws_auth, prepared_req)
|
40 |
+
|
41 |
+
async with session.get(endpoint, headers=signed_req.headers, data=signed_req.body) as response:
|
42 |
+
if response.status != 200:
|
43 |
+
response_data = await response.text()
|
44 |
+
return json.loads(response_data)
|
45 |
+
|
46 |
+
return await response.json()
|
47 |
+
|
48 |
+
# For sending FAKE form, to detect quarantine / blocked zone
|
49 |
+
async def bedrock_send_fake_form(session, key_id="", secret_key="", region="us-east-1", form_data=""):
|
50 |
+
endpoint = f'https://bedrock.{region}.amazonaws.com/use-case-for-model-access'
|
51 |
+
|
52 |
+
headers = {
|
53 |
+
'authority': 'bedrock.us-east-1.amazonaws.com',
|
54 |
+
'accept': '*/*',
|
55 |
+
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8,pl;q=0.7',
|
56 |
+
'amz-sdk-invocation-id': '17a6e1df-4d2a-41c8-87fd-991da34b2a92',
|
57 |
+
'amz-sdk-request': 'attempt=1; max=3',
|
58 |
+
'origin': 'https://us-east-1.console.aws.amazon.com',
|
59 |
+
'referer': 'https://us-east-1.console.aws.amazon.com/',
|
60 |
+
'sec-ch-ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
|
61 |
+
'sec-ch-ua-mobile': '?0',
|
62 |
+
'sec-ch-ua-platform': '"Windows"',
|
63 |
+
'sec-fetch-dest': 'empty',
|
64 |
+
'sec-fetch-mode': 'cors',
|
65 |
+
'sec-fetch-site': 'cross-site',
|
66 |
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
|
67 |
+
}
|
68 |
+
|
69 |
+
|
70 |
+
aws_auth = AWSSigV4("bedrock",
|
71 |
+
aws_access_key_id=key_id,
|
72 |
+
aws_secret_access_key=secret_key,
|
73 |
+
region=region
|
74 |
+
)
|
75 |
+
|
76 |
+
loop = asyncio.get_event_loop()
|
77 |
+
req = requests.Request('POST', endpoint, headers=headers)
|
78 |
+
prepared_req = req.prepare()
|
79 |
+
signed_req = await loop.run_in_executor(None, aws_auth, prepared_req)
|
80 |
+
|
81 |
+
async with session.post(endpoint, headers=signed_req.headers, data=signed_req.body) as response:
|
82 |
+
if response.status != 200:
|
83 |
+
response_data = await response.text()
|
84 |
+
return json.loads(response_data)
|
85 |
+
|
86 |
+
return await response.json()
|