The AI Book
    Facebook Twitter Instagram
    The AI BookThe AI Book
    • Home
    • Categories
      • AI Media Processing
      • AI Language processing (NLP)
      • AI Marketing
      • AI Business Applications
    • Guides
    • Contact
    Subscribe
    Facebook Twitter Instagram
    The AI Book
    AI Media Processing

    FastAPI and Streamlit: The Python Duo You Should Know About | by Paul Justin | June 2023

    13 June 2023No Comments3 Mins Read

    [ad_1]

    FastAPI Backend

    FastAPI backend overview [Video by the Author].

    As a reminder, the FastAPI code can be found below app-api/api.

    Step 1: Create a FastAPI application where we have configured documents, CORS middleware and root API router for endpoints.

    Step 2: Define the parameter class. The scope of this class is to store all the constants and configurations you need in your API code, such as:

    • General configurations: port, log level or version,
    • GCP Certificates: The bucket name or path to the JSON service account keys.

    You will use the Settings object in the project using get_settings() function.

    Also, inside configuration In class, we programmed FastAPI to find a .Envy Enter the current directory and load all variables with the prefix APP_API_.

    as you can see .env.default file, all variables are initialized APP_API_.

    A screenshot of the .env.default file [Image by the Author].

    Step 3: Define API data schemas using Pydantic. These schemes encode or decode data from JSON to Python objects or vice versa. Also, they check the type and structure of your JSON object based on your defined data model.

    When defining a Pydantic BaseModel, it is necessary to add a type to all variables that will be used in the validation step.

    Step 4: Define your endpoints with a web language known as views. Typically, a view has access to some data store and, upon request, returns a subset of the data source to the requester.

    Thus, the standard flow for retrieving data (aka GET request) looks like this:

    “Client → Request data → Endpoint → Access data store → Encrypt to pedant schema → Decrypt to JSON → Respond with requested data”

    Let’s see how we define an endpoint to get all user types:

    we used “gcsfs.GCSFileSystem” To access the GCS bucket as in a standard file system.

    We attached the last point api_router.

    by using api_router.get() Python decorator, we attached the main function /user_type_values final point.

    In the above example, when calling “https://:8001/api/v1/consumer_type_values” The user_type_values() The function will be invoked and the endpoint response will be strictly based on what the function returns.

    Another important thing to emphasize is that by definition answer_model (aka schema) in the Python decorator, You don’t need to create a pedantic diagram outright.

    If you return a dictionary that is 1:1 given the schema structure, FastAPI will automatically create a pedant object for you.

    He is. Now we will repeat the same logic to define the rest of the endpoints. FastAPI makes everything so easy and intuitive for you.

    Now let’s look at the whole views.py The file where we defined the endpoints for:

    • /health → Health check
    • /user_type_values → Get all possible user types
    • /area_values → Get all possible terrain types
    • /predictions/area/consumer_type → Get forecasts for a given area and user type. Note that syntax, you can add parameters to your endpoint — FastAPI Docs [2].
    • /monitoring/metrics → Get overall monitoring metrics
    • /monitoring/values/area/consumer_type → Get monitoring values ​​for a given area and user type

    I want to emphasize again that the FastAPI backend only reads GCS bucket predictions. The inference step is done only in the image prediction pipeline.

    you can also go”http://:8001/api/v1/docs“ To access the API’s Swagger docs where you can easily view and test all your endpoints:

    Screenshot of the Swapper API docs [Image by the Author].

    He is! Now you know how to build a FastAPI backend. Things can get more complicated when you add a database layer and user sessions, but you’ve learned all the basic concepts that will help!

    [ad_2]

    Source link

    Previous ArticleCreate a time series ratio analysis dashboard
    Next Article Nvidia teams with Hexagon to transform industrial digital twins
    The AI Book

    Related Posts

    AI Media Processing

    A new set of Arctic images will help artificial intelligence research MIT News

    25 July 2023
    AI Media Processing

    Analyzing rodent infestations using the geospatial capabilities of Amazon SageMaker

    24 July 2023
    AI Media Processing

    Using knowledge of social context for responsible use of artificial intelligence – Google Research Blog

    23 July 2023
    Add A Comment

    Leave A Reply Cancel Reply

    • Privacy Policy
    • Terms and Conditions
    • About Us
    • Contact Form
    © 2025 The AI Book.

    Type above and press Enter to search. Press Esc to cancel.