File size: 8,059 Bytes
26c7cba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""
Test script for Pathora Colposcopy API endpoints
Demonstrates how to use both AI model and LLM endpoints
"""

import requests
import json
import base64
from pathlib import Path

# API Configuration
BASE_URL = "http://localhost:8000"  # Change to your deployment URL
API_KEY = "your_gemini_api_key_here"  # For local testing

def test_health_check():
    """Test the health check endpoint"""
    print("=" * 60)
    print("Testing Health Check Endpoint")
    print("=" * 60)
    
    response = requests.get(f"{BASE_URL}/health")
    print(f"Status Code: {response.status_code}")
    print(f"Response: {json.dumps(response.json(), indent=2)}")
    print()

def test_acetowhite_detection(image_path: str):
    """Test acetowhite contour detection"""
    print("=" * 60)
    print("Testing Acetowhite Contour Detection")
    print("=" * 60)
    
    with open(image_path, 'rb') as f:
        files = {'file': f}
        data = {'conf_threshold': 0.5}
        
        response = requests.post(
            f"{BASE_URL}/api/infer-aw-contour",
            files=files,
            data=data
        )
    
    print(f"Status Code: {response.status_code}")
    result = response.json()
    
    # Print without base64 image for readability
    print(f"Status: {result.get('status')}")
    print(f"Detections: {result.get('detections')}")
    print(f"Contours: {len(result.get('contours', []))}")
    print(f"Confidence Threshold: {result.get('confidence_threshold')}")
    
    # Save result image if available
    if result.get('result_image'):
        output_path = "test_output_aw.png"
        img_data = base64.b64decode(result['result_image'])
        with open(output_path, 'wb') as f:
            f.write(img_data)
        print(f"Result image saved to: {output_path}")
    print()

def test_cervix_detection(image_path: str):
    """Test cervix bounding box detection"""
    print("=" * 60)
    print("Testing Cervix Bounding Box Detection")
    print("=" * 60)
    
    with open(image_path, 'rb') as f:
        files = {'file': f}
        data = {'conf_threshold': 0.4}
        
        response = requests.post(
            f"{BASE_URL}/api/infer-cervix-bbox",
            files=files,
            data=data
        )
    
    print(f"Status Code: {response.status_code}")
    result = response.json()
    
    print(f"Status: {result.get('status')}")
    print(f"Detections: {result.get('detections')}")
    print(f"Bounding Boxes: {json.dumps(result.get('bounding_boxes', []), indent=2)}")
    
    # Save result image if available
    if result.get('result_image'):
        output_path = "test_output_cervix.png"
        img_data = base64.b64decode(result['result_image'])
        with open(output_path, 'wb') as f:
            f.write(img_data)
        print(f"Result image saved to: {output_path}")
    print()

def test_batch_inference(image_paths: list):
    """Test batch inference on multiple images"""
    print("=" * 60)
    print("Testing Batch Inference")
    print("=" * 60)
    
    files = [('files', open(img, 'rb')) for img in image_paths]
    data = {'conf_threshold': 0.5}
    
    response = requests.post(
        f"{BASE_URL}/api/batch-infer",
        files=files,
        data=data
    )
    
    # Close file handles
    for _, f in files:
        f.close()
    
    print(f"Status Code: {response.status_code}")
    result = response.json()
    
    print(f"Status: {result.get('status')}")
    print(f"Total Files: {result.get('total_files')}")
    
    for i, res in enumerate(result.get('results', [])):
        print(f"\nImage {i+1}: {res.get('filename')}")
        print(f"  Status: {res.get('status')}")
        print(f"  Detections: {res.get('detections')}")
    print()

def test_chat():
    """Test LLM chat endpoint"""
    print("=" * 60)
    print("Testing Chat Endpoint")
    print("=" * 60)
    
    payload = {
        "message": "What are the typical signs of a high-grade squamous intraepithelial lesion (HSIL) on colposcopy?",
        "history": []
    }
    
    response = requests.post(
        f"{BASE_URL}/api/chat",
        json=payload
    )
    
    print(f"Status Code: {response.status_code}")
    
    if response.status_code == 200:
        result = response.json()
        print(f"Status: {result.get('status')}")
        print(f"Model: {result.get('model')}")
        print(f"Response:\n{result.get('response')}")
    else:
        print(f"Error: {response.json()}")
    print()

def test_chat_with_history():
    """Test chat with conversation history"""
    print("=" * 60)
    print("Testing Chat with History")
    print("=" * 60)
    
    payload = {
        "message": "What about low-grade lesions?",
        "history": [
            {
                "role": "user",
                "text": "What are high-grade lesions?"
            },
            {
                "role": "bot",
                "text": "High-grade lesions (HSIL) show dense acetowhite epithelium, coarse punctation, and sharp borders."
            }
        ]
    }
    
    response = requests.post(
        f"{BASE_URL}/api/chat",
        json=payload
    )
    
    print(f"Status Code: {response.status_code}")
    
    if response.status_code == 200:
        result = response.json()
        print(f"Response:\n{result.get('response')}")
    else:
        print(f"Error: {response.json()}")
    print()

def test_report_generation():
    """Test report generation endpoint"""
    print("=" * 60)
    print("Testing Report Generation")
    print("=" * 60)
    
    payload = {
        "patient_data": {
            "age": 35,
            "gravida": 2,
            "para": 2,
            "lmp": "2024-02-01",
            "indication": "Abnormal Pap smear - ASCUS",
            "menstrual_status": "Regular"
        },
        "exam_findings": {
            "native": {
                "cervix_visible": True,
                "transformation_zone": "Type 1 (fully visible)",
                "ectropion": "Mild",
                "discharge": "None"
            },
            "acetic_acid": {
                "acetowhite_lesions": True,
                "location": "6-9 o'clock position",
                "density": "Dense white",
                "borders": "Sharp, well-defined",
                "size": "Moderate (covering 2 quadrants)"
            },
            "green_filter": {
                "vascular_patterns": "Coarse punctation",
                "mosaic": "Present",
                "atypical_vessels": "None"
            },
            "lugol": {
                "iodine_uptake": "Partial iodine negative area",
                "pattern": "Corresponds to acetowhite area"
            }
        }
    }
    
    response = requests.post(
        f"{BASE_URL}/api/generate-report",
        json=payload
    )
    
    print(f"Status Code: {response.status_code}")
    
    if response.status_code == 200:
        result = response.json()
        print(f"Status: {result.get('status')}")
        print(f"Model: {result.get('model')}")
        print(f"\nGenerated Report:\n{'-' * 60}")
        print(result.get('report'))
        print('-' * 60)
    else:
        print(f"Error: {response.json()}")
    print()

def main():
    """Run all tests"""
    print("\n" + "=" * 60)
    print("PATHORA COLPOSCOPY API TEST SUITE")
    print("=" * 60 + "\n")
    
    # Test health check
    test_health_check()
    
    # Test AI model endpoints (you'll need to provide actual image paths)
    # Uncomment and add your image paths:
    # test_acetowhite_detection("path/to/your/image.jpg")
    # test_cervix_detection("path/to/your/image.jpg")
    # test_batch_inference(["image1.jpg", "image2.jpg"])
    
    # Test LLM endpoints
    test_chat()
    test_chat_with_history()
    test_report_generation()
    
    print("\n" + "=" * 60)
    print("ALL TESTS COMPLETED")
    print("=" * 60 + "\n")

if __name__ == "__main__":
    # Check if requests is installed
    try:
        import requests
    except ImportError:
        print("Please install requests: pip install requests")
        exit(1)
    
    main()