@@ -116,9 +116,7 @@ def _load_model(self):
116
116
config = AutoConfig .from_pretrained (self .model_path )
117
117
118
118
# Load tokenizer
119
- self .tokenizer = AutoTokenizer .from_pretrained (
120
- self .model_path / "llm" , use_fast = False
121
- )
119
+ self .tokenizer = AutoTokenizer .from_pretrained (self .model_path / "llm" , use_fast = False )
122
120
123
121
# For LLaVA-style models, we typically need to handle image processing
124
122
# and model loading in a specific way. For now, we'll create a simplified
@@ -156,17 +154,15 @@ def _preprocess_image(self, image: Image) -> torch.Tensor:
156
154
# For now, we'll just convert to tensor
157
155
return torch .from_numpy (image_array ).float ()
158
156
159
- def _generate_response (
160
- self , image_tensor : torch .Tensor , prompt : str , generation_params : Dict [str , Any ]
161
- ) -> str :
157
+ def _generate_response (self , image_tensor : torch .Tensor , prompt : str , generation_params : Dict [str , Any ]) -> str :
162
158
"""Generate text response from the model."""
163
159
if self ._mock_mode :
164
160
# Mock response based on common medical VQA patterns
165
161
mock_responses = {
166
- "what is this image showing" : "This medical image shows anatomical structures with various tissue densities and contrast patterns." ,
167
- "summarize key findings" : "Key findings include: 1) Normal anatomical structures visible, 2) No obvious pathological changes detected, 3) Image quality is adequate for assessment." ,
168
- "is there a focal lesion" : "No focal lesion is identified in the visible field of view." ,
169
- "describe the image" : "This appears to be a medical imaging study showing cross-sectional anatomy with good tissue contrast." ,
162
+ "what is this image showing" : "This medical image shows anatomical structures with various tissue densities and contrast patterns." , # noqa: B950
163
+ "summarize key findings" : "Key findings include: 1) Normal anatomical structures visible, 2) No obvious pathological changes detected, 3) Image quality is adequate for assessment." , # noqa: B950
164
+ "is there a focal lesion" : "No focal lesion is identified in the visible field of view." , # noqa: B950
165
+ "describe the image" : "This appears to be a medical imaging study showing cross-sectional anatomy with good tissue contrast." , # noqa: B950
170
166
}
171
167
172
168
# Find best matching response
@@ -176,7 +172,7 @@ def _generate_response(
176
172
return response
177
173
178
174
# Default response
179
- return f"Analysis of the medical image based on the prompt: ' { prompt } ' . [Mock response - actual model not loaded]"
175
+ return f"Analysis of the medical image based on the prompt: { prompt !r } . [Mock response - actual model not loaded]"
180
176
181
177
# In a real implementation, you would:
182
178
# 1. Tokenize the prompt
@@ -189,8 +185,8 @@ def _create_json_result(
189
185
self ,
190
186
text_response : str ,
191
187
request_id : str ,
192
- prompt : str = None ,
193
- image_metadata : Dict = None ,
188
+ prompt : Optional [ str ] = None ,
189
+ image_metadata : Optional [ Dict ] = None ,
194
190
) -> Dict [str , Any ]:
195
191
"""Create a JSON result from the text response."""
196
192
result = {
@@ -276,44 +272,30 @@ def compute(self, op_input, op_output, context):
276
272
request_id = op_input .receive ("request_id" )
277
273
generation_params = op_input .receive ("generation_params" )
278
274
279
- self ._logger .info (
280
- f"Processing request { request_id } with output type '{ output_type } '"
281
- )
275
+ self ._logger .info (f"Processing request { request_id } with output type { output_type !r} " )
282
276
283
277
try :
284
278
# Preprocess image
285
279
image_tensor = self ._preprocess_image (image )
286
280
287
281
# Generate text response
288
- text_response = self ._generate_response (
289
- image_tensor , prompt , generation_params
290
- )
282
+ text_response = self ._generate_response (image_tensor , prompt , generation_params )
291
283
292
284
# Get image metadata if available
293
- image_metadata = (
294
- image .metadata ()
295
- if hasattr (image , "metadata" ) and callable (image .metadata )
296
- else None
297
- )
285
+ image_metadata = image .metadata () if hasattr (image , "metadata" ) and callable (image .metadata ) else None
298
286
299
287
# Create result based on output type
300
288
if output_type == "json" :
301
- result = self ._create_json_result (
302
- text_response , request_id , prompt , image_metadata
303
- )
289
+ result = self ._create_json_result (text_response , request_id , prompt , image_metadata )
304
290
elif output_type == "image" :
305
291
# For now, just return the original image
306
292
# In future, this could generate new images
307
293
result = image
308
294
elif output_type == "image_overlay" :
309
295
result = self ._create_image_overlay (image , text_response )
310
296
else :
311
- self ._logger .warning (
312
- f"Unknown output type: { output_type } , defaulting to json"
313
- )
314
- result = self ._create_json_result (
315
- text_response , request_id , prompt , image_metadata
316
- )
297
+ self ._logger .warning (f"Unknown output type: { output_type } , defaulting to json" )
298
+ result = self ._create_json_result (text_response , request_id , prompt , image_metadata )
317
299
318
300
# Emit outputs
319
301
op_output .emit (result , "result" )
@@ -335,3 +317,4 @@ def compute(self, op_input, op_output, context):
335
317
op_output .emit (error_result , "result" )
336
318
op_output .emit (output_type , "output_type" )
337
319
op_output .emit (request_id , "request_id" )
320
+ raise e from None
0 commit comments