Tag: Gradio Deployment

  • Deploy Machine Learning Model on Spaces by HuggingFace Using Gradio

    Deploy Machine Learning Model on Spaces by HuggingFace Using Gradio

    Once your Gradio application is ready and tested in the notebook, then the next thing you need to do is deploy it using spaces.

    In this demo example, we will deploy the Titanic model using spaces. You can visit the space here. There are only 4 steps involved –

    1. Create a new space – We will call this space titanic_demo, you can also use paid GPU instances if required.

    2. Create app.py – Here lies the code which runs the Gradio application. Below is the code used to run the space.

    import numpy as np
    import pandas as pd
    import gradio as gr
    from catboost import CatBoostClassifier
    
    clf = CatBoostClassifier()
    clf.load_model("./titanic_model.bin")
    
    def predict(pclass:int = 3,
                sex:str = "male",
                age:float = 30,
                fare:float = 100,
                embarked:str = "S"):
      prediction_array = np.array([pclass, sex, age, fare, embarked])
      survived = clf.predict(prediction_array)
      if survived == 1:
        return f"The passenger survived"
      else:
        return f"The passenger did not survive"
    
    
    
    with gr.Blocks() as demo:
      # Keeping the three categorical feature input in the same row
      with gr.Row() as row1:
        pclass = gr.Dropdown(choices=[1,2,3], label= "pclass")
        sex = gr.Dropdown(choices =["male", "female"], label = "sex")
        embarked = gr.Dropdown(choices =["C", "Q", "S"], label = "embarked")
      # Creating slider for the two numerical inputs and also defining the limits for both
      age = gr.Slider(1,100, label = "age", interactive = True
      )
      fare = gr.Slider(10,600, label = "fare", interactive = True
      )
    
      submit = gr.Button(value = 'Predict')
    
      # Showing the output
      output = gr.Textbox(label = "Whether the passenger survived ?", interactive = False,)
    
      # Defining what happens when the user clicks the submit button
      submit.click(predict, inputs = [pclass,sex, age,fare,embarked], outputs = [output])
    
    demo.launch(share = False, debug = False)

    Remember to make share=False, as in spaces you don’t need to create a shareable link.

    3. Create requirements.txt (optional )- This is optional in case you’re not using packages not pre-loaded into the space. We’re using catboost as the model, so we will specify the requirements.txt file

    4. Add your model file (optional ) – This is again optional as your ML application might not involve loading from a saved model file. Here we’ve stored our titanic model in a bin file, so we add it to the files.

    That’s it. Once you’ve followed these steps your ML model is up and running on Spaces and you don’t have to worry about the link expiring.