Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,28 +16,36 @@ bnb_config = BitsAndBytesConfig(
|
|
| 16 |
bnb_4bit_compute_dtype=torch.float16
|
| 17 |
)
|
| 18 |
|
| 19 |
-
# Load the
|
| 20 |
-
|
| 21 |
"ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1",
|
| 22 |
-
quantization_config=bnb_config,
|
| 23 |
-
device_map="auto",
|
| 24 |
-
torch_dtype=torch.float16,
|
| 25 |
trust_remote_code=True,
|
| 26 |
token=api_token
|
| 27 |
)
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
"ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1",
|
|
|
|
|
|
|
|
|
|
| 31 |
trust_remote_code=True,
|
| 32 |
token=api_token
|
| 33 |
)
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
eos_token_id = tokenizer.eos_token_id
|
| 37 |
-
|
| 38 |
-
raise ValueError(
|
| 39 |
-
"Neither `eos_token_id` nor `pad_token_id` is defined in the tokenizer. Please specify one explicitly."
|
| 40 |
-
)
|
| 41 |
|
| 42 |
# Preprocess image
|
| 43 |
def preprocess_image(image):
|
|
@@ -47,37 +55,33 @@ def preprocess_image(image):
|
|
| 47 |
# Handle queries
|
| 48 |
def analyze_input(image, question):
|
| 49 |
try:
|
| 50 |
-
#
|
| 51 |
pixel_values = None
|
| 52 |
if image is not None:
|
| 53 |
image = image.convert('RGB')
|
| 54 |
pixel_values = preprocess_image(image)
|
| 55 |
-
|
| 56 |
# Tokenize the question
|
| 57 |
tokenized = tokenizer(question, return_tensors="pt")
|
| 58 |
input_ids = tokenized.input_ids.to(model.device)
|
| 59 |
-
|
| 60 |
-
# Calculate target size
|
| 61 |
-
tgt_size = input_ids.size(1) + 256 #
|
| 62 |
-
|
| 63 |
# Construct the model_inputs dictionary
|
| 64 |
model_inputs = {
|
| 65 |
"input_ids": input_ids,
|
| 66 |
"pixel_values": pixel_values,
|
| 67 |
-
"tgt_sizes": [tgt_size]
|
| 68 |
}
|
| 69 |
-
|
| 70 |
-
# Generate response
|
| 71 |
-
outputs = model.generate(
|
| 72 |
-
|
| 73 |
-
max_new_tokens=256,
|
| 74 |
-
eos_token_id=eos_token_id
|
| 75 |
-
)
|
| 76 |
-
|
| 77 |
# Decode the response
|
| 78 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 79 |
return {"status": "success", "response": response}
|
| 80 |
-
|
| 81 |
except Exception as e:
|
| 82 |
return {"status": "error", "message": str(e)}
|
| 83 |
|
|
@@ -98,4 +102,4 @@ demo.launch(
|
|
| 98 |
share=True,
|
| 99 |
server_name="0.0.0.0",
|
| 100 |
server_port=7860
|
| 101 |
-
)
|
|
|
|
| 16 |
bnb_4bit_compute_dtype=torch.float16
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# Load the tokenizer first
|
| 20 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 21 |
"ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1",
|
|
|
|
|
|
|
|
|
|
| 22 |
trust_remote_code=True,
|
| 23 |
token=api_token
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Set default tokens if they're missing
|
| 27 |
+
if tokenizer.eos_token is None:
|
| 28 |
+
tokenizer.eos_token = "</s>"
|
| 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 with updated tokenizer
|
| 37 |
+
model = AutoModel.from_pretrained(
|
| 38 |
"ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1",
|
| 39 |
+
quantization_config=bnb_config,
|
| 40 |
+
device_map="auto",
|
| 41 |
+
torch_dtype=torch.float16,
|
| 42 |
trust_remote_code=True,
|
| 43 |
token=api_token
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# Update model's generation config
|
| 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):
|
|
|
|
| 55 |
# Handle queries
|
| 56 |
def analyze_input(image, question):
|
| 57 |
try:
|
| 58 |
+
# Process the image if provided
|
| 59 |
pixel_values = None
|
| 60 |
if image is not None:
|
| 61 |
image = image.convert('RGB')
|
| 62 |
pixel_values = preprocess_image(image)
|
| 63 |
+
|
| 64 |
# Tokenize the question
|
| 65 |
tokenized = tokenizer(question, return_tensors="pt")
|
| 66 |
input_ids = tokenized.input_ids.to(model.device)
|
| 67 |
+
|
| 68 |
+
# Calculate target size
|
| 69 |
+
tgt_size = input_ids.size(1) + 256 # Original input size + max new tokens
|
| 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] # Add target sizes for generation
|
| 76 |
}
|
| 77 |
+
|
| 78 |
+
# Generate the response - Note the changed calling convention
|
| 79 |
+
outputs = model.generate(model_inputs)
|
| 80 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# Decode the response
|
| 82 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 83 |
return {"status": "success", "response": response}
|
| 84 |
+
|
| 85 |
except Exception as e:
|
| 86 |
return {"status": "error", "message": str(e)}
|
| 87 |
|
|
|
|
| 102 |
share=True,
|
| 103 |
server_name="0.0.0.0",
|
| 104 |
server_port=7860
|
| 105 |
+
)
|