File size: 3,021 Bytes
19d6abb
 
 
3538827
 
 
 
 
 
19d6abb
3538827
19d6abb
 
 
 
3973c7a
19d6abb
3973c7a
 
 
 
 
 
 
19d6abb
 
 
3973c7a
 
 
19d6abb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import axios from 'axios';

// Configure axios to connect to FastAPI backend
// If NEXT_PUBLIC_API_URL is provided, use it. Otherwise, use relative paths (empty string)
// which works perfectly when serving frontend and backend from the same Docker container.
const apiBaseUrl = process.env.NEXT_PUBLIC_API_URL !== undefined 
  ? process.env.NEXT_PUBLIC_API_URL 
  : 'http://127.0.0.1:8000';

const apiClient = axios.create({
  baseURL: apiBaseUrl,
  timeout: 180000, // 3 minute timeout for analysis
});

export interface PatientData {
  age: number | string;
  sex: string;
  blood_pressure_systolic: number | string;
  blood_pressure_diastolic: number | string;
  blood_sugar_fasting: number | string;
  cholesterol_total: number | string;
  hdl_cholesterol: number | string;
  bmi: number | string;
  sleep_hours: number | string;
  stress_level: string;
  smoking: boolean;
  alcohol: string;
  exercise_days_per_week: number | string;
  iron_level: number | string;
  cortisol: number | string;
  family_history: string[];
}

export interface HealthAnalysisResponse {
  overall_health_score: number;
  risk_scores: {
    diabetes: number;
    hypertension: number;
    cardiovascular: number;
  };
  risk_explanations: string;
  profile_summary: string;
  evidence: string;
  intervention_plan: string;
}

export const healthApi = {
  // Check API health
  async checkHealth() {
    try {
      const response = await apiClient.get('/health');
      return response.data;
    } catch (error) {
      console.error('API health check failed:', error);
      throw error;
    }
  },

  // Analyze patient profile
  async analyzePatient(patientData: PatientData): Promise<HealthAnalysisResponse> {
    try {
      const response = await apiClient.post('/analyze', patientData);
      return response.data;
    } catch (error) {
      if (axios.isAxiosError(error)) {
        const errorData = error.response?.data;
        console.error('Full API Error Context:', JSON.stringify(errorData, null, 2));
        
        let errorMessage = 'Failed to analyze patient data';
        if (errorData?.detail) {
          if (Array.isArray(errorData.detail)) {
            errorMessage = errorData.detail.map((d: any) => `${d.loc.join('.')}: ${d.msg}`).join(', ');
          } else {
            errorMessage = errorData.detail;
          }
        }
        throw new Error(errorMessage);
      }
      throw error;
    }
  },

  // Upload medical images (if needed for future enhancement)
  async uploadImage(file: File, patientId?: string) {
    const formData = new FormData();
    formData.append('file', file);
    if (patientId) {
      formData.append('patient_id', patientId);
    }

    try {
      const response = await apiClient.post('/upload-image', formData, {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      });
      return response.data;
    } catch (error) {
      console.error('Image upload failed:', error);
      throw error;
    }
  },
};

export default healthApi;