abhi12ravi commited on
Commit
e63e93f
1 Parent(s): df3948b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -8
app.py CHANGED
@@ -3,22 +3,38 @@ import random
3
  import requests
4
  import os
5
  from datetime import datetime
 
 
6
 
 
 
 
 
 
 
 
 
 
7
 
8
  def generate_response(input, history):
9
  #bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
10
 
 
 
 
 
 
 
11
  #Hit the Sagemaker API and get results
12
 
13
  url = "https://runtime.sagemaker.eu-west-1.amazonaws.com/endpoints/jumpstart-dft-meta-textgeneration-llama-2-7b-f/invocations"
14
- authorization_token = os.environ['Authorization']
15
- headers = {
16
- "Content-Type": "application/json",
17
- "Authorization": authorization_token, # Replace with your actual token
18
- "X-Amz-Date": datetime.utcnow().strftime("%Y%m%dT%H%M%SZ"), # Using ISO-8601 basic format
19
- "X-Amzn-SageMaker-Custom-Attributes": "accept_eula=true",
20
- "Connection": "keep-alive"
21
- }
22
  payload = {
23
  "inputs": [
24
  [
@@ -39,6 +55,37 @@ def generate_response(input, history):
39
  }
40
  }
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  response = requests.post(url, json=payload, headers=headers)
44
 
 
3
  import requests
4
  import os
5
  from datetime import datetime
6
+ import hashlib
7
+ import hmac
8
 
9
+ def sign(key, msg):
10
+ return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
11
+
12
+ def get_signature_key(secret_key, date_stamp, region_name, service_name):
13
+ k_date = sign(('AWS4' + secret_key).encode('utf-8'), date_stamp)
14
+ k_region = sign(k_date, region_name)
15
+ k_service = sign(k_region, service_name)
16
+ k_signing = sign(k_service, 'aws4_request')
17
+ return k_signing
18
 
19
  def generate_response(input, history):
20
  #bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
21
 
22
+ #Authentication AWS
23
+ access_key = os.environ['AccessKey']
24
+ secret_key = os.environ['SecretKey']
25
+ region = 'eu-west-1' # e.g., 'us-west-2'
26
+ service = 'sagemaker'
27
+
28
  #Hit the Sagemaker API and get results
29
 
30
  url = "https://runtime.sagemaker.eu-west-1.amazonaws.com/endpoints/jumpstart-dft-meta-textgeneration-llama-2-7b-f/invocations"
31
+ #authorization_token = os.environ['Authorization']
32
+ # headers = {
33
+ # "Content-Type": "application/json",
34
+ # "X-Amz-Date": datetime.utcnow().strftime("%Y%m%dT%H%M%SZ"), # Using ISO-8601 basic format
35
+ # "X-Amzn-SageMaker-Custom-Attributes": "accept_eula=true",
36
+ # "Connection": "keep-alive"
37
+ # }
 
38
  payload = {
39
  "inputs": [
40
  [
 
55
  }
56
  }
57
 
58
+ payload_str = json.dumps(payload)
59
+
60
+ t = datetime.utcnow()
61
+ amz_date = t.strftime('%Y%m%dT%H%M%SZ')
62
+ date_stamp = t.strftime('%Y%m%d')
63
+
64
+ canonical_uri = '/data' # Your canonical URI here
65
+ canonical_headers = f'host:{endpoint}\nx-amz-date:{amz_date}\n'
66
+ signed_headers = 'host;x-amz-date'
67
+
68
+ hashlib_payload = hashlib.sha256(payload_str.encode('utf-8')).hexdigest()
69
+ canonical_request = f'POST\n{canonical_uri}\n\n{canonical_headers}\n{signed_headers}\n{hashlib_payload}'
70
+
71
+ algorithm = 'AWS4-HMAC-SHA256'
72
+ credential_scope = f'{date_stamp}/{region}/{service}/aws4_request'
73
+ string_to_sign = f'{algorithm}\n{amz_date}\n{credential_scope}\n{hashlib.sha256(canonical_request.encode("utf-8")).hexdigest()}'
74
+
75
+ signing_key = get_signature_key(secret_key, date_stamp, region, service)
76
+ signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
77
+
78
+ authorization_header = f'{algorithm} Credential={access_key}/{credential_scope}, SignedHeaders={signed_headers}, Signature={signature}'
79
+
80
+ headers = {
81
+ 'x-amz-date': amz_date,
82
+ 'Authorization': authorization_header,
83
+ 'Content-Type': 'application/json',
84
+ "X-Amzn-SageMaker-Custom-Attributes": "accept_eula=true"
85
+ }
86
+
87
+ #response = requests.post(endpoint, headers=headers, data=payload_str)
88
+
89
 
90
  response = requests.post(url, json=payload, headers=headers)
91