File size: 988 Bytes
24f8f89
c6fae16
 
 
00c8a57
c6fae16
1344c31
107fcf0
00c8a57
107fcf0
c6fae16
107fcf0
00c8a57
c6fae16
107fcf0
 
 
1344c31
 
107fcf0
 
 
c6fae16
 
 
00c8a57
1344c31
107fcf0
 
 
00c8a57
c6fae16
107fcf0
c6fae16
107fcf0
c6fae16
 
 
00c8a57
1344c31
107fcf0
 
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
import torch
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModelForCausalLM

MODEL_ID = "saadkhi/SQL_Chat_finetuned_model"

app = FastAPI()

# ---- LOAD ONCE ONLY ----
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)

model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    dtype=torch.float16,     # use dtype, not torch_dtype
    device_map="auto",
    low_cpu_mem_usage=True
)

model.eval()


class QueryRequest(BaseModel):
    prompt: str
    max_new_tokens: int = 256


@app.post("/generate")
def generate(req: QueryRequest):
    inputs = tokenizer(req.prompt, return_tensors="pt").to(model.device)

    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=req.max_new_tokens,
            do_sample=True,
            temperature=0.7,
            top_p=0.9
        )

    text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return {"response": text}