Spaces:
Runtime error
Runtime error
File size: 5,112 Bytes
0b89080 15c5b7e 0b89080 |
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 |
import os, subprocess
def setup():
install_cmds = [
['pip', 'install', 'gradio'],
['pip', 'install', 'open_clip_torch'],
['pip', 'install', 'clip-interrogator'],
['pip', 'install', 'git+https://github.com/pharmapsychotic/BLIP.git'],
]
for cmd in install_cmds:
print(subprocess.run(cmd, stdout=subprocess.PIPE).stdout.decode('utf-8'))
setup()
clip_model_name = 'ViT-L-14/openai' #@param ["ViT-L-14/openai", "ViT-H-14/laion2b_s32b_b79k"]
print("Download preprocessed cache files...")
CACHE_URLS = [
'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-L-14_openai_artists.pkl',
'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-L-14_openai_flavors.pkl',
'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-L-14_openai_mediums.pkl',
'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-L-14_openai_movements.pkl',
'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-L-14_openai_trendings.pkl',
] if clip_model_name == 'ViT-L-14/openai' else [
'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_artists.pkl',
'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_flavors.pkl',
'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_mediums.pkl',
'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_movements.pkl',
'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_trendings.pkl',
]
os.makedirs('cache', exist_ok=True)
for url in CACHE_URLS:
print(subprocess.run(['wget', url, '-P', 'cache'], stdout=subprocess.PIPE).stdout.decode('utf-8'))
import gradio as gr
from clip_interrogator import Config, Interrogator
config = Config()
config.blip_num_beams = 64
config.blip_offload = False
config.clip_model_name = clip_model_name
ci = Interrogator(config)
def inference(image, mode, best_max_flavors=32):
ci.config.chunk_size = 2048 if ci.config.clip_model_name == "ViT-L-14/openai" else 1024
ci.config.flavor_intermediate_count = 2048 if ci.config.clip_model_name == "ViT-L-14/openai" else 1024
image = image.convert('RGB')
if mode == 'best':
return ci.interrogate(image, max_flavors=int(best_max_flavors))
elif mode == 'classic':
return ci.interrogate_classic(image)
else:
return ci.interrogate_fast(image)
#@title Image to prompt! 🖼️ -> 📝
inputs = [
gr.inputs.Image(type='pil'),
gr.Radio(['best', 'fast'], label='', value='best'),
gr.Number(value=16, label='best mode max flavors'),
]
outputs = [
gr.outputs.Textbox(label="Output"),
]
io = gr.Interface(
inference,
inputs,
outputs,
allow_flagging=False,
)
io.launch(debug=False)
import csv
import os
from IPython.display import clear_output, display
from PIL import Image
from tqdm import tqdm
folder_path = "/content/my_images" #@param {type:"string"}
prompt_mode = 'best' #@param ["best","fast"]
output_mode = 'rename' #@param ["desc.csv","rename"]
max_filename_len = 128 #@param {type:"integer"}
best_max_flavors = 16 #@param {type:"integer"}
def sanitize_for_filename(prompt: str, max_len: int) -> str:
name = "".join(c for c in prompt if (c.isalnum() or c in ",._-! "))
name = name.strip()[:(max_len-4)] # extra space for extension
return name
ci.config.quiet = True
files = [f for f in os.listdir(folder_path) if f.endswith('.jpg') or f.endswith('.png')] if os.path.exists(folder_path) else []
prompts = []
for idx, file in enumerate(tqdm(files, desc='Generating prompts')):
if idx > 0 and idx % 100 == 0:
clear_output(wait=True)
image = Image.open(os.path.join(folder_path, file)).convert('RGB')
prompt = inference(image, prompt_mode, best_max_flavors=best_max_flavors)
prompts.append(prompt)
print(prompt)
thumb = image.copy()
thumb.thumbnail([256, 256])
display(thumb)
if output_mode == 'rename':
name = sanitize_for_filename(prompt, max_filename_len)
ext = os.path.splitext(file)[1]
filename = name + ext
idx = 1
while os.path.exists(os.path.join(folder_path, filename)):
print(f'File {filename} already exists, trying {idx+1}...')
filename = f"{name}_{idx}{ext}"
idx += 1
os.rename(os.path.join(folder_path, file), os.path.join(folder_path, filename))
if len(prompts):
if output_mode == 'desc.csv':
csv_path = os.path.join(folder_path, 'desc.csv')
with open(csv_path, 'w', encoding='utf-8', newline='') as f:
w = csv.writer(f, quoting=csv.QUOTE_MINIMAL)
w.writerow(['image', 'prompt'])
for file, prompt in zip(files, prompts):
w.writerow([file, prompt])
print(f"\n\n\n\nGenerated {len(prompts)} prompts and saved to {csv_path}, enjoy!")
else:
print(f"\n\n\n\nGenerated {len(prompts)} prompts and renamed your files, enjoy!")
else:
print(f"Sorry, I couldn't find any images in {folder_path}") |