myownskyW7
commited on
Commit
•
4e6d71b
1
Parent(s):
194829f
Update README.md
Browse files
README.md
CHANGED
@@ -29,14 +29,13 @@ We release InternLM-XComposer2 series in two versions:
|
|
29 |
To load the InternLM-XComposer2-7B model using Transformers, use the following code:
|
30 |
```python
|
31 |
import torch
|
32 |
-
from PIL import
|
33 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
34 |
ckpt_path = "internlm/internlm-xcomposer2-7b"
|
35 |
tokenizer = AutoTokenizer.from_pretrained(ckpt_path, trust_remote_code=True).cuda()
|
36 |
# Set `torch_dtype=torch.float16` to load model in float16, otherwise it will be loaded as float32 and might cause OOM Error.
|
37 |
model = AutoModelForCausalLM.from_pretrained(ckpt_path, torch_dtype=torch.float16, trust_remote_code=True).cuda()
|
38 |
model = model.eval()
|
39 |
-
model.vit.resize_pos()
|
40 |
img_path_list = [
|
41 |
'./panda.jpg',
|
42 |
'./bamboo.jpeg',
|
@@ -48,9 +47,18 @@ for img_path in img_path_list:
|
|
48 |
images.append(image)
|
49 |
image = torch.stack(images)
|
50 |
query = '<ImageHere> <ImageHere>please write an article based on the images. Title: my favorite animal.'
|
51 |
-
|
|
|
52 |
print(response)
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
```
|
55 |
|
56 |
### 通过 Transformers 加载
|
@@ -63,8 +71,7 @@ ckpt_path = "internlm/internlm-xcomposer2-7b"
|
|
63 |
tokenizer = AutoTokenizer.from_pretrained(ckpt_path, trust_remote_code=True).cuda()
|
64 |
# `torch_dtype=torch.float16` 可以令模型以 float16 精度加载,否则 transformers 会将模型加载为 float32,导致显存不足
|
65 |
model = AutoModelForCausalLM.from_pretrained(ckpt_path, torch_dtype=torch.float16, trust_remote_code=True).cuda()
|
66 |
-
model = model.eval()
|
67 |
-
model.vit.resize_pos()
|
68 |
img_path_list = [
|
69 |
'./panda.jpg',
|
70 |
'./bamboo.jpeg',
|
@@ -76,7 +83,15 @@ for img_path in img_path_list:
|
|
76 |
images.append(image)
|
77 |
image = torch.stack(images)
|
78 |
query = '<ImageHere> <ImageHere>请根据图片写一篇作文:我最喜欢的小动物。要求:选准角度,确定立意,明确文体,自拟标题;不要套作,不得抄袭;不得泄露个人信息。'
|
79 |
-
|
|
|
80 |
print(response)
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
```
|
|
|
29 |
To load the InternLM-XComposer2-7B model using Transformers, use the following code:
|
30 |
```python
|
31 |
import torch
|
32 |
+
from PIL import Image
|
33 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
34 |
ckpt_path = "internlm/internlm-xcomposer2-7b"
|
35 |
tokenizer = AutoTokenizer.from_pretrained(ckpt_path, trust_remote_code=True).cuda()
|
36 |
# Set `torch_dtype=torch.float16` to load model in float16, otherwise it will be loaded as float32 and might cause OOM Error.
|
37 |
model = AutoModelForCausalLM.from_pretrained(ckpt_path, torch_dtype=torch.float16, trust_remote_code=True).cuda()
|
38 |
model = model.eval()
|
|
|
39 |
img_path_list = [
|
40 |
'./panda.jpg',
|
41 |
'./bamboo.jpeg',
|
|
|
47 |
images.append(image)
|
48 |
image = torch.stack(images)
|
49 |
query = '<ImageHere> <ImageHere>please write an article based on the images. Title: my favorite animal.'
|
50 |
+
with torch.cuda.amp.autocast():
|
51 |
+
response, history = model.chat(tokenizer, query=query, image=image, history=[], do_sample=False)
|
52 |
print(response)
|
53 |
+
""""
|
54 |
+
# My favorite animal is the panda. Pandas are one of the most popular animals in the world, and for good reason. They are adorable, cuddly creatures that have captured the hearts of people all over the globe.
|
55 |
+
Pandas are native to China and can be found in the wild in a few specific regions. However, they are also very popular in captivity, with many zoos around the world housing pandas as part of their exhibits. I have been fortunate enough to see pandas up close at several different zoos, and each time it was an amazing experience.
|
56 |
+
One thing that always strikes me about pandas is how much they love to eat bamboo. In fact, pandas spend almost all of their waking hours eating bamboo. This may not seem like a lot of fun, but pandas actually enjoy munching on this tough plant. It's fascinating to watch them chew through the tough stalks and leaves, and then lick their lips in satisfaction.
|
57 |
+
Another thing that I find interesting about pandas is their black and white fur. The combination of these two colors creates a striking contrast that makes pandas instantly recognizable. In addition, the black patches on their face give them a unique expression that seems to convey both playfulness and seriousness.
|
58 |
+
Despite their popularity, pandas do face some challenges. Their habitat is being destroyed by human activities such as logging and agriculture, which has led to a decline in their population. Additionally, pandas are considered endangered due to factors such as low reproductive rates and limited genetic diversity.
|
59 |
+
However, there are efforts underway to protect pandas and their habitats. Many organizations work to raise awareness about the importance of preserving these beautiful creatures, and governments in countries where pandas live are taking steps to conserve their natural environment.
|
60 |
+
In conclusion, pandas are truly remarkable animals that deserve our admiration and protection. With their distinctive appearance, playful personalities, and love of bamboo, it's no wonder that pandas have become so beloved around the world. Let's do what we can to ensure that future generations can continue to appreciate these wonderful creatures.
|
61 |
+
"""
|
62 |
```
|
63 |
|
64 |
### 通过 Transformers 加载
|
|
|
71 |
tokenizer = AutoTokenizer.from_pretrained(ckpt_path, trust_remote_code=True).cuda()
|
72 |
# `torch_dtype=torch.float16` 可以令模型以 float16 精度加载,否则 transformers 会将模型加载为 float32,导致显存不足
|
73 |
model = AutoModelForCausalLM.from_pretrained(ckpt_path, torch_dtype=torch.float16, trust_remote_code=True).cuda()
|
74 |
+
model = model.eval()
|
|
|
75 |
img_path_list = [
|
76 |
'./panda.jpg',
|
77 |
'./bamboo.jpeg',
|
|
|
83 |
images.append(image)
|
84 |
image = torch.stack(images)
|
85 |
query = '<ImageHere> <ImageHere>请根据图片写一篇作文:我最喜欢的小动物。要求:选准角度,确定立意,明确文体,自拟标题;不要套作,不得抄袭;不得泄露个人信息。'
|
86 |
+
with torch.cuda.amp.autocast():
|
87 |
+
response, history = model.chat(tokenizer, query=query, image=image, history=[], do_sample=False)
|
88 |
print(response)
|
89 |
+
"""
|
90 |
+
# 我最喜欢的小动物
|
91 |
+
我喜欢许多小动物,有活泼可爱的小狗、美丽高贵的孔雀……但我最喜欢的是那一只憨态可掬的大熊猫。
|
92 |
+
大熊猫的毛色黑白分明,身体胖乎乎的,四肢短小,走起路来摇摇晃晃,样子十分可爱。它的头又圆又大,耳朵小小的,眼睛黑黑的,像两颗闪闪发光的黑宝石。它还有一张大大的嘴巴,里面长着锋利的牙齿,看起来非常凶猛。但是,别看它长得这么凶猛,其实它很胆小,一听到一点声音就会吓得躲到树后面去。
|
93 |
+
大熊猫是国宝,也是我们中国的象征。它们喜欢吃竹子,每天要吃30多斤的竹子,所以人们给它取了一个外号叫“食竹动物”。
|
94 |
+
大熊猫不仅爱吃竹子,还爱睡觉。一天中,除了吃就是睡,吃饱了就睡,睡醒了再吃,真是一个名副其实的“大懒虫”。
|
95 |
+
这就是我喜欢的动物——大熊猫,你们喜欢吗?
|
96 |
+
"""
|
97 |
```
|