Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
MiniCPM-V-2_6_pytorch
Commits
d0778a76
Commit
d0778a76
authored
Apr 15, 2025
by
wanglch
Browse files
Update minicpm-v_version.py
parent
dad37c3a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
22 deletions
+20
-22
minicpm-v_version.py
minicpm-v_version.py
+20
-22
No files found.
minicpm-v_version.py
View file @
d0778a76
# test.py
import
torch
import
torch
from
PIL
import
Image
from
PIL
import
Image
from
modelscope
import
AutoModel
,
AutoTokenizer
from
modelscope
import
AutoModel
,
AutoTokenizer
# load omni model default, the default init_vision/init_audio/init_tts is True
model
=
AutoModel
.
from_pretrained
(
'OpenBMB/MiniCPM-V-2_6'
,
trust_remote_code
=
True
,
# if load vision-only model, please set init_audio=False and init_tts=False
attn_implementation
=
'sdpa'
,
torch_dtype
=
torch
.
bfloat16
)
# sdpa or flash_attention_2, no eager
# if load audio-only model, please set init_vision=False
model
=
AutoModel
.
from_pretrained
(
'openbmb/MiniCPM-V-2_6'
,
trust_remote_code
=
True
,
attn_implementation
=
'sdpa'
,
# sdpa or flash_attention_2
torch_dtype
=
torch
.
bfloat16
,
init_vision
=
True
,
init_audio
=
True
,
init_tts
=
True
)
model
=
model
.
eval
().
cuda
()
model
=
model
.
eval
().
cuda
()
tokenizer
=
AutoTokenizer
.
from_pretrained
(
'openbmb/MiniCPM-V-2_6'
,
trust_remote_code
=
True
)
tokenizer
=
AutoTokenizer
.
from_pretrained
(
'OpenBMB/MiniCPM-V-2_6'
,
trust_remote_code
=
True
)
# In addition to vision-only mode, tts processor and vocos also needs to be initialized
model
.
init_tts
()
# test.py
image
=
Image
.
open
(
'xx.jpg'
).
convert
(
'RGB'
)
image
=
Image
.
open
(
'../images/XXXX.jpg'
).
convert
(
'RGB'
)
question
=
'What is in the image?'
question
=
'ocr this image?'
msgs
=
[{
'role'
:
'user'
,
'content'
:
[
image
,
question
]}]
msgs
=
[{
'role'
:
'user'
,
'content'
:
[
image
,
question
]}]
res
=
model
.
chat
(
res
=
model
.
chat
(
...
@@ -35,3 +19,17 @@ res = model.chat(
...
@@ -35,3 +19,17 @@ res = model.chat(
)
)
print
(
res
)
print
(
res
)
## if you want to use streaming, please make sure sampling=True and stream=True
## the model.chat will return a generator
res
=
model
.
chat
(
image
=
None
,
msgs
=
msgs
,
tokenizer
=
tokenizer
,
sampling
=
True
,
stream
=
True
)
generated_text
=
""
for
new_text
in
res
:
generated_text
+=
new_text
print
(
new_text
,
flush
=
True
,
end
=
''
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment