File size: 1,133 Bytes
11ff83b 7a240a4 11ff83b 7a240a4 1d80ba8 11ff83b 1d80ba8 b34ee0a 11ff83b |
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 |
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import os
# Ensure Hugging Face cache uses a writable path
os.environ["TRANSFORMERS_CACHE"] = "/app/.cache"
os.environ["HF_HOME"] = "/app/.cache"
app = FastAPI()
class ChatRequest(BaseModel):
message: str
# Load DeepSeek model (small one for local use)
model_name = "deepseek-ai/deepseek-coder-1.3b-instruct"
print("Loading model... this may take a minute ⏳")
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
device_map="auto"
)
print("Model loaded ✅")
@app.get("/")
def root():
return {"status": "ok"}
@app.post("/chat")
def chat(request: ChatRequest):
"""Chat endpoint using DeepSeek model"""
inputs = tokenizer(request.message, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=200)
reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
return {"reply": reply}
|