Spaces:
Sleeping
Sleeping
| """ | |
| Simple API Test Script - Multilingual Pain Assessment System Demo | |
| """ | |
| import requests | |
| import json | |
| BASE_URL = "http://localhost:8000" | |
| def test_health(): | |
| """Check server health status""" | |
| try: | |
| response = requests.get(f"{BASE_URL}/health", timeout=5) | |
| print("β Server is running") | |
| print(f" Version: {response.json()['version']}") | |
| return True | |
| except requests.exceptions.ConnectionError: | |
| print("β Server not running") | |
| print(" Please start server: python Backend/main.py") | |
| return False | |
| except Exception as e: | |
| print(f"β Connection error: {e}") | |
| return False | |
| def test_system_info(): | |
| """Get system information""" | |
| print("\n" + "="*70) | |
| print("π System Information") | |
| print("="*70) | |
| try: | |
| response = requests.get(f"{BASE_URL}/api/system-info") | |
| data = response.json() | |
| if data["status"] == "success": | |
| info = data["system_info"]["pipeline_info"] | |
| print(f"Version: {info['pipeline_version']}") | |
| print(f"Architecture: {info['architecture']}") | |
| print(f"Supported Languages: {', '.join(info['supported_languages'])}") | |
| print(f"\nOntology Coverage:") | |
| print(f" - Total: {info['ontology_coverage']['total_descriptors']} terms") | |
| print(f" - Chinese: {info['ontology_coverage']['chinese_terms']}") | |
| print(f" - Korean: {info['ontology_coverage']['korean_terms']}") | |
| print(f" - Spanish: {info['ontology_coverage']['spanish_terms']}") | |
| print(f" - Hmong: {info['ontology_coverage']['hmong_terms']}") | |
| except Exception as e: | |
| print(f"β Failed to get system info: {e}") | |
| def test_analysis(language_name, text): | |
| """Test pain analysis""" | |
| print("\n" + "="*70) | |
| print(f"π Testing {language_name}") | |
| print("="*70) | |
| print(f"Input: {text}") | |
| try: | |
| response = requests.post( | |
| f"{BASE_URL}/api/analyze-text-neuro-symbolic", | |
| json={"text": text}, | |
| timeout=30 | |
| ) | |
| result = response.json() | |
| if result["status"] == "success": | |
| print("\nβ Analysis succeeded!") | |
| # Display structured data | |
| data = result["structured_data"] | |
| print(f"\nπ Structured Data:") | |
| print(f" Pain Type: {data['pain_type']}") | |
| print(f" Location: {data['location']}") | |
| print(f" Temporal Pattern: {data['temporal_pattern']}") | |
| print(f" Intensity: {data['intensity']}") | |
| if data['emotion']: | |
| print(f" Emotion: {data['emotion']}") | |
| # Display ontology mappings | |
| if result["ontology_mapping_trace"]: | |
| print(f"\nπ Ontology Mappings ({len(result['ontology_mapping_trace'])} items):") | |
| for mapping in result["ontology_mapping_trace"]: | |
| lang_code = mapping.get('detected_language', '?') | |
| print(f" - [{lang_code}] {mapping['original_term']} β " | |
| f"{mapping['mapped_english']} ({mapping.get('pain_type', 'N/A')})") | |
| # Display clinical recommendations | |
| if result["clinical_recommendations"]: | |
| print(f"\nπ Clinical Recommendations ({len(result['clinical_recommendations'])} items):") | |
| for i, rec in enumerate(result["clinical_recommendations"], 1): | |
| print(f"\n {i}. {rec['recommendation'][:100]}...") | |
| print(f" Rule: {rec['triggered_by_rule']}") | |
| print(f" Evidence: {rec['evidence']}") | |
| else: | |
| print("\nπ Clinical Recommendations: Standard assessment recommended") | |
| return True | |
| else: | |
| print(f"\nβ Analysis failed: {result.get('message')}") | |
| return False | |
| except requests.exceptions.Timeout: | |
| print("\nβ Request timeout (OpenAI API might be slow)") | |
| return False | |
| except Exception as e: | |
| print(f"\nβ Error: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False | |
| def main(): | |
| """Main test function""" | |
| print("="*70) | |
| print("π Multilingual Pain Assessment System - API Test") | |
| print("="*70) | |
| # 1. Check server | |
| if not test_health(): | |
| return | |
| # 2. Get system info | |
| test_system_info() | |
| # 3. Test different languages | |
| test_cases = [ | |
| { | |
| "name": "Chinese π¨π³", | |
| "text": "ζζη«θΎ£θΎ£ηηΌηοΌε·²η»ε₯½ε δΈͺζδΊοΌθ °ι¨εΎιΎε" | |
| }, | |
| { | |
| "name": "Korean π°π·", | |
| "text": "νλ¦¬κ° λ°λ거리λ―μ΄ μνλ€" | |
| }, | |
| { | |
| "name": "Spanish πͺπΈ", | |
| "text": "Tengo un dolor agudo y punzante en la espalda" | |
| }, | |
| { | |
| "name": "Hmong", | |
| "text": "Kuv mob Kub Heev heev" | |
| } | |
| ] | |
| passed = 0 | |
| total = len(test_cases) | |
| for test_case in test_cases: | |
| if test_analysis(test_case["name"], test_case["text"]): | |
| passed += 1 | |
| # Summary | |
| print("\n" + "="*70) | |
| print("π Test Summary") | |
| print("="*70) | |
| print(f"Passed: {passed}/{total}") | |
| if passed == total: | |
| print("β All tests passed!") | |
| else: | |
| print(f"β οΈ {total - passed} test(s) failed") | |
| if __name__ == "__main__": | |
| main() | |