From 89cea797b80b338c852018f42824156b0aaf8e5c Mon Sep 17 00:00:00 2001 From: Misha4ca228 Date: Wed, 30 Apr 2025 21:41:23 +0300 Subject: [PATCH] dev --- modules/api/api.py | 11 +++++++---- modules/api/models.py | 2 +- modules/download_from_s3.py | 22 ++++++++++++++++++++++ modules/env_to_yaml.py | 2 +- modules/s3.py | 3 +-- requirements.txt | 5 +++-- 6 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 modules/download_from_s3.py diff --git a/modules/api/api.py b/modules/api/api.py index 6de2ffb3b..e48aacd9c 100644 --- a/modules/api/api.py +++ b/modules/api/api.py @@ -490,11 +490,16 @@ class Api: return models.TextToImageResponse(images=b64images, parameters=vars(txt2imgreq), info=processed.js()) def img2imgapi(self, img2imgreq: models.StableDiffusionImg2ImgProcessingAPI): + from modules.env_to_yaml import get_env_var + from modules.s3 import s3_client + from modules.download_from_s3 import get_photo_base64 + task_id = img2imgreq.force_task_id or create_task_id("img2img") - init_images = img2imgreq.init_images - if init_images is None: + + if img2imgreq.init_images is None: raise HTTPException(status_code=404, detail="Init image not found") + init_images = [get_photo_base64(s3_url=img2imgreq.init_images)] mask = img2imgreq.mask if mask: @@ -562,8 +567,6 @@ class Api: img2imgreq.init_images = None img2imgreq.mask = None - from modules.s3 import s3_client - from modules.env_to_yaml import get_env_var import uuid b64_string = b64images[0].decode("utf-8") diff --git a/modules/api/models.py b/modules/api/models.py index 16497e94d..f9f4263a7 100644 --- a/modules/api/models.py +++ b/modules/api/models.py @@ -117,7 +117,7 @@ StableDiffusionImg2ImgProcessingAPI = PydanticModelGenerator( StableDiffusionProcessingImg2Img, [ {"key": "sampler_index", "type": str, "default": "Euler"}, - {"key": "init_images", "type": list, "default": None}, + {"key": "init_images", "type": str, "default": None}, {"key": "denoising_strength", "type": float, "default": 0.75}, {"key": "mask", "type": str, "default": None}, {"key": "include_init_images", "type": bool, "default": False, "exclude" : True}, diff --git a/modules/download_from_s3.py b/modules/download_from_s3.py new file mode 100644 index 000000000..85173bc28 --- /dev/null +++ b/modules/download_from_s3.py @@ -0,0 +1,22 @@ +import base64 +from modules.s3 import s3_client +from urllib.parse import urlparse + + +def get_photo_base64(s3_url) -> str: + try: + s3_url = s3_url + parsed = urlparse(s3_url) + path_parts = parsed.path.lstrip('/').split('/', 1) + + if len(path_parts) != 2: + raise ValueError("Не удалось извлечь bucket и key из URL.") + + bucket_name, s3_key = path_parts + + response = s3_client.get_object(Bucket=bucket_name, Key=s3_key) + image_data = response['Body'].read() + return base64.b64encode(image_data).decode('utf-8') + except Exception as e: + print(f"Ошибка при получении файла: {e}") + return "" diff --git a/modules/env_to_yaml.py b/modules/env_to_yaml.py index a17d741e1..58f9cbdf1 100644 --- a/modules/env_to_yaml.py +++ b/modules/env_to_yaml.py @@ -5,4 +5,4 @@ load_dotenv() def get_env_var(key: str) -> str: value = os.getenv(key) - return value \ No newline at end of file + return value diff --git a/modules/s3.py b/modules/s3.py index 4ce3aeb0d..1e17b9845 100644 --- a/modules/s3.py +++ b/modules/s3.py @@ -1,3 +1,4 @@ + import boto3 from botocore.client import Config @@ -11,5 +12,3 @@ s3_client = boto3.client( region_name='ru-1', config=Config(s3={'addressing_style': 'path'}) ) - - diff --git a/requirements.txt b/requirements.txt index e048612a0..90e053bc1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,18 +13,19 @@ gradio==3.41.2 inflection jsonmerge kornia +urllib lark numpy omegaconf open-clip-torch - +dotenv piexif protobuf==3.20.0 psutil pytorch_lightning requests resize-right - +botocore safetensors scikit-image>=0.19 tomesd