Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	
		YanBoChen
		
	commited on
		
		
					Commit 
							
							·
						
						b14dfa7
	
1
								Parent(s):
							
							9a63f99
								
feat: enhance medical query processing with user-friendly output and debug mode handling
Browse files- app.py +68 -19
- manual_modifications.md +126 -0
- src/user_prompt.py +13 -2
    	
        app.py
    CHANGED
    
    | @@ -173,8 +173,11 @@ class OnCallAIInterface: | |
| 173 | 
             
                        processing_steps.append(f"   💊 Treatment guidelines: {treatment_count}")
         | 
| 174 | 
             
                        processing_steps.append(f"   ⏱️ Retrieval time: {step3_time:.3f}s")
         | 
| 175 |  | 
| 176 | 
            -
                        # Format retrieved guidelines for display
         | 
| 177 | 
            -
                         | 
|  | |
|  | |
|  | |
| 178 |  | 
| 179 | 
             
                        # STEP 4: Medical Advice Generation
         | 
| 180 | 
             
                        processing_steps.append("\n🧠 Step 4: Generating evidence-based medical advice...")
         | 
| @@ -277,6 +280,40 @@ class OnCallAIInterface: | |
| 277 | 
             
                        "displayed_guidelines": guidelines
         | 
| 278 | 
             
                    }, indent=2)
         | 
| 279 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 280 | 
             
                def _detect_query_intention(self, user_query: str) -> str:
         | 
| 281 | 
             
                    """Simple intention detection based on query content"""
         | 
| 282 | 
             
                    query_lower = user_query.lower()
         | 
| @@ -415,25 +452,31 @@ def create_oncall_interface(): | |
| 415 | 
             
                                elem_classes="processing-steps"
         | 
| 416 | 
             
                            )
         | 
| 417 |  | 
| 418 | 
            -
             | 
| 419 | 
            -
             | 
|  | |
| 420 | 
             
                            guidelines_output = gr.JSON(
         | 
| 421 | 
            -
                                label="📚 Retrieved Medical Guidelines",
         | 
| 422 | 
             
                                elem_classes="guidelines-display"
         | 
| 423 | 
             
                            )
         | 
| 424 |  | 
| 425 | 
            -
                             | 
| 426 | 
            -
             | 
| 427 | 
            -
                                 | 
| 428 | 
            -
             | 
| 429 | 
            -
             | 
| 430 | 
            -
             | 
| 431 | 
            -
             | 
| 432 | 
            -
             | 
| 433 | 
            -
             | 
| 434 | 
            -
             | 
| 435 | 
            -
             | 
| 436 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 437 |  | 
| 438 | 
             
                    # Audio input section (placeholder for future)
         | 
| 439 | 
             
                    with gr.Accordion("🎙️ Audio Input (Coming Soon)", open=False):
         | 
| @@ -452,18 +495,24 @@ def create_oncall_interface(): | |
| 452 | 
             
                            interactive=False
         | 
| 453 | 
             
                        )
         | 
| 454 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 455 | 
             
                    # Event handlers
         | 
| 456 | 
             
                    submit_btn.click(
         | 
| 457 | 
             
                        fn=oncall_system.process_medical_query,
         | 
| 458 | 
             
                        inputs=[user_input, intention_override] if DEBUG_MODE else [user_input],
         | 
| 459 | 
            -
                        outputs= | 
| 460 | 
             
                    )
         | 
| 461 |  | 
| 462 | 
             
                    # Enter key support
         | 
| 463 | 
             
                    user_input.submit(
         | 
| 464 | 
             
                        fn=oncall_system.process_medical_query,
         | 
| 465 | 
             
                        inputs=[user_input, intention_override] if DEBUG_MODE else [user_input],
         | 
| 466 | 
            -
                        outputs= | 
| 467 | 
             
                    )
         | 
| 468 |  | 
| 469 | 
             
                    # Footer
         | 
|  | |
| 173 | 
             
                        processing_steps.append(f"   💊 Treatment guidelines: {treatment_count}")
         | 
| 174 | 
             
                        processing_steps.append(f"   ⏱️ Retrieval time: {step3_time:.3f}s")
         | 
| 175 |  | 
| 176 | 
            +
                        # Format retrieved guidelines for display - conditional based on debug mode
         | 
| 177 | 
            +
                        if DEBUG_MODE:
         | 
