Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import os
|
2 |
import torch
|
3 |
-
from transformers import AutoModel, AutoTokenizer, BitsAndBytesConfig
|
4 |
import gradio as gr
|
5 |
from PIL import Image
|
6 |
from torchvision.transforms import ToTensor
|
@@ -16,26 +16,29 @@ bnb_config = BitsAndBytesConfig(
|
|
16 |
bnb_4bit_compute_dtype=torch.float16
|
17 |
)
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
if tokenizer.pad_token is None:
|
30 |
-
tokenizer.pad_token = tokenizer.eos_token
|
31 |
-
|
32 |
-
# Update the tokenizer's token IDs
|
33 |
-
tokenizer.pad_token_id = tokenizer.convert_tokens_to_ids(tokenizer.pad_token)
|
34 |
-
tokenizer.eos_token_id = tokenizer.convert_tokens_to_ids(tokenizer.eos_token)
|
35 |
|
36 |
-
# Load the model
|
37 |
model = AutoModel.from_pretrained(
|
38 |
-
|
39 |
quantization_config=bnb_config,
|
40 |
device_map="auto",
|
41 |
torch_dtype=torch.float16,
|
@@ -43,9 +46,15 @@ model = AutoModel.from_pretrained(
|
|
43 |
token=api_token
|
44 |
)
|
45 |
|
46 |
-
#
|
|
|
|
|
|
|
|
|
47 |
model.generation_config.eos_token_id = tokenizer.eos_token_id
|
48 |
model.generation_config.pad_token_id = tokenizer.pad_token_id
|
|
|
|
|
49 |
|
50 |
# Preprocess image
|
51 |
def preprocess_image(image):
|
@@ -66,16 +75,22 @@ def analyze_input(image, question):
|
|
66 |
input_ids = tokenized.input_ids.to(model.device)
|
67 |
|
68 |
# Calculate target size
|
69 |
-
tgt_size = input_ids.size(1) + 256
|
70 |
|
71 |
# Construct the model_inputs dictionary
|
72 |
model_inputs = {
|
73 |
"input_ids": input_ids,
|
74 |
"pixel_values": pixel_values,
|
75 |
-
"tgt_sizes": [tgt_size]
|
|
|
|
|
76 |
}
|
77 |
|
78 |
-
#
|
|
|
|
|
|
|
|
|
79 |
outputs = model.generate(model_inputs)
|
80 |
|
81 |
# Decode the response
|
@@ -83,6 +98,8 @@ def analyze_input(image, question):
|
|
83 |
return {"status": "success", "response": response}
|
84 |
|
85 |
except Exception as e:
|
|
|
|
|
86 |
return {"status": "error", "message": str(e)}
|
87 |
|
88 |
# Create Gradio interface
|
@@ -98,8 +115,9 @@ demo = gr.Interface(
|
|
98 |
)
|
99 |
|
100 |
# Launch the Gradio app
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
1 |
import os
|
2 |
import torch
|
3 |
+
from transformers import AutoModel, AutoTokenizer, BitsAndBytesConfig, LlamaTokenizer
|
4 |
import gradio as gr
|
5 |
from PIL import Image
|
6 |
from torchvision.transforms import ToTensor
|
|
|
16 |
bnb_4bit_compute_dtype=torch.float16
|
17 |
)
|
18 |
|
19 |
+
# Initialize tokenizer using LlamaTokenizer specifically
|
20 |
+
model_name = "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1"
|
21 |
+
try:
|
22 |
+
tokenizer = LlamaTokenizer.from_pretrained(
|
23 |
+
model_name,
|
24 |
+
trust_remote_code=True,
|
25 |
+
token=api_token
|
26 |
+
)
|
27 |
+
except Exception as e:
|
28 |
+
print(f"Failed to load LlamaTokenizer, falling back to AutoTokenizer: {e}")
|
29 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
30 |
+
model_name,
|
31 |
+
trust_remote_code=True,
|
32 |
+
token=api_token
|
33 |
+
)
|
34 |
|
35 |
+
# Explicitly set special tokens
|
36 |
+
tokenizer.pad_token = tokenizer.eos_token = "</s>"
|
37 |
+
tokenizer.pad_token_id = tokenizer.eos_token_id = 2 # Common EOS token ID for Llama models
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# Load the model
|
40 |
model = AutoModel.from_pretrained(
|
41 |
+
model_name,
|
42 |
quantization_config=bnb_config,
|
43 |
device_map="auto",
|
44 |
torch_dtype=torch.float16,
|
|
|
46 |
token=api_token
|
47 |
)
|
48 |
|
49 |
+
# Ensure the model's generation config is properly set
|
50 |
+
if not hasattr(model, 'generation_config'):
|
51 |
+
from transformers import GenerationConfig
|
52 |
+
model.generation_config = GenerationConfig()
|
53 |
+
|
54 |
model.generation_config.eos_token_id = tokenizer.eos_token_id
|
55 |
model.generation_config.pad_token_id = tokenizer.pad_token_id
|
56 |
+
model.config.eos_token_id = tokenizer.eos_token_id
|
57 |
+
model.config.pad_token_id = tokenizer.pad_token_id
|
58 |
|
59 |
# Preprocess image
|
60 |
def preprocess_image(image):
|
|
|
75 |
input_ids = tokenized.input_ids.to(model.device)
|
76 |
|
77 |
# Calculate target size
|
78 |
+
tgt_size = input_ids.size(1) + 256
|
79 |
|
80 |
# Construct the model_inputs dictionary
|
81 |
model_inputs = {
|
82 |
"input_ids": input_ids,
|
83 |
"pixel_values": pixel_values,
|
84 |
+
"tgt_sizes": [tgt_size],
|
85 |
+
"pad_token_id": tokenizer.pad_token_id,
|
86 |
+
"eos_token_id": tokenizer.eos_token_id
|
87 |
}
|
88 |
|
89 |
+
# Print debugging information
|
90 |
+
print(f"Token IDs - EOS: {tokenizer.eos_token_id}, PAD: {tokenizer.pad_token_id}")
|
91 |
+
print(f"Model config - EOS: {model.config.eos_token_id}, PAD: {model.config.pad_token_id}")
|
92 |
+
|
93 |
+
# Generate the response
|
94 |
outputs = model.generate(model_inputs)
|
95 |
|
96 |
# Decode the response
|
|
|
98 |
return {"status": "success", "response": response}
|
99 |
|
100 |
except Exception as e:
|
101 |
+
import traceback
|
102 |
+
print(f"Error details: {traceback.format_exc()}")
|
103 |
return {"status": "error", "message": str(e)}
|
104 |
|
105 |
# Create Gradio interface
|
|
|
115 |
)
|
116 |
|
117 |
# Launch the Gradio app
|
118 |
+
if __name__ == "__main__":
|
119 |
+
demo.launch(
|
120 |
+
share=True,
|
121 |
+
server_name="0.0.0.0",
|
122 |
+
server_port=7860
|
123 |
+
)
|