Tensor flow application in Hugging Face Spaces Lab

Steps to create a minimal viable product (MVP) TensorFlow application in Hugging Face Spaces:
This example will demonstrate how to create a basic image classification model using TensorFlow and deploy it on Hugging Face Spaces.

Step 1: Setting Up the Environment

First, students need to create an account on Hugging Face and set up a new Space. They can do this by:
Go to the and sign up/log in.
Navigate to "Spaces" and create a new Space.
Choose "TensorFlow" as the library and select a "Gradio" interface for easy interaction.

Step 2: Writing the TensorFlow Code

Here is a simple TensorFlow application for image classification using a pre-trained model:
import tensorflow as tf
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2, preprocess_input, decode_predictions
from PIL import Image
import numpy as np
import gradio as gr

# Load pre-trained MobileNetV2 model
model = MobileNetV2(weights='imagenet')

def classify_image(inp):
inp = inp.reshape((-1, 224, 224, 3))
inp = preprocess_input(inp)
prediction = model.predict(inp).flatten()
return decode_predictions(np.array([prediction]), top=5)[0]

# Define Gradio interface
image_input = gr.inputs.Image(shape=(224, 224))
label_output = gr.outputs.Label(num_top_classes=5)

interface = gr.Interface(fn=classify_image, inputs=image_input, outputs=label_output)

# Launch the application
if __name__ == "__main__":

Step 3: Deploying on Hugging Face Spaces

After writing the code, students can deploy it on Hugging Face Spaces:
Save the above code in a Python file (e.g.,
Add any necessary requirements in a requirements.txt file, like:

tensorflow gradio
Push these files to the repository in their Hugging Face Space.

Step 4: Sharing the Space

Students can share their lab spaces with you:
In their Space, they can go to "Settings" and find the "Collaborators" section.
They can add your Hugging Face username as a collaborator.
This setup provides a simple, yet effective, demonstration of TensorFlow's capabilities integrated with Hugging Face Spaces. It's important for students to understand the model's structure and how it interfaces with Gradio for user interaction. As they progress, they can explore more complex models and custom datasets.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.