| 178 | 
            +
                            guidelines_display = self._format_guidelines_display(processed_results)
         | 
| 179 | 
            +
                        else:
         | 
| 180 | 
            +
                            guidelines_display = self._format_user_friendly_sources(processed_results)
         | 
| 181 |  | 
| 182 | 
             
                        # STEP 4: Medical Advice Generation
         | 
| 183 | 
             
                        processing_steps.append("\n🧠 Step 4: Generating evidence-based medical advice...")
         | 
|  | |
| 280 | 
             
                        "displayed_guidelines": guidelines
         | 
| 281 | 
             
                    }, indent=2)
         | 
| 282 |  | 
| 283 | 
            +
                def _format_user_friendly_sources(self, processed_results: List[Dict]) -> str:
         | 
| 284 | 
            +
                    """Format retrieved guidelines for production mode - user-friendly text format"""
         | 
| 285 | 
            +
                    if not processed_results:
         | 
| 286 | 
            +
                        return "No relevant medical guidelines found for this query."
         | 
| 287 | 
            +
                    
         | 
| 288 | 
            +
                    sources = []
         | 
| 289 | 
            +
                    emergency_count = 0
         | 
| 290 | 
            +
                    treatment_count = 0
         | 
| 291 | 
            +
                    
         | 
| 292 | 
            +
                    # Extract top 5 most relevant sources
         | 
| 293 | 
            +
                    for i, result in enumerate(processed_results[:5], 1):
         | 
| 294 | 
            +
                        source_type = result.get('type', 'medical').title()
         | 
| 295 | 
            +
                        confidence = f"{(1 - result.get('distance', 1)) * 100:.0f}%"
         | 
| 296 | 
            +
                        
         | 
| 297 | 
            +
                        if source_type.lower() == 'emergency':
         | 
| 298 | 
            +
                            emergency_count += 1
         | 
| 299 | 
            +
                        elif source_type.lower() == 'treatment':
         | 
| 300 | 
            +
                            treatment_count += 1
         | 
| 301 | 
            +
                        
         | 
| 302 | 
            +
                        sources.append(f"{i}. {source_type} Guideline (Relevance: {confidence})")
         | 
| 303 | 
            +
                    
         | 
| 304 | 
            +
                    # Build user-friendly text output
         | 
| 305 | 
            +
                    result_text = "\n".join(sources)
         | 
| 306 | 
            +
                    result_text += f"\n\n📊 Summary:"
         | 
| 307 | 
            +
                    result_text += f"\n• Total guidelines consulted: {len(processed_results)}"
         | 
| 308 | 
            +
                    if emergency_count > 0:
         | 
| 309 | 
            +
                        result_text += f"\n• Emergency protocols: {emergency_count}"
         | 
| 310 | 
            +
                    if treatment_count > 0:
         | 
| 311 | 
            +
                        result_text += f"\n• Treatment guidelines: {treatment_count}"
         | 
| 312 | 
            +
                    
         | 
| 313 | 
            +
                    result_text += f"\n\n✅ Evidence-based recommendations provided above"
         | 
| 314 | 
            +
                    
         | 
| 315 | 
            +
                    return result_text
         | 
| 316 | 
            +
             | 
| 317 | 
             
                def _detect_query_intention(self, user_query: str) -> str:
         | 
| 318 | 
             
                    """Simple intention detection based on query content"""
         | 
| 319 | 
             
                    query_lower = user_query.lower()
         | 
|  | |
| 452 | 
             
                                elem_classes="processing-steps"
         | 
| 453 | 
             
                            )
         | 
| 454 |  | 
| 455 | 
            +
                    with gr.Column(scale=1):
         | 
| 456 | 
            +
                        # Debug Mode: Show full technical details
         | 
| 457 | 
            +
                        if DEBUG_MODE:
         | 
| 458 | 
             
                            guidelines_output = gr.JSON(
         | 
| 459 | 
            +
                                label="📚 Retrieved Medical Guidelines (Debug)",
         | 
| 460 | 
             
                                elem_classes="guidelines-display"
         | 
| 461 | 
             
                            )
         | 
| 462 |  | 
| 463 | 
            +
                            technical_output = gr.JSON(
         | 
| 464 | 
            +
                                label="⚙️ Technical Details (Debug Mode)",
         | 
| 465 | 
            +
                                elem_classes="technical-details"
         | 
| 466 | 
            +
                            )
         | 
| 467 | 
            +
                        
         | 
