Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,94 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
-
import torchvision
|
| 5 |
-
from PIL import Image
|
| 6 |
-
from transformers import AutoModel, AutoTokenizer
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
MODEL_ID,
|
| 14 |
-
|
| 15 |
-
|
| 16 |
)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
torch.bfloat16
|
| 20 |
-
if torch.cuda.is_available()
|
| 21 |
-
else torch.float32
|
| 22 |
-
)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
#MODEL_ID,
|
| 26 |
-
#trust_remote_code=True,
|
| 27 |
-
|
| 28 |
-
#torch_dtype=dtype,
|
| 29 |
-
#attn_implementation="sdpa",
|
| 30 |
-
#device_map="auto"
|
| 31 |
-
MODEL_ID,
|
| 32 |
-
trust_remote_code=True,
|
| 33 |
-
attn_implementation="sdpa",
|
| 34 |
-
torch_dtype=torch.bfloat16,
|
| 35 |
-
token=HF_TOKEN)
|
| 36 |
|
| 37 |
-
model.eval()
|
| 38 |
-
#model = model.float()
|
| 39 |
|
| 40 |
def predict(image):
|
| 41 |
|
| 42 |
if image is None:
|
| 43 |
-
return "No image uploaded"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
answer = model.chat(
|
| 62 |
-
image=None,
|
| 63 |
-
msgs=msgs,
|
| 64 |
-
tokenizer=tokenizer
|
| 65 |
-
)
|
| 66 |
|
| 67 |
-
return answer
|
| 68 |
|
| 69 |
demo = gr.Interface(
|
| 70 |
fn=predict,
|
| 71 |
-
inputs=gr.Image(type="pil"),
|
| 72 |
-
outputs=gr.Textbox(),
|
| 73 |
-
title="MiniCPM-V
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
from transformers import (
|
| 5 |
+
AutoProcessor,
|
| 6 |
+
AutoModelForImageTextToText
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
MODEL_ID = "openbmb/MiniCPM-V-4.6-Thinking"
|
| 10 |
|
| 11 |
+
print("Loading processor...")
|
| 12 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 13 |
|
| 14 |
+
print("Loading model...")
|
| 15 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
| 16 |
MODEL_ID,
|
| 17 |
+
torch_dtype="auto",
|
| 18 |
+
device_map="auto"
|
| 19 |
)
|
| 20 |
|
| 21 |
+
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
print("Model loaded.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def predict(image):
|
| 27 |
|
| 28 |
if image is None:
|
| 29 |
+
return "No image uploaded."
|
| 30 |
+
|
| 31 |
+
messages = [
|
| 32 |
+
{
|
| 33 |
+
"role": "user",
|
| 34 |
+
"content": [
|
| 35 |
+
{"type": "image", "image": image},
|
| 36 |
+
{
|
| 37 |
+
"type": "text",
|
| 38 |
+
"text": (
|
| 39 |
+
"Identify the object in this image. "
|
| 40 |
+
"Return:\n"
|
| 41 |
+
"German common name | Scientific name | confidence\n"
|
| 42 |
+
"Keep the answer short."
|
| 43 |
+
)
|
| 44 |
+
},
|
| 45 |
+
],
|
| 46 |
+
}
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
inputs = processor.apply_chat_template(
|
| 50 |
+
messages,
|
| 51 |
+
tokenize=True,
|
| 52 |
+
add_generation_prompt=True,
|
| 53 |
+
return_dict=True,
|
| 54 |
+
return_tensors="pt",
|
| 55 |
+
downsample_mode="16x",
|
| 56 |
+
max_slice_nums=9,
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
inputs = {
|
| 60 |
+
k: v.to(model.device)
|
| 61 |
+
for k, v in inputs.items()
|
| 62 |
+
}
|
| 63 |
|
| 64 |
+
with torch.no_grad():
|
| 65 |
+
generated_ids = model.generate(
|
| 66 |
+
**inputs,
|
| 67 |
+
downsample_mode="16x",
|
| 68 |
+
max_new_tokens=256,
|
| 69 |
+
)
|
| 70 |
|
| 71 |
+
generated_ids_trimmed = [
|
| 72 |
+
out_ids[len(in_ids):]
|
| 73 |
+
for in_ids, out_ids
|
| 74 |
+
in zip(inputs["input_ids"], generated_ids)
|
| 75 |
+
]
|
| 76 |
|
| 77 |
+
output = processor.batch_decode(
|
| 78 |
+
generated_ids_trimmed,
|
| 79 |
+
skip_special_tokens=True,
|
| 80 |
+
clean_up_tokenization_spaces=False,
|
| 81 |
+
)[0]
|
| 82 |
|
| 83 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
|
|
|
| 85 |
|
| 86 |
demo = gr.Interface(
|
| 87 |
fn=predict,
|
| 88 |
+
inputs=gr.Image(type="pil", label="Image"),
|
| 89 |
+
outputs=gr.Textbox(label="Result"),
|
| 90 |
+
title="MiniCPM-V-4.6-Thinking",
|
| 91 |
+
description="Upload an image and identify plants, animals, or objects."
|
| 92 |
)
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|