from transformers import pipeline from fastapi import FastAPI from fastapi import FastAPI from transformers import pipeline # NOTE - we configure docs_url to serve the interactive Docs at the root path # of the app. This way, we can use the docs as a landing page for the app on Spaces. from fastapi import FastAPI from pydantic import BaseModel from transformers import pipeline # You can check any other model in the Hugging Face Hub pipe = pipeline(model="distilbert/distilbert-base-uncased-finetuned-sst-2-english") app = FastAPI(docs_url="/") @app.get("/") def greet_json(): return {"working..."} # We define that we expect our input to be a string class RequestModel(BaseModel): input: str # Now we define that we accept post requests @app.post("/sentiment") def get_response(request: RequestModel): prompt = request.input response = pipe(prompt) label = response[0]["label"] score = response[0]["score"] return f"The '{prompt}' input is {label} with a score of {score}"