Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
import torch
|
| 4 |
import subprocess
|
| 5 |
|
|
@@ -9,7 +9,7 @@ from transformers import (
|
|
| 9 |
)
|
| 10 |
import os
|
| 11 |
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
|
| 12 |
-
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 13 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 14 |
|
| 15 |
|
|
@@ -17,8 +17,8 @@ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
| 17 |
MODEL_NAME = "fancyfeast/llama-joycaption-beta-one-hf-llava"
|
| 18 |
llava_processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
| 19 |
llava_model = LlavaForConditionalGeneration.from_pretrained(MODEL_NAME,
|
| 20 |
-
torch_dtype=torch.bfloat16
|
| 21 |
-
|
| 22 |
# attn_implementation="eager")
|
| 23 |
|
| 24 |
|
|
@@ -46,11 +46,25 @@ print(f"Loading models on {device}...")
|
|
| 46 |
# -----------------------------
|
| 47 |
# Prediction Function (EXTENDED, NOT BROKEN)
|
| 48 |
# -----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def predict(image, prompt):
|
| 50 |
|
| 51 |
print("predict")
|
| 52 |
|
| 53 |
-
return predict_internal(image, "")
|
| 54 |
|
| 55 |
|
| 56 |
def predict_internal(image, prompt, textInput = None):
|
|
@@ -67,7 +81,7 @@ def predict_internal(image, prompt, textInput = None):
|
|
| 67 |
convo = [
|
| 68 |
{
|
| 69 |
"role": "system",
|
| 70 |
-
"content": "You are a helpful image captioner who describes nsfw images in great detail",
|
| 71 |
},
|
| 72 |
{
|
| 73 |
"role": "user",
|
|
@@ -84,7 +98,11 @@ def predict_internal(image, prompt, textInput = None):
|
|
| 84 |
|
| 85 |
# Process the inputs
|
| 86 |
inputs = llava_processor(text=[convo_string], images=[image], return_tensors="pt").to(device)
|
| 87 |
-
inputs['pixel_values'] = inputs['pixel_values'].to(torch.bfloat16)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
# Generate the captions
|
| 90 |
generate_ids = llava_model.generate(
|
|
@@ -102,10 +120,12 @@ def predict_internal(image, prompt, textInput = None):
|
|
| 102 |
|
| 103 |
# Trim off the prompt
|
| 104 |
generate_ids = generate_ids[inputs['input_ids'].shape[1]:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
# Decode the caption
|
| 107 |
-
caption = llava_processor.tokenizer.decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)
|
| 108 |
-
caption = caption.strip()
|
| 109 |
|
| 110 |
|
| 111 |
|
|
@@ -153,4 +173,16 @@ with gr.Blocks(title="BLIP Image Captioning - Advanced", theme=gr.themes.Soft())
|
|
| 153 |
# Launch
|
| 154 |
# -----------------------------
|
| 155 |
if __name__ == "__main__":
|
| 156 |
-
interface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageOps
|
| 3 |
import torch
|
| 4 |
import subprocess
|
| 5 |
|
|
|
|
| 9 |
)
|
| 10 |
import os
|
| 11 |
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
|
| 12 |
+
#subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 13 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 14 |
|
| 15 |
|
|
|
|
| 17 |
MODEL_NAME = "fancyfeast/llama-joycaption-beta-one-hf-llava"
|
| 18 |
llava_processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
| 19 |
llava_model = LlavaForConditionalGeneration.from_pretrained(MODEL_NAME,
|
| 20 |
+
torch_dtype=torch.bfloat16,
|
| 21 |
+
low_cpu_mem_usage=True)
|
| 22 |
# attn_implementation="eager")
|
| 23 |
|
| 24 |
|
|
|
|
| 46 |
# -----------------------------
|
| 47 |
# Prediction Function (EXTENDED, NOT BROKEN)
|
| 48 |
# -----------------------------
|
| 49 |
+
|
| 50 |
+
def resize_image_for_fast_caption(image, max_size=336):
|
| 51 |
+
"""Resize BEFORE processing - biggest speed gain"""
|
| 52 |
+
if not isinstance(image, Image.Image):
|
| 53 |
+
image = Image.open(image)
|
| 54 |
+
|
| 55 |
+
# Calculate resize dimensions
|
| 56 |
+
w, h = image.size
|
| 57 |
+
if max(w, h) > max_size:
|
| 58 |
+
ratio = max_size / max(w, h)
|
| 59 |
+
new_w, new_h = int(w * ratio), int(h * ratio)
|
| 60 |
+
image = image.resize((new_w, new_h), Image.Resampling.LANCZOS)
|
| 61 |
+
|
| 62 |
+
return image
|
| 63 |
def predict(image, prompt):
|
| 64 |
|
| 65 |
print("predict")
|
| 66 |
|
| 67 |
+
return predict_internal(resize_image_for_fast_caption(image), "")
|
| 68 |
|
| 69 |
|
| 70 |
def predict_internal(image, prompt, textInput = None):
|
|
|
|
| 81 |
convo = [
|
| 82 |
{
|
| 83 |
"role": "system",
|
| 84 |
+
"content": "You are a helpful image captioner who describes nsfw images in great detail, and starts with nsfw before describing the rest",
|
| 85 |
},
|
| 86 |
{
|
| 87 |
"role": "user",
|
|
|
|
| 98 |
|
| 99 |
# Process the inputs
|
| 100 |
inputs = llava_processor(text=[convo_string], images=[image], return_tensors="pt").to(device)
|
| 101 |
+
#inputs['pixel_values'] = inputs['pixel_values'].to(torch.bfloat16)
|
| 102 |
+
if device.type == 'cpu':
|
| 103 |
+
inputs['pixel_values'] = inputs['pixel_values'].to(torch.float32) # CPU likes float32
|
| 104 |
+
else:
|
| 105 |
+
inputs['pixel_values'] = inputs['pixel_values'].to(torch.bfloat16)
|
| 106 |
|
| 107 |
# Generate the captions
|
| 108 |
generate_ids = llava_model.generate(
|
|
|
|
| 120 |
|
| 121 |
# Trim off the prompt
|
| 122 |
generate_ids = generate_ids[inputs['input_ids'].shape[1]:]
|
| 123 |
+
caption = llava_processor.tokenizer.decode(
|
| 124 |
+
generate_ids,
|
| 125 |
+
skip_special_tokens=True,
|
| 126 |
+
clean_up_tokenization_spaces=True
|
| 127 |
+
).strip()
|
| 128 |
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
|
| 131 |
|
|
|
|
| 173 |
# Launch
|
| 174 |
# -----------------------------
|
| 175 |
if __name__ == "__main__":
|
| 176 |
+
interface.launch(share=True)
|
| 177 |
+
print("Warming up model...")
|
| 178 |
+
dummy_image = Image.new('RGB', (224, 224), color='white')
|
| 179 |
+
dummy_inputs = llava_processor(
|
| 180 |
+
text="Describe:",
|
| 181 |
+
images=dummy_image,
|
| 182 |
+
return_tensors="pt"
|
| 183 |
+
).to(device)
|
| 184 |
+
with torch.no_grad():
|
| 185 |
+
_ = llava_model.generate(**dummy_inputs, max_new_tokens=1)
|
| 186 |
+
del dummy_image, dummy_inputs
|
| 187 |
+
torch.cuda.empty_cache() if torch.cuda.is_available() else None
|
| 188 |
+
print("Model warmed up!")
|