8-5 Alibaba: Qwen2.5 モデル
学習目標
Pythonプログラムを使用してHugging FaceプラットフォームからQwen 2.5モデルをダウンロードし、簡単なプロンプトを使用してQwen 2.5に質問し、回答を取得します。

Qwen2.5とは?
Qwen 2.5は、アリババが開発した大規模言語モデルです。チャットボットのように会話できるだけでなく、記事作成やテキスト分析にも利用できます。
Qwen2.5でできること
1. 記事生成/要約:トピックに基づいて段落を自動生成したり、長文を要約に圧縮したりできます。
2. 対話型インタラクション:チャットアシスタントのように質問に答えたり、様々なトピックについて議論したりできます。
3. テキスト分析:感情分析やキーワード抽出を行い、記事の内容理解を支援します。
使い始めるには?
1. 以下のサンプルプログラムは、Qwen 2.5を使って大規模言語モデルの概要を簡単に紹介します。
from transformers import AutoModelForCausalLM, AutoTokenizer
# Set model name
model_name = "Qwen/Qwen2.5-7B-Instruct-1M"
# Load the model, automatically selecting the appropriate Torch data type
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto", # Automatically choose data type (e.g., bfloat16 or float16)
device_map="auto", # Automatically select device (CPU or GPU)
cache_dir="./model", # Set path to store the model (default is ~/.cache/huggingface)
)
# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir="./model")
# Set the conversation messages, including system and user messages
prompt = "Introduce large language models briefly."
messages = [
{"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
{"role": "user", "content": prompt}
]
# Use tokenizer to apply chat template to the message list
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
# Convert the text to tensor and move it to the model's device
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
# Generate response using the model
generated_ids = model.generate(
**model_inputs,
max_new_tokens=512
)
# Remove the input portion from the output to get only the generated response
generated_ids = [
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
# Decode the output, skipping special tokens, and print the response
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(response)
2. 実行後、以下のような応答が表示されます。
(1) モデルは最適化されていないため、生成速度は比較的遅く、待ち時間が発生します。
(2) max_new_tokens の値を小さくすることで、出力されるトークンの数を減らすことができます。

設定が完了したら、Qwen 2.5を使って中国語の文章作成、会話、テキスト分析を行うことができます。例えば、プログラムのプロンプトを修正することで、Qwen 2.5に記事の要点を要約させることも可能です。
prompt = """Organize the highlights of the following article:
Founded more than 100 years ago,
National Taiwan University Hospital (NTUH) has nurtured countless medical professionals
and is known for its trusted clinical care.
The national teaching hospital is now adopting AI imaging technology to diagnose
patients more quickly and accurately.
Trained on more than 70,000 axial images of 200 patients, NTUH's HeaortaNet model at
automatically performs segmentation of cardiac computed tomography (CT) scans in 3D,
including the aorta and other arteries, to quickly analyze cardiovascular disease risk.
The model's segmentation of the pericardium and aorta is highly accurate, dramatically
reducing the data processing time from one hour to approximately 0.4 seconds per case.
In addition, NTUH, in collaboration with the Good Liver Foundation and system builder
Smartech, developed a diagnostic aid system,
, to detect liver cancer during ultrasound examinations.
The system utilizes NVIDIA's Jetson Orin NX module and a deep learning model trained
on more than 5,000 labeled ultrasound images,
to identify malignant and benign liver tumors in real-time.
NVIDIA DeepStream and TensorRT SDKs accelerate the system's deep learning models,
ultimately helping clinicians detect tumors earlier and more reliably.
In addition, NTU Hospital is using NVIDIA DGX to train AI models for the pancreatic
cancer detection system from CT scans.
"""
参考資料:
Qwen/Qwen2.5-7B-Instruct-1M · Hugging Face
https://blogs.nvidia.com.tw/blog/taiwan-medical-centers-system-builder-partners/