Spaces:
Sleeping
Sleeping
File size: 6,209 Bytes
5048eb4 a01e50d 5048eb4 a01e50d 5048eb4 ed2b581 5048eb4 a01e50d ed2b581 a01e50d 04f1dbd 5048eb4 04f1dbd 5048eb4 411fbd1 3e7cff9 a01e50d de34e36 a01e50d de34e36 a01e50d de34e36 a01e50d de34e36 a01e50d de34e36 5048eb4 3e7cff9 de34e36 96fce2c 04f1dbd 96fce2c 04f1dbd 5048eb4 04f1dbd de34e36 3e7cff9 5048eb4 de34e36 96fce2c a01e50d 96fce2c a01e50d 5a60604 a01e50d de34e36 a01e50d de34e36 a01e50d de34e36 a01e50d de34e36 a01e50d de34e36 a01e50d 5048eb4 5a60604 5048eb4 5a60604 5048eb4 5a60604 5048eb4 3e7cff9 04f1dbd de34e36 96fce2c 04f1dbd 5048eb4 de34e36 5048eb4 a01e50d 96fce2c a01e50d 5a60604 a01e50d 5a60604 a01e50d 5048eb4 a01e50d 5048eb4 5a60604 5048eb4 a01e50d 5048eb4 a01e50d 5048eb4 411fbd1 ed2b581 |
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 |
import base64
import json
import logging
import os
import time
from io import BytesIO
from typing import List, BinaryIO
from PIL import Image
from octoai.client import Client as OctoAiClient
logger = logging.getLogger()
import requests
import tempfile
SERVER_ADDRESS = "https://faircompute.com:8000/api/v1"
ENDPOINT_ADDRESS = "http://dikobraz.mooo.com:5000"
TARGET_NODE = "119eccba-2388-43c1-bdb9-02133049604c"
# SERVER_ADDRESS = "http://localhost:8000/api/v1"
# ENDPOINT_ADDRESS = "http://localhost:5000"
# TARGET_NODE = None
DOCKER_IMAGE = "faircompute/diffusion-octo:v1"
class FairApiClient:
def __init__(self, server_address: str):
self.server_address = server_address
self.token = None
def authenticate(self, email: str, password: str):
url = f'{self.server_address}/auth/login'
json_obj = {"email": email, "password": password}
resp = requests.post(url, json=json_obj)
self.token = resp.json()["token"]
def get(self, url, **kwargs):
headers = {
'Authorization': f'Bearer {self.token}'
}
response = requests.get(url, headers=headers, **kwargs)
if not response.ok:
raise Exception(f"Error! status: {response.status_code}")
return response
def put(self, url, data):
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {self.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_job(self, image: str, command: List[str], ports: List[tuple[int, int]], input_files, output_files):
url = f"{self.server_address}/jobs"
data = {
'version': 'V018',
'container_desc': {
'type': 'V018',
'image': image,
'runtime': 'nvidia',
'ports': [[{"port": host_port, "ip": 'null'}, {"port": container_port, "protocol": "Tcp"}] for (host_port, container_port) in ports],
'command': command,
},
'input_files': input_files,
'output_files': output_files,
'target_node': TARGET_NODE,
}
response = self.put(url=url, data=data).json()
return response['id'], response['pid']
def get_job_info(self, job_id):
url = f"{self.server_address}/jobs/{job_id}/stat"
response = self.get(url=url).json()
return response
def get_cluster_summary(self):
url = f"{self.server_address}/nodes/summary"
response = self.get(url=url)
return response.json()
def put_job_stream_data(self, job_id, name, data):
url = f"{self.server_address}/jobs/{job_id}/data/streams/{name}"
response = self.put(url=url, data=data)
return response.text
def put_job_stream_eof(self, job_id, name):
url = f"{self.server_address}/jobs/{job_id}/data/streams/{name}/eof"
response = self.put(url=url, data=None)
return response.text
def wait_for_file(self, job_id, path, attempts=10) -> BinaryIO:
headers = {
'Authorization': f'Bearer {self.token}'
}
for i in range(attempts):
url = f"{self.server_address}/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()
f = tempfile.TemporaryFile()
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
print(f"File {path} ready")
f.seek(0, 0)
return f
except Exception as e:
print(e)
time.sleep(0.5)
print(f"Failed to receive {path}")
class EndpointClient:
def infer(self, prompt):
client = OctoAiClient()
inputs = {"prompt": {"text": prompt}}
response = client.infer(endpoint_url=f"{ENDPOINT_ADDRESS}/infer", inputs=inputs)
image_b64 = response["output"]["image_b64"]
image_data = base64.b64decode(image_b64)
image_data = BytesIO(image_data)
image = Image.open(image_data)
return image
class ServerNotReadyException(Exception):
pass
def wait_for_server(retries, timeout, delay=1.0):
for i in range(retries):
try:
r = requests.get(ENDPOINT_ADDRESS, timeout=timeout)
r.raise_for_status()
return
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError, requests.exceptions.Timeout):
logger.info("Server is not ready yet")
time.sleep(delay)
else:
raise ServerNotReadyException("Failed to start the server")
def start_server():
# default credentials will work only for local server built in debug mode
email = os.getenv('FAIRCOMPUTE_EMAIL', "debug-email")
password = os.environ.get('FAIRCOMPUTE_PASSWORD', "debug-pwd")
client = FairApiClient(SERVER_ADDRESS)
client.authenticate(email=email, password=password)
job_id, job_pid = client.put_job(
image=DOCKER_IMAGE,
command=[],
ports=[(5000, 8080)],
input_files=[],
output_files=[])
logger.info(f"Job id: {job_id}, pid: {job_pid}")
info = client.get_job_info(job_id=job_id)
logger.info(info)
while info["status"] != "Processing":
info = client.get_job_info(job_id=job_id)
logger.info(info)
time.sleep(0.5)
# wait until the server is ready
wait_for_server(retries=10, timeout=1.0)
def text_to_image(text):
try:
wait_for_server(retries=1, timeout=0.1, delay=0.0)
except ServerNotReadyException:
start_server()
client = EndpointClient()
return client.infer(text)
if __name__ == "__main__":
image = text_to_image(text="Robot dinosaur\n")
image.save("result.png")
|