Views
No views yet
pip install git+https://github.com/TIGER-AI-Lab/Mantis.git1import requests
2from PIL import Image
3
4import torch
5from mantis.models.conversation import Conversation, SeparatorStyle
6from mantis.models.mllava import chat_mllava, LlavaForConditionalGeneration, MLlavaProcessor
7from mantis.models.mllava.utils import conv_templates
8from transformers import AutoTokenizer
9
10# 1. Set the system prompt
11conv_llama_3_elyza = Conversation(
12 system="<|start_header_id|>system<|end_header_id|>\n\nあなたは誠実で優秀な日本人のアシスタントです。特に指示が無い場合は、常に日本語で回答してください。",
13 roles=("user", "assistant"),
14 messages=(),
15 offset=0,
16 sep_style=SeparatorStyle.LLAMA_3,
17 sep="<|eot_id|>",
18)
19conv_templates["llama_3"] = conv_llama_3_elyza
20
21# 2. Load model
22device = "cuda" if torch.cuda.is_available() else "cpu"
23model_id = "SakanaAI/Llama-3-EvoVLM-JP-v2"
24
25processor = MLlavaProcessor.from_pretrained("TIGER-Lab/Mantis-8B-siglip-llama3")
26processor.tokenizer.pad_token = processor.tokenizer.eos_token
27
28model = LlavaForConditionalGeneration.from_pretrained(model_id, torch_dtype=torch.float16, device_map=device).eval()
29
30# 3. Prepare a generate config
31generation_kwargs = {
32 "max_new_tokens": 128,
33 "num_beams": 1,
34 "do_sample": False,
35 "no_repeat_ngram_size": 3,
36}
37
38# 4. Generate
39text = "<image>の信号は何色ですか?"
40url_list = [
41 "https://images.unsplash.com/photo-1694831404826-3400c48c188d?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
42 "https://images.unsplash.com/photo-1693240876439-473af88b4ed7?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
43]
44images = [
45 Image.open(requests.get(url_list[0], stream=True).raw).convert("RGB")
46]
47
48response, history = chat_mllava(text, images, model, processor, **generation_kwargs)
49
50print(response)
51# 信号の色は、青色です。
52
53# 5. Multi-turn conversation
54text = "では、<image>の信号は?"
55images += [
56 Image.open(requests.get(url_list[1], stream=True).raw).convert("RGB")
57]
58response, history = chat_mllava(text, images, model, processor, history=history, **generation_kwargs)
59
60print(response)
61# 赤色1@misc{Llama-3-EvoVLM-JP-v2,
2url = {[https://huggingface.co/SakanaAI/Llama-3-EvoVLM-JP-v2](https://huggingface.co/SakanaAI/Llama-3-EvoVLM-JP-v2)},
3title = {Llama-3-EvoVLM-JP-v2},
4author = {Yuichi, Inoue and Takuya, Akiba and Shing, Makoto}
5}1@misc{akiba2024evomodelmerge,
2 title = {Evolutionary Optimization of Model Merging Recipes},
3 author. = {Takuya Akiba and Makoto Shing and Yujin Tang and Qi Sun and David Ha},
4 year = {2024},
5 eprint = {2403.13187},
6 archivePrefix = {arXiv},
7 primaryClass = {cs.NE}
8}