| 468 | 
            +
                        # Production Mode: User-friendly simplified version
         | 
| 469 | 
            +
                        else:
         | 
| 470 | 
            +
                            guidelines_output = gr.Textbox(
         | 
| 471 | 
            +
                                label="📖 Evidence Sources",
         | 
| 472 | 
            +
                                lines=5,
         | 
| 473 | 
            +
                                max_lines=8,
         | 
| 474 | 
            +
                                interactive=False,
         | 
| 475 | 
            +
                                elem_classes="evidence-sources"
         | 
| 476 | 
            +
                            )
         | 
| 477 | 
            +
                            
         | 
| 478 | 
            +
                            # Hide technical details - no system information shown
         | 
| 479 | 
            +
                            technical_output = gr.State(None)
         | 
| 480 |  | 
| 481 | 
             
                    # Audio input section (placeholder for future)
         | 
| 482 | 
             
                    with gr.Accordion("🎙️ Audio Input (Coming Soon)", open=False):
         | 
|  | |
| 495 | 
             
                            interactive=False
         | 
| 496 | 
             
                        )
         | 
| 497 |  | 
| 498 | 
            +
                    # Conditional outputs based on debug mode
         | 
| 499 | 
            +
                    if DEBUG_MODE:
         | 
| 500 | 
            +
                        handler_outputs = [medical_advice_output, processing_steps_output, guidelines_output, technical_output]
         | 
| 501 | 
            +
                    else:
         | 
| 502 | 
            +
                        handler_outputs = [medical_advice_output, processing_steps_output, guidelines_output]
         | 
| 503 | 
            +
             | 
| 504 | 
             
                    # Event handlers
         | 
| 505 | 
             
                    submit_btn.click(
         | 
| 506 | 
             
                        fn=oncall_system.process_medical_query,
         | 
| 507 | 
             
                        inputs=[user_input, intention_override] if DEBUG_MODE else [user_input],
         | 
| 508 | 
            +
                        outputs=handler_outputs
         | 
| 509 | 
             
                    )
         | 
| 510 |  | 
| 511 | 
             
                    # Enter key support
         | 
| 512 | 
             
                    user_input.submit(
         | 
| 513 | 
             
                        fn=oncall_system.process_medical_query,
         | 
| 514 | 
             
                        inputs=[user_input, intention_override] if DEBUG_MODE else [user_input],
         | 
| 515 | 
            +
                        outputs=handler_outputs
         | 
| 516 | 
             
                    )
         | 
| 517 |  | 
| 518 | 
             
                    # Footer
         | 
    	
        manual_modifications.md
    ADDED
    
    | @@ -0,0 +1,126 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            """
         | 
| 2 | 
            +
            Manual modification instructions for app.py
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            你需要在以下位置手動修改代碼:
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            ## 修改 1: 添加新方法 (在第 280 行 \_detect_query_intention 之前)
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            """
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            def \_format_user_friendly_sources(self, processed_results: List[Dict]) -> str:
         | 
| 11 | 
            +
            """Format retrieved guidelines for production mode - user-friendly text format"""
         | 
| 12 | 
            +
            if not processed_results:
         | 
| 13 | 
            +
            return "No relevant medical guidelines found for this query."
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                sources = []
         | 
| 16 | 
            +
                emergency_count = 0
         | 
| 17 | 
            +
                treatment_count = 0
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                # Extract top 5 most relevant sources
         | 
| 20 | 
            +
                for i, result in enumerate(processed_results[:5], 1):
         | 
| 21 | 
            +
                    source_type = result.get('type', 'medical').title()
         | 
| 22 | 
            +
                    confidence = f"{(1 - result.get('distance', 1)) * 100:.0f}%"
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                    if source_type.lower() == 'emergency':
         | 
| 25 | 
            +
                        emergency_count += 1
         | 
| 26 | 
            +
                    elif source_type.lower() == 'treatment':
         | 
| 27 | 
            +
                        treatment_count += 1
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                    sources.append(f"{i}. {source_type} Guideline (Relevance: {confidence})")
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                # Build user-friendly text output
         | 
| 32 | 
            +
                result_text = "\n".join(sources)
         | 
| 33 | 
            +
                result_text += f"\n\n📊 Summary:"
         | 
| 34 | 
            +
                result_text += f"\n• Total guidelines consulted: {len(processed_results)}"
         | 
