File size: 36,509 Bytes
4721ef4
291b2db
 
4721ef4
291b2db
 
 
 
 
 
4721ef4
 
291b2db
 
4721ef4
 
a6e4bae
4721ef4
 
a6e4bae
4721ef4
a6e4bae
4721ef4
 
 
 
 
 
 
 
 
291b2db
 
4721ef4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6e4bae
4721ef4
291b2db
 
 
 
 
 
 
 
4721ef4
 
 
291b2db
 
a6e4bae
 
4721ef4
 
291b2db
4721ef4
 
291b2db
4721ef4
 
 
 
 
291b2db
 
4721ef4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
291b2db
 
 
 
 
 
 
 
 
4721ef4
 
 
 
 
a6e4bae
291b2db
 
a6e4bae
4721ef4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8489fe2
 
 
 
7b883e9
 
 
8489fe2
 
 
 
 
 
 
 
 
4721ef4
8489fe2
 
 
 
 
 
4721ef4
 
8489fe2
 
4721ef4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6e4bae
 
 
4721ef4
a6e4bae
4721ef4
a6e4bae
 
 
4721ef4
a6e4bae
 
4721ef4
a6e4bae
4721ef4
 
 
 
 
 
 
 
 
 
a6e4bae
4721ef4
a6e4bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4721ef4
a6e4bae
4721ef4
a6e4bae
 
 
 
4721ef4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
291b2db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4721ef4
 
 
 
 
a6e4bae
4721ef4
 
 
 
 
a6e4bae
4721ef4
 
 
 
a6e4bae
291b2db
a6e4bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
291b2db
 
a6e4bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4721ef4
 
a6e4bae
 
 
4721ef4
a6e4bae
4721ef4
 
a6e4bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4721ef4
a6e4bae
4721ef4
a6e4bae
 
4721ef4
a6e4bae
 
4721ef4
 
 
 
a6e4bae
4721ef4
a6e4bae
 
 
 
 
 
291b2db
a6e4bae
 
 
 
 
4721ef4
291b2db
a6e4bae
 
4721ef4
 
 
 
 
 
a6e4bae
 
 
 
 
 
 
4721ef4
291b2db
 
a6e4bae
 
 
291b2db
 
 
 
 
a6e4bae
 
291b2db
 
a6e4bae
 
 
 
 
4721ef4
 
 
 
 
 
a6e4bae
 
 
 
 
 
 
 
4721ef4
a6e4bae
4721ef4
 
 
 
 
a6e4bae
 
291b2db
 
a6e4bae
4721ef4
 
 
 
 
 
 
a6e4bae
 
 
 
4721ef4
 
a6e4bae
 
4721ef4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6e4bae
 
 
 
 
 
4721ef4
 
a6e4bae
4721ef4
 
 
a6e4bae
4721ef4
 
a6e4bae
4721ef4
 
 
291b2db
4721ef4
 
291b2db
 
4721ef4
291b2db
 
 
 
4721ef4
 
291b2db
4721ef4
 
 
 
a6e4bae
 
 
 
 
4721ef4
 
 
 
 
 
291b2db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4721ef4
 
a6e4bae
4721ef4
 
 
 
 
 
 
 
 
a6e4bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4721ef4
 
a6e4bae
 
291b2db
 
 
 
a6e4bae
 
 
 
 
 
 
 
 
 
 
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
"""
EASI Severity Prediction REST API with Batch Processing
========================================================
FastAPI-based REST API for predicting EASI scores from dermatological images.
Now supports both single and batch image processing!

New Features:
- POST /predict/batch - Process multiple images in one request
- Configurable max batch size and timeout
- Parallel processing for faster batch predictions

Endpoints:
- POST /predict - Upload single image and get EASI predictions
- POST /predict/batch - Upload multiple images (up to 10 at once)
- GET /health - Health check endpoint
- GET /conditions - Get list of available conditions
- GET /docs - Interactive API documentation

Installation:
pip install fastapi uvicorn python-multipart pillow tensorflow numpy pandas huggingface-hub

Run locally:
uvicorn api:app --host 0.0.0.0 --port 8000 --reload
"""

import os
import warnings
import logging
from typing import List, Dict, Any, Optional
from io import BytesIO
from pathlib import Path
import asyncio
from concurrent.futures import ThreadPoolExecutor

# Suppress warnings
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
os.environ['MLIR_CRASH_REPRODUCER_DIRECTORY'] = ''
warnings.filterwarnings('ignore')
logging.getLogger('absl').setLevel(logging.ERROR)

