JarvisLabs commited on
Commit
8d5111c
1 Parent(s): 8276f79

Update src/utils.py

Browse files
Files changed (1) hide show
  1. src/utils.py +100 -90
src/utils.py CHANGED
@@ -1,90 +1,100 @@
1
- import b2sdk.v2 as b2 #Backblaze img2img upload bucket
2
- import base64
3
- import shutil
4
- import os
5
- import requests
6
- import io
7
- from PIL import Image
8
- import numpy as npzipfile
9
- import zipfile
10
-
11
- import tempfile
12
- import json
13
- import gradio as gr
14
-
15
- def update_model_dicts(traning_finnal,token_string,style_json="model_dict.json"):
16
-
17
- print(traning_finnal,token_string)
18
- current_style_dict=json.load(open(style_json,"r"))
19
- current_style_dict[token_string]=traning_finnal
20
- with open(style_json, "w") as json_file:
21
- json.dump(current_style_dict, json_file, indent=4)
22
- json_file.close()
23
- # Return the updated dictionary keys for updating the Dropdown
24
- return list(current_style_dict.keys())
25
-
26
- def update_dropdown(traning_finnal, token_string):
27
- updated_keys = update_model_dicts(traning_finnal, token_string)
28
- return gr.Dropdown.update(choices=updated_keys)
29
-
30
- def add_to_prompt(existing_prompt, new_prompt):
31
- if existing_prompt:
32
- return f"{existing_prompt}, {new_prompt}"
33
- else:
34
- return new_prompt
35
-
36
- def image_to_base64(img):
37
- buffered = io.BytesIO()
38
- img.save(buffered, format="PNG")
39
- img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
40
- return "data:image/png;base64,"+img_str
41
-
42
- def create_zip(files,captions,trigger):
43
- #Caption processing
44
- captions=captions.split("\n")
45
- #cute files and "tags:"
46
- captions= [cap.split("file:")[0][5:] for cap in captions]
47
- print("files",len(files),"captions",len(captions))
48
- #assert len(files)==len(captions) , "File amount does not equal the captions amount please check"
49
- temp_dir="./datasets/"
50
- os.makedirs(temp_dir,exist_ok=True)
51
-
52
- zip_path = os.path.join(temp_dir, f"training_data_{trigger}.zip")
53
- if os.path.exists(zip_path):
54
- os.remove(zip_path)
55
-
56
- with zipfile.ZipFile(zip_path, "w") as zip_file:
57
- for i, file in enumerate(files):
58
- # Add image to zip
59
- image_name = f"image_{i}.jpg"
60
- print(file)
61
- zip_file.write(file, image_name)
62
- # Add caption to zip
63
- caption_name = f"image_{i}.txt"
64
- caption_content = captions[i] +f", {trigger}"
65
- zip_file.writestr(caption_name, caption_content)
66
- return zip_path
67
-
68
- def BB_uploadfile(local_file,file_name,BB_bucket_name,FRIENDLY_URL=True):
69
- info = b2.InMemoryAccountInfo()
70
- b2_api = b2.B2Api(info)
71
- #print(application_key_id,application_key)
72
- application_key_id = os.getenv("BB_KeyID")
73
- application_key = os.getenv("BB_AppKey")
74
- b2_api.authorize_account("production", application_key_id, application_key)
75
- BB_bucket=b2_api.get_bucket_by_name(BB_bucket_name)
76
- BB_defurl="https://f005.backblazeb2.com/file/"
77
-
78
- metadata = {"key": "value"}
79
- uploaded_file = BB_bucket.upload_local_file(
80
- local_file=local_file,
81
- file_name=file_name,
82
- file_infos=metadata,
83
- )
84
- img_url=b2_api.get_download_url_for_fileid(uploaded_file.id_)
85
- if FRIENDLY_URL: #Get friendly URP
86
- img_url=BB_defurl+BB_bucket_name+"/"+file_name
87
- print("backblaze", img_url)
88
- return img_url
89
- #file="/content/training_data.zip"
90
-
 
 
 
 
 
 
 
 
 
 
 