| 35 | 
            +
                if emergency_count > 0:
         | 
| 36 | 
            +
                    result_text += f"\n• Emergency protocols: {emergency_count}"
         | 
| 37 | 
            +
                if treatment_count > 0:
         | 
| 38 | 
            +
                    result_text += f"\n• Treatment guidelines: {treatment_count}"
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                result_text += f"\n\n✅ Evidence-based recommendations provided above"
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                return result_text
         | 
| 43 | 
            +
             | 
| 44 | 
            +
            """
         | 
| 45 | 
            +
             | 
| 46 | 
            +
            ## 修改 2: 更改第 177 行
         | 
| 47 | 
            +
             | 
| 48 | 
            +
            從:
         | 
| 49 | 
            +
            guidelines_display = self.\_format_guidelines_display(processed_results)
         | 
| 50 | 
            +
             | 
| 51 | 
            +
            改為: # Format retrieved guidelines for display - conditional based on debug mode
         | 
| 52 | 
            +
            if DEBUG_MODE:
         | 
| 53 | 
            +
            guidelines_display = self.\_format_guidelines_display(processed_results)
         | 
| 54 | 
            +
            else:
         | 
| 55 | 
            +
            guidelines_display = self.\_format_user_friendly_sources(processed_results)
         | 
| 56 | 
            +
             | 
| 57 | 
            +
            ## 修改 3: 更改第 418-434 行的界面定義
         | 
| 58 | 
            +
             | 
| 59 | 
            +
            從:
         | 
| 60 | 
            +
            with gr.Column(scale=1): # Retrieved guidelines
         | 
| 61 | 
            +
            guidelines_output = gr.JSON(
         | 
| 62 | 
            +
            label="📚 Retrieved Medical Guidelines",
         | 
| 63 | 
            +
            elem_classes="guidelines-display"
         | 
| 64 | 
            +
            )
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                            # Technical details (collapsible in production)
         | 
| 67 | 
            +
                            if DEBUG_MODE:
         | 
| 68 | 
            +
                                technical_output = gr.JSON(
         | 
| 69 | 
            +
                                    label="⚙️ Technical Details (Debug Mode)",
         | 
| 70 | 
            +
                                    elem_classes="technical-details"
         | 
| 71 | 
            +
                                )
         | 
| 72 | 
            +
                            else:
         | 
| 73 | 
            +
                                with gr.Accordion("🔧 System Information", open=False):
         | 
| 74 | 
            +
                                    technical_output = gr.JSON(
         | 
| 75 | 
            +
                                        label="Processing Information",
         | 
| 76 | 
            +
                                        elem_classes="technical-details"
         | 
| 77 | 
            +
                                    )
         | 
| 78 | 
            +
             | 
| 79 | 
            +
            改為:
         | 
| 80 | 
            +
            with gr.Column(scale=1): # Debug Mode: Show full technical details
         | 
| 81 | 
            +
            if DEBUG_MODE:
         | 
| 82 | 
            +
            guidelines_output = gr.JSON(
         | 
| 83 | 
            +
            label="📚 Retrieved Medical Guidelines (Debug)",
         | 
| 84 | 
            +
            elem_classes="guidelines-display"
         | 
| 85 | 
            +
            )
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                                technical_output = gr.JSON(
         | 
| 88 | 
            +
                                    label="⚙️ Technical Details (Debug Mode)",
         | 
| 89 | 
            +
                                    elem_classes="technical-details"
         | 
| 90 | 
            +
                                )
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                            # Production Mode: User-friendly simplified version
         | 
| 93 | 
            +
                            else:
         | 
| 94 | 
            +
                                guidelines_output = gr.Textbox(
         | 
| 95 | 
            +
                                    label="📖 Evidence Sources",
         | 
| 96 | 
            +
                                    lines=5,
         | 
| 97 | 
            +
                                    max_lines=8,
         | 
| 98 | 
            +
                                    interactive=False,
         | 
| 99 | 
            +
                                    elem_classes="evidence-sources"
         | 
| 100 | 
            +
                                )
         | 
| 101 | 
            +
             | 
| 102 | 
            +
                                # Hide technical details - no system information shown
         | 
| 103 | 
            +
                                technical_output = gr.State(None)
         | 
| 104 | 
            +
             | 
| 105 | 
            +
            ## 修改 4: 更改事件處理器 (第 470 行左右)
         | 
| 106 | 
            +
             | 
| 107 | 
            +
            在 submit_btn.click 和 user_input.submit 之前添加: # Conditional outputs based on debug mode
         | 
| 108 | 
            +
            if DEBUG_MODE:
         | 
| 109 | 
            +
            handler_outputs = [medical_advice_output, processing_steps_output, guidelines_output, technical_output]
         | 
| 110 | 
            +
            else:
         | 
| 111 | 
            +
            handler_outputs = [medical_advice_output, processing_steps_output, guidelines_output]
         | 
| 112 | 
            +
             | 
| 113 | 
            +
            然後修改兩個事件處理器的 outputs:
         | 
| 114 | 
            +
            submit_btn.click(
         | 
| 115 | 
            +
            fn=oncall_system.process_medical_query,
         | 
| 116 | 
            +
            inputs=[user_input, intention_override] if DEBUG_MODE else [user_input],
         | 
| 117 | 
            +
            outputs=handler_outputs
         | 
| 118 | 
            +
            )
         | 
| 119 | 
            +
             | 
| 120 | 
            +
                user_input.submit(
         | 
| 121 | 
            +
                    fn=oncall_system.process_medical_query,
         | 
| 122 | 
            +
                    inputs=[user_input, intention_override] if DEBUG_MODE else [user_input],
         | 
| 123 | 
            +
                    outputs=handler_outputs
         | 
| 124 | 
            +
                )
         | 
| 125 | 
            +
             | 
| 126 | 
            +
            """
         | 
    	
        src/user_prompt.py
    CHANGED
    
    | @@ -227,17 +227,28 @@ class UserPromptProcessor: | |
