|
| 1 | +import requests |
| 2 | +from cmind import utils |
| 3 | +import cmind as cm |
| 4 | +import os |
| 5 | +import json |
| 6 | + |
| 7 | + |
| 8 | +def preprocess(i): |
| 9 | + |
| 10 | + os_info = i['os_info'] |
| 11 | + env = i['env'] |
| 12 | + meta = i['meta'] |
| 13 | + automation = i['automation'] |
| 14 | + |
| 15 | + server = env['CM_MLPERF_SUBMISSION_URL'] |
| 16 | + benchmark = env['CM_MLPERF_BENCHMARK'] |
| 17 | + submitter_id = env['CM_MLPERF_SUBMITTER_ID'] |
| 18 | + file_path = env['CM_MLPERF_SUBMISSION_FILE'] |
| 19 | + |
| 20 | + r = get_signed_url(server, benchmark, submitter_id, file_path) |
| 21 | + if r['return'] > 0: |
| 22 | + return r |
| 23 | + |
| 24 | + signed_url = r['signed_url'] |
| 25 | + submission_id = r['submission_id'] |
| 26 | + |
| 27 | + # print(signed_url) |
| 28 | + # print(submission_id) |
| 29 | + r = upload_file_to_signed_url(file_path, signed_url) |
| 30 | + if r['return'] > 0: |
| 31 | + return r |
| 32 | + |
| 33 | + r = trigger_submission_checker( |
| 34 | + server, submitter_id, benchmark, submission_id) |
| 35 | + if r['return'] > 0: |
| 36 | + return r |
| 37 | + |
| 38 | + return {'return': 0} |
| 39 | + |
| 40 | + |
| 41 | +def get_signed_url(server, benchmark, submitter_id, file_path): |
| 42 | + # Define the URL |
| 43 | + url = f"{server}/index/url" |
| 44 | + |
| 45 | + # Define the headers |
| 46 | + headers = { |
| 47 | + "Content-Type": "application/json" |
| 48 | + } |
| 49 | + |
| 50 | + # Define the payload |
| 51 | + payload = { |
| 52 | + "submitter_id": submitter_id, |
| 53 | + "benchmark": benchmark, |
| 54 | + "filename": file_path |
| 55 | + } |
| 56 | + |
| 57 | + try: |
| 58 | + # Make the POST request |
| 59 | + response = requests.post(url, json=payload, headers=headers) |
| 60 | + |
| 61 | + # Check the response status |
| 62 | + if response.status_code == 200: |
| 63 | + # print("Request successful!") |
| 64 | + # print("Response:", response.json()) |
| 65 | + pass |
| 66 | + else: |
| 67 | + # print(f"Request failed with status code {response.status_code}") |
| 68 | + # print("Response:", response.text) |
| 69 | + pass |
| 70 | + |
| 71 | + except requests.exceptions.RequestException as e: |
| 72 | + return {"return": 1, |
| 73 | + "error": f"An error occurred in connecting to the server: {e}"} |
| 74 | + |
| 75 | + response_json = response.json() |
| 76 | + # print(response_json) |
| 77 | + # response = json.loads(response_json) |
| 78 | + try: |
| 79 | + signed_url = response_json['signed_url'] |
| 80 | + submission_id = response_json['submission_id'] |
| 81 | + except Exception as e: |
| 82 | + return { |
| 83 | + "return": 1, "error": f"An error occurred while processing the response: {e}"} |
| 84 | + |
| 85 | + return {'return': 0, 'signed_url': signed_url, |
| 86 | + 'submission_id': submission_id} |
| 87 | + |
| 88 | + |
| 89 | +def upload_file_to_signed_url(file_path, signed_url): |
| 90 | + """ |
| 91 | + Uploads a file to a signed URL using HTTP PUT. |
| 92 | +
|
| 93 | + Parameters: |
| 94 | + file_path (str): The path to the file you want to upload. |
| 95 | + signed_url (str): The pre-signed URL for uploading the file. |
| 96 | +
|
| 97 | + Returns: |
| 98 | + dict: A dictionary with 'status_code' and 'response' keys. |
| 99 | + """ |
| 100 | + headers = { |
| 101 | + 'Content-Type': 'application/octet-stream', |
| 102 | + 'Access-Control-Allow-Headers': '*' |
| 103 | + } |
| 104 | + |
| 105 | + try: |
| 106 | + # Open the file in binary mode |
| 107 | + with open(file_path, 'rb') as file: |
| 108 | + response = requests.put( |
| 109 | + signed_url, |
| 110 | + data=file, |
| 111 | + headers=headers |
| 112 | + ) |
| 113 | + |
| 114 | + if response.status_code in [200, 201, 204]: |
| 115 | + print("File uploaded successfully!") |
| 116 | + return { |
| 117 | + 'return': 0 |
| 118 | + } |
| 119 | + else: |
| 120 | + print( |
| 121 | + f"Failed to upload file. Status code: {response.status_code}") |
| 122 | + print("Response:", response.text) |
| 123 | + |
| 124 | + return { |
| 125 | + 'return': response.status_code, |
| 126 | + 'error': response.text |
| 127 | + } |
| 128 | + |
| 129 | + except FileNotFoundError: |
| 130 | + print("Error: File not found.") |
| 131 | + return { |
| 132 | + 'return': 400, |
| 133 | + 'error': f'''File {file_path} not found''' |
| 134 | + } |
| 135 | + |
| 136 | + except requests.exceptions.RequestException as e: |
| 137 | + print(f"Request failed: {e}") |
| 138 | + return { |
| 139 | + 'return': 500, |
| 140 | + 'error': str(e) |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | +def trigger_submission_checker( |
| 145 | + server_url, submitter_id, benchmark, submission_id): |
| 146 | + """ |
| 147 | + Sends a POST request with URL-encoded form data. |
| 148 | +
|
| 149 | + Parameters: |
| 150 | + server_url (str): The server endpoint URL (e.g., https://example.com/index). |
| 151 | + submitter_id (str): The ID of the submitter. |
| 152 | + benchmark (str): The benchmark identifier. |
| 153 | + submission_id (str): The submission ID. |
| 154 | +
|
| 155 | + Returns: |
| 156 | + dict: A dictionary containing status code and response content. |
| 157 | + """ |
| 158 | + url = f"{server_url}/index" |
| 159 | + headers = { |
| 160 | + "Content-Type": "application/x-www-form-urlencoded" |
| 161 | + } |
| 162 | + payload = { |
| 163 | + "submitter_id": submitter_id, |
| 164 | + "benchmark": benchmark, |
| 165 | + "submission_id": submission_id |
| 166 | + } |
| 167 | + |
| 168 | + try: |
| 169 | + # Make the POST request with URL-encoded data |
| 170 | + response = requests.post(url, data=payload, headers=headers) |
| 171 | + |
| 172 | + if response.ok: |
| 173 | + print("Submission Check Request successful!") |
| 174 | + pass |
| 175 | + else: |
| 176 | + print( |
| 177 | + f"Submission Check Request failed with status code: {response.status_code}") |
| 178 | + print("Response:", response.text) |
| 179 | + |
| 180 | + return { |
| 181 | + "return": 0, |
| 182 | + "response": response.text |
| 183 | + } |
| 184 | + |
| 185 | + except requests.exceptions.RequestException as e: |
| 186 | + print("An error occurred:", e) |
| 187 | + return { |
| 188 | + "return": 500, |
| 189 | + "error": str(e) |
| 190 | + } |
| 191 | + |
| 192 | + |
| 193 | +def postprocess(i): |
| 194 | + return {'return': 0} |
0 commit comments