From 95356adf9425a0a82fa0c3195171a9fb725967c3 Mon Sep 17 00:00:00 2001 From: Han Wang Date: Fri, 20 Oct 2023 16:27:43 +0800 Subject: [PATCH] fix dict subscribing in py38 --- .../promptflow/_utils/multimedia_utils.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/promptflow/promptflow/_utils/multimedia_utils.py b/src/promptflow/promptflow/_utils/multimedia_utils.py index fd059b07345..b6bc68d8793 100644 --- a/src/promptflow/promptflow/_utils/multimedia_utils.py +++ b/src/promptflow/promptflow/_utils/multimedia_utils.py @@ -2,14 +2,14 @@ import imghdr import os import re -import requests import uuid - from functools import partial from pathlib import Path -from typing import Any, Callable +from typing import Any, Callable, Dict from urllib.parse import urlparse +import requests + from promptflow.contracts._errors import InvalidImageInput from promptflow.contracts.flow import FlowInputDefinition from promptflow.contracts.multimedia import Image, PFBytes @@ -146,12 +146,10 @@ def create_image(value: any, base_dir: Path = None): def save_image_to_file(image: Image, file_name: str, folder_path: Path, relative_path: Path = None): ext = get_extension_from_mime_type(image._mime_type) file_name = f"{file_name}.{ext}" if ext else file_name - image_reference = { - f"data:{image._mime_type};path": str(relative_path / file_name) if relative_path else file_name - } + image_reference = {f"data:{image._mime_type};path": str(relative_path / file_name) if relative_path else file_name} path = folder_path / relative_path if relative_path else folder_path os.makedirs(path, exist_ok=True) - with open(os.path.join(path, file_name), 'wb') as file: + with open(os.path.join(path, file_name), "wb") as file: file.write(image) return image_reference @@ -163,6 +161,7 @@ def pfbytes_file_reference_encoder(obj): file_name = str(uuid.uuid4()) return save_image_to_file(obj, file_name, folder_path, relative_path) raise TypeError(f"Not supported to dump type '{type(obj).__name__}'.") + return pfbytes_file_reference_encoder @@ -185,7 +184,7 @@ def convert_multimedia_data_to_base64(value: Any): # TODO: Move this function to a more general place and integrate serialization to this function. -def recursive_process(value: Any, process_funcs: dict[type, Callable] = None) -> dict: +def recursive_process(value: Any, process_funcs: Dict[type, Callable] = None) -> dict: if process_funcs: for cls, f in process_funcs.items(): if isinstance(value, cls): @@ -197,7 +196,7 @@ def recursive_process(value: Any, process_funcs: dict[type, Callable] = None) -> return value -def load_multimedia_data(inputs: dict[str, FlowInputDefinition], line_inputs: dict, base_dir: Path): +def load_multimedia_data(inputs: Dict[str, FlowInputDefinition], line_inputs: dict, base_dir: Path): updated_inputs = dict(line_inputs or {}) for key, value in inputs.items(): if value.type == ValueType.IMAGE: