This commit is contained in:
Misha4ca228 2025-04-30 21:41:23 +03:00
parent 504abc5ad8
commit 89cea797b8
6 changed files with 35 additions and 10 deletions

View File

@ -490,11 +490,16 @@ class Api:
return models.TextToImageResponse(images=b64images, parameters=vars(txt2imgreq), info=processed.js()) return models.TextToImageResponse(images=b64images, parameters=vars(txt2imgreq), info=processed.js())
def img2imgapi(self, img2imgreq: models.StableDiffusionImg2ImgProcessingAPI): 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") 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") raise HTTPException(status_code=404, detail="Init image not found")
init_images = [get_photo_base64(s3_url=img2imgreq.init_images)]
mask = img2imgreq.mask mask = img2imgreq.mask
if mask: if mask:
@ -562,8 +567,6 @@ class Api:
img2imgreq.init_images = None img2imgreq.init_images = None
img2imgreq.mask = None img2imgreq.mask = None
from modules.s3 import s3_client
from modules.env_to_yaml import get_env_var
import uuid import uuid
b64_string = b64images[0].decode("utf-8") b64_string = b64images[0].decode("utf-8")

View File

@ -117,7 +117,7 @@ StableDiffusionImg2ImgProcessingAPI = PydanticModelGenerator(
StableDiffusionProcessingImg2Img, StableDiffusionProcessingImg2Img,
[ [
{"key": "sampler_index", "type": str, "default": "Euler"}, {"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": "denoising_strength", "type": float, "default": 0.75},
{"key": "mask", "type": str, "default": None}, {"key": "mask", "type": str, "default": None},
{"key": "include_init_images", "type": bool, "default": False, "exclude" : True}, {"key": "include_init_images", "type": bool, "default": False, "exclude" : True},

View File

@ -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 ""

View File

@ -5,4 +5,4 @@ load_dotenv()
def get_env_var(key: str) -> str: def get_env_var(key: str) -> str:
value = os.getenv(key) value = os.getenv(key)
return value return value

View File

@ -1,3 +1,4 @@
import boto3 import boto3
from botocore.client import Config from botocore.client import Config
@ -11,5 +12,3 @@ s3_client = boto3.client(
region_name='ru-1', region_name='ru-1',
config=Config(s3={'addressing_style': 'path'}) config=Config(s3={'addressing_style': 'path'})
) )

View File

@ -13,18 +13,19 @@ gradio==3.41.2
inflection inflection
jsonmerge jsonmerge
kornia kornia
urllib
lark lark
numpy numpy
omegaconf omegaconf
open-clip-torch open-clip-torch
dotenv
piexif piexif
protobuf==3.20.0 protobuf==3.20.0
psutil psutil
pytorch_lightning pytorch_lightning
requests requests
resize-right resize-right
botocore
safetensors safetensors
scikit-image>=0.19 scikit-image>=0.19
tomesd tomesd