File size: 6,783 Bytes
564ac48
 
 
 
 
 
 
 
 
 
 
 
ac2193d
564ac48
ac2193d
 
564ac48
ac2193d
 
 
 
 
 
 
 
 
766c4fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
564ac48
 
 
 
ac2193d
 
 
 
 
 
 
564ac48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
064aede
 
 
 
 
 
564ac48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import b2sdk.v2 as b2 #Backblaze img2img upload bucket
import base64
import os
from io import BytesIO
import requests
import io
from PIL import Image
import numpy as npzipfile
import zipfile
import numpy as np
import json 
import gradio as gr
import subprocess

class dwebpException(Exception):
    pass


def dwebp(file: str):
    webp = subprocess.run(
        f"dwebp {file} -quiet -o -", shell=True, capture_output=True
    )
    if webp.returncode != 0:
        raise dwebpException(webp.stderr.decode())
    else:
        return Image.open(BytesIO(webp.stdout))
def resize_image(img, target_width, target_height):
  """Resizes an image while maintaining aspect ratio.



  Args:

    img: The PIL Image object to resize.

    target_width: The desired width.

    target_height: The desired height.



  Returns:

    The resized PIL Image object.

  """
  width, height = img.size
  aspect_ratio = width / height

  # Calculate new dimensions based on aspect ratio and target dimensions
  if width > height:
    new_width = target_width
    new_height = int(new_width / aspect_ratio)
  else:
    new_height = target_height
    new_width = int(new_height * aspect_ratio)

  # Resize the image
  resized_img = img.resize((new_width, new_height))
  return resized_img

# Example usage:
# Assuming img is your PIL Image object
# target_width = 512
# target_height = 512
# resized_img = resize_image(img, target_width, target_height)
# resized_img.show()

def find_closest_valid_dimension(dimension, valid_dimensions= [256, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024]):
  """Finds the closest valid dimension from a list of valid dimensions.



  Args:

    dimension: The target dimension.

    valid_dimensions: A list of valid dimensions.



  Returns:

    The closest valid dimension.

  """
  closest_dimension = min(valid_dimensions, key=lambda x: abs(x - dimension))
  return closest_dimension


def convert_to_pil(img):
    if isinstance(img, np.ndarray):
        img = Image.fromarray(img)
    # If the image is a URL, fetch the image and convert it to a PIL image
    elif isinstance(img, str):
        if "https:" in img or "http:" in img:
          response = requests.get(img)
          img = Image.open(BytesIO(response.content))
        #else is pil type 
        else:
          img=Image.open(img)
    return img 

def open_image_from_url(image_url):
  response = requests.get(image_url)
  img = Image.open(BytesIO(response.content))
  return img
def image_to_base64(img):
  buffered = io.BytesIO()
  img.save(buffered, format="PNG")
  img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
  return "data:image/png;base64,"+img_str

def cut_alpha_from_image(image):
  """Cuts out the alpha channel from a Pillow image, returning a new image.



  Args:

    image: The Pillow image to cut the alpha channel from.



  Returns:

    A new Pillow image with the alpha channel removed.

  """
  if image.mode == 'RGBA':
    # Extract RGB channels and create a new image without alpha
    rgb_image = Image.new("RGB", image.size, (0, 0, 0))
    rgb_image.paste(image, mask=image.split()[3])  # Use alpha channel as mask
    return rgb_image
  else:
    return image  # Image doesn't have an alpha channel, return as is

def update_model_dicts(traning_finnal,token_string,style_json="model_dict.json"):

  print(traning_finnal,token_string)
  current_style_dict=json.load(open(style_json,"r"))
  current_style_dict[token_string]=traning_finnal
  with open(style_json, "w") as json_file:
    json.dump(current_style_dict, json_file, indent=4)
  json_file.close()
  # Return the updated dictionary keys for updating the Dropdown
  return list(current_style_dict.keys())

def update_dropdown(traning_finnal, token_string):
    updated_keys = update_model_dicts(traning_finnal, token_string)
    return gr.Dropdown.update(choices=updated_keys)

def add_to_prompt(existing_prompt, new_prompt):
    if existing_prompt:
        return f"{existing_prompt}, {new_prompt}"
    else:
        return new_prompt
    
def update_gallery(img,gallery_list):
    img=convert_to_pil(img)
    
    gallery_list.append(img)
    return gallery_list



def numpy_to_base64(image_np):
  """Converts a numpy image to base64 string."""
  img = Image.fromarray(image_np)
  buffered = io.BytesIO()
  img.save(buffered, format="PNG")
  img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
  return "data:image/png;base64,"+img_str

def image_to_base64(img):
  buffered = io.BytesIO()
  if isinstance(img, np.ndarray):
     img=Image.fromarray(img)
  img.save(buffered, format="PNG")
  img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
  return "data:image/png;base64,"+img_str

def create_zip(files,captions,trigger):
    #Caption processing
    captions=captions.split("\n")
    #cute files and "tags:"
    captions= [cap.split("file:")[0][5:] for cap in captions]
    print("files",len(files),"captions",len(captions))
    #assert len(files)==len(captions) , "File amount does not equal the captions amount please check"
    temp_dir="./datasets/"
    os.makedirs(temp_dir,exist_ok=True)
    
    zip_path = os.path.join(temp_dir, f"training_data_{trigger}.zip")
    if os.path.exists(zip_path):
       os.remove(zip_path)

    with zipfile.ZipFile(zip_path, "w") as zip_file:
      for i, file in enumerate(files):
          # Add image to zip
          image_name = f"image_{i}.jpg"
          print(file)
          zip_file.write(file, image_name)
          # Add caption to zip
          caption_name = f"image_{i}.txt"
          caption_content = captions[i] +f", {trigger}"
          zip_file.writestr(caption_name, caption_content)
    return zip_path

def BB_uploadfile(local_file,file_name,BB_bucket_name,FRIENDLY_URL=True):
    info = b2.InMemoryAccountInfo()
    b2_api = b2.B2Api(info)
    #print(application_key_id,application_key)
    application_key_id = os.getenv("BB_KeyID")
    application_key = os.getenv("BB_AppKey")
    b2_api.authorize_account("production", application_key_id, application_key)
    BB_bucket=b2_api.get_bucket_by_name(BB_bucket_name)
    BB_defurl="https://f005.backblazeb2.com/file/"

    metadata = {"key": "value"}
    uploaded_file = BB_bucket.upload_local_file(
            local_file=local_file,
            file_name=file_name,
            file_infos=metadata,
    )
    img_url=b2_api.get_download_url_for_fileid(uploaded_file.id_)
    if FRIENDLY_URL: #Get friendly URP
        img_url=BB_defurl+BB_bucket_name+"/"+file_name
    print("backblaze", img_url)
    return img_url
#file="/content/training_data.zip"