File size: 44,478 Bytes
93bab70
 
 
48fa2fe
 
 
 
 
 
 
 
 
 
 
93bab70
 
 
 
 
 
 
 
 
 
 
 
 
 
48fa2fe
 
 
 
93bab70
 
7e483d6
48fa2fe
 
 
 
 
 
 
 
 
 
 
 
7e483d6
 
 
 
 
48fa2fe
 
7e483d6
48fa2fe
 
7e483d6
 
 
 
48fa2fe
 
7e483d6
 
48fa2fe
 
7e483d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48fa2fe
 
93bab70
 
 
 
 
 
48fa2fe
93bab70
48fa2fe
93bab70
48fa2fe
 
93bab70
48fa2fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93bab70
48fa2fe
 
 
 
 
7e483d6
 
48fa2fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93bab70
7e483d6
3149176
7e483d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3149176
 
 
 
 
7e483d6
 
 
 
 
 
 
3149176
93bab70
 
 
 
48fa2fe
 
93bab70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e483d6
93bab70
 
 
 
 
 
 
 
 
7e483d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93bab70
 
7e483d6
93bab70
7e483d6
93bab70
 
 
 
 
 
 
 
 
48fa2fe
93bab70
48fa2fe
 
 
 
 
 
 
 
 
 
 
 
 
7e483d6
 
93bab70
48fa2fe
7e483d6
48fa2fe
 
 
 
 
 
 
 
 
 
 
 
93bab70
 
48fa2fe
 
 
 
 
 
 
 
93bab70
48fa2fe
 
 
 
 
93bab70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e483d6
 
93bab70
 
 
 
 
 
7e483d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93bab70
 
 
 
7e483d6
 
 
 
93bab70
 
7e483d6
93bab70
7e483d6
93bab70
 
 
 
 
 
 
 
 
48fa2fe
93bab70
48fa2fe
 
 
 
 
 
 
 
 
 
 
 
 
7e483d6
 
93bab70
48fa2fe
7e483d6
93bab70
 
48fa2fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93bab70
48fa2fe
 
 
 
 
93bab70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e483d6
 
93bab70
 
 
 
 
 
 
 
 
 
 
 
 
7e483d6
93bab70
7e483d6
93bab70
7e483d6
 
93bab70
7e483d6
93bab70
7e483d6
 
93bab70
 
7e483d6
 
 
93bab70
 
7e483d6
 
 
 
93bab70
7e483d6
93bab70
 
7e483d6
93bab70
7e483d6
93bab70
 
 
 
 
 
 
 
 
48fa2fe
93bab70
48fa2fe
 
 
 
 
 
 
 
 
 
 
 
 
7e483d6
 
93bab70
48fa2fe
7e483d6
93bab70
 
48fa2fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93bab70
48fa2fe
 
 
 
 
93bab70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e483d6
93bab70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import torch
from typing import Dict, List, Any, Optional, Union
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig

# Import PEFT for adapter handling
try:
    import peft
    from peft import PeftModel, PeftConfig
    PEFT_AVAILABLE = True
except ImportError:
    PEFT_AVAILABLE = False
    print("Warning: PEFT library not available. Adapter loading may fail.")


