File size: 1,264 Bytes
f0a5521
738974d
f0a5521
 
 
 
 
 
 
 
738974d
 
f0a5521
738974d
 
 
 
 
 
 
 
 
f0a5521
738974d
f0a5521
738974d
f0a5521
 
 
738974d
f0a5521
 
 
738974d
 
f0a5521
 
 
 
 
738974d
f0a5521
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
from fastapi import FastAPI, HTTPException
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import PeftModel
import torch

app = FastAPI()

@app.on_event("startup")
async def load_model():
    try:
        # 4-bit config
        bnb_config = BitsAndBytesConfig(
            load_in_4bit=True,
            bnb_4bit_quant_type="nf4",
            bnb_4bit_compute_dtype=torch.float16,
            bnb_4bit_use_double_quant=True,
        )

        # Load base model
        app.state.base_model = AutoModelForCausalLM.from_pretrained(
            "unsloth/deepseek-r1-distill-llama-8b-unsloth-bnb-4bit",
            quantization_config=bnb_config,
            device_map="auto",
            trust_remote_code=True
        )

        # Attach PEFT adapter
        app.state.model = PeftModel.from_pretrained(
            app.state.base_model,
            "LAWSA07/medical_fine_tuned_deepseekR1"
        )
        
        # Load tokenizer
        app.state.tokenizer = AutoTokenizer.from_pretrained(
            "unsloth/deepseek-r1-distill-llama-8b-unsloth-bnb-4bit"
        )

    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"Model loading failed: {str(e)}"
        )