i use : serve run model:my_app for serving
The used code :
from fastapi import FastAPI
from transformers import pipeline
import ray
from ray import serve
from ray.runtime_env import RuntimeEnv
ray.init(address="ray://localhost:10001",runtime_env=RuntimeEnv( pip={"packages":["transformers", "fastapi"],"pip_version": "==22.0.2;python_version=='3.8.18'"}))
app = FastAPI()
@serve.deployment
@serve.ingress(app)
class SimpleModel:
def __init__(self):
self._classifier = pipeline("sentiment-analysis")
@app.get("/")
def classify(self, input_text: str) -> str:
return self._classifier(input_text)[0]["label"]
my_app = SimpleModel.bind()