@@ -116,9 +116,7 @@ def _load_model(self):
116116 config = AutoConfig .from_pretrained (self .model_path )
117117
118118 # 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 )
122120
123121 # For LLaVA-style models, we typically need to handle image processing
124122 # 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:
156154 # For now, we'll just convert to tensor
157155 return torch .from_numpy (image_array ).float ()
158156
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 :
162158 """Generate text response from the model."""
163159 if self ._mock_mode :
164160 # Mock response based on common medical VQA patterns
165161 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
170166 }
171167
172168 # Find best matching response
@@ -176,7 +172,7 @@ def _generate_response(
176172 return response
177173
178174 # 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]"
180176
181177 # In a real implementation, you would:
182178 # 1. Tokenize the prompt
@@ -189,8 +185,8 @@ def _create_json_result(
189185 self ,
190186 text_response : str ,
191187 request_id : str ,
192- prompt : str = None ,
193- image_metadata : Dict = None ,
188+ prompt : Optional [ str ] = None ,
189+ image_metadata : Optional [ Dict ] = None ,
194190 ) -> Dict [str , Any ]:
195191 """Create a JSON result from the text response."""
196192 result = {
@@ -276,44 +272,30 @@ def compute(self, op_input, op_output, context):
276272 request_id = op_input .receive ("request_id" )
277273 generation_params = op_input .receive ("generation_params" )
278274
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} " )
282276
283277 try :
284278 # Preprocess image
285279 image_tensor = self ._preprocess_image (image )
286280
287281 # 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 )
291283
292284 # 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
298286
299287 # Create result based on output type
300288 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 )
304290 elif output_type == "image" :
305291 # For now, just return the original image
306292 # In future, this could generate new images
307293 result = image
308294 elif output_type == "image_overlay" :
309295 result = self ._create_image_overlay (image , text_response )
310296 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 )
317299
318300 # Emit outputs
319301 op_output .emit (result , "result" )
@@ -335,3 +317,4 @@ def compute(self, op_input, op_output, context):
335317 op_output .emit (error_result , "result" )
336318 op_output .emit (output_type , "output_type" )
337319 op_output .emit (request_id , "request_id" )
320+ raise e from None
0 commit comments