import tensorflow as tf
tf.get_logger().setLevel('ERROR')
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

from fastapi import FastAPI, File, UploadFile, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
import numpy as np
from PIL import Image
import pickle
import pandas as pd
from huggingface_hub import hf_hub_download

# Configuration
MAX_BATCH_SIZE = 10  # Maximum images per batch request
BATCH_TIMEOUT = 300  # Timeout in seconds for batch processing
HF_REPO_ID = "google/derm-foundation"
DERM_FOUNDATION_PATH = "./derm_foundation/"
EASI_MODEL_PATH = './trained_model/easi_severity_model_derm_foundation_individual.pkl'
HF_TOKEN = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")

# Initialize FastAPI app
app = FastAPI(
    title="EASI Severity Prediction API",
    description="REST API for predicting EASI scores from skin images. Supports single and batch processing.",
    version="2.1.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

# CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Thread pool for parallel processing
executor = ThreadPoolExecutor(max_workers=4)

# Response Models
class ConditionPrediction(BaseModel):
    condition: str
    probability: float = Field(..., ge=0, le=1)
    confidence: float = Field(..., ge=0)
    weight: float = Field(..., ge=0)
    easi_category: Optional[str] = None
    easi_contribution: int = Field(..., ge=0, le=3)

class EASIComponent(BaseModel):
    name: str
    score: int = Field(..., ge=0, le=3)
    contributing_conditions: List[Dict[str, Any]]

class PredictionResponse(BaseModel):
    success: bool
    total_easi_score: int = Field(..., ge=0, le=12)
    severity_interpretation: str
    easi_components: Dict[str, EASIComponent]
    predicted_conditions: List[ConditionPrediction]
    summary_statistics: Dict[str, float]
    image_info: Dict[str, Any]

class BatchPredictionResponse(BaseModel):
    success: bool
    total_images_processed: int
    successful_predictions: int
    failed_predictions: int
    results: List[Optional[PredictionResponse]]
    errors: List[Optional[str]]
    processing_time_seconds: float

class HealthResponse(BaseModel):
    status: str
    models_loaded: Dict[str, bool]
    available_conditions: int
    hf_token_configured: bool
    deployment_platform: str
    batch_processing_enabled: bool
    max_batch_size: int
    space_info: Optional[Dict[str, str]] = None

class ErrorResponse(BaseModel):
    success: bool = False
    error: str
    detail: Optional[str] = None


# Model wrapper class
class DermFoundationNeuralNetwork:
    def __init__(self):
        self.model = None
        self.mlb = None
        self.embedding_scaler = None
        self.confidence_scaler = None
        self.weighted_scaler = None
    
    def load_model(self, filepath):
        try:
            with open(filepath, 'rb') as f:
                model_data = pickle.load(f)
            
            self.mlb = model_data['mlb']
            self.embedding_scaler = model_data['embedding_scaler']
            self.confidence_scaler = model_data['confidence_scaler']
            self.weighted_scaler = model_data['weighted_scaler']
            
            keras_model_path = model_data['keras_model_path']
            
            if not os.path.exists(keras_model_path):
                print(f"Original keras path not found: {keras_model_path}")
                pickle_dir = os.path.dirname(os.path.abspath(filepath))
                normalized_path = keras_model_path.replace('\\', '/')
                keras_filename = normalized_path.split('/')[-1]
                print(f"Extracted filename: {keras_filename}")
                alternative_path = os.path.join(pickle_dir, keras_filename)
                print(f"Trying alternative path: {alternative_path}")
                
                if os.path.exists(alternative_path):
                    keras_model_path = alternative_path
                    print(f"βœ“ Found keras model at: {keras_model_path}")
                else:
                    print(f"βœ— Keras model not found at alternative path either")
                    return False
            else:
                print(f"βœ“ Found keras model at original path: {keras_model_path}")
            
            self.model = tf.keras.models.load_model(keras_model_path)
            print(f"βœ“ Keras model loaded successfully")
            return True
            
        except Exception as e:
            print(f"Error loading model: {e}")
            import traceback
            traceback.print_exc()
            return False
    
    def predict(self, embedding):
        if self.model is None:
            return None
        
        if len(embedding.shape) == 1:
            embedding = embedding.reshape(1, -1)
        
        embedding_scaled = self.embedding_scaler.transform(embedding)
        predictions = self.model.predict(embedding_scaled, verbose=0)
        
        condition_probs = predictions['conditions'][0]
        individual_confidences = predictions['individual_confidences'][0]
        individual_weights = predictions['individual_weights'][0]
        
        condition_threshold = 0.3
        predicted_condition_indices = np.where(condition_probs > condition_threshold)[0]
        
        predicted_conditions = []
        predicted_confidences = []
        predicted_weights_dict = {}
        
        for idx in predicted_condition_indices:
            condition_name = self.mlb.classes_[idx]
            condition_prob = float(condition_probs[idx])
            
            if individual_confidences[idx] > 0:
                confidence_orig = self.confidence_scaler.inverse_transform([[individual_confidences[idx]]])[0, 0]
            else:
                confidence_orig = 0.0
                
            if individual_weights[idx] > 0:
                weight_orig = self.weighted_scaler.inverse_transform([[individual_weights[idx]]])[0, 0]
            else:
                weight_orig = 0.0
            
            predicted_conditions.append(condition_name)
            predicted_confidences.append(max(0, confidence_orig))
            predicted_weights_dict[condition_name] = max(0, weight_orig)
        
        all_condition_probs = {}
        all_confidences = {}
        all_weights = {}
        
        for i, class_name in enumerate(self.mlb.classes_):
            all_condition_probs[class_name] = float(condition_probs[i])
            
            if individual_confidences[i] > 0:
                conf_orig = self.confidence_scaler.inverse_transform([[individual_confidences[i]]])[0, 0]
                all_confidences[class_name] = max(0, conf_orig)
            else:
                all_confidences[class_name] = 0.0
                
            if individual_weights[i] > 0:
                weight_orig = self.weighted_scaler.inverse_transform([[individual_weights[i]]])[0, 0]
                all_weights[class_name] = max(0, weight_orig)
            else:
                all_weights[class_name] = 0.0
        
        return {
            'dermatologist_skin_condition_on_label_name': predicted_conditions,
            'dermatologist_skin_condition_confidence': predicted_confidences,
            'weighted_skin_condition_label': predicted_weights_dict,
            'all_condition_probabilities': all_condition_probs,
            'all_individual_confidences': all_confidences,
            'all_individual_weights': all_weights,
            'condition_threshold': condition_threshold
        }


# Helper function to download from Hugging Face
def download_derm_foundation_from_hf(output_dir):
    """Download Derm Foundation model from Hugging Face Hub"""
    try:
        hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")
        
        print("=" * 80)
        print("DOWNLOADING DERM FOUNDATION MODEL FROM HUGGING FACE")
        print("=" * 80)
        
        if hf_token:
            print(f"βœ“ HF Token found (length: {len(hf_token)})")
        else:
            print("⚠ No HF Token found - attempting anonymous download")
        
        os.makedirs(output_dir, exist_ok=True)
        
        files_to_download = [
            "saved_model.pb",
            "variables/variables.data-00000-of-00001",
            "variables/variables.index"
        ]
        
        for file_path in files_to_download:
            print(f"\nπŸ“₯ Downloading: {file_path}")
            
            try:
                downloaded_path = hf_hub_download(
                    repo_id=HF_REPO_ID,
                    filename=file_path,
                    token=hf_token,
                    local_dir=output_dir,
                    local_dir_use_symlinks=False,
                    resume_download=True
                )
                
                if os.path.exists(downloaded_path):
                    file_size_mb = os.path.getsize(downloaded_path) / (1024 * 1024)
                    print(f"βœ“ Downloaded successfully ({file_size_mb:.2f} MB)")
                else:
                    print(f"βœ— File not found after download: {downloaded_path}")
                    return False
                    
            except Exception as download_error:
                print(f"βœ— Failed to download {file_path}")
                print(f"  Error: {str(download_error)}")
                raise
        
        print("\n" + "=" * 80)
        print("βœ“ DERM FOUNDATION MODEL DOWNLOADED SUCCESSFULLY")
        print("=" * 80)
        return True
        
    except Exception as e:
        print("\n" + "=" * 80)
        print("βœ— ERROR DOWNLOADING MODEL")
        print("=" * 80)
        print(f"Error: {str(e)}")
        import traceback
        traceback.print_exc()
        return False


# EASI calculation functions
def calculate_easi_scores(predictions):
    easi_categories = {
        'erythema': {
            'name': 'Erythema (Redness)',
            'conditions': [
                'Post-Inflammatory hyperpigmentation', 'Erythema ab igne', 'Erythema annulare centrifugum',
                'Erythema elevatum diutinum', 'Erythema gyratum repens', 'Erythema multiforme',
                'Erythema nodosum', 'Flagellate erythema', 'Annular erythema', 'Drug Rash',
                'Allergic Contact Dermatitis', 'Irritant Contact Dermatitis', 'Contact dermatitis',
                'Acute dermatitis', 'Chronic dermatitis', 'Acute and chronic dermatitis',
                'Sunburn', 'Photodermatitis', 'Phytophotodermatitis', 'Rosacea',
                'Seborrheic Dermatitis', 'Stasis Dermatitis', 'Perioral Dermatitis',
                'Burn erythema of abdominal wall', 'Burn erythema of back of hand',
                'Burn erythema of lower leg', 'Cellulitis', 'Infection of skin',
                'Viral Exanthem', 'Infected eczema', 'Crusted eczematous dermatitis',
                'Inflammatory dermatosis', 'Vasculitis of the skin', 'Leukocytoclastic Vasculitis',
                'Cutaneous lupus', 'CD - Contact dermatitis', 'Acute dermatitis, NOS',
                'Herpes Simplex', 'Hypersensitivity', 'Impetigo', 'Pigmented purpuric eruption',
                'Pityriasis rosea', 'Tinea', 'Tinea Versicolor'
            ]
        },
        'induration': {
            'name': 'Induration/Papulation (Swelling/Bumps)',
            'conditions': [
                'Prurigo nodularis', 'Urticaria', 'Granuloma annulare', 'Morphea',
                'Scleroderma', 'Lichen Simplex Chronicus', 'Lichen planus', 'lichenoid eruption',
                'Lichen nitidus', 'Lichen spinulosus', 'Lichen striatus', 'Keratosis pilaris',
                'Molluscum Contagiosum', 'Verruca vulgaris', 'Folliculitis', 'Acne',
                'Hidradenitis', 'Nodular vasculitis', 'Sweet syndrome', 'Necrobiosis lipoidica',
                'Basal Cell Carcinoma', 'SCC', 'SCCIS', 'SK', 'ISK',
                'Cutaneous T Cell Lymphoma', 'Skin cancer', 'Adnexal neoplasm',
                'Insect Bite', 'Milia', 'Miliaria', 'Xanthoma', 'Psoriasis',
                'Lichen planus/lichenoid eruption'
            ]
        },
        'excoriation': {
            'name': 'Excoriation (Scratching Damage)',
            'conditions': [
                'Inflicted skin lesions', 'Scabies', 'Abrasion', 'Abrasion of wrist',
                'Superficial wound of body region', 'Scrape', 'Animal bite - wound',
                'Pruritic dermatitis', 'Prurigo', 'Atopic dermatitis', 'Scab'
            ]
        },
        'lichenification': {
            'name': 'Lichenification (Skin Thickening)',
            'conditions': [
                'Lichenified eczematous dermatitis', 'Acanthosis nigricans',
                'Hyperkeratosis of skin', 'HK - Hyperkeratosis', 'Keratoderma',
                'Ichthyosis', 'Ichthyosiform dermatosis', 'Chronic eczema',
                'Psoriasis', 'Xerosis'
            ]
        }
    }
    
    def probability_to_score(prob):
        if prob < 0.171:
            return 0
        elif prob < 0.238:
            return 1
        elif prob < 0.421:
            return 2
        elif prob < 0.614:
            return 3
        else:
            return 3
    
    easi_results = {}
    all_condition_probs = predictions['all_condition_probabilities']
    
    for component, category_info in easi_categories.items():
        category_conditions = []
        
        for condition_name, probability in all_condition_probs.items():
            if condition_name.lower() == 'eczema':
                continue
                
            if condition_name in category_info['conditions']:
                category_conditions.append({
                    'condition': condition_name,
                    'probability': probability,
                    'individual_score': probability_to_score(probability)
                })
        
        category_conditions = [c for c in category_conditions if c['individual_score'] > 0]
        category_conditions.sort(key=lambda x: x['probability'], reverse=True)
        
        component_score = sum(c['individual_score'] for c in category_conditions)
        component_score = min(component_score, 3)
        
        easi_results[component] = {
            'name': category_info['name'],
            'score': component_score,
            'contributing_conditions': category_conditions
        }
    
    total_easi = sum(result['score'] for result in easi_results.values())
    
    return easi_results, total_easi


def get_severity_interpretation(total_easi):
    if total_easi == 0:
        return "No significant EASI features detected"
    elif total_easi <= 3:
        return "Mild EASI severity"
    elif total_easi <= 6:
        return "Moderate EASI severity"
    elif total_easi <= 9:
        return "Severe EASI severity"
    else:
        return "Very Severe EASI severity"


# Image processing functions
def smart_crop_to_square(image):
    width, height = image.size
    if width == height:
        return image
    
    size = min(width, height)
    left = (width - size) // 2
    top = (height - size) // 2
    right = left + size
    bottom = top + size
    
    return image.crop((left, top, right, bottom))


def generate_derm_foundation_embedding(model, image):
    try:
        if image.mode != 'RGB':
            image = image.convert('RGB')
        
        buf = BytesIO()
        image.save(buf, format='JPEG')
        image_bytes = buf.getvalue()
        
        input_tensor = tf.train.Example(features=tf.train.Features(
            feature={'image/encoded': tf.train.Feature(
                bytes_list=tf.train.BytesList(value=[image_bytes]))
            })).SerializeToString()
        
        infer = model.signatures["serving_default"]
        output = infer(inputs=tf.constant([input_tensor]))
        
        if 'embedding' in output:
            embedding_vector = output['embedding'].numpy().flatten()
        else:
            key = list(output.keys())[0]
            embedding_vector = output[key].numpy().flatten()
        
        return embedding_vector
    except Exception as e:
        raise Exception(f"Error generating embedding: {str(e)}")


def process_single_image_sync(image_bytes: bytes, filename: str) -> Dict[str, Any]:
    """
    Synchronous function to process a single image.
    Returns dict with 'success', 'result', and 'error' keys.
    """
    try:
        # Read and process image
        original_image = Image.open(BytesIO(image_bytes)).convert('RGB')
        original_size = original_image.size
        
        # Process to 448x448
        cropped_img = smart_crop_to_square(original_image)
        processed_img = cropped_img.resize((448, 448), Image.Resampling.LANCZOS)
        
        # Generate embedding
        embedding = generate_derm_foundation_embedding(derm_model, processed_img)
        
        # Make prediction
        predictions = easi_model.predict(embedding)
        
        if predictions is None:
            return {
                'success': False,
                'result': None,
                'error': "Prediction failed - model returned None"
            }
        
        # Calculate EASI scores
        easi_results, total_easi = calculate_easi_scores(predictions)
        severity = get_severity_interpretation(total_easi)
        
        # Format predicted conditions
        predicted_conditions = []
        for i, condition in enumerate(predictions['dermatologist_skin_condition_on_label_name']):
            prob = predictions['all_condition_probabilities'][condition]
            conf = predictions['dermatologist_skin_condition_confidence'][i]
            weight = predictions['weighted_skin_condition_label'][condition]
            
            # Find EASI category
            easi_category = None
            easi_contribution = 0
            for cat_key, cat_info in easi_results.items():
                for contrib in cat_info['contributing_conditions']:
                    if contrib['condition'] == condition:
                        easi_category = cat_info['name']
                        easi_contribution = contrib['individual_score']
                        break
            
            predicted_conditions.append(ConditionPrediction(
                condition=condition,
                probability=float(prob),
                confidence=float(conf),
                weight=float(weight),
                easi_category=easi_category,
                easi_contribution=easi_contribution
            ))
        
        # Summary statistics
        summary_stats = {
            "total_conditions": len(predicted_conditions),
            "average_confidence": float(np.mean(predictions['dermatologist_skin_condition_confidence'])) if predicted_conditions else 0.0,
            "average_weight": float(np.mean(list(predictions['weighted_skin_condition_label'].values()))) if predicted_conditions else 0.0,
            "total_weight": float(sum(predictions['weighted_skin_condition_label'].values()))
        }
        
        # Format EASI components
        easi_components_formatted = {
            component: EASIComponent(
                name=result['name'],
                score=result['score'],
                contributing_conditions=result['contributing_conditions']
            )
            for component, result in easi_results.items()
        }
        
        result = PredictionResponse(
            success=True,
            total_easi_score=total_easi,
            severity_interpretation=severity,
            easi_components=easi_components_formatted,
            predicted_conditions=predicted_conditions,
            summary_statistics=summary_stats,
            image_info={
                "original_size": f"{original_size[0]}x{original_size[1]}",
                "processed_size": "448x448",
                "filename": filename
            }
        )
        
        return {
            'success': True,
            'result': result,
            'error': None
        }
        
    except Exception as e:
        import traceback
        error_traceback = traceback.format_exc()
        print(f"Error processing image {filename}: {str(e)}")
        print(error_traceback)
        
        return {
            'success': False,
            'result': None,
            'error': str(e)
        }


# Global model instances
derm_model = None
easi_model = None
deployment_platform = "huggingface_spaces"


@app.on_event("startup")
async def load_models():
    """Load models on startup"""
    global derm_model, easi_model, deployment_platform
    
    import gc
    gc.collect()
    
    print("\n" + "=" * 80)
    print("πŸš€ STARTING EASI API WITH BATCH PROCESSING")
    print("=" * 80)
    
    # Detect if running on HF Spaces
    space_id = os.environ.get("SPACE_ID")
    space_author = os.environ.get("SPACE_AUTHOR_NAME")
    space_host = os.environ.get("SPACE_HOST")
    
    if space_id:
        deployment_platform = f"huggingface_spaces ({space_id})"
        print(f"πŸ“ Space: {space_id}")
        print(f"πŸ‘€ Author: {space_author}")
        print(f"🌐 Host: {space_host}")
    else:
        deployment_platform = "local"
        print("πŸ“ Running locally")
    
    print(f"πŸ”’ Max batch size: {MAX_BATCH_SIZE}")
    print(f"⏱️  Batch timeout: {BATCH_TIMEOUT}s")
    print("=" * 80)
    
    # Check HF Token
    hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")
    if hf_token:
        print(f"βœ“ HF Token configured (length: {len(hf_token)})")
    else:
        print("⚠ No HF Token found")
    
    print("=" * 80)
    
    # Check if Derm Foundation model exists locally
    model_files_exist = (
        os.path.exists(os.path.join(DERM_FOUNDATION_PATH, "saved_model.pb")) and
        os.path.exists(os.path.join(DERM_FOUNDATION_PATH, "variables"))
    )
    
    if not model_files_exist:
        print("\nπŸ“₯ Derm Foundation model not found - downloading from Hugging Face...")
        success = download_derm_foundation_from_hf(DERM_FOUNDATION_PATH)
        
        if not success:
            print("\n❌ CRITICAL: Failed to download Derm Foundation model!")
            return
    else:
        print("\nβœ“ Derm Foundation model found locally (using cache)")
    
    # Load Derm Foundation model
    print("\n" + "=" * 80)
    print("πŸ“¦ LOADING DERM FOUNDATION MODEL")
    print("=" * 80)
    
    try:
        print(f"Loading from: {DERM_FOUNDATION_PATH}")
        gc.collect()
        
        derm_model = tf.saved_model.load(DERM_FOUNDATION_PATH)
        print("βœ“ Derm Foundation model loaded successfully!")
        
        gc.collect()
    except Exception as e:
        print(f"βœ— Failed to load Derm Foundation model: {str(e)}")
        import traceback
        traceback.print_exc()
    
    # Load EASI model
    print("\n" + "=" * 80)
    print("πŸ“¦ LOADING EASI PREDICTION MODEL")
    print("=" * 80)
    
    if os.path.exists(EASI_MODEL_PATH):
        easi_model = DermFoundationNeuralNetwork()
        success = easi_model.load_model(EASI_MODEL_PATH)
        
        if success:
            print(f"βœ“ EASI model loaded from: {EASI_MODEL_PATH}")
            print(f"  Available conditions: {len(easi_model.mlb.classes_)}")
        else:
            print(f"βœ— Failed to load EASI model")
            easi_model = None
    else:
        print(f"βœ— EASI model not found at: {EASI_MODEL_PATH}")
    
    # Final status
    print("\n" + "=" * 80)
    print("🏁 STARTUP COMPLETE")
    print("=" * 80)
    print(f"Derm Foundation Model: {'βœ“ Loaded' if derm_model else 'βœ— Failed'}")
    print(f"EASI Prediction Model: {'βœ“ Loaded' if easi_model else 'βœ— Failed'}")
    print(f"Batch Processing: βœ“ Enabled (max {MAX_BATCH_SIZE} images)")
    print(f"Platform: {deployment_platform}")
    print("=" * 80)
    
    if derm_model and easi_model:
        print("βœ… All systems ready! API is operational.")
    else:
        print("⚠️  WARNING: Some models failed to load.")
    
    print("=" * 80 + "\n")


# API Endpoints

@app.get("/")
async def root():
    """Root endpoint with API information"""
    space_info = {
        "space_id": os.environ.get("SPACE_ID", "local"),
        "space_author": os.environ.get("SPACE_AUTHOR_NAME", "unknown"),
        "space_host": os.environ.get("SPACE_HOST", "localhost")
    }
    
    return {
        "message": "EASI Severity Prediction API with Batch Processing",
        "version": "2.1.0",
        "platform": deployment_platform,
        "space_info": space_info,
        "status": "operational" if (derm_model and easi_model) else "degraded",
        "batch_processing": {
            "enabled": True,
            "max_batch_size": MAX_BATCH_SIZE,
            "timeout_seconds": BATCH_TIMEOUT
        },
        "endpoints": {
            "health": "/health",
            "predict": "/predict (single image)",
            "predict_batch": "/predict/batch (multiple images)",
            "conditions": "/conditions",
            "docs": "/docs",
            "redoc": "/redoc"
        },
        "documentation": "Visit /docs for interactive API documentation"
    }


@app.get("/health", response_model=HealthResponse)
async def health_check():
    """Health check endpoint"""
    space_info = None
    if os.environ.get("SPACE_ID"):
        space_info = {
            "space_id": os.environ.get("SPACE_ID"),
            "space_author": os.environ.get("SPACE_AUTHOR_NAME"),
            "space_host": os.environ.get("SPACE_HOST")
        }
    
    return {
        "status": "healthy" if (derm_model is not None and easi_model is not None) else "degraded",
        "models_loaded": {
            "derm_foundation": derm_model is not None,
            "easi_model": easi_model is not None
        },
        "available_conditions": len(easi_model.mlb.classes_) if easi_model else 0,
        "hf_token_configured": (os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")) is not None,
        "deployment_platform": deployment_platform,
        "batch_processing_enabled": True,
        "max_batch_size": MAX_BATCH_SIZE,
        "space_info": space_info
    }


@app.get("/conditions", response_model=Dict[str, List[str]])
async def get_conditions():
    """Get list of available conditions"""
    if easi_model is None:
        raise HTTPException(
            status_code=503,
            detail="EASI model not loaded. Check server logs or /health endpoint."
        )
    
    return {
        "conditions": easi_model.mlb.classes_.tolist(),
        "total_count": len(easi_model.mlb.classes_)
    }


@app.post("/predict", response_model=PredictionResponse)
async def predict_easi(
    file: UploadFile = File(..., description="Skin image file (JPG, JPEG, PNG)")
):
    """
    Predict EASI scores from uploaded skin image.
    
    - **file**: Image file (JPG, JPEG, PNG)
    - Returns: EASI scores, component breakdown, and condition predictions
    """
    
    # Validate models loaded
    if derm_model is None or easi_model is None:
        error_detail = []
        if derm_model is None:
            error_detail.append("Derm Foundation model not loaded")
        if easi_model is None:
            error_detail.append("EASI model not loaded")
        
        raise HTTPException(
            status_code=503,
            detail=f"Models not available: {', '.join(error_detail)}. Check /health endpoint for details."
        )
    
    # Validate file type
    if not file.content_type or not file.content_type.startswith('image/'):
        raise HTTPException(
            status_code=400,
            detail="File must be an image (JPG, JPEG, PNG). Received: " + str(file.content_type)
        )
    
    try:
        # Read image bytes
        image_bytes = await file.read()
        
        # Process image synchronously
        result = process_single_image_sync(image_bytes, file.filename)
        
        if not result['success']:
            raise HTTPException(
                status_code=500,
                detail=f"Error processing image: {result['error']}"
            )
        
        return result['result']
        
    except HTTPException:
        raise
    except Exception as e:
        import traceback
        error_traceback = traceback.format_exc()
        print(f"Error processing image: {str(e)}")
        print(error_traceback)
        
        raise HTTPException(
            status_code=500,
            detail=f"Error processing image: {str(e)}"
        )


@app.post("/predict/batch", response_model=BatchPredictionResponse)
async def predict_easi_batch(
    files: List[UploadFile] = File(..., description=f"Multiple skin image files (max {MAX_BATCH_SIZE})")
):
    """
    Predict EASI scores from multiple uploaded skin images in parallel.
    
    - **files**: List of image files (JPG, JPEG, PNG) - max 10 images per request
    - Returns: Batch results with individual predictions and errors
    
    **Example Usage (Python):**
    ```python
    import requests
    
    files = [
        ('files', open('image1.jpg', 'rb')),
        ('files', open('image2.jpg', 'rb')),
        ('files', open('image3.jpg', 'rb'))
    ]
    
    response = requests.post('http://localhost:8000/predict/batch', files=files)
    results = response.json()
    ```
    
    **Example Usage (cURL):**
    ```bash
    curl -X POST "http://localhost:8000/predict/batch" \
      -F "[email protected]" \
      -F "[email protected]" \
      -F "[email protected]"
    ```
    """
    
    import time
    start_time = time.time()
    
    # Validate models loaded
    if derm_model is None or easi_model is None:
        error_detail = []
        if derm_model is None:
            error_detail.append("Derm Foundation model not loaded")
        if easi_model is None:
            error_detail.append("EASI model not loaded")
        
        raise HTTPException(
            status_code=503,
            detail=f"Models not available: {', '.join(error_detail)}. Check /health endpoint."
        )
    
    # Validate batch size
    num_files = len(files)
    if num_files == 0:
        raise HTTPException(
            status_code=400,
            detail="No files provided. Please upload at least one image."
        )
    
    if num_files > MAX_BATCH_SIZE:
        raise HTTPException(
            status_code=400,
            detail=f"Too many files. Maximum batch size is {MAX_BATCH_SIZE}, received {num_files}."
        )
    
    print(f"\nπŸ”„ Processing batch of {num_files} images...")
    
    # Validate file types and read all files
    image_data = []
    for idx, file in enumerate(files):
        if not file.content_type or not file.content_type.startswith('image/'):
            raise HTTPException(
                status_code=400,
                detail=f"File {idx+1} ('{file.filename}') is not an image. Received: {file.content_type}"
            )
        
        try:
            image_bytes = await file.read()
            image_data.append({
                'bytes': image_bytes,
                'filename': file.filename,
                'index': idx
            })
        except Exception as e:
            raise HTTPException(
                status_code=400,
                detail=f"Error reading file {idx+1} ('{file.filename}'): {str(e)}"
            )
    
    # Process images in parallel using thread pool
    try:
        loop = asyncio.get_event_loop()
        
        # Create tasks for parallel processing
        tasks = [
            loop.run_in_executor(
                executor,
                process_single_image_sync,
                img['bytes'],
                img['filename']
            )
            for img in image_data
        ]
        
        # Wait for all tasks with timeout
        results = await asyncio.wait_for(
            asyncio.gather(*tasks, return_exceptions=True),
            timeout=BATCH_TIMEOUT
        )
        
    except asyncio.TimeoutError:
        raise HTTPException(
            status_code=504,
            detail=f"Batch processing timeout after {BATCH_TIMEOUT} seconds. Try reducing batch size."
        )
    except Exception as e:
        import traceback
        traceback.print_exc()
        raise HTTPException(
            status_code=500,
            detail=f"Error during batch processing: {str(e)}"
        )
    
    # Collect results and errors
    prediction_results = []
    error_messages = []
    successful_count = 0
    failed_count = 0
    
    for idx, result in enumerate(results):
        if isinstance(result, Exception):
            # Handle exception during processing
            prediction_results.append(None)
            error_messages.append(f"Exception: {str(result)}")
            failed_count += 1
            print(f"  βœ— Image {idx+1} failed: {str(result)}")
        elif result['success']:
            prediction_results.append(result['result'])
            error_messages.append(None)
            successful_count += 1
            print(f"  βœ“ Image {idx+1} processed successfully")
        else:
            prediction_results.append(None)
            error_messages.append(result['error'])
            failed_count += 1
            print(f"  βœ— Image {idx+1} failed: {result['error']}")
    
    processing_time = time.time() - start_time
    
    print(f"βœ… Batch complete: {successful_count} successful, {failed_count} failed in {processing_time:.2f}s\n")
    
    return BatchPredictionResponse(
        success=True,
        total_images_processed=num_files,
        successful_predictions=successful_count,
        failed_predictions=failed_count,
        results=prediction_results,
        errors=error_messages,
        processing_time_seconds=round(processing_time, 2)
    )


@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc):
    """Custom HTTP exception handler"""
    return JSONResponse(
        status_code=exc.status_code,
        content=ErrorResponse(
            error=exc.detail,
            detail=str(exc)
        ).dict()
    )


@app.exception_handler(Exception)
async def general_exception_handler(request, exc):
    """General exception handler for unexpected errors"""
    import traceback
    error_traceback = traceback.format_exc()
    print(f"Unexpected error: {str(exc)}")
    print(error_traceback)
    
    return JSONResponse(
        status_code=500,
        content=ErrorResponse(
            error="Internal server error",
            detail=str(exc)
        ).dict()
    )


if __name__ == "__main__":
    import uvicorn
    
    print("=" * 80)
    print("πŸš€ Starting EASI API Server with Batch Processing")
    print("=" * 80)
    print(f"Max batch size: {MAX_BATCH_SIZE} images")
    print(f"Batch timeout: {BATCH_TIMEOUT} seconds")
    print("=" * 80)
    print("Access the API at: http://localhost:8000")
    print("Interactive docs: http://localhost:8000/docs")
    print("=" * 80)
    
    uvicorn.run(
        app,
        host="0.0.0.0",
        port=8000,
        log_level="info"
    )