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
|
4 |
import gradio as gr
|
5 |
from PIL import Image
|
6 |
from torchvision.transforms import ToTensor
|
@@ -16,45 +16,55 @@ bnb_config = BitsAndBytesConfig(
|
|
16 |
bnb_4bit_compute_dtype=torch.float16
|
17 |
)
|
18 |
|
19 |
-
#
|
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 |
-
#
|
36 |
-
tokenizer
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
# Load the model
|
40 |
-
model =
|
41 |
model_name,
|
42 |
quantization_config=bnb_config,
|
43 |
device_map="auto",
|
44 |
torch_dtype=torch.float16,
|
45 |
trust_remote_code=True,
|
46 |
-
token=api_token
|
|
|
47 |
)
|
48 |
|
49 |
-
# Ensure
|
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):
|
@@ -64,6 +74,12 @@ def preprocess_image(image):
|
|
64 |
# Handle queries
|
65 |
def analyze_input(image, question):
|
66 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
# Process the image if provided
|
68 |
pixel_values = None
|
69 |
if image is not None:
|
@@ -71,35 +87,36 @@ def analyze_input(image, question):
|
|
71 |
pixel_values = preprocess_image(image)
|
72 |
|
73 |
# Tokenize the question
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
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 |
-
#
|
90 |
-
|
91 |
-
|
92 |
|
93 |
-
# Generate
|
94 |
-
outputs = model.generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
-
# Decode
|
97 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
98 |
return {"status": "success", "response": response}
|
99 |
|
100 |
except Exception as e:
|
101 |
import traceback
|
102 |
-
|
|
|
103 |
return {"status": "error", "message": str(e)}
|
104 |
|
105 |
# Create Gradio interface
|
|
|
1 |
import os
|
2 |
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, GenerationConfig
|
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 |
+
# Model name
|
20 |
model_name = "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
# Initialize tokenizer
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
24 |
+
model_name,
|
25 |
+
trust_remote_code=True,
|
26 |
+
token=api_token
|
27 |
+
)
|
28 |
+
|
29 |
+
# Set up tokenizer with default tokens
|
30 |
+
default_tokens = {
|
31 |
+
"pad_token": "[PAD]",
|
32 |
+
"eos_token": "</s>",
|
33 |
+
"bos_token": "<s>",
|
34 |
+
"unk_token": "<unk>",
|
35 |
+
}
|
36 |
+
|
37 |
+
for token_name, token_value in default_tokens.items():
|
38 |
+
if getattr(tokenizer, token_name) is None:
|
39 |
+
setattr(tokenizer, token_name, token_value)
|
40 |
+
token_id_name = f"{token_name}_id"
|
41 |
+
if getattr(tokenizer, token_id_name) is None:
|
42 |
+
token_id = tokenizer.convert_tokens_to_ids(token_value)
|
43 |
+
setattr(tokenizer, token_id_name, token_id)
|
44 |
+
|
45 |
+
# Create generation config
|
46 |
+
generation_config = GenerationConfig(
|
47 |
+
pad_token_id=tokenizer.pad_token_id,
|
48 |
+
eos_token_id=tokenizer.eos_token_id,
|
49 |
+
bos_token_id=tokenizer.bos_token_id,
|
50 |
+
max_new_tokens=256,
|
51 |
+
)
|
52 |
|
53 |
# Load the model
|
54 |
+
model = AutoModelForCausalLM.from_pretrained(
|
55 |
model_name,
|
56 |
quantization_config=bnb_config,
|
57 |
device_map="auto",
|
58 |
torch_dtype=torch.float16,
|
59 |
trust_remote_code=True,
|
60 |
+
token=api_token,
|
61 |
+
generation_config=generation_config
|
62 |
)
|
63 |
|
64 |
+
# Ensure model configs are set
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
model.config.pad_token_id = tokenizer.pad_token_id
|
66 |
+
model.config.eos_token_id = tokenizer.eos_token_id
|
67 |
+
model.config.bos_token_id = tokenizer.bos_token_id
|
68 |
|
69 |
# Preprocess image
|
70 |
def preprocess_image(image):
|
|
|
74 |
# Handle queries
|
75 |
def analyze_input(image, question):
|
76 |
try:
|
77 |
+
# Debug print
|
78 |
+
print(f"Tokenizer config:")
|
79 |
+
print(f"EOS token: {tokenizer.eos_token} (id: {tokenizer.eos_token_id})")
|
80 |
+
print(f"PAD token: {tokenizer.pad_token} (id: {tokenizer.pad_token_id})")
|
81 |
+
print(f"BOS token: {tokenizer.bos_token} (id: {tokenizer.bos_token_id})")
|
82 |
+
|
83 |
# Process the image if provided
|
84 |
pixel_values = None
|
85 |
if image is not None:
|
|
|
87 |
pixel_values = preprocess_image(image)
|
88 |
|
89 |
# Tokenize the question
|
90 |
+
inputs = tokenizer(
|
91 |
+
question,
|
92 |
+
return_tensors="pt",
|
93 |
+
padding=True,
|
94 |
+
truncation=True,
|
95 |
+
max_length=512
|
96 |
+
).to(model.device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
+
# Add image if provided
|
99 |
+
if pixel_values is not None:
|
100 |
+
inputs['pixel_values'] = pixel_values
|
101 |
|
102 |
+
# Generate response
|
103 |
+
outputs = model.generate(
|
104 |
+
**inputs,
|
105 |
+
generation_config=generation_config,
|
106 |
+
max_new_tokens=256,
|
107 |
+
do_sample=True,
|
108 |
+
temperature=0.7,
|
109 |
+
top_p=0.9,
|
110 |
+
)
|
111 |
|
112 |
+
# Decode response
|
113 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
114 |
return {"status": "success", "response": response}
|
115 |
|
116 |
except Exception as e:
|
117 |
import traceback
|
118 |
+
error_trace = traceback.format_exc()
|
119 |
+
print(f"Error occurred: {error_trace}")
|
120 |
return {"status": "error", "message": str(e)}
|
121 |
|
122 |
# Create Gradio interface
|