forked from udhayk7/OneAI-Medical
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_report.py
More file actions
179 lines (150 loc) · 6.81 KB
/
Copy pathtest_report.py
File metadata and controls
179 lines (150 loc) · 6.81 KB
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
#!/usr/bin/env python3
"""
Test script for report generation API
"""
import requests
import json
import os
from io import BytesIO
# API base URL
BASE_URL = "http://localhost:8000"
def test_list_reports():
"""Test the list reports endpoint"""
url = f"{BASE_URL}/api/reports/list"
try:
print("Testing list reports endpoint...")
response = requests.get(url)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
result = response.json()
print("✅ Reports listed successfully!")
print(f"Number of reports: {len(result.get('reports', []))}")
for report in result.get('reports', []):
print(f" - Report ID: {report.get('id')}")
print(f" Patient ID: {report.get('patient_id')}")
print(f" Created: {report.get('created_at')}")
print(f" Transcription: {report.get('transcription', '')[:50]}...")
print()
else:
print("❌ Failed to list reports!")
print(f"Error: {response.text}")
except requests.exceptions.ConnectionError:
print("❌ Connection error! Make sure the API server is running.")
except Exception as e:
print(f"❌ Error: {str(e)}")
def test_add_report_with_dummy_audio():
"""Test the add report endpoint with dummy audio data"""
url = f"{BASE_URL}/api/reports/add"
# Create a dummy audio file (this won't actually work with Whisper, but tests the endpoint)
dummy_audio_content = b"dummy audio content for testing"
try:
print("Testing add report endpoint with dummy audio...")
print("⚠️ Note: This will fail at the transcription step since we're using dummy audio")
# Use a valid patient ID from previous tests
patient_id = "8cc7dcee-e22e-436d-adf6-504b53568f4b" # John Doe's ID
files = {
'audio_file': ('test_audio.webm', dummy_audio_content, 'audio/webm')
}
data = {
'patient_id': patient_id,
'department': 'general'
}
response = requests.post(url, files=files, data=data)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
result = response.json()
print("✅ Report created successfully!")
print(f"Report ID: {result.get('id')}")
print(f"Patient ID: {result.get('patient_id')}")
print(f"Department: {result.get('department')}")
print(f"Transcription: {result.get('transcription', '')[:100]}...")
print(f"Clinical Report: {result.get('report', '')[:100]}...")
print(f"AI Summary: {result.get('summary', '')[:100]}...")
else:
print("❌ Report creation failed (expected with dummy audio)!")
print(f"Error: {response.text}")
except requests.exceptions.ConnectionError:
print("❌ Connection error! Make sure the API server is running.")
except Exception as e:
print(f"❌ Error: {str(e)}")
def test_file_upload_validation():
"""Test file upload validation"""
url = f"{BASE_URL}/api/reports/add"
try:
print("\nTesting file upload validation...")
# Test with invalid file type
print("1. Testing with invalid file type...")
files = {
'audio_file': ('test.txt', b"text content", 'text/plain')
}
data = {
'patient_id': "8cc7dcee-e22e-436d-adf6-504b53568f4b",
'department': 'general'
}
response = requests.post(url, files=files, data=data)
print(f"Status Code: {response.status_code}")
if response.status_code == 400:
print("✅ File type validation working!")
print(f"Error message: {response.json().get('detail', '')}")
else:
print("❌ File type validation not working as expected")
# Test with empty file
print("\n2. Testing with empty file...")
files = {
'audio_file': ('empty.webm', b"", 'audio/webm')
}
response = requests.post(url, files=files, data=data)
print(f"Status Code: {response.status_code}")
if response.status_code == 400:
print("✅ Empty file validation working!")
print(f"Error message: {response.json().get('detail', '')}")
else:
print("❌ Empty file validation not working as expected")
except requests.exceptions.ConnectionError:
print("❌ Connection error! Make sure the API server is running.")
except Exception as e:
print(f"❌ Error: {str(e)}")
def print_usage_instructions():
"""Print instructions for using the report API"""
print("\n" + "="*60)
print("📋 REPORT API USAGE INSTRUCTIONS")
print("="*60)
print("\n🎙️ To use the enhanced report API in production:")
print("1. Record doctor-patient conversation from microphone")
print("2. Save as .webm, .wav, or .mp3 format")
print("3. Send POST request to /api/reports/add with:")
print(" - patient_id: UUID of existing patient")
print(" - department: Medical department (general, cardiology, ent, etc.)")
print(" - audio_file: The recorded conversation file")
print("\n📝 Example with curl:")
print("curl -X POST http://localhost:8000/api/reports/add \\")
print(" -F 'patient_id=8cc7dcee-e22e-436d-adf6-504b53568f4b' \\")
print(" -F 'department=cardiology' \\")
print(" -F 'audio_file=@doctor_conversation.webm'")
print("\n🔑 Requirements:")
print("- Valid OpenAI API key in .env file")
print("- Patient must exist in users table")
print("- Audio file must be < 10MB")
print("- Supported formats: webm, wav, mp3, m4a")
print("- Valid department: general, cardiology, ent, neurology, orthopedics, pediatrics, dermatology")
print("\n📊 Enhanced AI Processing:")
print("1. Transcribe audio using OpenAI Whisper")
print("2. Clean transcription (remove filler words)")
print("3. Extract clinical context (vitals, symptoms, locations)")
print("4. Generate department-specific medical report using GPT-3.5")
print("5. Create AI summary with health alerts")
print("6. Save all data to Supabase reports table")
print("7. Return comprehensive medical report with AI insights")
if __name__ == "__main__":
print("OneAI Report API Test")
print("=" * 30)
# Test list reports
test_list_reports()
print()
# Test file upload validation
test_file_upload_validation()
print()
# Test add report with dummy audio (will fail at transcription)
test_add_report_with_dummy_audio()
# Print usage instructions
print_usage_instructions()