-
Notifications
You must be signed in to change notification settings - Fork 0
/
caption_cleanup_gemini_pass2.py
133 lines (104 loc) · 5.22 KB
/
caption_cleanup_gemini_pass2.py
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
import os
import glob
import time
import random
from openai import OpenAI
from dotenv import load_dotenv
from tqdm import tqdm
# Load environment variables from .env file
load_dotenv()
# function to clean up phrases from the caption. Because LLMs/VLMs are not perfect
def process_caption(caption, max_retries=5):
retry_count = 0
base_delay = 10
delay_multiplier = 2.5
while retry_count < max_retries:
try:
client = OpenAI(
api_key=os.getenv("GEMINI_API_KEY"),
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
response = client.chat.completions.create(
# model="gemini-1.5-flash",
model="gemini-2.0-flash-exp",
n=1,
messages=[
{
"role": "system",
"content": "You are an expert prompt editor and crafter, and a helpful assistant. You are an expert at cleaning up prompts from image captions."
},
{
"role": "user",
"content": (
f"Please remove any sentences that discuss depth. Please remove any sentences that discuss color palette directly, "
f"such as 'The pastel color palette of soft blues, greens, and browns, accented by vibrant flower colors, enhances the peaceful mood'. It is okay to talk about color, "
f"such as blue shorts, yellow skin, but not about color palette directly in a sentence. Please remove any sentence that discusses atmosphere, such "
f"as 'Soft, diffused daylight illuminates the scene, creating a calm and tranquil atmosphere'. It is okay to discuss lighting, such as daylight, but no need to mention atmosphere. "
f"Please only return a cleaned caption: {caption}"
)
}
],
timeout=120.0 # Add timeout parameter
)
return response.choices[0].message.content.strip()
except Exception as e: # if there is an error, print the error and retry
print(f"Attempt {retry_count + 1} failed. Error: {e}")
retry_count += 1
if retry_count == max_retries:
raise
delay = base_delay * (delay_multiplier ** retry_count) + \
random.uniform(0, base_delay *
retry_count) # calculate the delay
print(f"Retrying in {delay:.2f} seconds...")
time.sleep(delay)
# function to process the files
def process_files(folder_path, prepend_text):
# Create a 'cleaned' subdirectory if it doesn't exist
cleaned_folder = os.path.join(folder_path, "cleaned")
os.makedirs(cleaned_folder, exist_ok=True)
# Get a list of of all txt files for tqdm
txt_files = glob.glob(os.path.join(folder_path, '*.txt'))
# Create a progress bar for the files processing progress
for filename in tqdm(txt_files, desc="Processing captions", unit="file"):
try:
# Create new filename for cleaned caption
base_name = os.path.basename(filename)
name_without_p = base_name[2:] if base_name.startswith(
'p_') else base_name
name_without_ext = os.path.splitext(name_without_p)[0]
new_filename = os.path.join(
cleaned_folder, f"{name_without_ext}_c.txt")
# Skip if already processed
# checks if the pathfile exists and is not empty
if os.path.exists(new_filename) and os.path.getsize(new_filename) > 0:
print(f"Skipping {filename}: Already processed")
continue
# Process the caption
with open(filename, 'r') as file:
original_caption = file.read().strip()
# process the caption
processed_caption = process_caption(original_caption)
# use cleaned_caption if prepend_text is 'n', otherwise use full_caption
final_caption = processed_caption if prepend_text.lower(
) == 'n' else f"{prepend_text}, {processed_caption}"
# Write the caption
with open(new_filename, 'w', encoding='utf-8') as file:
file.write(final_caption)
# basename -> name_without_ext_c.txt
print(f"Processed: {base_name} -> {name_without_ext}_c.txt")
except Exception as e:
print(f"Error processing {filename}: {e}")
continue # Continue with next file if there's an error
def main():
folder_path = input("Enter the path to the folder containing the prompt text files: ").strip()
# if the input from the user has backquotes, single quotes, or double quotes, remove them
folder_path = folder_path.replace(
'"', '').replace("'", '').replace('`', '')
if not os.path.isdir(folder_path):
print(f"Error: '{folder_path}' is not a valid directory.")
return
prepend_text = input(
"Enter the prepend text/token (e.g., 'arcane oil') or 'n' to skip: ").strip()
process_files(folder_path, prepend_text)
if __name__ == "__main__":
main()