File size: 843 Bytes
2809bee c815242 2809bee c815242 2809bee c815242 2809bee c815242 2809bee c815242 2809bee 1ad1638 c815242 |
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 |
from fastapi import FastAPI
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import torch
MODEL_NAME = "EQUES/TinyDeepSeek-1.5B"
# メモリ最適化を適用
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
torch_dtype=torch.float16, # メモリ節約のため16bit
device_map="auto", # CPUメモリへ分割割り当て
low_cpu_mem_usage=True # 初期化時のメモリ削減
)
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
app = FastAPI()
@app.get("/")
def root():
return {"message": "TinyDeepSeek API is running!"}
@app.get("/generate")
def generate(prompt: str, max_length: int = 100):
result = generator(prompt, max_length=max_length)[0]['generated_text']
return {"response": result}
|