class EndpointHandler:
    def __init__(self, model_dir=None):
        """
        Initialize the endpoint handler.
        Args:
            model_dir: Directory where model and tokenizer are stored
        """
        self.model = None
        self.tokenizer = None
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        self.model_dir = model_dir or os.getenv("MODEL_PATH", "/model")
        
        # GPU performance optimization flags
        self.flash_attention_supported = False  # Will be set during model loading
        self.use_sampling = True                # Better quality but slightly slower than greedy
        
        # Load model immediately
        self.load_model()
        
    def generate_optimized(self, inputs, attention_mask=None, max_new_tokens=512):
        """
        Optimized generation function that maximizes GPU utilization
        while respecting model constraints.
        """
        # Check if we need to create an attention mask
        if attention_mask is None:
            attention_mask = inputs.ne(self.tokenizer.pad_token_id).long()
        
        # Find input length to properly calculate output length
        input_length = inputs.shape[1]
        
        # Basic generation parameters
        generation_kwargs = {
            "inputs": inputs,
            "attention_mask": attention_mask,
            "max_new_tokens": max_new_tokens,
            
            # Performance options
            "use_cache": True,                         # Use KV cache for faster generation
            
            # Quality vs. speed tradeoff
            "temperature": 0.7 if self.use_sampling else 1.0,
            "top_p": 0.9 if self.use_sampling else 1.0,
            "do_sample": self.use_sampling,            # Sampling is slightly slower but better quality
            "num_beams": 1,                            # Beam search is slower but better quality (1 = no beam search)
            
            # Token handling
            "pad_token_id": self.tokenizer.pad_token_id,
            "eos_token_id": self.tokenizer.eos_token_id,
            
            # Content quality
            "repetition_penalty": 1.1,                 # Reduce repetition
        }
        
        # Add Flash Attention parameters only if supported by the transformers version
        # We check the transformer version by testing in a safe way
        try:
            import importlib
            transformers_version = importlib.import_module('transformers').__version__
            major, minor = map(int, transformers_version.split('.')[:2])
            
            if major > 4 or (major == 4 and minor >= 32):
                # Flash Attention support was added in transformers 4.32.0
                if self.flash_attention_supported:
                    print("Using Flash Attention in generation")
                    generation_kwargs["flash_attn"] = True
                    generation_kwargs["flash_attn_cross_entropy"] = True
            else:
                print(f"Flash Attention not added - transformers version {transformers_version} doesn't support it")
        except Exception as e:
            print(f"Error checking transformers version, skipping Flash Attention: {e}")
        
        # Generate with optimized parameters for GPU performance
        outputs = self.model.generate(**generation_kwargs)
        
        return outputs, input_length
    
    def load_model(self):
        """Load the finetuned model and tokenizer."""
        try:
            print(f"Loading model from {self.model_dir} to {self.device}...")
            
            # Load tokenizer with explicit padding token configuration
            try:
                self.tokenizer = AutoTokenizer.from_pretrained(
                    self.model_dir,
                    padding_side="left",  # Set padding to left side for causal LM
                    trust_remote_code=False
                )
                
                # Ensure pad token is set properly (important for attention masks)
                if self.tokenizer.pad_token is None:
                    self.tokenizer.pad_token = self.tokenizer.eos_token
                    print("Set pad_token to eos_token")
                    
            except Exception as tokenizer_error:
                print(f"Error loading tokenizer from {self.model_dir}: {tokenizer_error}")
                print("Attempting to load base Phi-2 tokenizer...")
                
                # Fall back to base Phi-2 tokenizer if model dir tokenizer fails
                self.tokenizer = AutoTokenizer.from_pretrained(
                    "microsoft/phi-2",
                    padding_side="left",
                    trust_remote_code=False
                )
                if self.tokenizer.pad_token is None:
                    self.tokenizer.pad_token = self.tokenizer.eos_token
            
            # Try to load model with quantization with consistent dtype settings
            try:
                from bitsandbytes.nn import Linear4bit
                from transformers import BitsAndBytesConfig
                
                print("Using 4-bit quantization with float16 compute type")
                
                # Use consistent float16 for both compute and parameters
                quantization_config = BitsAndBytesConfig(
                    load_in_4bit=True,
                    bnb_4bit_compute_dtype=torch.float16,  # Match with model dtype
                    bnb_4bit_use_double_quant=True,
                    bnb_4bit_quant_type="nf4"
                )
                
                # Try to load with base model specification for better adapter compatibility
                if os.path.exists(os.path.join(self.model_dir, "adapter_model.safetensors")):
                    print("Found adapter model, loading Phi-2 base with adapter")
                    
                    # Check if PEFT is available - using the global variable
                    global PEFT_AVAILABLE
                    if not PEFT_AVAILABLE:
                        print("PEFT not available, installing...")
                        try:
                            import pip
                            pip.main(['install', 'peft'])
                            import peft
                            from peft import PeftModel, PeftConfig
                            PEFT_AVAILABLE = True
                        except Exception as e:
                            print(f"Failed to install PEFT: {e}")
                    
                    # First load base model with quantization
                    base_model = AutoModelForCausalLM.from_pretrained(
                        "microsoft/phi-2",
                        quantization_config=quantization_config,
                        torch_dtype=torch.float16,
                        device_map="auto"
                    )
                    
                    try:
                        # Then load adapter on top
                        self.model = PeftModel.from_pretrained(
                            base_model,
                            self.model_dir,
                            torch_dtype=torch.float16,
                            device_map="auto"
                        )
                        print("Successfully loaded adapter model")
                    except Exception as adapter_error:
                        print(f"Error loading adapter: {adapter_error}")
                        # Fall back to just using the base model
                        print("Falling back to base model without adapter")
                        self.model = base_model
                else:
                    # Load as a standard model if no adapter is found
                    print("Loading model directly from directory")
                    self.model = AutoModelForCausalLM.from_pretrained(
                        self.model_dir,
                        torch_dtype=torch.float16,
                        device_map="auto",
                        quantization_config=quantization_config
                    )
                
            except ImportError as e:
                print(f"Warning: Could not use bitsandbytes quantization, falling back to standard loading: {e}")
                
                # Fallback to standard FP16 loading without quantization
                try:
                    self.model = AutoModelForCausalLM.from_pretrained(
                        self.model_dir,
                        torch_dtype=torch.float16,
                        device_map="auto",
                    )
                except Exception as model_error:
                    print(f"Error loading from model directory: {model_error}")
                    print("Attempting to load base Phi-2 model...")
                    
                    # Final fallback - try loading just the base model
                    self.model = AutoModelForCausalLM.from_pretrained(
                        "microsoft/phi-2",
                        torch_dtype=torch.float16,
                        device_map="auto",
                    )
            
            # Check for Flash Attention support with better error handling
            try:
                # First check if the transformers version supports it
                import importlib
                transformers_version = importlib.import_module('transformers').__version__
                major, minor = map(int, transformers_version.split('.')[:2])
                
                if major > 4 or (major == 4 and minor >= 32):
                    # Flash Attention support was added in transformers 4.32.0
                    try:
                        import flash_attn
                        self.flash_attention_supported = True
                        print(f"Flash Attention {flash_attn.__version__} detected and will be used if available!")
                    except ImportError:
                        print("Flash Attention library not installed. Using standard attention mechanism.")
                        self.flash_attention_supported = False
                else:
                    print(f"Transformers version {transformers_version} doesn't support Flash Attention parameters. Using standard attention.")
                    self.flash_attention_supported = False
            except Exception as e:
                print(f"Error checking Flash Attention support: {e}")
                print("Falling back to standard attention mechanism.")
                self.flash_attention_supported = False
            
            # Enable TF32 precision for higher performance on newer NVIDIA GPUs
            if self.device == "cuda":
                # Only available on Ampere+ GPUs (A100, RTX 3090, etc.)
                try:
                    if torch.cuda.get_device_capability()[0] >= 8:
                        print("Enabling TF32 precision for faster matrix operations")
                        torch.backends.cuda.matmul.allow_tf32 = True
                        torch.backends.cudnn.allow_tf32 = True
                except Exception as e:
                    print(f"Error enabling TF32 precision: {e}")
            
            print(f"Model loaded successfully on {self.device}")
            return True
        except Exception as e:
            print(f"Error loading model: {e}")
            import traceback
            print(traceback.format_exc())
            return False

    def format_candidates_for_prompt(self, candidates: List[Dict[str, Any]]) -> str:
        """Format candidate information for the prompt."""
        candidate_summary = ""
        for i, candidate in enumerate(candidates):
            # Create a concise summary for each candidate
            summary = f"Candidate {i+1}: {candidate.get('name', 'Unnamed')}\n"
            
            if candidate.get('education'):
                edu = candidate['education'][0] if isinstance(candidate['education'], list) and len(candidate['education']) > 0 else candidate['education']
                summary += f"Education: {edu.get('institution', 'Unknown')}, {edu.get('degree', 'Unknown')}\n"
            
            if candidate.get('experience'):
                exp = candidate['experience'][0] if isinstance(candidate['experience'], list) and len(candidate['experience']) > 0 else candidate['experience']
                summary += f"Experience: {exp.get('title', 'Unknown')} at {exp.get('company', 'Unknown')}\n"
            
            if candidate.get('skills'):
                skills = candidate['skills'] if isinstance(candidate['skills'], list) else [candidate['skills']]
                summary += f"Skills: {', '.join(skills[:5])}\n"
            
            candidate_summary += summary + "\n"
        
        return candidate_summary

    def generate_team(self, candidates: List[Dict[str, Any]], requirements: str = None, team_size: int = 5) -> str:
        """Generate a team based on candidates and requirements."""
        if self.model is None or self.tokenizer is None:
            self.load_model()
            if self.model is None:
                return "Model could not be loaded. The AI analysis service is currently unavailable."
        
        try:
            # Format candidate information
            candidate_summary = self.format_candidates_for_prompt(candidates)
            
            # Build the enhanced prompt for more detailed analysis
            prompt = f"""Analyze these candidates and create THREE different optimal startup team compositions of {team_size} people each.

CANDIDATES:
{candidate_summary}

TEAM REQUIREMENTS:
{requirements or "Create a balanced team with complementary skills"}

For EACH team composition, please provide:
1. Team Name: Give this team composition a memorable name based on its strengths
2. Selected Members: List each selected team member with:
   - Their name
   - Recommended role in the team
   - 2-3 sentences on WHY they specifically are valuable to this team composition
   - How they complement other team members

3. Team Analysis (minimum 250 words):
   - Detailed strengths of this specific team combination
   - Potential weaknesses or challenges this team might face
   - Assessment of skill coverage and diversity of thinking
   - Team dynamics and how members would likely work together
   - How this team aligns with the stated requirements

4. Alternative Applications:
   - What type of startup would be MOST successful with this team
   - What type of startup would be LEAST successful with this team

After presenting all three team compositions, provide a final recommendation on which team would be best and why.

Format your response carefully with clear headings and make it comprehensive enough for founders to make informed decisions.
"""

            # Format as chat with improved system prompt
            messages = [
                {"role": "system", "content": "You are an elite startup advisor with deep expertise in team composition and founder dynamics. You specialize in analyzing candidate profiles and determining optimal team compositions that maximize chances of startup success."},
                {"role": "user", "content": prompt}
            ]
            
            # Convert to model input format
            inputs = self.tokenizer.apply_chat_template(
                messages, 
                return_tensors="pt"
            ).to(self.device)
            
            # Generate with proper context limits and attention masks
            with torch.no_grad():
                # Find input length to set appropriate output length
                input_length = inputs.shape[1]
                # Phi-2 has a context limit of 2048
                max_context_length = 2048
                
                # Calculate max new tokens to avoid exceeding model's context limits
                max_new_tokens = max(100, min(1024, max_context_length - input_length))
                
                print(f"Input length: {input_length}, Max new tokens: {max_new_tokens}")
                
                # Create attention mask (explicitly handle padding)
                attention_mask = inputs.ne(self.tokenizer.pad_token_id).long()
                
                # Use the optimized generator instead of direct model.generate call
                outputs, input_length = self.generate_optimized(
                    inputs,
                    attention_mask=attention_mask,
                    max_new_tokens=max_new_tokens
                )
                
            # Decode more carefully
            try:
                # Get only the generated part (exclude input tokens)
                generated_output = outputs[0][input_length:]
                
                # Decode just the new tokens
                generated_text = self.tokenizer.decode(
                    generated_output, 
                    skip_special_tokens=True,
                    clean_up_tokenization_spaces=True
                )
                
                # Remove any model-specific artifacts
                generated_text = generated_text.replace("<|im_end|>", "").replace("<|im_start|>", "")
                assistant_response = generated_text.strip()
                
                # If that failed, try traditional approach
                if not assistant_response:
                    full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
                    assistant_response = full_response.split(prompt)[-1].strip()
            
            except Exception as decode_error:
                print(f"Error decoding response: {decode_error}")
                # Fallback to simpler decoding
                full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
                assistant_response = full_response.split(prompt)[-1].strip()
            
            return assistant_response
        
        except Exception as e:
            import traceback
            print(f"Error generating team: {e}")
            print(traceback.format_exc())
            # Return a user-friendly error message as a string
            return f"We encountered an error while analyzing the team. The AI service may be temporarily unavailable. You can try again later or use the basic team creation features. Error details: {str(e)}"

    def analyze_team(self, team: List[Dict[str, Any]], include_startup_comparison: bool = True) -> str:
        """
        Analyze an existing team and provide insights.
        Args:
            team: List of team members
            include_startup_comparison: Whether to compare to successful startups
        Returns:
            Team analysis as string
        """
        if self.model is None or self.tokenizer is None:
            self.load_model()
            if self.model is None:
                return "Model could not be loaded. The AI analysis service is currently unavailable."
        
        try:
            # Format team information
            team_summary = self.format_candidates_for_prompt(team)
            
            # Build the prompt
            prompt = f"""Analyze this existing startup team in depth:

TEAM MEMBERS:
{team_summary}

Please provide:

1. Team Composition Analysis (minimum 150 words):
   - Overall assessment of the team's strengths and complementary skills
   - Key skill coverage and potential skill gaps
   - Team dynamics and how members would likely work together
   - Potential areas of conflict or collaboration challenges

2. Success Factors (minimum 100 words):
   - What types of startups would be MOST successful with this team
   - Key advantages this team has compared to typical startup teams
   - How team members' backgrounds create competitive advantages

3. Risk Factors (minimum 100 words):
   - What types of startups would be LEAST successful with this team
   - Potential blind spots or weaknesses in the team composition
   - Suggested additions or changes to strengthen the team
"""

            if include_startup_comparison:
                prompt += """
4. Comparison to Successful Startups (minimum 100 words):
   - How this team compares to founding teams of successful startups
   - Historical examples of similar team compositions that succeeded
   - Key differentiating factors from typical successful startup teams
"""

            # Format as chat with improved system prompt
            messages = [
                {"role": "system", "content": "You are an elite startup advisor with deep expertise in team composition and founder dynamics. You specialize in analyzing team profiles and providing actionable insights to maximize chances of startup success."},
                {"role": "user", "content": prompt}
            ]
            
            # Convert to model input format
            inputs = self.tokenizer.apply_chat_template(
                messages, 
                return_tensors="pt"
            ).to(self.device)
            
            # Generate with proper context limits and attention masks
            with torch.no_grad():
                # Find input length to set appropriate output length
                input_length = inputs.shape[1]
                # Phi-2 has a context limit of 2048
                max_context_length = 2048
                
                # Calculate max new tokens to avoid exceeding model's context limits
                max_new_tokens = max(100, min(1024, max_context_length - input_length))
                
                print(f"Team analysis - Input length: {input_length}, Max new tokens: {max_new_tokens}")
                
                # Create attention mask (explicitly handle padding)
                attention_mask = inputs.ne(self.tokenizer.pad_token_id).long()
                
                # Use the optimized generator instead of direct model.generate call
                outputs, input_length = self.generate_optimized(
                    inputs,
                    attention_mask=attention_mask,
                    max_new_tokens=max_new_tokens
                )
                
            # Decode more carefully
            try:
                # Get only the generated part (exclude input tokens)
                generated_output = outputs[0][input_length:]
                
                # Decode just the new tokens
                generated_text = self.tokenizer.decode(
                    generated_output, 
                    skip_special_tokens=True,
                    clean_up_tokenization_spaces=True
                )
                
                # Remove any model-specific artifacts
                generated_text = generated_text.replace("<|im_end|>", "").replace("<|im_start|>", "")
                assistant_response = generated_text.strip()
                
                # If that failed, try traditional approach
                if not assistant_response:
                    full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
                    assistant_response = full_response.split(prompt)[-1].strip()
            
            except Exception as decode_error:
                print(f"Error decoding team analysis response: {decode_error}")
                # Fallback to simpler decoding
                full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
                assistant_response = full_response.split(prompt)[-1].strip()
            
            return assistant_response
        
        except Exception as e:
            import traceback
            print(f"Error analyzing team: {e}")
            print(traceback.format_exc())
            # Return a user-friendly error message as a string
            return f"We encountered an error while analyzing the team. The AI service may be temporarily unavailable. You can try again later or use the basic team features. Error details: {str(e)}"

    def analyze_candidate(self, candidate: Dict[str, Any]) -> str:
        """
        Analyze an individual candidate and provide insights.
        Args:
            candidate: Candidate information
        Returns:
            Candidate analysis as string
        """
        if self.model is None or self.tokenizer is None:
            self.load_model()
            if self.model is None:
                return "Model could not be loaded. The AI analysis service is currently unavailable."
        
        try:
            # Extract candidate information
            name = candidate.get('name', 'Unnamed Candidate')
            
            education_info = ""
            if candidate.get('education'):
                if isinstance(candidate['education'], list):
                    for edu in candidate['education']:
                        education_info += f"- {edu.get('degree', 'Degree')} from {edu.get('institution', 'Institution')}\n"
                else:
                    edu = candidate['education']
                    education_info += f"- {edu.get('degree', 'Degree')} from {edu.get('institution', 'Institution')}\n"
            
            experience_info = ""
            if candidate.get('experience'):
                if isinstance(candidate['experience'], list):
                    for exp in candidate['experience']:
                        experience_info += f"- {exp.get('title', 'Role')} at {exp.get('company', 'Company')}\n"
                else:
                    exp = candidate['experience']
                    experience_info += f"- {exp.get('title', 'Role')} at {exp.get('company', 'Company')}\n"
            
            skills_info = ""
            if candidate.get('skills'):
                skills = candidate['skills'] if isinstance(candidate['skills'], list) else [candidate['skills']]
                skills_info = ", ".join(skills)
            
            # Build comprehensive prompt
            prompt = f"""Analyze this candidate in depth for a startup founder or early employee role:

CANDIDATE PROFILE:
Name: {name}

Education:
{education_info}

Experience:
{experience_info}

Skills:
{skills_info}

Please provide a comprehensive analysis including:

1. Strengths Analysis (minimum 150 words):
   - Key professional strengths based on background and skills
   - Notable accomplishments and their significance
   - Areas of deep expertise and how they apply to startups

2. Founder/Early Employee Fit (minimum 150 words):
   - Assessment of suitability for founder or early employee roles
   - Specific founder archetype this candidate represents
   - Optimal startup stages for this candidate
   - Ideal role recommendations in a startup team

3. Complementary Team Members (minimum 100 words):
   - What types of co-founders or team members would complement this candidate
   - Potential team dynamics when working with different personality types
   - Skills gaps that should be filled by other team members

4. Risk Assessment (minimum 100 words):
   - Potential blind spots or weaknesses based on background
   - Areas where the candidate might need support or development
   - Situations where this candidate might struggle in a startup environment

Format your analysis with clear sections and detailed insights to help assess this candidate for startup roles.
"""

            # Format as chat with system prompt
            messages = [
                {"role": "system", "content": "You are an elite talent assessor specializing in startup founders and early employees. You provide in-depth analysis of candidates' strengths, founder fit, and team compatibility."},
                {"role": "user", "content": prompt}
            ]
            
            # Convert to model input format
            inputs = self.tokenizer.apply_chat_template(
                messages, 
                return_tensors="pt"
            ).to(self.device)
            
            # Generate with proper context limits and attention masks
            with torch.no_grad():
                # Find input length to set appropriate output length
                input_length = inputs.shape[1]
                # Phi-2 has a context limit of 2048
                max_context_length = 2048
                
                # Calculate max new tokens to avoid exceeding model's context limits
                max_new_tokens = max(100, min(1024, max_context_length - input_length))
                
                print(f"Candidate analysis - Input length: {input_length}, Max new tokens: {max_new_tokens}")
                
                # Create attention mask (explicitly handle padding)
                attention_mask = inputs.ne(self.tokenizer.pad_token_id).long()
                
                # Use the optimized generator instead of direct model.generate call
                outputs, input_length = self.generate_optimized(
                    inputs,
                    attention_mask=attention_mask,
                    max_new_tokens=max_new_tokens
                )
                
            # Decode more carefully
            try:
                # Get only the generated part (exclude input tokens)
                generated_output = outputs[0][input_length:]
                
                # Decode just the new tokens
                generated_text = self.tokenizer.decode(
                    generated_output, 
                    skip_special_tokens=True,
                    clean_up_tokenization_spaces=True
                )
                
                # Remove any model-specific artifacts
                generated_text = generated_text.replace("<|im_end|>", "").replace("<|im_start|>", "")
                assistant_response = generated_text.strip()
                
                # If that failed, try traditional approach
                if not assistant_response:
                    full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
                    assistant_response = full_response.split(prompt)[-1].strip()
            
            except Exception as decode_error:
                print(f"Error decoding candidate analysis response: {decode_error}")
                # Fallback to simpler decoding
                full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
                assistant_response = full_response.split(prompt)[-1].strip()
            
            return assistant_response
        
        except Exception as e:
            import traceback
            print(f"Error analyzing candidate: {e}")
            print(traceback.format_exc())
            # Return a user-friendly error message as a string
            return f"We encountered an error while analyzing this candidate. The AI service may be temporarily unavailable. You can try again later. Error details: {str(e)}"

    def parse_natural_language_requirements(self, text: str) -> Dict[str, Any]:
        """
        Parse natural language team requirements into structured format.
        Args:
            text: Natural language description of team requirements
        Returns:
            Dictionary with structured requirements
        """
        if self.model is None or self.tokenizer is None:
            self.load_model()
            if self.model is None:
                return {"error": "Model could not be loaded"}
        
        try:
            # Build parsing prompt
            prompt = f"""Parse the following team requirements into a structured format:

REQUIREMENTS:
{text}

Extract the following information:
1. Required skills (comma-separated list)
2. Required roles (comma-separated list)
3. Industry focus (single industry)
4. Startup stage (idea, mvp, growth, or scaling)
5. Special requirements (comma-separated list)

Format your response as a JSON object with these keys: requiredSkills, requiredRoles, industryFocus, startupStage, specialRequirements
"""

            # Format as chat
            messages = [
                {"role": "system", "content": "You are a helpful assistant that extracts structured information from text."},
                {"role": "user", "content": prompt}
            ]
            
            # Convert to model input format
            inputs = self.tokenizer.apply_chat_template(
                messages, 
                return_tensors="pt"
            ).to(self.device)
            
            # Generate
            with torch.no_grad():
                outputs = self.model.generate(
                    inputs,
                    max_length=1024,
                    temperature=0.2,  # Lower temperature for more deterministic output
                    top_p=0.9,
                )
                
            # Decode
            response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
            
            # Extract the JSON part
            assistant_response = response.split(prompt)[-1].strip()
            
            # Find JSON in the response - look for text between curly braces
            import re
            json_match = re.search(r'\{.*\}', assistant_response, re.DOTALL)
            if json_match:
                json_str = json_match.group(0)
                try:
                    return json.loads(json_str)
                except json.JSONDecodeError:
                    print(f"Error parsing JSON: {json_str}")
                    
            # Fallback to a default structure if JSON parsing fails
            return {
                "requiredSkills": [],
                "requiredRoles": [],
                "industryFocus": "",
                "startupStage": "",
                "specialRequirements": []
            }
            
        except Exception as e:
            print(f"Error parsing requirements: {e}")
            return {
                "requiredSkills": [],
                "requiredRoles": [],
                "industryFocus": "",
                "startupStage": "",
                "specialRequirements": []
            }
    
    def search_candidates_by_description(self, description: str, candidates: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
        """
        Search for candidates matching a natural language description.
        Args:
            description: Natural language description of desired candidate
            candidates: List of candidates to search
        Returns:
            Filtered list of candidates matching the description
        """
        if self.model is None or self.tokenizer is None:
            self.load_model()
            if self.model is None:
                return []
        
        try:
            # Build search prompt
            candidate_summary = self.format_candidates_for_prompt(candidates)
            
            prompt = f"""I'm looking for candidates matching this description: "{description}"

Here are the available candidates:
{candidate_summary}

Return a JSON array containing ONLY the candidate numbers (starting from 1) that best match this description. Format it like this: [1, 4, 7]
"""

            # Format as chat
            messages = [
                {"role": "system", "content": "You are a helpful assistant that finds the best matching candidates based on descriptions."},
                {"role": "user", "content": prompt}
            ]
            
            # Convert to model input format
            inputs = self.tokenizer.apply_chat_template(
                messages, 
                return_tensors="pt"
            ).to(self.device)
            
            # Generate
            with torch.no_grad():
                outputs = self.model.generate(
                    inputs,
                    max_length=1024,
                    temperature=0.3,
                    top_p=0.9,
                )
                
            # Decode
            response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
            
            # Extract the array part
            assistant_response = response.split(prompt)[-1].strip()
            
            # Find array in the response
            import re
            array_match = re.search(r'\[.*\]', assistant_response, re.DOTALL)
            if array_match:
                array_str = array_match.group(0)
                try:
                    indices = json.loads(array_str)
                    # Convert 1-based indices to 0-based
                    indices = [i-1 for i in indices if i > 0 and i <= len(candidates)]
                    return [candidates[i] for i in indices]
                except json.JSONDecodeError:
                    print(f"Error parsing indices: {array_str}")
            
            # Fallback to empty list if parsing fails
            return []
            
        except Exception as e:
            print(f"Error searching candidates: {e}")
            return []

    def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """
        Perform inference on the input data.
        Args:
            data: Dictionary with inputs key containing the operation parameters
        Returns:
            Dictionary with the generated team analysis
        """
        # Extract inputs field if present (standard Hugging Face format)
        if "inputs" in data:
            inputs_data = data["inputs"]
        else:
            inputs_data = data  # Fallback to backward compatibility
            
        # Check for ping operation first - always respond to status checks
        operation = inputs_data.get("operation", "generate_team")
        
        # Special handling for ping - always respond even if model is not loaded
        if operation == "ping":
            model_status = "loaded" if self.model is not None else "not loaded"
            return {
                "status": "ok",
                "model_status": model_status,
                "model_info": {
                    "device": str(self.device),
                    "model_type": "phi-2-qlora-finetuned"
                }
            }
        
        # Try loading the model again if it failed previously
        if self.model is None:
            print("Model wasn't loaded initially, trying to load it now...")
            self.load_model()
        
        # If the model still couldn't be loaded, return an error that the frontend can handle
        if self.model is None:
            # The frontend expects strings for analysis fields, not nested objects
            if operation == "generate_team":
                return {
                    "team_analysis": "Model could not be loaded. The AI analysis service is currently unavailable. We're working to restore it as soon as possible. In the meantime, you can still use the basic team creation features.",
                    "error": "Model could not be loaded, using fallback mode",
                    "fallback": True,
                    "model_info": {
                        "device": str(self.device),
                        "model_type": "phi-2-qlora-finetuned",
                        "status": "failed"
                    }
                }
            elif operation == "analyze_team":
                return {
                    "team_analysis": "Model could not be loaded. The AI analysis service is currently unavailable. We're working to restore it as soon as possible.",
                    "error": "Model could not be loaded, using fallback mode",
                    "fallback": True,
                    "model_info": {
                        "device": str(self.device),
                        "model_type": "phi-2-qlora-finetuned",
                        "status": "failed"
                    }
                }
            elif operation == "analyze_candidate":
                return {
                    "candidate_analysis": "Model could not be loaded. The AI analysis service is currently unavailable. We're working to restore it as soon as possible.",
                    "error": "Model could not be loaded, using fallback mode",
                    "fallback": True,
                    "model_info": {
                        "device": str(self.device),
                        "model_type": "phi-2-qlora-finetuned",
                        "status": "failed"
                    }
                }
            else:
                # For other operations
                return {
                    "error": "Model could not be loaded, using fallback mode",
                    "fallback": True,
                    "model_info": {
                        "device": str(self.device),
                        "model_type": "phi-2-qlora-finetuned",
                        "status": "failed"
                    }
                }
        
        # Continue with normal operation handling
        if operation == "generate_team":
            candidates = inputs_data.get("candidates", [])
            requirements = inputs_data.get("requirements", "")
            team_size = int(inputs_data.get("team_size", 5))
            
            if not candidates:
                return {"error": "No candidates provided"}
            
            team_analysis = self.generate_team(candidates, requirements, team_size)
            
            return {
                "team_analysis": team_analysis,
                "model_info": {
                    "device": str(self.device),
                    "model_type": "phi-2-qlora-finetuned"
                }
            }
            
        elif operation == "analyze_team":
            team = inputs_data.get("team", [])
            include_startup_comparison = inputs_data.get("include_startup_comparison", True)
            
            if not team:
                return {"error": "No team provided"}
            
            team_analysis = self.analyze_team(team, include_startup_comparison)
            
            return {
                "team_analysis": team_analysis,
                "model_info": {
                    "x": str(self.device),
                    "model_type": "phi-2-qlora-finetuned"
                }
            }
            
        elif operation == "analyze_candidate":
            candidate = inputs_data.get("candidate", {})
            
            if not candidate:
                return {"error": "No candidate provided"}
            
            candidate_analysis = self.analyze_candidate(candidate)
            
            return {
                "candidate_analysis": candidate_analysis,
                "model_info": {
                    "device": str(self.device),
                    "model_type": "phi-2-qlora-finetuned"
                }
            }
            
        elif operation == "parse_requirements":
            requirements_text = inputs_data.get("requirements_text", "")
            parsed_requirements = self.parse_natural_language_requirements(requirements_text)
            
            return {
                "parsed_requirements": parsed_requirements,
                "model_info": {
                    "device": str(self.device),
                    "model_type": "phi-2-qlora-finetuned"
                }
            }
            
        elif operation == "search_candidates":
            candidates = inputs_data.get("candidates", [])
            description = inputs_data.get("description", "")
            
            if not candidates:
                return {"error": "No candidates provided"}
                
            if not description:
                return {"error": "No search description provided"}
                
            matching_candidates = self.search_candidates_by_description(description, candidates)
            
            return {
                "matching_candidates": matching_candidates,
                "count": len(matching_candidates),
                "model_info": {
                    "device": str(self.device),
                    "model_type": "phi-2-qlora-finetuned"
                }
            }
            
        # Ping operation is now handled at the beginning for better reliability
            
        else:
            return {"error": f"Unknown operation: {operation}"}