Close Menu
    Trending
    • Cognitive Stretching in AI: How Specific Prompts Change Language Model Response Patterns | by Response Lab | Jun, 2025
    • Recogni and DataVolt Partner on Energy-Efficient AI Cloud Infrastructure
    • What I Learned From my First Major Crisis as a CEO
    • Vision Transformer on a Budget
    • Think You Know AI? Nexus Reveals What Everyone Should Really Know | by Thiruvarudselvam suthesan | Jun, 2025
    • How Cloud Innovations Empower Hospitality Professionals
    • Disney Is Laying Off Hundreds of Workers Globally
    • LLMs + Pandas: How I Use Generative AI to Generate Pandas DataFrame Summaries
    Finance StarGate
    • Home
    • Artificial Intelligence
    • AI Technology
    • Data Science
    • Machine Learning
    • Finance
    • Passive Income
    Finance StarGate
    Home»Machine Learning»How to use Fast API to deploy your NLP project | by Panayiotis | Apr, 2025
    Machine Learning

    How to use Fast API to deploy your NLP project | by Panayiotis | Apr, 2025

    FinanceStarGateBy FinanceStarGateApril 8, 2025No Comments5 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Picture by Aaron McLean on Unsplash

    On the subject of deploying your mannequin, it may be tough in some cases usually being a time-consuming step. Nevertheless, FastAPI is a framework that enables anybody to simply construct API’s with Python.

    On this 5 minute tutorial, we’ll stroll via on how one can take a useful NLP pipeline and deploy it as an internet API in order that it’s usable as a HTTP endpoint making it straightforward to include in any web site.

    There are a number of comparable Python frameworks on the market resembling Flask, Django, Twister — so why use FastAPI?

    FastAPI is asynchronous which means that it is extremely quick, it additionally gives computerized request validations utilizing Pydantic fashions which means that it checks whether or not the information matches the anticipated format. Most significantly, it’s light-weight, straightforward to configure and to mess around with making it excellent for small-scale tasks.

    Let’s assume you’ve acquired the mannequin, it may be something however in our occasion it’ll be a sentiment evaluation pipeline utilizing a transformer mannequin from HuggingFace.

    # Imports
    from helper_functions import video_functions, predict_functions, translate_functions
    import pandas as pd

    # ---------------------------------------------------------------------------------------------------------------------------------------
    # Load the video
    # ---------------------------------------------------------------------------------------------------------------------------------------

    # Load the video and convert to audio
    samples, sample_rate = video_functions.youtube_converter('YouTube Hyperlink goes right here')

    print('Video loaded')

    # ---------------------------------------------------------------------------------------------------------------------------------------
    # Transcribe, language between 'el' and 'en' or None for auto-detect and mannequin choice
    # ---------------------------------------------------------------------------------------------------------------------------------------

    print('Starting transcription')

    # Transcribe the audio
    transcription_df = video_functions.transcribe_and_save_from_array(samples, sample_rate=44100, chunk_length=30, language=None, mannequin="large-v3-turbo")

    # Drop nan's
    transcription_df = transcription_df.dropna(subset=['Sentence'])

    print('Audio Transcribed')

    # ---------------------------------------------------------------------------------------------------------------------------------------
    # Predict
    # ---------------------------------------------------------------------------------------------------------------------------------------

    print(transcription_df.head(10))

    print('Starting emotion prediction')

    # Predict the dataset
    emotion_df = predict_functions.predict_dataset(transcription_df, 'Sentence',
    api_key="API Key would go right here...",
    model_dir=r"emotion_classifier_model_v10.7_Bilingual")

    print('Feelings predicted')

    Now that we’ve got the pipeline, I wish to deploy it, however how do I try this?

    Let’s first set up FastAPI:

    pip set up "fastapi[standard]"

    You’ll additionally want uvicorn which permits server manufacturing:

    pip set up uvicorn

    Now that we’ve got the packages, we have to make just a few modifications on our pipeline in addition to what it returns in order that it’s despatched to our front-end web site which is usually a React.JS web site.

    Let’s start by making some changes:

    # Imports
    from helper_functions import video_functions, predict_functions, translate_functions
    import pandas as pd

    # Outline the imports for fastapi
    from fastapi import FastAPI, HTTPException

    # We will use this to validate the information inputted
    from pydantic import BaseModel

    # ---------------------------------------------------------------------------------------------------------------------------------------
    # Load the video
    # ---------------------------------------------------------------------------------------------------------------------------------------

    # Assign the hyperlink variable
    hyperlink = youtube_link.hyperlink
    print('YouTube hyperlink:', hyperlink)

    # Outline our app
    app = FastAPI()

    # App CORS configurations
    app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], # Permits for requests from any origin, you will have to limit this to your area finally.
    allow_credentials=True, # Permits credentials like cookies.
    allow_methods=["GET", "POST", "OPTIONS"], # Specifies which HTTP strategies are allowed.
    allow_headers=["Content-Type","Set-Cookie"], # Specifices which HTTP headers can be utilized.

    )

    # Create a youtube hyperlink class
    class YoutubeLink(BaseModel):
    hyperlink: str

    # Now we wrap the pipeline right into a FastAPI route handler
    @app.submit("/course of/")
    async def process_video(youtube_link: YoutubeLink):
    strive:
    # Load the video and convert to audio, modified to the hyperlink assigment
    samples, sample_rate = video_functions.youtube_converter(hyperlink)

    print('Video loaded')

    # ---------------------------------------------------------------------------------------------------------------------------------------
    # Transcribe, language between 'el' and 'en' or None for auto-detect and mannequin choice
    # ---------------------------------------------------------------------------------------------------------------------------------------

    print('Starting transcription')

    # Transcribe the audio
    transcription_df = video_functions.transcribe_and_save_from_array(samples, sample_rate=44100, chunk_length=30, language=None, mannequin="large-v3-turbo")

    # Drop nan's
    transcription_df = transcription_df.dropna(subset=['Sentence'])

    print('Audio Transcribed')

    # ---------------------------------------------------------------------------------------------------------------------------------------
    # Predict
    # ---------------------------------------------------------------------------------------------------------------------------------------

    # Confirm pipeline course of
    print(transcription_df.head(10))

    print('Starting emotion prediction')

    # Predict the dataset
    emotion_df = predict_functions.predict_dataset(transcription_df, 'Sentence',
    api_key="API Key would go right here...",
    model_dir=r"emotion_classifier_model_v10.7_Billingual")

    print('Feelings predicted')

    # Now we return a JSON which will likely be used to move to the web site
    consequence = emotion_df.to_dict(orient='information')
    return {"standing": "success", "knowledge": consequence}

    # Return error 500
    besides Exception as e:
    increase HTTPException(status_code=500, element=str(e))

    Let’s undergo the adjustments we made, first we put in ‘fastapi’ in addition to ‘uvicorn’ in order that server manufacturing is allowed, we then imported our new packages and wrote our hyperlink variable the place it takes in YouTube hyperlinks. We use ‘pydantic’ to test the enter knowledge in a category after which we wrap your complete pipeline into route handler the place your complete pipeline course of happens. We additionally must outline the CORS app configurations the place we will change just a few parameters. It is strongly recommended that you simply change the origin when you deploy to your area as a result of on this occasion, it’ll settle for requests from any origin.

    The remainder is sort of easy and yow will discover extra about what configurations you possibly can implement at FastAPI’s documentation web site. As soon as the pipeline finishes, it should return a JSON file which can be utilized to show knowledge in a dashboard for instance.

    To run the pipeline regionally, you possibly can merely execute this command in your anaconda terminal. Be sure you are additionally working your appropriate atmosphere.

    uvicorn principal:app --reload

    If you could really deploy, there are a number of different companies which offer cloud computing and simple internet hosting resembling:

    • Render
    • DigitalOcean
    • Railway
    • Amazon Net Internet hosting

    These additionally embrace free internet hosting tiers however might not fit your computational energy wants…

    The whole course of is pretty easy, this tutorial is aimed toward implementing a easy pipeline. This could possibly be used to construct portfolio’s and even dashboard web sites to showcase knowledge utilizing totally different statistical elements.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleWhy Enterprises Need a Comprehensive Data Modernization Strategy Now
    Next Article Avoid Burnout by Rethinking the 30,000 Daily Decisions You Make
    FinanceStarGate

    Related Posts

    Machine Learning

    Cognitive Stretching in AI: How Specific Prompts Change Language Model Response Patterns | by Response Lab | Jun, 2025

    June 3, 2025
    Machine Learning

    Think You Know AI? Nexus Reveals What Everyone Should Really Know | by Thiruvarudselvam suthesan | Jun, 2025

    June 3, 2025
    Machine Learning

    Genel Yapay Zeka Eşiği. Analitik düşünme yapımızı, insani… | by Yucel | Jun, 2025

    June 2, 2025
    Add A Comment

    Comments are closed.

    Top Posts

    Chapter 4: The Watchers, the Vigils, and The Will | by David Samuel Joy | Apr, 2025

    April 10, 2025

    This benchmark used Reddit’s AITA to test how much AI models suck up to us

    May 30, 2025

    Winsorization: A Simple and Effective Way to Handle Outliers in Your Data | by Sugavasilakshmisahithi | Feb, 2025

    February 23, 2025

    How to Make Your Business Look Bigger Than It Is

    March 12, 2025

    The Power of Data Science: Shaping the Future Across Industries and Technologies | by Kasa | Apr, 2025

    April 12, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Data Science
    • Finance
    • Machine Learning
    • Passive Income
    Most Popular

    The Medical Engine Paving the Way for a New Era of Healthcare | by Eke Obong | Feb, 2025

    February 2, 2025

    6 Simple Steps to Revamp Your To-Do List in Just 30 Minutes

    April 15, 2025

    The Intelligent Relay: How Agentic AI and RPA are Reinventing the Supply Chain | by Vikas Kulhari | May, 2025

    May 9, 2025
    Our Picks

    Do European M&Ms Actually Taste Better than American M&Ms?

    February 22, 2025

    Pay it Forward this International Women’s Day — Here’s What I Look for as a CEO to Uplift More Women

    March 8, 2025

    The Great Workforce Reconfiguration: Navigating Career Security in the Age of Intelligent Automation | by Toni Maxx | May, 2025

    May 23, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Data Science
    • Finance
    • Machine Learning
    • Passive Income
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 Financestargate.com All Rights Reserved.

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