Views
No views yet
pip install torch transformers accelerate pillow
1from PIL import Image
2import requests
3from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
4
5model_name = "scb10x/typhoon2-qwen2vl-7b-vision-instruct"
6
7model = Qwen2VLForConditionalGeneration.from_pretrained(
8 model_name, torch_dtype="auto", device_map="auto"
9)
10processor = AutoProcessor.from_pretrained(model_name)
11
12# Image
13url = "https://cdn.pixabay.com/photo/2023/05/16/09/15/bangkok-7997046_1280.jpg"
14image = Image.open(requests.get(url, stream=True).raw)
15
16conversation = [
17 {
18 "role": "user",
19 "content": [
20 {
21 "type": "image",
22 },
23 {"type": "text", "text": "ระบุชื่อสถานที่และประเทศของภาพนี้เป็นภาษาไทย"},
24 ],
25 }
26]
27
28
29# Preprocess the inputs
30text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
31
32inputs = processor(
33 text=[text_prompt], images=[image], padding=True, return_tensors="pt"
34)
35inputs = inputs.to("cuda")
36
37output_ids = model.generate(**inputs, max_new_tokens=128)
38generated_ids = [
39 output_ids[len(input_ids) :]
40 for input_ids, output_ids in zip(inputs.input_ids, output_ids)
41]
42output_text = processor.batch_decode(
43 generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
44)
45print(output_text)
46# ['พระบรมมหาราชวัง, กรุงเทพฯ, ประเทศไทย']1from PIL import Image
2import requests
3from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
4
5model_name = "scb10x/typhoon2-qwen2vl-7b-vision-instruct"
6
7model = Qwen2VLForConditionalGeneration.from_pretrained(
8 model_name, torch_dtype="auto", device_map="auto"
9)
10processor = AutoProcessor.from_pretrained(model_name)
11
12# Messages containing multiple images and a text query
13conversation = [
14 {
15 "role": "user",
16 "content": [
17 {
18 "type": "image",
19 },
20 {
21 "type": "image",
22 },
23 {"type": "text", "text": "ระบุ 3 สิ่งที่คล้ายกันในสองภาพนี้"},
24 ],
25 }
26]
27
28urls = [
29 "https://cdn.pixabay.com/photo/2023/05/16/09/15/bangkok-7997046_1280.jpg",
30 "https://cdn.pixabay.com/photo/2020/08/10/10/09/bangkok-5477405_1280.jpg",
31]
32images = [Image.open(requests.get(url, stream=True).raw) for url in urls]
33
34# Preprocess the inputs
35text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
36
37inputs = processor(text=[text_prompt], images=images, padding=True, return_tensors="pt")
38inputs = inputs.to("cuda")
39
40# Inference
41generated_ids = model.generate(**inputs, max_new_tokens=128)
42generated_ids_trimmed = [
43 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
44]
45output_text = processor.batch_decode(
46 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
47)
48print(output_text)
49# ['1. ทั้งสองภาพแสดงสถาปัตยกรรมที่มีลักษณะคล้ายกัน\n2. ทั้งสองภาพมีสีสันที่สวยงาม\n3. ทั้งสองภาพมีทิวทัศน์ที่สวยงาม']1min_pixels = 128 * 28 * 28
2max_pixels = 2560 * 28 * 28
3processor = AutoProcessor.from_pretrained(
4 model_name, min_pixels=min_pixels, max_pixels=max_pixels
5)| Benchmark | Llama-3.2-11B-Vision-Instruct | Qwen2-VL-7B-Instruct | Pathumma-llm-vision-1.0.0 | Typhoon2-qwen2vl-7b-vision-instruct |
|---|---|---|---|---|
| OCRBench Liu et al., 2024c | 72.84 / 51.10 | 72.31 / 57.90 | 32.74 / 25.87 | 64.38 / 49.60 |
| MMBench (Dev) Liu et al., 2024b | 76.54 / - | 84.10 / - | 19.51 / - | 83.66 / - |
| ChartQA Masry et al., 2022 | 13.41 / x | 47.45 / 45.00 | 64.20 / 57.83 | 75.71 / 72.56 |
| TextVQA Singh et al., 2019 | 32.82 / x | 91.40 / 88.70 | 32.54 / 28.84 | 91.45 / 88.97 |
| OCR (TH) OpenThaiGPT, 2024 | 64.41 / 35.58 | 56.47 / 55.34 | 6.38 / 2.88 | 64.24 / 63.11 |
| M3Exam Images (TH) Zhang et al., 2023c | 25.46 / - | 32.17 / - | 29.01 / - | 33.67 / - |
| GQA (TH) Hudson et al., 2019 | 31.33 / - | 34.55 / - | 10.20 / - | 50.25 / - |
| MTVQ (TH) Tang et al., 2024b | 11.21 / 4.31 | 23.39 / 13.79 | 7.63 / 1.72 | 30.59 / 21.55 |
| Average | 37.67 / x | 54.26 / 53.85 | 25.61 / 23.67 | 62.77 / 59.02 |
/) represents Accuracy, normalized such that Rouge-L = 100%.@misc{typhoon2,
title={Typhoon 2: A Family of Open Text and Multimodal Thai Large Language Models},
author={Kunat Pipatanakul and Potsawee Manakul and Natapong Nitarach and Warit Sirichotedumrong and Surapon Nonesung and Teetouch Jaknamon and Parinthapat Pengpun and Pittawat Taveekitworachai and Adisai Na-Thalang and Sittipong Sripaisarnmongkol and Krisanapong Jirayoot and Kasima Tharnpipitchai},
year={2024},
eprint={2412.13702},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2412.13702},
}