|
7 | 7 | from pathlib import Path |
8 | 8 |
|
9 | 9 | import httpx |
10 | | -import aiofiles |
11 | 10 |
|
12 | 11 | from aymara_ai import AymaraAI, AsyncAymaraAI |
| 12 | +from aymara_ai.lib.uploads import upload_file, upload_file_async |
13 | 13 | from aymara_ai.lib.async_utils import wait_until_complete, async_wait_until_complete |
14 | 14 | from aymara_ai.types.eval_prompt import EvalPrompt |
15 | 15 | from aymara_ai.types.shared_params import FileReference |
@@ -57,27 +57,6 @@ def _default_response_adapter(model_output: Union[str, Path], prompt: EvalPrompt |
57 | 57 | else: |
58 | 58 | raise ValueError("Unsupported model output type for response adapter.") |
59 | 59 |
|
60 | | - def upload_file_content( |
61 | | - self, *, client: httpx.Client, file_name: str, file_content: Union[Path, bytes] |
62 | | - ) -> FileReference: |
63 | | - """ |
64 | | - Helper to upload file content (from Path or bytes) and return (FileReference). |
65 | | - """ |
66 | | - upload_resp = self.client.files.create(files=[{"local_file_path": file_name}]) |
67 | | - file_info = upload_resp.files[0] |
68 | | - if not file_info.file_url: |
69 | | - raise RuntimeError("No presigned file_url returned for upload.") |
70 | | - if isinstance(file_content, Path): |
71 | | - with open(str(file_content), "rb") as f: |
72 | | - put_resp = client.put(file_info.file_url, content=f) |
73 | | - put_resp.raise_for_status() |
74 | | - elif isinstance(file_content, bytes): # type: ignore |
75 | | - put_resp = client.put(file_info.file_url, content=file_content) |
76 | | - put_resp.raise_for_status() |
77 | | - else: |
78 | | - raise ValueError("Unsupported file_content type for upload.") |
79 | | - return FileReference(remote_file_path=file_info.remote_file_path) |
80 | | - |
81 | 60 | def run_eval( |
82 | 61 | self, |
83 | 62 | eval_params: Dict[str, Any], |
@@ -114,17 +93,23 @@ def run_eval( |
114 | 93 | continue |
115 | 94 |
|
116 | 95 | file_content = model_output |
117 | | - file_name = "model_output.png" |
| 96 | + file_name = None |
118 | 97 | if isinstance(model_output, Path): # type: ignore |
119 | 98 | file_content = model_output |
120 | 99 | file_name = str(model_output) |
121 | | - file_ref = self.upload_file_content(client=client, file_content=file_content, file_name=file_name) |
| 100 | + file_ref = upload_file( |
| 101 | + client=self.client, |
| 102 | + http_client=client, |
| 103 | + file_name=file_name, |
| 104 | + file_content=file_content, |
| 105 | + content_type=None, |
| 106 | + ) |
122 | 107 | response = EvalResponseParam( |
123 | 108 | content=file_ref, |
124 | 109 | prompt_uuid=prompt.prompt_uuid, |
125 | 110 | content_type="image", |
126 | 111 | ) |
127 | | - response["local_file_path"] = file_name # type: ignore |
| 112 | + response["local_file_path"] = file_name if file_name is not None else "file.png" # type: ignore |
128 | 113 | responses.append(response) |
129 | 114 |
|
130 | 115 | # 5. Create eval run |
@@ -175,29 +160,6 @@ def _default_response_adapter(model_output: Union[str, Path], prompt: EvalPrompt |
175 | 160 | else: |
176 | 161 | raise ValueError("Unsupported model output type for response adapter.") |
177 | 162 |
|
178 | | - async def upload_file_content_async( |
179 | | - self, *, client: httpx.AsyncClient, file_name: str, file_content: Union[Path, bytes] |
180 | | - ) -> FileReference: |
181 | | - """ |
182 | | - Async helper to upload file content (from Path or bytes) and return (FileReference). |
183 | | - """ |
184 | | - upload_resp = await self.client.files.create(files=[{"local_file_path": file_name}]) |
185 | | - file_info = upload_resp.files[0] |
186 | | - if not file_info.file_url: |
187 | | - raise RuntimeError("No presigned file_url returned for upload.") |
188 | | - if isinstance(file_content, Path): |
189 | | - async with aiofiles.open(str(file_content), mode="rb") as f: |
190 | | - put_resp = await client.put(file_info.file_url, content=f) |
191 | | - if put_resp.status_code != 200: |
192 | | - raise RuntimeError(f"Failed to upload file: {put_resp.status_code}") |
193 | | - elif isinstance(file_content, bytes): # type: ignore |
194 | | - put_resp = await client.put(file_info.file_url, content=file_content) |
195 | | - if put_resp.status_code != 200: |
196 | | - raise RuntimeError(f"Failed to upload file: {put_resp.status_code}") |
197 | | - else: |
198 | | - raise ValueError("Unsupported file_content type for upload.") |
199 | | - return FileReference(remote_file_path=file_info.remote_file_path) |
200 | | - |
201 | 163 | async def run_eval( |
202 | 164 | self, |
203 | 165 | eval_params: Dict[str, Any], |
@@ -237,19 +199,23 @@ async def run_eval( |
237 | 199 | continue |
238 | 200 |
|
239 | 201 | file_content = model_output |
240 | | - file_name = "model_output.png" |
| 202 | + file_name = None |
241 | 203 | if isinstance(model_output, Path): # type: ignore |
242 | 204 | file_content = model_output |
243 | 205 | file_name = str(model_output) |
244 | | - file_ref = await self.upload_file_content_async( |
245 | | - client=client, file_name=file_name, file_content=file_content |
| 206 | + file_ref = await upload_file_async( |
| 207 | + client=self.client, |
| 208 | + http_client=client, |
| 209 | + file_name=file_name, |
| 210 | + file_content=file_content, |
| 211 | + content_type=None, |
246 | 212 | ) |
247 | 213 | response = EvalResponseParam( |
248 | 214 | content=file_ref, |
249 | 215 | prompt_uuid=prompt.prompt_uuid, |
250 | 216 | content_type="image", |
251 | 217 | ) |
252 | | - response["local_file_path"] = file_name # type: ignore |
| 218 | + response["local_file_path"] = file_name if file_name is not None else "file.png" # type: ignore |
253 | 219 | responses.append(response) |
254 | 220 |
|
255 | 221 | # 5. Create eval run |
|
0 commit comments