From df30642effad5a07a89dadbf2bc7a8fd3d76ea1d Mon Sep 17 00:00:00 2001 From: BrianGuo Date: Wed, 28 Aug 2024 17:47:17 -0700 Subject: [PATCH 1/3] add `test_vlm_generation.py` for ci --- tests/test_vlm_generation.py | 67 ++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/test_vlm_generation.py diff --git a/tests/test_vlm_generation.py b/tests/test_vlm_generation.py new file mode 100644 index 00000000..a8363aa5 --- /dev/null +++ b/tests/test_vlm_generation.py @@ -0,0 +1,67 @@ +from nexa.gguf import NexaVLMInference +from nexa.gguf.lib_utils import is_gpu_available +from tempfile import TemporaryDirectory +from .utils import download_model + +# Initialize the model +model = NexaVLMInference( + model_path="nanollava", + verbose=False, + n_gpu_layers=-1 if is_gpu_available() else 0, +) + +# Test VLM generation without an image +def test_text_only_generation(): + output = model.create_chat_completion( + messages=[ + {"role": "user", "content": "What is the capital of France?"} + ], + max_tokens=100, + stream=False + ) + response = output["choices"][0]["message"]["content"] + print("Text-only response:", response) + assert "Paris" in response, "The response should mention Paris" + +# Test VLM generation with an image +def test_image_description(): + img_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" + with TemporaryDirectory() as temp_dir: + img_path = download_model(img_url, temp_dir) + user_input = "Describe this image in detail." + output = model._chat(user_input, img_path) + response = "" + for chunk in output: + delta = chunk["choices"][0]["delta"] + if "content" in delta: + response += delta["content"] + print("Image description:", response) + assert len(response) > 50, "The image description should be detailed" + +# Test streaming output +def test_streaming_output(): + global model + messages = [ + {"role": "user", "content": "Write a short story about a robot learning to paint."} + ] + output = model.create_chat_completion(messages=messages, max_tokens=200, stream=True) + story = "" + for chunk in output: + if "choices" in chunk and len(chunk["choices"]) > 0: + delta = chunk["choices"][0]["delta"] + if "content" in delta: + story += delta["content"] + print(delta["content"], end="", flush=True) + print("\nFull story:", story) + assert len(story) > 100, "The generated story should be of substantial length" + +# Main execution +if __name__ == "__main__": + print("=== Testing Text-Only Generation ===") + test_text_only_generation() + + print("\n=== Testing Image Generation ===") + test_image_description() + + print("\n=== Testing Streaming Output ===") + test_streaming_output() \ No newline at end of file From 6f36a6788e6c569180e877e1fe06898adac521f1 Mon Sep 17 00:00:00 2001 From: JoyboyBrian Date: Thu, 29 Aug 2024 16:41:32 +0000 Subject: [PATCH 2/3] comment main execution --- tests/test_vlm_generation.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/test_vlm_generation.py b/tests/test_vlm_generation.py index a8363aa5..7d0d234d 100644 --- a/tests/test_vlm_generation.py +++ b/tests/test_vlm_generation.py @@ -56,12 +56,12 @@ def test_streaming_output(): assert len(story) > 100, "The generated story should be of substantial length" # Main execution -if __name__ == "__main__": - print("=== Testing Text-Only Generation ===") - test_text_only_generation() +# if __name__ == "__main__": +# print("=== Testing Text-Only Generation ===") +# test_text_only_generation() - print("\n=== Testing Image Generation ===") - test_image_description() +# print("\n=== Testing Image Generation ===") +# test_image_description() - print("\n=== Testing Streaming Output ===") - test_streaming_output() \ No newline at end of file +# print("\n=== Testing Streaming Output ===") +# test_streaming_output() \ No newline at end of file From 2afc02b4a77aa5bcb2634fd3cb4e0276fbae53dc Mon Sep 17 00:00:00 2001 From: BrianGuo Date: Thu, 29 Aug 2024 10:19:38 -0700 Subject: [PATCH 3/3] comment whole `test_vlm_generation.py` file --- tests/test_vlm_generation.py | 106 +++++++++++++++++------------------ 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/tests/test_vlm_generation.py b/tests/test_vlm_generation.py index 7d0d234d..0bd88d84 100644 --- a/tests/test_vlm_generation.py +++ b/tests/test_vlm_generation.py @@ -1,61 +1,61 @@ -from nexa.gguf import NexaVLMInference -from nexa.gguf.lib_utils import is_gpu_available -from tempfile import TemporaryDirectory -from .utils import download_model +# from nexa.gguf import NexaVLMInference +# from nexa.gguf.lib_utils import is_gpu_available +# from tempfile import TemporaryDirectory +# from .utils import download_model -# Initialize the model -model = NexaVLMInference( - model_path="nanollava", - verbose=False, - n_gpu_layers=-1 if is_gpu_available() else 0, -) +# # Initialize the model +# model = NexaVLMInference( +# model_path="nanollava", +# verbose=False, +# n_gpu_layers=-1 if is_gpu_available() else 0, +# ) -# Test VLM generation without an image -def test_text_only_generation(): - output = model.create_chat_completion( - messages=[ - {"role": "user", "content": "What is the capital of France?"} - ], - max_tokens=100, - stream=False - ) - response = output["choices"][0]["message"]["content"] - print("Text-only response:", response) - assert "Paris" in response, "The response should mention Paris" +# # Test VLM generation without an image +# def test_text_only_generation(): +# output = model.create_chat_completion( +# messages=[ +# {"role": "user", "content": "What is the capital of France?"} +# ], +# max_tokens=100, +# stream=False +# ) +# response = output["choices"][0]["message"]["content"] +# print("Text-only response:", response) +# assert "Paris" in response, "The response should mention Paris" -# Test VLM generation with an image -def test_image_description(): - img_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" - with TemporaryDirectory() as temp_dir: - img_path = download_model(img_url, temp_dir) - user_input = "Describe this image in detail." - output = model._chat(user_input, img_path) - response = "" - for chunk in output: - delta = chunk["choices"][0]["delta"] - if "content" in delta: - response += delta["content"] - print("Image description:", response) - assert len(response) > 50, "The image description should be detailed" +# # Test VLM generation with an image +# def test_image_description(): +# img_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" +# with TemporaryDirectory() as temp_dir: +# img_path = download_model(img_url, temp_dir) +# user_input = "Describe this image in detail." +# output = model._chat(user_input, img_path) +# response = "" +# for chunk in output: +# delta = chunk["choices"][0]["delta"] +# if "content" in delta: +# response += delta["content"] +# print("Image description:", response) +# assert len(response) > 50, "The image description should be detailed" -# Test streaming output -def test_streaming_output(): - global model - messages = [ - {"role": "user", "content": "Write a short story about a robot learning to paint."} - ] - output = model.create_chat_completion(messages=messages, max_tokens=200, stream=True) - story = "" - for chunk in output: - if "choices" in chunk and len(chunk["choices"]) > 0: - delta = chunk["choices"][0]["delta"] - if "content" in delta: - story += delta["content"] - print(delta["content"], end="", flush=True) - print("\nFull story:", story) - assert len(story) > 100, "The generated story should be of substantial length" +# # Test streaming output +# def test_streaming_output(): +# global model +# messages = [ +# {"role": "user", "content": "Write a short story about a robot learning to paint."} +# ] +# output = model.create_chat_completion(messages=messages, max_tokens=200, stream=True) +# story = "" +# for chunk in output: +# if "choices" in chunk and len(chunk["choices"]) > 0: +# delta = chunk["choices"][0]["delta"] +# if "content" in delta: +# story += delta["content"] +# print(delta["content"], end="", flush=True) +# print("\nFull story:", story) +# assert len(story) > 100, "The generated story should be of substantial length" -# Main execution +# # Main execution # if __name__ == "__main__": # print("=== Testing Text-Only Generation ===") # test_text_only_generation()