1. Learn
  2. /
  3. Courses
  4. /
  5. Deploying AI into Production with FastAPI

Connected

Exercise

Returning structured output from API

You're building a content moderation system where you need to define a POST endpoint to test a pre-trained sentiment analysis model on user comments.

You need to create an endpoint that leverages pydantic models to return predictions in a structured format.

Note: Pydantic models - CommentRequest and CommentResponse are already created for you to use along with the pre-trained sentiment_model from pre-defined SentimentAnalyzer class.

Instructions

100 XP
  • Implement a POST endpoint at route /analyze.
  • Validate the request in the analyze_comment() function as per the CommentRequest.
  • Make predictions using sentiment_model by passing the request's text.
  • Return the prediction attributes (text from request, "label" and "score" from result[0]) to format the outgoing response in CommentResponse.