Welcome to this lesson on building a Flask API for our short story generation service. In previous lessons, we've laid the groundwork by creating components like the PromptManager
, StoryManager
, and StoryGeneratorService
. Now, we'll bring these components together by building a Flask API that will serve as the backbone of our application. An API, or 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.
Before we dive into building our API, let's quickly recall how to set up a Flask environment. If you're working on your own device, you can install Flask using the following command:
However, if you're using the CodeSignal environment, Flask is already pre-installed, so you can focus on writing and testing your code without worrying about setup.
Let's start by creating a simple Flask application. This will serve as the foundation for our API.
First, import the Flask
class from the flask
module and create an instance of it:
Here, app
is an instance of the Flask
class, which represents our web application. The __name__
argument helps Flask determine the root path of the application.
Next, let's define a basic route that will render the homepage:
In this code snippet, @app.route('/')
is a decorator that tells Flask to execute the index
function when the root URL (/
) is accessed. The function returns a simple welcome message.
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.
@app.route('/api/generate_story', methods=['POST'])
: This decorator defines a route for the/api/generate_story
URL, specifying that it accepts POST requests.request.json.get('user_input')
: This line extracts theuser_input
from the JSON payload of the request.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.route('/api/get_stories', methods=['GET'])
: 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.
The StoryGeneratorController
is a crucial component that manages story generation and retrieval. Let's integrate it into our Flask application.
First, import the StoryGeneratorController
and create an instance of it:
Now, use this instance within the API endpoints to handle story generation and retrieval, as shown in the previous section. This integration allows our Flask app to leverage the functionality provided by the StoryGeneratorController
.
To run the Flask application, use the following command in your terminal:
This will start the Flask development server, and you should see output indicating that the server is running.
To test the API endpoints, you can use tools like Postman or curl
. Here's an example of how to test the /api/generate_story
endpoint using curl
:
This command sends a POST request with JSON data to the /api/generate_story
endpoint. You should receive a response containing a generated story.
In this lesson, we've built a Flask API for our short story generation service. We covered the creation of a basic Flask application, implemented API endpoints for generating and retrieving stories, and integrated the StoryGeneratorController
. You also learned how to run and test the application locally.
As you move on to the practice exercises, you'll have the opportunity to reinforce your understanding of building a Flask API. Feel free to experiment with modifying the API and adding new features. Congratulations on reaching this point in the course, and keep up the great work!