1
+ import b2sdk.v2 as b2 #Backblaze img2img upload bucket
2
+ import base64
3
+ import shutil
4
+ import os
5
+ import requests
6
+ import io
7
+ from PIL import Image
8
+ import numpy as npzipfile
9
+ import zipfile
10
+
11
+ import tempfile
12
+ import json
13
+ import gradio as gr
14
+
15
+ def update_model_dicts(traning_finnal,token_string,style_json="model_dict.json"):
16
+
17
+ print(traning_finnal,token_string)
18
+ current_style_dict=json.load(open(style_json,"r"))
19
+ current_style_dict[token_string]=traning_finnal
20
+ with open(style_json, "w") as json_file:
21
+ json.dump(current_style_dict, json_file, indent=4)
22
+ json_file.close()
23
+ # Return the updated dictionary keys for updating the Dropdown
24
+ return list(current_style_dict.keys())
25
+
26
+ def update_dropdown(traning_finnal, token_string):
27
+ updated_keys = update_model_dicts(traning_finnal, token_string)
28
+ return gr.Dropdown.update(choices=updated_keys)
29
+
30
+ def add_to_prompt(existing_prompt, new_prompt):
31
+ if existing_prompt:
32
+ return f"{existing_prompt}, {new_prompt}"
33
+ else:
34
+ return new_prompt
35
+
36
+
37
+ def numpy_to_base64(image_np):
38
+ """Converts a numpy image to base64 string."""
39
+ img = Image.fromarray(image_np)
40
+ buffered = io.BytesIO()
41
+ img.save(buffered, format="PNG")
42
+ img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
43
+ return "data:image/png;base64,"+img_str
44
+
45
+ def image_to_base64(img):
46
+ buffered = io.BytesIO()
47
+ img.save(buffered, format="PNG")
48
+ img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
49
+ return "data:image/png;base64,"+img_str
50
+
51
+
52
+ def create_zip(files,captions,trigger):
53
+ #Caption processing
54
+ captions=captions.split("\n")
55
+ #cute files and "tags:"
56
+ captions= [cap.split("file:")[0][5:] for cap in captions]
57
+ print("files",len(files),"captions",len(captions))
58
+ #assert len(files)==len(captions) , "File amount does not equal the captions amount please check"
59
+ temp_dir="./datasets/"
60
+ os.makedirs(temp_dir,exist_ok=True)
61
+
62
+ zip_path = os.path.join(temp_dir, f"training_data_{trigger}.zip")
63
+ if os.path.exists(zip_path):
64
+ os.remove(zip_path)
65
+
66
+ with zipfile.ZipFile(zip_path, "w") as zip_file:
67
+ for i, file in enumerate(files):
68
+ # Add image to zip
69
+ image_name = f"image_{i}.jpg"
70
+ print(file)
71
+ zip_file.write(file, image_name)
72
+ # Add caption to zip
73
+ caption_name = f"image_{i}.txt"
74
+ caption_content = captions[i] +f", {trigger}"
75
+ zip_file.writestr(caption_name, caption_content)
76
+ return zip_path
77
+
78
+ def BB_uploadfile(local_file,file_name,BB_bucket_name,FRIENDLY_URL=True):
79
+ info = b2.InMemoryAccountInfo()
80
+ b2_api = b2.B2Api(info)
81
+ #print(application_key_id,application_key)
82
+ application_key_id = os.getenv("BB_KeyID")
83
+ application_key = os.getenv("BB_AppKey")
84
+ b2_api.authorize_account("production", application_key_id, application_key)
85
+ BB_bucket=b2_api.get_bucket_by_name(BB_bucket_name)
86
+ BB_defurl="https://f005.backblazeb2.com/file/"
87
+
88
+ metadata = {"key": "value"}
89
+ uploaded_file = BB_bucket.upload_local_file(
90
+ local_file=local_file,
91
+ file_name=file_name,
92
+ file_infos=metadata,
93
+ )
94
+ img_url=b2_api.get_download_url_for_fileid(uploaded_file.id_)
95
+ if FRIENDLY_URL: #Get friendly URP
96
+ img_url=BB_defurl+BB_bucket_name+"/"+file_name
97
+ print("backblaze", img_url)
98
+ return img_url
99
+ #file="/content/training_data.zip"
100
+