Spaces:
Sleeping
Sleeping
File size: 7,351 Bytes
a01e50d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
import json
import os
import time
from typing import List
import logging
logger = logging.getLogger()
import requests
#SERVER_ADRESS="https://faircompute.com:8000/api/v1"
SERVER_ADRESS="http://localhost:8000/api/v1"
DOCKER_IMAGE="faircompute/stable-diffusion:pytorch-1.13.1-cu116"
#DOCKER_IMAGE="sha256:e06453fe869556ea3e63572a935aed4261337b261fdf7bda370472b0587409a9"
def authenticate(email: str, password: str):
url = f'{SERVER_ADRESS}/auth/login'
json_obj = {"email": email, "password": password}
resp = requests.post(url, json=json_obj)
token = resp.json()["token"]
return token
def get(url, token, **kwargs):
headers = {
'Authorization': f'Bearer {token}'
}
response = requests.get(url, headers=headers, **kwargs)
if not response.ok:
raise Exception(f"Error! status: {response.status_code}")
return response
def put(url, token, data):
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
}
if not isinstance(data, str):
data = json.dumps(data)
response = requests.put(url, headers=headers, data=data)
if not response.ok and response.status_code != 206:
raise Exception(f"Error! status: {response.status_code}")
return response
def put_program(token, launcher: str, image: str, runtime: str, command: List[str]):
url = f"{SERVER_ADRESS}/programs"
data = {
launcher: {
"image": image,
"command": command,
"runtime": runtime
}
}
response = put(url=url, token=token, data=data)
return int(response.text)
def put_job(token, program_id, input_files, output_files):
url = f"{SERVER_ADRESS}/jobs?program={program_id}"
data = {
'input_files': input_files,
'output_files': output_files
}
response = put(url=url, token=token, data=data)
return int(response.text)
def get_job_status(token, job_id):
url = f"{SERVER_ADRESS}/jobs/{job_id}/status"
response = get(url=url, token=token)
return response.text
def get_cluster_summary(token):
url = f"{SERVER_ADRESS}/nodes/summary"
response = get(token=token, url=url)
return response.json()
def put_job_stream_data(token, job_id, name, data):
url = f"{SERVER_ADRESS}/jobs/{job_id}/data/streams/{name}"
response = put(url=url, token=token, data=data)
return response.text
def put_job_stream_eof(token, job_id, name):
url = f"{SERVER_ADRESS}/jobs/{job_id}/data/streams/{name}/eof"
response = put(url=url, token=token, data=None)
return response.text
def wait_for_file(token, job_id, path, local_path, attempts=10):
headers = {
'Authorization': f'Bearer {token}'
}
for i in range(attempts):
url = f"{SERVER_ADRESS}/jobs/{job_id}/data/files/{path}"
print(f"Waiting for file {path}...")
try:
with requests.get(url=url, headers=headers, stream=True) as r:
r.raise_for_status()
with open(local_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
print(f"File {local_path} ready")
return local_path
except Exception as e:
print(e)
time.sleep(0.5)
print(f"Failed to receive {local_path}")
def text_to_image(text):
email = os.getenv('FAIRCOMPUTE_EMAIL')
password = os.environ.get('FAIRCOMPUTE_PASSWORD')
token = authenticate(email=email, password=password)
logger.info(token)
summary = get_cluster_summary(token=token)
logger.info("Summary:")
logger.info(summary)
program_id = put_program(token=token,
launcher="Docker",
image=DOCKER_IMAGE,
runtime="nvidia",
command=[])
logger.info(program_id)
job_id = put_job(token=token,
program_id=program_id,
input_files=[],
output_files=["/workspace/result.png"])
logger.info(job_id)
status = get_job_status(token=token,
job_id=job_id)
logger.info(status)
while status != "Processing" and status != "Completed":
status = get_job_status(token=token,
job_id=job_id)
logger.info(status)
time.sleep(0.5)
res = put_job_stream_data(token=token,
job_id=job_id,
name="stdin",
data=text + "\n")
logger.info(res)
res = put_job_stream_eof(token=token,
job_id=job_id,
name="stdin")
logger.info(res)
status = get_job_status(token=token,
job_id=job_id)
logger.info(status)
while status == "Processing":
status = get_job_status(token=token,
job_id=job_id)
logger.info(status)
time.sleep(0.5)
if status == "Completed":
logger.info("Done!")
else:
logger.info("Job Failed")
resp = wait_for_file(token=token,
job_id=job_id,
path="%2Fworkspace%2Fresult.png",
local_path="result.png")
logger.info(resp)
return resp
if __name__=="__main__":
email = os.getenv('FAIRCOMPUTE_EMAIL')
password = os.environ.get('FAIRCOMPUTE_PASSWORD')
token = authenticate(email=email, password=password)
print(token)
summary = get_cluster_summary(token=token)
print("Summary:")
print(summary)
program_id = put_program(token=token,
launcher="Docker",
image=DOCKER_IMAGE,
runtime="nvidia",
command=[])
print(program_id)
job_id = put_job(token=token,
program_id=program_id,
input_files=[],
output_files=["/workspace/result.png"])
print(job_id)
status = get_job_status(token=token,
job_id=job_id)
print(status)
while status != "Processing" and status != "Completed":
status = get_job_status(token=token,
job_id=job_id)
print(status)
time.sleep(0.5)
res = put_job_stream_data(token=token,
job_id=job_id,
name="stdin",
data="Robot dinozaur\n")
print(res)
res = put_job_stream_eof(token=token,
job_id=job_id,
name="stdin")
print(res)
status = get_job_status(token=token,
job_id=job_id)
print(status)
while status == "Processing":
status = get_job_status(token=token,
job_id=job_id)
print(status)
time.sleep(0.5)
if status == "Completed":
print("Done!")
else:
print("Job Failed")
resp = wait_for_file(token=token,
job_id=job_id,
path="%2Fworkspace%2Fresult.png",
local_path="result.png")
print(resp)
|