| 227 | 
             
                    generic_query = f"{user_query} medical treatment"
         | 
| 228 |  | 
| 229 | 
             
                    try:
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 230 | 
             
                        # Perform generic medical search
         | 
| 231 | 
             
                        generic_results = self.retrieval_system.search_generic_medical_content(generic_query)
         | 
| 232 |  | 
| 233 | 
             
                        if generic_results:
         | 
| 234 | 
            -
                            return  | 
|  | |
| 235 | 
             
                                'condition': 'generic medical query',
         | 
| 236 | 
             
                                'emergency_keywords': 'medical|emergency',
         | 
| 237 | 
             
                                'treatment_keywords': 'treatment|management',
         | 
| 238 | 
             
                                'generic_confidence': 0.5
         | 
| 239 | 
             
                            }
         | 
| 240 | 
            -
             | 
| 241 | 
             
                        return None 
         | 
| 242 | 
             
                    except Exception as e:
         | 
| 243 | 
             
                        logger.error(f"Generic medical search error: {e}")
         | 
|  | |
| 227 | 
             
                    generic_query = f"{user_query} medical treatment"
         | 
| 228 |  | 
| 229 | 
             
                    try:
         | 
| 230 | 
            +
                        # Check if query contains any generic medical terms
         | 
| 231 | 
            +
                        if not any(term in generic_query.lower() for term in generic_medical_terms):
         | 
| 232 | 
            +
                            logger.info("No generic medical terms found in query")
         | 
| 233 | 
            +
                            return None
         | 
| 234 | 
            +
                        
         | 
| 235 | 
            +
                        # Check if retrieval system is available
         | 
| 236 | 
            +
                        if not self.retrieval_system:
         | 
| 237 | 
            +
                            logger.warning("No retrieval system available for generic search")
         | 
| 238 | 
            +
                            return None
         | 
| 239 | 
            +
             | 
| 240 | 
             
                        # Perform generic medical search
         | 
| 241 | 
             
                        generic_results = self.retrieval_system.search_generic_medical_content(generic_query)
         | 
| 242 |  | 
| 243 | 
             
                        if generic_results:
         | 
| 244 | 
            +
                            return 
         | 
| 245 | 
            +
                            {
         | 
| 246 | 
             
                                'condition': 'generic medical query',
         | 
| 247 | 
             
                                'emergency_keywords': 'medical|emergency',
         | 
| 248 | 
             
                                'treatment_keywords': 'treatment|management',
         | 
| 249 | 
             
                                'generic_confidence': 0.5
         | 
| 250 | 
             
                            }
         | 
| 251 | 
            +
             | 
| 252 | 
             
                        return None 
         | 
| 253 | 
             
                    except Exception as e:
         | 
| 254 | 
             
                        logger.error(f"Generic medical search error: {e}")
         | 
