Chris4K commited on
Commit
a00111b
·
verified ·
1 Parent(s): a15c04c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -51
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
- MODEL_ID = "openbmb/MiniCPM-V-4.6" # "openbmb/MiniCPM-V-2_6" #openbmb/MiniCPM-o-2_6 #"openbmb/MiniCPM-V-2_6-int4"
 
 
 
 
 
9
 
10
- HF_TOKEN = os.getenv("HF_TOKEN")
 
11
 
12
- tokenizer = AutoTokenizer.from_pretrained(
 
13
  MODEL_ID,
14
- trust_remote_code=True,
15
- token=HF_TOKEN
16
  )
17
 
18
- dtype = (
19
- torch.bfloat16
20
- if torch.cuda.is_available()
21
- else torch.float32
22
- )
23
 
24
- model = AutoModel.from_pretrained(
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
- image = image.convert("RGB")
46
-
47
- prompt = """
48
- Identify the object in this image.
49
-
50
- German common name | Scientific name | confidence
51
 
52
- Keep the answer short.
53
- """
 
 
 
54
 
55
- msgs = [{
56
- "role": "user",
57
- "content": [image, prompt]
58
- }]
 
59
 
60
- with torch.no_grad():
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 2.6 "
 
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__":