Spaces:
Running
on
Zero
Running
on
Zero
Upload 4 files
Browse files
README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: π
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 4.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
|
|
|
| 1 |
---
|
| 2 |
+
title: LlavaGuard
|
| 3 |
emoji: π
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.16.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
app.py
CHANGED
|
@@ -11,11 +11,11 @@ import gradio as gr
|
|
| 11 |
import spaces
|
| 12 |
import torch
|
| 13 |
|
|
|
|
| 14 |
from llava.constants import IMAGE_TOKEN_INDEX
|
| 15 |
from llava.constants import LOGDIR
|
| 16 |
from llava.conversation import (default_conversation, conv_templates)
|
| 17 |
from llava.mm_utils import KeywordsStoppingCriteria, tokenizer_image_token
|
| 18 |
-
from llava.model.builder import load_pretrained_model
|
| 19 |
from llava.utils import (build_logger, violates_moderation, moderation_msg)
|
| 20 |
from taxonomy import wrap_taxonomy, default_taxonomy
|
| 21 |
|
|
@@ -66,16 +66,16 @@ def run_llava(prompt, pil_image, temperature, top_p, max_new_tokens):
|
|
| 66 |
return outputs[0].strip()
|
| 67 |
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
|
| 81 |
def get_conv_log_filename():
|
|
@@ -260,11 +260,6 @@ def llava_bot(state, model_selector, temperature, top_p, max_new_tokens, request
|
|
| 260 |
if not os.path.isfile(filename):
|
| 261 |
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
| 262 |
image.save(filename)
|
| 263 |
-
# check if model is not None
|
| 264 |
-
if model is None or tokenizer is None:
|
| 265 |
-
print(model)
|
| 266 |
-
print(tokenizer)
|
| 267 |
-
|
| 268 |
|
| 269 |
output = run_llava(prompt, all_images[0], temperature, top_p, max_new_tokens)
|
| 270 |
|
|
@@ -507,7 +502,8 @@ Set the environment variable `model` to change the model:
|
|
| 507 |
'LukasHug/LlavaGuard-13B-hf',
|
| 508 |
'LukasHug/LlavaGuard-34B-hf', ]
|
| 509 |
bits = int(os.getenv("bits", 16))
|
| 510 |
-
model = os.getenv("model", models[
|
|
|
|
| 511 |
model_path, model_name = model, model.split("/")[0]
|
| 512 |
if api_key:
|
| 513 |
cmd = f"huggingface-cli login --token {api_key} --add-to-git-credential"
|
|
@@ -520,7 +516,7 @@ Set the environment variable `model` to change the model:
|
|
| 520 |
model_path = '/common-repos/LlavaGuard/models/LlavaGuard-v1.1-7b-full/smid_and_crawled_v2_with_augmented_policies/json-v16/llava'
|
| 521 |
|
| 522 |
print(f"Loading model {model_path}")
|
| 523 |
-
tokenizer, model, image_processor, context_len = load_pretrained_model(model_path, None, model_name)
|
| 524 |
model.config.tokenizer_model_max_length = 2048 * 2
|
| 525 |
|
| 526 |
exit_status = 0
|
|
|
|
| 11 |
import spaces
|
| 12 |
import torch
|
| 13 |
|
| 14 |
+
from builder import load_pretrained_model
|
| 15 |
from llava.constants import IMAGE_TOKEN_INDEX
|
| 16 |
from llava.constants import LOGDIR
|
| 17 |
from llava.conversation import (default_conversation, conv_templates)
|
| 18 |
from llava.mm_utils import KeywordsStoppingCriteria, tokenizer_image_token
|
|
|
|
| 19 |
from llava.utils import (build_logger, violates_moderation, moderation_msg)
|
| 20 |
from taxonomy import wrap_taxonomy, default_taxonomy
|
| 21 |
|
|
|
|
| 66 |
return outputs[0].strip()
|
| 67 |
|
| 68 |
|
| 69 |
+
def load_selected_model(model_path):
|
| 70 |
+
model_name = model_path.split("/")[-1]
|
| 71 |
+
global tokenizer, model, image_processor, context_len
|
| 72 |
+
with warnings.catch_warnings(record=True) as w:
|
| 73 |
+
warnings.simplefilter("always")
|
| 74 |
+
tokenizer, model, image_processor, context_len = load_pretrained_model(model_path, None, model_name)
|
| 75 |
+
for warning in w:
|
| 76 |
+
if "vision" not in str(warning.message).lower():
|
| 77 |
+
print(warning.message)
|
| 78 |
+
model.config.tokenizer_model_max_length = 2048 * 2
|
| 79 |
|
| 80 |
|
| 81 |
def get_conv_log_filename():
|
|
|
|
| 260 |
if not os.path.isfile(filename):
|
| 261 |
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
| 262 |
image.save(filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
output = run_llava(prompt, all_images[0], temperature, top_p, max_new_tokens)
|
| 265 |
|
|
|
|
| 502 |
'LukasHug/LlavaGuard-13B-hf',
|
| 503 |
'LukasHug/LlavaGuard-34B-hf', ]
|
| 504 |
bits = int(os.getenv("bits", 16))
|
| 505 |
+
model = os.getenv("model", models[1])
|
| 506 |
+
available_devices = os.getenv("CUDA_VISIBLE_DEVICES", "0")
|
| 507 |
model_path, model_name = model, model.split("/")[0]
|
| 508 |
if api_key:
|
| 509 |
cmd = f"huggingface-cli login --token {api_key} --add-to-git-credential"
|
|
|
|
| 516 |
model_path = '/common-repos/LlavaGuard/models/LlavaGuard-v1.1-7b-full/smid_and_crawled_v2_with_augmented_policies/json-v16/llava'
|
| 517 |
|
| 518 |
print(f"Loading model {model_path}")
|
| 519 |
+
tokenizer, model, image_processor, context_len = load_pretrained_model(model_path, None, model_name, token=api_key)
|
| 520 |
model.config.tokenizer_model_max_length = 2048 * 2
|
| 521 |
|
| 522 |
exit_status = 0
|