|
2 | 2 | import base64 |
3 | 3 | import json |
4 | 4 | import tempfile |
5 | | -import websocket |
6 | | - |
| 5 | +import time |
| 6 | +from .spz_loader import get_spz |
7 | 7 |
|
8 | 8 | from .protocol import Auth, PromptData, TaskStatus, TaskUpdate |
| 9 | +from .gateway.gateway_api import GatewayApi |
| 10 | +from .gateway.gateway_task import GatewayTask, GatewayTaskStatusResponse, GatewayTaskStatus |
| 11 | + |
| 12 | + |
| 13 | +_GATEWAY_STATUS_CHECK_INTERVAL_SEC: int = 5 |
| 14 | +_GATEWAY_TASK_TIMEOUT_SEC: int = 10 * 60 # 10 minutes |
| 15 | + |
| 16 | + |
| 17 | +class GatewayErrorBase(Exception): |
| 18 | + pass |
| 19 | + |
| 20 | + |
| 21 | +class GatewayTimeoutError(GatewayErrorBase): |
| 22 | + pass |
| 23 | + |
| 24 | + |
| 25 | +class GatewayFailureError(GatewayErrorBase): |
| 26 | + pass |
| 27 | + |
| 28 | + |
| 29 | +class Client: |
| 30 | + |
| 31 | + def __init__(self) -> None: |
| 32 | + self._task: GatewayTask | None = None |
| 33 | + self._task_start_time: float | None = None |
| 34 | + gateway_url = bpy.context.preferences.addons[__package__].preferences.url |
| 35 | + gateway_api_key = bpy.context.preferences.addons[__package__].preferences.token |
| 36 | + self._gateway_api: GatewayApi = GatewayApi(gateway_url=gateway_url, gateway_api_key=gateway_api_key) |
| 37 | + |
| 38 | + @property |
| 39 | + def task_id(self) -> str | None: |
| 40 | + return self._task.id if self._task else None |
| 41 | + |
| 42 | + @property |
| 43 | + def prompt(self) -> str | None: |
| 44 | + return self._task.prompt if self._task else None |
| 45 | + |
| 46 | + def request_model(self, prompt: str) -> None: |
| 47 | + task = self._gateway_api.add_task(text_prompt=prompt) |
| 48 | + print(f"Task added: {task.id}") |
| 49 | + self._task = task |
| 50 | + self._task_start_time = time.time() |
| 51 | + |
| 52 | + def get_result(self) -> str | None: |
| 53 | + if self._task is None or self._task_start_time is None: |
| 54 | + return None |
| 55 | + |
| 56 | + # Check for timeout |
| 57 | + if time.time() - self._task_start_time > _GATEWAY_TASK_TIMEOUT_SEC: |
| 58 | + raise GatewayTimeoutError("Gateway timeout error") |
| 59 | + |
| 60 | + task_status_response = self._gateway_api.get_status(task=self._task) |
| 61 | + task_status = task_status_response.status |
| 62 | + |
| 63 | + if task_status not in [GatewayTaskStatus.SUCCESS, GatewayTaskStatus.FAILURE]: |
| 64 | + return None |
| 65 | + |
| 66 | + if task_status == GatewayTaskStatus.FAILURE: |
| 67 | + raise GatewayFailureError(f"Gateway failure error") |
| 68 | + |
| 69 | + if task_status == GatewayTaskStatus.SUCCESS: |
| 70 | + spz_data = self._gateway_api.get_result(task=self._task) |
| 71 | + print(f"Received result for task: {self._task.id}") |
9 | 72 |
|
| 73 | + loader = get_spz() |
| 74 | + with tempfile.NamedTemporaryFile(delete=False, suffix=".ply") as temp_file: |
| 75 | + ply_data = loader.decompress(spz_data, include_normals=False) |
| 76 | + temp_file.write(ply_data) |
| 77 | + filepath = temp_file.name |
| 78 | + print(f"Saved result to: {filepath}") |
| 79 | + return filepath |
10 | 80 |
|
11 | | -def request_model(prompt: str) -> tuple[None, None] | tuple[str, str]: |
12 | | - url = bpy.context.preferences.addons[__package__].preferences.url |
13 | | - api_key = bpy.context.preferences.addons[__package__].preferences.token |
14 | | - filepath = None |
15 | | - winner_hotkey = None |
16 | | - |
17 | | - def on_message(ws, message): |
18 | | - nonlocal filepath, winner_hotkey |
19 | | - update = TaskUpdate(**json.loads(message)) |
20 | | - if update.status == TaskStatus.STARTED: |
21 | | - print("Task started") |
22 | | - elif update.status == TaskStatus.FIRST_RESULTS: |
23 | | - score = update.results.score if update.results else None |
24 | | - assets = update.results.assets or "" if update.results else "" |
25 | | - print(f"First results. Score: {score}. Size: {len(assets)}") |
26 | | - elif update.status == TaskStatus.BEST_RESULTS: |
27 | | - score = update.results.score if update.results else None |
28 | | - assets = update.results.assets or "" if update.results else "" |
29 | | - print(f"Best results. Score: {score}. Size: {len(assets)}") |
30 | | - print(f"Stats: {update.statistics}") |
31 | | - |
32 | | - if assets: |
33 | | - with tempfile.NamedTemporaryFile(delete=False, suffix=".ply") as temp_file: |
34 | | - temp_file.write(base64.b64decode(assets.encode("utf-8"))) |
35 | | - filepath = temp_file.name |
36 | | - ws.close() |
37 | | - |
38 | | - def on_error(ws, error): |
39 | | - print(f"WebSocket connection error: {error}") |
40 | | - |
41 | | - def on_close(ws, close_status_code, close_msg): |
42 | | - print(f"WebSocket connection closed: {close_status_code} {close_msg}") |
43 | | - |
44 | | - def on_open(ws): |
45 | | - auth_data = Auth(api_key=api_key).dict() |
46 | | - prompt_data = PromptData(prompt=prompt, send_first_results=True).dict() |
47 | | - ws.send(json.dumps(auth_data)) |
48 | | - ws.send(json.dumps(prompt_data)) |
49 | | - |
50 | | - ws = websocket.WebSocketApp(url, on_message=on_message, on_error=on_error, on_close=on_close) |
51 | | - ws.on_open = on_open |
52 | | - ws.run_forever() |
53 | | - |
54 | | - return (filepath, winner_hotkey) |
| 81 | + return None |
0 commit comments