Building a FastAPI Application for the Short Story Generator

Introduction to FastAPI and API Development

Welcome to the last lesson on building a FastAPI application for our short story generation service. In previous lessons, we've created essential components such as the PromptManager, StoryManager, and StoryGeneratorService. Now, we'll bring these components together by building a FastAPI application that will serve as the backbone of our service. An API (Application Programming Interface) allows different software components to communicate with each other. In our case, it will enable users to interact with our story generation service through HTTP requests.

FastAPI is a modern, fast (high-performance) web framework for building APIs. It is designed to be easy to use and to help you build robust APIs quickly and efficiently.

Creating a Basic FastAPI Application

Let's start by creating a simple FastAPI application. This will serve as the foundation for our API.

First, import the FastAPI class from the fastapi module and create an instance of it:

from fastapi import FastAPI

app = FastAPI()

Here, app is an instance of the FastAPI class, which represents our web application.

Next, let's define a basic route that will render the homepage:

from fastapi.responses import HTMLResponse

@app.get("/", response_class=HTMLResponse)
async def index():
    return "<h1>Welcome to the Short Story Generator!</h1>"

In this code snippet, @app.get("/") is a decorator that tells FastAPI to execute the index function when the root URL (/) is accessed. The function returns a simple welcome message as HTML.

Implementing API Endpoints

Now, let's implement the API endpoints that will handle story generation and retrieval.

We'll start by creating a POST endpoint to generate stories. This endpoint will accept user input and return a generated story.

from fastapi import Request

@app.post("/api/generate_story")
async def generate_story(request: Request):
    data = await request.json()
    user_input = data.get("user_input")
    return story_generator_controller.generate_story(user_input)
  • @app.post("/api/generate_story"): This decorator defines a route for the /api/generate_story URL, specifying that it accepts POST requests.
  • await request.json(): This line extracts the JSON payload from the request.
  • data.get("user_input"): This retrieves the user_input field from the JSON data.
  • story_generator_controller.generate_story(user_input): This function call generates a story based on the user input.

Next, we'll create a GET endpoint to retrieve all generated stories.

@app.get("/api/get_stories")
async def get_stories():
    return story_generator_controller.get_stories()
  • @app.get("/api/get_stories"): This decorator defines a route for the /api/get_stories URL, specifying that it accepts GET requests.
  • story_generator_controller.get_stories(): This function call retrieves all generated stories.
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal