dot.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """
  2. dot
  3. """
  4. import concurrent.futures
  5. import time
  6. import hashlib
  7. from http.client import HTTPException
  8. import requests
  9. import json
  10. from aistudio_sdk.config import STUDIO_MODEL_API_URL_PREFIX_DEFAULT, SALT
  11. from datetime import datetime
  12. from aistudio_sdk import log
  13. def generate_api_key(salt: str, api_time) -> str:
  14. """get param"""
  15. raw = f"{api_time}_{salt}"
  16. md5 = hashlib.md5(raw.encode()).hexdigest() # MD5 加密
  17. return md5
  18. def post_repo_statistic(
  19. repo_id: str,
  20. revision: str,
  21. action: dict,
  22. ) -> requests.Response:
  23. """post info"""
  24. address = STUDIO_MODEL_API_URL_PREFIX_DEFAULT
  25. url = f"{address}/modelcenter/v2/statistic/repo"
  26. api_time = int(time.time() * 1000)
  27. api_key = generate_api_key(SALT, api_time)
  28. payload = {
  29. "biz_id": "model",
  30. "repo_id": repo_id,
  31. "ac_type": "download",
  32. "client_type": "sdk",
  33. "revision": revision,
  34. "action": json.dumps(action), # 序列化为 JSON 字符串
  35. "api_time": api_time,
  36. "api_key": api_key
  37. }
  38. headers = {
  39. "Content-Type": "application/json"
  40. }
  41. try:
  42. response = requests.post(url, json=payload, headers=headers)
  43. if response.status_code != 200:
  44. log.debug(f"dot.response:{response.status_code}")
  45. return response
  46. except Exception as e:
  47. pass
  48. def post_upload_statistic(
  49. token: str,
  50. repo_id: str,
  51. file_path: str,
  52. file_size: int,
  53. ) -> requests.Response:
  54. """post info"""
  55. address = STUDIO_MODEL_API_URL_PREFIX_DEFAULT
  56. url = f"{address}/studio-dot/report"
  57. api_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  58. action = {
  59. "repoId": repo_id,
  60. "clientType": "sdk",
  61. "filePath": file_path,
  62. "fileSize": file_size,
  63. "token": token,
  64. "eid": "137"
  65. }
  66. payload = {
  67. "action": action,
  68. "time": api_time,
  69. }
  70. compact_json = json.dumps(payload, separators=(",", ":"))
  71. headers = {
  72. "Content-Type": "application/json"
  73. }
  74. try:
  75. response = requests.post(url, json=compact_json, headers=headers)
  76. if response.status_code != 200:
  77. log.debug(f"dot.response:{response.status_code}")
  78. return response
  79. except Exception as e:
  80. pass
  81. def post_repo_statistic_async(repo_id: str,
  82. revision: str,
  83. action: dict,):
  84. """
  85. async
  86. """
  87. with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
  88. executor.submit(post_repo_statistic, repo_id, revision, action)
  89. def post_upload_statistic_async(
  90. token: str,
  91. repo_id: str,
  92. file_path: str,
  93. file_size: int,
  94. ):
  95. """
  96. async
  97. """
  98. with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
  99. executor.submit(post_upload_statistic, token, repo_id, file_path, file_size)