Spaces:
Paused
Paused
alessandro trinca tornidor
commited on
Commit
·
2640499
1
Parent(s):
ca22ec3
[debug] now some functions can use an external logger, bump to version 1.0.5
Browse files- lisa_on_cuda/app/main.py +5 -5
- lisa_on_cuda/utils/app_helpers.py +31 -20
- poetry.lock +3 -3
- pyproject.toml +2 -2
lisa_on_cuda/app/main.py
CHANGED
@@ -21,12 +21,12 @@ app.mount("/static", StaticFiles(directory=utils.FASTAPI_STATIC), name="static")
|
|
21 |
templates = Jinja2Templates(directory="templates")
|
22 |
|
23 |
|
24 |
-
|
25 |
args = app_helpers.parse_args([])
|
26 |
-
|
27 |
inference_fn = app_helpers.get_inference_model_by_args(args)
|
28 |
-
|
29 |
io = app_helpers.get_gradio_interface(inference_fn)
|
30 |
-
|
31 |
app = gr.mount_gradio_app(app, io, path=CUSTOM_GRADIO_PATH)
|
32 |
-
|
|
|
21 |
templates = Jinja2Templates(directory="templates")
|
22 |
|
23 |
|
24 |
+
app_helpers.app_logger.info(f"sys.argv:{sys.argv}.")
|
25 |
args = app_helpers.parse_args([])
|
26 |
+
app_helpers.app_logger.info(f"prepared default arguments:{args}.")
|
27 |
inference_fn = app_helpers.get_inference_model_by_args(args)
|
28 |
+
app_helpers.app_logger.info(f"prepared inference_fn function:{inference_fn.__name__}, creating gradio interface...")
|
29 |
io = app_helpers.get_gradio_interface(inference_fn)
|
30 |
+
app_helpers.app_logger.info("created gradio interface")
|
31 |
app = gr.mount_gradio_app(app, io, path=CUSTOM_GRADIO_PATH)
|
32 |
+
app_helpers.app_logger.info("mounted gradio app within fastapi")
|
lisa_on_cuda/utils/app_helpers.py
CHANGED
@@ -17,13 +17,15 @@ from lisa_on_cuda.llava import conversation as conversation_lib
|
|
17 |
from lisa_on_cuda.llava.mm_utils import tokenizer_image_token
|
18 |
from lisa_on_cuda.segment_anything.utils.transforms import ResizeLongestSide
|
19 |
|
20 |
-
|
21 |
placeholders = utils.create_placeholder_variables()
|
|
|
22 |
|
23 |
|
24 |
@session_logger.set_uuid_logging
|
25 |
-
def parse_args(args_to_parse):
|
26 |
-
|
|
|
|
|
27 |
parser = argparse.ArgumentParser(description="LISA chat")
|
28 |
parser.add_argument("--version", default="xinlai/LISA-13B-llama2-v1-explanatory")
|
29 |
parser.add_argument("--vis_save_path", default=str(utils.VIS_OUTPUT), type=str)
|
@@ -54,8 +56,10 @@ def parse_args(args_to_parse):
|
|
54 |
|
55 |
|
56 |
@session_logger.set_uuid_logging
|
57 |
-
def get_cleaned_input(input_str):
|
58 |
-
|
|
|
|
|
59 |
input_str = nh3.clean(
|
60 |
input_str,
|
61 |
tags={
|
@@ -80,7 +84,7 @@ def get_cleaned_input(input_str):
|
|
80 |
url_schemes={"http", "https", "mailto"},
|
81 |
link_rel=None,
|
82 |
)
|
83 |
-
|
84 |
return input_str
|
85 |
|
86 |
|
@@ -207,16 +211,20 @@ def get_inference_model_by_args(args_to_parse):
|
|
207 |
no_seg_out = placeholders["no_seg_out"]
|
208 |
|
209 |
@session_logger.set_uuid_logging
|
210 |
-
def inference(input_str: str, input_image: str | np.ndarray):
|
211 |
-
|
|
|
|
|
|
|
|
|
212 |
input_str = get_cleaned_input(input_str)
|
213 |
-
|
214 |
-
|
215 |
|
216 |
-
|
217 |
if not re.match(r"^[A-Za-z ,.!?\'\"]+$", input_str) or len(input_str) < 1:
|
218 |
output_str = f"[Error] Unprocessable Entity input: {input_str}."
|
219 |
-
|
220 |
|
221 |
from fastapi import status
|
222 |
from fastapi.responses import JSONResponse
|
@@ -241,6 +249,7 @@ def get_inference_model_by_args(args_to_parse):
|
|
241 |
conv.append_message(conv.roles[1], "")
|
242 |
prompt = conv.get_prompt()
|
243 |
|
|
|
244 |
image_np = input_image
|
245 |
if isinstance(input_image, str):
|
246 |
image_np = cv2.imread(input_image)
|
@@ -254,7 +263,7 @@ def get_inference_model_by_args(args_to_parse):
|
|
254 |
.unsqueeze(0)
|
255 |
.cuda()
|
256 |
)
|
257 |
-
|
258 |
image_clip = set_image_precision_by_args(image_clip, args_to_parse.precision)
|
259 |
|
260 |
image = transform.apply_image(image_np)
|
@@ -265,12 +274,13 @@ def get_inference_model_by_args(args_to_parse):
|
|
265 |
.unsqueeze(0)
|
266 |
.cuda()
|
267 |
)
|
268 |
-
|
269 |
image = set_image_precision_by_args(image, args_to_parse.precision)
|
270 |
|
271 |
input_ids = tokenizer_image_token(prompt, tokenizer, return_tensors="pt")
|
272 |
input_ids = input_ids.unsqueeze(0).cuda()
|
273 |
|
|
|
274 |
output_ids, pred_masks = model.evaluate(
|
275 |
image_clip,
|
276 |
image,
|
@@ -280,14 +290,15 @@ def get_inference_model_by_args(args_to_parse):
|
|
280 |
max_new_tokens=512,
|
281 |
tokenizer=tokenizer,
|
282 |
)
|
|
|
283 |
output_ids = output_ids[0][output_ids[0] != utils.IMAGE_TOKEN_INDEX]
|
284 |
|
285 |
text_output = tokenizer.decode(output_ids, skip_special_tokens=False)
|
286 |
text_output = text_output.replace("\n", "").replace(" ", " ")
|
287 |
text_output = text_output.split("ASSISTANT: ")[-1]
|
288 |
|
289 |
-
|
290 |
-
f"found n {len(pred_masks)} prediction masks, "
|
291 |
f"text_output type: {type(text_output)}, text_output: {text_output}."
|
292 |
)
|
293 |
output_image = no_seg_out
|
@@ -301,15 +312,15 @@ def get_inference_model_by_args(args_to_parse):
|
|
301 |
|
302 |
output_image = image_np.copy()
|
303 |
output_image[pred_mask_bool] = (
|
304 |
-
|
305 |
-
|
306 |
)[pred_mask_bool]
|
307 |
|
308 |
output_str = f"ASSISTANT: {text_output} ..."
|
309 |
-
|
310 |
return output_image, output_mask, output_str
|
311 |
|
312 |
-
|
313 |
return inference
|
314 |
|
315 |
|
|
|
17 |
from lisa_on_cuda.llava.mm_utils import tokenizer_image_token
|
18 |
from lisa_on_cuda.segment_anything.utils.transforms import ResizeLongestSide
|
19 |
|
|
|
20 |
placeholders = utils.create_placeholder_variables()
|
21 |
+
app_logger = logging.getLogger(__name__)
|
22 |
|
23 |
|
24 |
@session_logger.set_uuid_logging
|
25 |
+
def parse_args(args_to_parse, internal_logger=None):
|
26 |
+
if internal_logger is None:
|
27 |
+
internal_logger = app_logger
|
28 |
+
internal_logger.info(f"ROOT_PROJECT:{utils.PROJECT_ROOT_FOLDER}, default vis_output:{utils.VIS_OUTPUT}.")
|
29 |
parser = argparse.ArgumentParser(description="LISA chat")
|
30 |
parser.add_argument("--version", default="xinlai/LISA-13B-llama2-v1-explanatory")
|
31 |
parser.add_argument("--vis_save_path", default=str(utils.VIS_OUTPUT), type=str)
|
|
|
56 |
|
57 |
|
58 |
@session_logger.set_uuid_logging
|
59 |
+
def get_cleaned_input(input_str, internal_logger=None):
|
60 |
+
if internal_logger is None:
|
61 |
+
internal_logger = app_logger
|
62 |
+
internal_logger.info(f"start cleaning of input_str: {input_str}.")
|
63 |
input_str = nh3.clean(
|
64 |
input_str,
|
65 |
tags={
|
|
|
84 |
url_schemes={"http", "https", "mailto"},
|
85 |
link_rel=None,
|
86 |
)
|
87 |
+
internal_logger.info(f"cleaned input_str: {input_str}.")
|
88 |
return input_str
|
89 |
|
90 |
|
|
|
211 |
no_seg_out = placeholders["no_seg_out"]
|
212 |
|
213 |
@session_logger.set_uuid_logging
|
214 |
+
def inference(input_str: str, input_image: str | np.ndarray, internal_logger: logging = None):
|
215 |
+
|
216 |
+
if internal_logger is None:
|
217 |
+
internal_logger = app_logger
|
218 |
+
|
219 |
+
# filter out special chars
|
220 |
input_str = get_cleaned_input(input_str)
|
221 |
+
internal_logger.info(f" input_str type: {type(input_str)}, input_image type: {type(input_image)}.")
|
222 |
+
internal_logger.info(f"input_str: {input_str}, input_image: {type(input_image)}.")
|
223 |
|
224 |
+
# input valid check
|
225 |
if not re.match(r"^[A-Za-z ,.!?\'\"]+$", input_str) or len(input_str) < 1:
|
226 |
output_str = f"[Error] Unprocessable Entity input: {input_str}."
|
227 |
+
internal_logger.error(output_str)
|
228 |
|
229 |
from fastapi import status
|
230 |
from fastapi.responses import JSONResponse
|
|
|
249 |
conv.append_message(conv.roles[1], "")
|
250 |
prompt = conv.get_prompt()
|
251 |
|
252 |
+
internal_logger.info("read and preprocess image.")
|
253 |
image_np = input_image
|
254 |
if isinstance(input_image, str):
|
255 |
image_np = cv2.imread(input_image)
|
|
|
263 |
.unsqueeze(0)
|
264 |
.cuda()
|
265 |
)
|
266 |
+
internal_logger.info(f"image_clip type: {type(image_clip)}.")
|
267 |
image_clip = set_image_precision_by_args(image_clip, args_to_parse.precision)
|
268 |
|
269 |
image = transform.apply_image(image_np)
|
|
|
274 |
.unsqueeze(0)
|
275 |
.cuda()
|
276 |
)
|
277 |
+
internal_logger.info(f"image_clip type: {type(image_clip)}.")
|
278 |
image = set_image_precision_by_args(image, args_to_parse.precision)
|
279 |
|
280 |
input_ids = tokenizer_image_token(prompt, tokenizer, return_tensors="pt")
|
281 |
input_ids = input_ids.unsqueeze(0).cuda()
|
282 |
|
283 |
+
internal_logger.info("start model evaluation...")
|
284 |
output_ids, pred_masks = model.evaluate(
|
285 |
image_clip,
|
286 |
image,
|
|
|
290 |
max_new_tokens=512,
|
291 |
tokenizer=tokenizer,
|
292 |
)
|
293 |
+
internal_logger.info("model evaluation done, start token decoding...")
|
294 |
output_ids = output_ids[0][output_ids[0] != utils.IMAGE_TOKEN_INDEX]
|
295 |
|
296 |
text_output = tokenizer.decode(output_ids, skip_special_tokens=False)
|
297 |
text_output = text_output.replace("\n", "").replace(" ", " ")
|
298 |
text_output = text_output.split("ASSISTANT: ")[-1]
|
299 |
|
300 |
+
internal_logger.info(
|
301 |
+
f"token decoding ended,found n {len(pred_masks)} prediction masks, "
|
302 |
f"text_output type: {type(text_output)}, text_output: {text_output}."
|
303 |
)
|
304 |
output_image = no_seg_out
|
|
|
312 |
|
313 |
output_image = image_np.copy()
|
314 |
output_image[pred_mask_bool] = (
|
315 |
+
image_np * 0.5
|
316 |
+
+ pred_mask_bool[:, :, None].astype(np.uint8) * np.array([255, 0, 0]) * 0.5
|
317 |
)[pred_mask_bool]
|
318 |
|
319 |
output_str = f"ASSISTANT: {text_output} ..."
|
320 |
+
internal_logger.info(f"output_image type: {type(output_mask)}.")
|
321 |
return output_image, output_mask, output_str
|
322 |
|
323 |
+
app_logger.info("prepared inference function!")
|
324 |
return inference
|
325 |
|
326 |
|
poetry.lock
CHANGED
@@ -659,13 +659,13 @@ files = [
|
|
659 |
|
660 |
[[package]]
|
661 |
name = "fsspec"
|
662 |
-
version = "2024.3.
|
663 |
description = "File-system specification"
|
664 |
optional = false
|
665 |
python-versions = ">=3.8"
|
666 |
files = [
|
667 |
-
{file = "fsspec-2024.3.
|
668 |
-
{file = "fsspec-2024.3.
|
669 |
]
|
670 |
|
671 |
[package.extras]
|
|
|
659 |
|
660 |
[[package]]
|
661 |
name = "fsspec"
|
662 |
+
version = "2024.3.1"
|
663 |
description = "File-system specification"
|
664 |
optional = false
|
665 |
python-versions = ">=3.8"
|
666 |
files = [
|
667 |
+
{file = "fsspec-2024.3.1-py3-none-any.whl", hash = "sha256:918d18d41bf73f0e2b261824baeb1b124bcf771767e3a26425cd7dec3332f512"},
|
668 |
+
{file = "fsspec-2024.3.1.tar.gz", hash = "sha256:f39780e282d7d117ffb42bb96992f8a90795e4d0fb0f661a70ca39fe9c43ded9"},
|
669 |
]
|
670 |
|
671 |
[package.extras]
|
pyproject.toml
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
[tool.poetry]
|
2 |
name = "lisa-on-cuda"
|
3 |
-
version = "1.0.
|
4 |
description = ""
|
5 |
authors = ["alessandro trinca tornidor <alessandro@trinca.tornidor.com>"]
|
6 |
license = "Apache 2.0"
|
@@ -8,7 +8,7 @@ readme = "README.md"
|
|
8 |
|
9 |
[metadata]
|
10 |
name = "lisa-on-cuda"
|
11 |
-
version = "1.0.
|
12 |
|
13 |
[tool.poetry.dependencies]
|
14 |
python = "~3.10"
|
|
|
1 |
[tool.poetry]
|
2 |
name = "lisa-on-cuda"
|
3 |
+
version = "1.0.5"
|
4 |
description = ""
|
5 |
authors = ["alessandro trinca tornidor <alessandro@trinca.tornidor.com>"]
|
6 |
license = "Apache 2.0"
|
|
|
8 |
|
9 |
[metadata]
|
10 |
name = "lisa-on-cuda"
|
11 |
+
version = "1.0.5"
|
12 |
|
13 |
[tool.poetry.dependencies]
|
14 |
python = "~3.10"
|