chatgpt_api.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. import uuid
  2. import time
  3. import asyncio
  4. import json
  5. from pathlib import Path
  6. from transformers import AutoTokenizer
  7. from typing import List, Literal, Union, Dict
  8. from aiohttp import web
  9. import aiohttp_cors
  10. import traceback
  11. import signal
  12. from exo import DEBUG, VERSION
  13. from exo.download.download_progress import RepoProgressEvent
  14. from exo.helpers import PrefixDict, shutdown, get_exo_images_dir
  15. from exo.inference.tokenizers import resolve_tokenizer
  16. from exo.orchestration import Node
  17. from exo.models import build_base_shard, model_cards, get_repo, pretty_name, get_supported_models
  18. from exo.apputil import create_animation_mp4
  19. from typing import Callable, Optional
  20. from PIL import Image
  21. import numpy as np
  22. import base64
  23. from io import BytesIO
  24. import mlx.core as mx
  25. import tempfile
  26. class Message:
  27. def __init__(self, role: str, content: Union[str, List[Dict[str, Union[str, Dict[str, str]]]]]):
  28. self.role = role
  29. self.content = content
  30. def to_dict(self):
  31. return {"role": self.role, "content": self.content}
  32. class ChatCompletionRequest:
  33. def __init__(self, model: str, messages: List[Message], temperature: float):
  34. self.model = model
  35. self.messages = messages
  36. self.temperature = temperature
  37. def to_dict(self):
  38. return {"model": self.model, "messages": [message.to_dict() for message in self.messages], "temperature": self.temperature}
  39. def generate_completion(
  40. chat_request: ChatCompletionRequest,
  41. tokenizer,
  42. prompt: str,
  43. request_id: str,
  44. tokens: List[int],
  45. stream: bool,
  46. finish_reason: Union[Literal["length", "stop"], None],
  47. object_type: Literal["chat.completion", "text_completion"],
  48. ) -> dict:
  49. completion = {
  50. "id": f"chatcmpl-{request_id}",
  51. "object": object_type,
  52. "created": int(time.time()),
  53. "model": chat_request.model,
  54. "system_fingerprint": f"exo_{VERSION}",
  55. "choices": [{
  56. "index": 0,
  57. "message": {"role": "assistant", "content": tokenizer.decode(tokens)},
  58. "logprobs": None,
  59. "finish_reason": finish_reason,
  60. }],
  61. }
  62. if not stream:
  63. completion["usage"] = {
  64. "prompt_tokens": len(tokenizer.encode(prompt)),
  65. "completion_tokens": len(tokens),
  66. "total_tokens": len(tokenizer.encode(prompt)) + len(tokens),
  67. }
  68. choice = completion["choices"][0]
  69. if object_type.startswith("chat.completion"):
  70. key_name = "delta" if stream else "message"
  71. choice[key_name] = {"role": "assistant", "content": tokenizer.decode(tokens)}
  72. elif object_type == "text_completion":
  73. choice["text"] = tokenizer.decode(tokens)
  74. else:
  75. ValueError(f"Unsupported response type: {object_type}")
  76. return completion
  77. def remap_messages(messages: List[Message]) -> List[Message]:
  78. remapped_messages = []
  79. last_image = None
  80. for message in messages:
  81. if not isinstance(message.content, list):
  82. remapped_messages.append(message)
  83. continue
  84. remapped_content = []
  85. for content in message.content:
  86. if isinstance(content, dict):
  87. if content.get("type") in ["image_url", "image"]:
  88. image_url = content.get("image_url", {}).get("url") or content.get("image")
  89. if image_url:
  90. last_image = {"type": "image", "image": image_url}
  91. remapped_content.append({"type": "text", "text": "[An image was uploaded but is not displayed here]"})
  92. else:
  93. remapped_content.append(content)
  94. else:
  95. remapped_content.append(content)
  96. remapped_messages.append(Message(role=message.role, content=remapped_content))
  97. if last_image:
  98. # Replace the last image placeholder with the actual image content
  99. for message in reversed(remapped_messages):
  100. for i, content in enumerate(message.content):
  101. if isinstance(content, dict):
  102. if content.get("type") == "text" and content.get("text") == "[An image was uploaded but is not displayed here]":
  103. message.content[i] = last_image
  104. return remapped_messages
  105. return remapped_messages
  106. def build_prompt(tokenizer, _messages: List[Message]):
  107. messages = remap_messages(_messages)
  108. prompt = tokenizer.apply_chat_template([m.to_dict() for m in messages], tokenize=False, add_generation_prompt=True)
  109. for message in messages:
  110. if not isinstance(message.content, list):
  111. continue
  112. return prompt
  113. def parse_message(data: dict):
  114. if "role" not in data or "content" not in data:
  115. raise ValueError(f"Invalid message: {data}. Must have 'role' and 'content'")
  116. return Message(data["role"], data["content"])
  117. def parse_chat_request(data: dict, default_model: str):
  118. return ChatCompletionRequest(
  119. data.get("model", default_model),
  120. [parse_message(msg) for msg in data["messages"]],
  121. data.get("temperature", 0.0),
  122. )
  123. class PromptSession:
  124. def __init__(self, request_id: str, timestamp: int, prompt: str):
  125. self.request_id = request_id
  126. self.timestamp = timestamp
  127. self.prompt = prompt
  128. class ChatGPTAPI:
  129. def __init__(self, node: Node, inference_engine_classname: str, response_timeout: int = 90, on_chat_completion_request: Callable[[str, ChatCompletionRequest, str], None] = None, default_model: Optional[str] = None):
  130. self.node = node
  131. self.inference_engine_classname = inference_engine_classname
  132. self.response_timeout = response_timeout
  133. self.on_chat_completion_request = on_chat_completion_request
  134. self.app = web.Application(client_max_size=100*1024*1024) # 100MB to support image upload
  135. self.prompts: PrefixDict[str, PromptSession] = PrefixDict()
  136. self.prev_token_lens: Dict[str, int] = {}
  137. self.stream_tasks: Dict[str, asyncio.Task] = {}
  138. self.default_model = default_model or "llama-3.2-1b"
  139. cors = aiohttp_cors.setup(self.app)
  140. cors_options = aiohttp_cors.ResourceOptions(
  141. allow_credentials=True,
  142. expose_headers="*",
  143. allow_headers="*",
  144. allow_methods="*",
  145. )
  146. cors.add(self.app.router.add_get("/models", self.handle_get_models), {"*": cors_options})
  147. cors.add(self.app.router.add_get("/v1/models", self.handle_get_models), {"*": cors_options})
  148. cors.add(self.app.router.add_post("/chat/token/encode", self.handle_post_chat_token_encode), {"*": cors_options})
  149. cors.add(self.app.router.add_post("/v1/chat/token/encode", self.handle_post_chat_token_encode), {"*": cors_options})
  150. cors.add(self.app.router.add_post("/chat/completions", self.handle_post_chat_completions), {"*": cors_options})
  151. cors.add(self.app.router.add_post("/v1/chat/completions", self.handle_post_chat_completions), {"*": cors_options})
  152. cors.add(self.app.router.add_post("/v1/image/generations", self.handle_post_image_generations), {"*": cors_options})
  153. cors.add(self.app.router.add_get("/v1/download/progress", self.handle_get_download_progress), {"*": cors_options})
  154. cors.add(self.app.router.add_get("/modelpool", self.handle_model_support), {"*": cors_options})
  155. cors.add(self.app.router.add_get("/healthcheck", self.handle_healthcheck), {"*": cors_options})
  156. cors.add(self.app.router.add_post("/quit", self.handle_quit), {"*": cors_options})
  157. cors.add(self.app.router.add_post("/create_animation", self.handle_create_animation), {"*": cors_options})
  158. cors.add(self.app.router.add_post("/download", self.handle_post_download), {"*": cors_options})
  159. if "__compiled__" not in globals():
  160. self.static_dir = Path(__file__).parent.parent/"tinychat"
  161. self.app.router.add_get("/", self.handle_root)
  162. self.app.router.add_static("/", self.static_dir, name="static")
  163. self.app.router.add_static('/images/', get_exo_images_dir(), name='static_images')
  164. self.app.middlewares.append(self.timeout_middleware)
  165. self.app.middlewares.append(self.log_request)
  166. async def handle_quit(self, request):
  167. if DEBUG>=1: print("Received quit signal")
  168. response = web.json_response({"detail": "Quit signal received"}, status=200)
  169. await response.prepare(request)
  170. await response.write_eof()
  171. await shutdown(signal.SIGINT, asyncio.get_event_loop(), self.node.server)
  172. async def timeout_middleware(self, app, handler):
  173. async def middleware(request):
  174. try:
  175. return await asyncio.wait_for(handler(request), timeout=self.response_timeout)
  176. except asyncio.TimeoutError:
  177. return web.json_response({"detail": "Request timed out"}, status=408)
  178. return middleware
  179. async def log_request(self, app, handler):
  180. async def middleware(request):
  181. if DEBUG >= 2: print(f"Received request: {request.method} {request.path}")
  182. return await handler(request)
  183. return middleware
  184. async def handle_root(self, request):
  185. return web.FileResponse(self.static_dir/"index.html")
  186. async def handle_healthcheck(self, request):
  187. return web.json_response({"status": "ok"})
  188. async def handle_model_support(self, request):
  189. return web.json_response({
  190. "model pool": {
  191. model_name: pretty_name.get(model_name, model_name)
  192. for model_name in get_supported_models(self.node.topology_inference_engines_pool)
  193. }
  194. })
  195. async def handle_get_models(self, request):
  196. return web.json_response([{"id": model_name, "object": "model", "owned_by": "exo", "ready": True} for model_name, _ in model_cards.items()])
  197. async def handle_post_chat_token_encode(self, request):
  198. data = await request.json()
  199. shard = build_base_shard(self.default_model, self.inference_engine_classname)
  200. messages = [parse_message(msg) for msg in data.get("messages", [])]
  201. tokenizer = await resolve_tokenizer(get_repo(shard.model_id, self.inference_engine_classname))
  202. return web.json_response({"length": len(build_prompt(tokenizer, messages)[0])})
  203. async def handle_get_download_progress(self, request):
  204. progress_data = {}
  205. for node_id, progress_event in self.node.node_download_progress.items():
  206. if isinstance(progress_event, RepoProgressEvent):
  207. progress_data[node_id] = progress_event.to_dict()
  208. else:
  209. print(f"Unknown progress event type: {type(progress_event)}. {progress_event}")
  210. return web.json_response(progress_data)
  211. async def handle_post_chat_completions(self, request):
  212. data = await request.json()
  213. if DEBUG >= 2: print(f"Handling chat completions request from {request.remote}: {data}")
  214. stream = data.get("stream", False)
  215. chat_request = parse_chat_request(data, self.default_model)
  216. if chat_request.model and chat_request.model.startswith("gpt-"): # to be compatible with ChatGPT tools, point all gpt- model requests to default model
  217. chat_request.model = self.default_model
  218. if not chat_request.model or chat_request.model not in model_cards:
  219. if DEBUG >= 1: print(f"Invalid model: {chat_request.model}. Supported: {list(model_cards.keys())}. Defaulting to {self.default_model}")
  220. chat_request.model = self.default_model
  221. shard = build_base_shard(chat_request.model, self.inference_engine_classname)
  222. if not shard:
  223. supported_models = [model for model, info in model_cards.items() if self.inference_engine_classname in info.get("repo", {})]
  224. return web.json_response(
  225. {"detail": f"Unsupported model: {chat_request.model} with inference engine {self.inference_engine_classname}. Supported models for this engine: {supported_models}"},
  226. status=400,
  227. )
  228. tokenizer = await resolve_tokenizer(get_repo(shard.model_id, self.inference_engine_classname))
  229. if DEBUG >= 4: print(f"Resolved tokenizer: {tokenizer}")
  230. prompt = build_prompt(tokenizer, chat_request.messages)
  231. request_id = str(uuid.uuid4())
  232. if self.on_chat_completion_request:
  233. try:
  234. self.on_chat_completion_request(request_id, chat_request, prompt)
  235. except Exception as e:
  236. if DEBUG >= 2: traceback.print_exc()
  237. # request_id = None
  238. # match = self.prompts.find_longest_prefix(prompt)
  239. # if match and len(prompt) > len(match[1].prompt):
  240. # if DEBUG >= 2:
  241. # print(f"Prompt for request starts with previous prompt {len(match[1].prompt)} of {len(prompt)}: {match[1].prompt}")
  242. # request_id = match[1].request_id
  243. # self.prompts.add(prompt, PromptSession(request_id=request_id, timestamp=int(time.time()), prompt=prompt))
  244. # # remove the matching prefix from the prompt
  245. # prompt = prompt[len(match[1].prompt):]
  246. # else:
  247. # request_id = str(uuid.uuid4())
  248. # self.prompts.add(prompt, PromptSession(request_id=request_id, timestamp=int(time.time()), prompt=prompt))
  249. callback_id = f"chatgpt-api-wait-response-{request_id}"
  250. callback = self.node.on_token.register(callback_id)
  251. if DEBUG >= 2: print(f"Sending prompt from ChatGPT api {request_id=} {shard=} {prompt=}")
  252. try:
  253. await asyncio.wait_for(asyncio.shield(asyncio.create_task(self.node.process_prompt(shard, prompt, request_id=request_id))), timeout=self.response_timeout)
  254. if DEBUG >= 2: print(f"Waiting for response to finish. timeout={self.response_timeout}s")
  255. if stream:
  256. response = web.StreamResponse(
  257. status=200,
  258. reason="OK",
  259. headers={
  260. "Content-Type": "text/event-stream",
  261. "Cache-Control": "no-cache",
  262. },
  263. )
  264. await response.prepare(request)
  265. async def stream_result(_request_id: str, tokens: List[int], is_finished: bool):
  266. prev_last_tokens_len = self.prev_token_lens.get(_request_id, 0)
  267. self.prev_token_lens[_request_id] = max(prev_last_tokens_len, len(tokens))
  268. new_tokens = tokens[prev_last_tokens_len:]
  269. finish_reason = None
  270. eos_token_id = tokenizer.special_tokens_map.get("eos_token_id") if hasattr(tokenizer, "_tokenizer") and isinstance(tokenizer._tokenizer,
  271. AutoTokenizer) else getattr(tokenizer, "eos_token_id", None)
  272. if len(new_tokens) > 0 and new_tokens[-1] == eos_token_id:
  273. new_tokens = new_tokens[:-1]
  274. if is_finished:
  275. finish_reason = "stop"
  276. if is_finished and not finish_reason:
  277. finish_reason = "length"
  278. completion = generate_completion(
  279. chat_request,
  280. tokenizer,
  281. prompt,
  282. request_id,
  283. new_tokens,
  284. stream,
  285. finish_reason,
  286. "chat.completion",
  287. )
  288. if DEBUG >= 2: print(f"Streaming completion: {completion}")
  289. try:
  290. await response.write(f"data: {json.dumps(completion)}\n\n".encode())
  291. except Exception as e:
  292. if DEBUG >= 2: print(f"Error streaming completion: {e}")
  293. if DEBUG >= 2: traceback.print_exc()
  294. def on_result(_request_id: str, tokens: List[int], is_finished: bool):
  295. if _request_id == request_id: self.stream_tasks[_request_id] = asyncio.create_task(stream_result(_request_id, tokens, is_finished))
  296. return _request_id == request_id and is_finished
  297. _, tokens, _ = await callback.wait(on_result, timeout=self.response_timeout)
  298. if request_id in self.stream_tasks: # in case there is still a stream task running, wait for it to complete
  299. if DEBUG >= 2: print("Pending stream task. Waiting for stream task to complete.")
  300. try:
  301. await asyncio.wait_for(self.stream_tasks[request_id], timeout=30)
  302. except asyncio.TimeoutError:
  303. print("WARNING: Stream task timed out. This should not happen.")
  304. await response.write_eof()
  305. return response
  306. else:
  307. _, tokens, _ = await callback.wait(
  308. lambda _request_id, tokens, is_finished: _request_id == request_id and is_finished,
  309. timeout=self.response_timeout,
  310. )
  311. finish_reason = "length"
  312. eos_token_id = tokenizer.special_tokens_map.get("eos_token_id") if isinstance(getattr(tokenizer, "_tokenizer", None), AutoTokenizer) else tokenizer.eos_token_id
  313. if DEBUG >= 2: print(f"Checking if end of tokens result {tokens[-1]=} is {eos_token_id=}")
  314. if tokens[-1] == eos_token_id:
  315. tokens = tokens[:-1]
  316. finish_reason = "stop"
  317. return web.json_response(generate_completion(chat_request, tokenizer, prompt, request_id, tokens, stream, finish_reason, "chat.completion"))
  318. except asyncio.TimeoutError:
  319. return web.json_response({"detail": "Response generation timed out"}, status=408)
  320. except Exception as e:
  321. if DEBUG >= 2: traceback.print_exc()
  322. return web.json_response({"detail": f"Error processing prompt (see logs with DEBUG>=2): {str(e)}"}, status=500)
  323. finally:
  324. deregistered_callback = self.node.on_token.deregister(callback_id)
  325. if DEBUG >= 2: print(f"Deregister {callback_id=} {deregistered_callback=}")
  326. async def handle_post_image_generations(self, request):
  327. data = await request.json()
  328. if DEBUG >= 2: print(f"Handling chat completions request from {request.remote}: {data}")
  329. stream = data.get("stream", False)
  330. model = data.get("model", "")
  331. prompt = data.get("prompt", "")
  332. image_url = data.get("image_url", "")
  333. print(f"model: {model}, prompt: {prompt}, stream: {stream}")
  334. shard = build_base_shard(model, self.inference_engine_classname)
  335. print(f"shard: {shard}")
  336. if not shard:
  337. return web.json_response({"error": f"Unsupported model: {model} with inference engine {self.inference_engine_classname}"}, status=400)
  338. request_id = str(uuid.uuid4())
  339. callback_id = f"chatgpt-api-wait-response-{request_id}"
  340. callback = self.node.on_token.register(callback_id)
  341. try:
  342. if image_url != "" and image_url != None:
  343. img = self.base64_decode(image_url)
  344. else:
  345. img = None
  346. await asyncio.wait_for(asyncio.shield(asyncio.create_task(self.node.process_prompt(shard, prompt, request_id=request_id, inference_state={"image": img}))), timeout=self.response_timeout)
  347. response = web.StreamResponse(status=200, reason='OK', headers={'Content-Type': 'application/octet-stream',"Cache-Control": "no-cache",})
  348. await response.prepare(request)
  349. def get_progress_bar(current_step, total_steps, bar_length=50):
  350. # Calculate the percentage of completion
  351. percent = float(current_step) / total_steps
  352. # Calculate the number of hashes to display
  353. arrow = '-' * int(round(percent * bar_length) - 1) + '>'
  354. spaces = ' ' * (bar_length - len(arrow))
  355. # Create the progress bar string
  356. progress_bar = f'Progress: [{arrow}{spaces}] {int(percent * 100)}% ({current_step}/{total_steps})'
  357. return progress_bar
  358. async def stream_image(_request_id: str, result, is_finished: bool):
  359. if isinstance(result, list):
  360. await response.write(json.dumps({'progress': get_progress_bar((result[0]), (result[1]))}).encode('utf-8') + b'\n')
  361. elif isinstance(result, np.ndarray):
  362. im = Image.fromarray(np.array(result))
  363. images_folder = get_exo_images_dir()
  364. # Save the image to a file
  365. image_filename = f"{_request_id}.png"
  366. image_path = images_folder / image_filename
  367. im.save(image_path)
  368. image_url = request.app.router['static_images'].url_for(filename=image_filename)
  369. base_url = f"{request.scheme}://{request.host}"
  370. # Construct the full URL correctly
  371. full_image_url = base_url + str(image_url)
  372. await response.write(json.dumps({'images': [{'url': str(full_image_url), 'content_type': 'image/png'}]}).encode('utf-8') + b'\n')
  373. if is_finished:
  374. await response.write_eof()
  375. stream_task = None
  376. def on_result(_request_id: str, result, is_finished: bool):
  377. nonlocal stream_task
  378. stream_task = asyncio.create_task(stream_image(_request_id, result, is_finished))
  379. return _request_id == request_id and is_finished
  380. await callback.wait(on_result, timeout=self.response_timeout*10)
  381. if stream_task:
  382. # Wait for the stream task to complete before returning
  383. await stream_task
  384. return response
  385. except Exception as e:
  386. if DEBUG >= 2: traceback.print_exc()
  387. return web.json_response({"detail": f"Error processing prompt (see logs with DEBUG>=2): {str(e)}"}, status=500)
  388. async def handle_create_animation(self, request):
  389. try:
  390. data = await request.json()
  391. replacement_image_path = data.get("replacement_image_path")
  392. device_name = data.get("device_name", "Local Device")
  393. prompt_text = data.get("prompt", "")
  394. if DEBUG >= 2: print(f"Creating animation with params: replacement_image={replacement_image_path}, device={device_name}, prompt={prompt_text}")
  395. if not replacement_image_path:
  396. return web.json_response({"error": "replacement_image_path is required"}, status=400)
  397. # Create temp directory if it doesn't exist
  398. tmp_dir = Path(tempfile.gettempdir())/"exo_animations"
  399. tmp_dir.mkdir(parents=True, exist_ok=True)
  400. # Generate unique output filename in temp directory
  401. output_filename = f"animation_{uuid.uuid4()}.mp4"
  402. output_path = str(tmp_dir/output_filename)
  403. if DEBUG >= 2: print(f"Animation temp directory: {tmp_dir}, output file: {output_path}, directory exists: {tmp_dir.exists()}, directory permissions: {oct(tmp_dir.stat().st_mode)[-3:]}")
  404. # Create the animation
  405. create_animation_mp4(
  406. replacement_image_path,
  407. output_path,
  408. device_name,
  409. prompt_text
  410. )
  411. return web.json_response({
  412. "status": "success",
  413. "output_path": output_path
  414. })
  415. except Exception as e:
  416. if DEBUG >= 2: traceback.print_exc()
  417. return web.json_response({"error": str(e)}, status=500)
  418. async def handle_post_download(self, request):
  419. try:
  420. data = await request.json()
  421. model_name = data.get("model")
  422. if not model_name: return web.json_response({"error": "model parameter is required"}, status=400)
  423. if model_name not in model_cards: return web.json_response({"error": f"Invalid model: {model_name}. Supported models: {list(model_cards.keys())}"}, status=400)
  424. shard = build_base_shard(model_name, self.inference_engine_classname)
  425. if not shard: return web.json_response({"error": f"Could not build shard for model {model_name}"}, status=400)
  426. asyncio.create_task(self.node.inference_engine.ensure_shard(shard))
  427. return web.json_response({
  428. "status": "success",
  429. "message": f"Download started for model: {model_name}"
  430. })
  431. except Exception as e:
  432. if DEBUG >= 2: traceback.print_exc()
  433. return web.json_response({"error": str(e)}, status=500)
  434. async def run(self, host: str = "0.0.0.0", port: int = 52415):
  435. runner = web.AppRunner(self.app)
  436. await runner.setup()
  437. site = web.TCPSite(runner, host, port)
  438. await site.start()
  439. def base64_decode(self, base64_string):
  440. #decode and reshape image
  441. if base64_string.startswith('data:image'):
  442. base64_string = base64_string.split(',')[1]
  443. image_data = base64.b64decode(base64_string)
  444. img = Image.open(BytesIO(image_data))
  445. W, H = (dim - dim % 64 for dim in (img.width, img.height))
  446. if W != img.width or H != img.height:
  447. print(f"Warning: image shape is not divisible by 64, downsampling to {W}x{H}")
  448. img = img.resize((W, H), Image.NEAREST) # use desired downsampling filter
  449. img = mx.array(np.array(img))
  450. img = (img[:, :, :3].astype(mx.float32) / 255) * 2 - 1
  451. img = img[None]
  452. return img