Peng-Wang commited on
Commit
caa74a1
1 Parent(s): 713bad6

update api

Browse files
Files changed (2) hide show
  1. caller.py +35 -4
  2. requirements.txt +5 -5
caller.py CHANGED
@@ -3,12 +3,17 @@
3
  # This source code is licensed under the license found in the
4
  # LICENSE file in the root directory of this source tree.
5
 
6
- from PIL import Image, ImageFilter
7
- from loguru import logger
8
  import requests
9
  import base64
10
  import json
11
  import io
 
 
 
 
 
 
 
12
 
13
  # 接口 URL
14
  t2i_url = 'https://magicarena.bytedance.com/api/evaluate/v1/algo/process'
@@ -19,6 +24,31 @@ headers = {
19
  'X-USE-PPE': '1'
20
  }
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  class SeedT2ICaller():
23
  def __init__(self, cfg, *args, **kwargs):
24
  self.cfg = cfg
@@ -34,6 +64,7 @@ class SeedT2ICaller():
34
  # "width": 64,
35
  # "height": 64
36
  })
 
37
  logger.info(f"{req_json}")
38
  # 请求发送
39
  response = requests.post(
@@ -87,8 +118,8 @@ class SeedEditCaller():
87
  data=json.dumps({
88
  'AlgoType': 2,
89
  'ReqJson': req_json,
90
- 'BinaryData': [binary]
91
- # 'Base': base
92
  })
93
  )
94
 
 
3
  # This source code is licensed under the license found in the
4
  # LICENSE file in the root directory of this source tree.
5
 
 
 
6
  import requests
7
  import base64
8
  import json
9
  import io
10
+ import os
11
+ import hashlib
12
+ import hmac
13
+ import time
14
+ import random
15
+ from PIL import Image, ImageFilter
16
+ from loguru import logger
17
 
18
  # 接口 URL
19
  t2i_url = 'https://magicarena.bytedance.com/api/evaluate/v1/algo/process'
 
24
  'X-USE-PPE': '1'
25
  }
26
 
27
+ APP_KEY = os.environ['APP_KEY']
28
+ SECRET_KEY = os.environ["SECRET_KEY"]
29
+
30
+ def get_auth(app_key, secret_key):
31
+ # 生成随机数作为 nonce
32
+ nonce = str(random.randint(0, 2**31 - 1))
33
+ # 获取当前时间戳
34
+ timestamp = str(int(time.time()))
35
+ # 调用 get_sign 函数生成签名
36
+ sign = get_sign(nonce, timestamp, secret_key)
37
+ return {
38
+ "AppKey": app_key,
39
+ "Timestamp":timestamp,
40
+ "Nonce":nonce,
41
+ "Sign": sign}
42
+
43
+ def get_sign(nonce, timestamp, secret_key):
44
+ keys = [nonce, secret_key, timestamp]
45
+ keys.sort()
46
+ key_str = ''.join(keys)
47
+ sha1_hash = hashlib.sha1()
48
+ sha1_hash.update(key_str.encode('utf-8'))
49
+ signature = sha1_hash.hexdigest()
50
+ return signature.lower()
51
+
52
  class SeedT2ICaller():
53
  def __init__(self, cfg, *args, **kwargs):
54
  self.cfg = cfg
 
64
  # "width": 64,
65
  # "height": 64
66
  })
67
+ authInfo = get_auth(APP_KEY, SECRET_KEY)
68
  logger.info(f"{req_json}")
69
  # 请求发送
70
  response = requests.post(
 
118
  data=json.dumps({
119
  'AlgoType': 2,
120
  'ReqJson': req_json,
121
+ 'BinaryData': [binary],
122
+ 'AuthInfo': get_auth(APP_KEY, SECRET_KEY)
123
  })
124
  )
125
 
requirements.txt CHANGED
@@ -1,5 +1,5 @@
1
- gradio
2
- numpy
3
- loguru
4
- Pillow
5
- requests
 
1
+ gradio==4.44.1
2
+ numpy==1.23.5
3
+ loguru==0.7.0
4
+ Pillow==9.4.0
5
+ requests==2.32.3