import pandas as pd
import numpy as np
# Make numpy values easier to read.
np.set_printoptions(precision=3, suppress=True)
import tensorflow as tf
from tensorflow.keras import layers
abalone_train = pd.read_csv(
"https://storage.googleapis.com/download.tensorflow.org/data/abalone_train.csv",
names=["Length", "Diameter", "Height", "Whole weight", "Shucked weight",
"Viscera weight", "Shell weight", "Age"])
abalone_train.head()
abalone_features = abalone_train.copy()
abalone_labels = abalone_features.pop('Age')
abalone_features = np.array(abalone_features)
abalone_features
abalone_model = tf.keras.Sequential()
abalone_model.add(layers.Dense(64))
abalone_model.add(layers.Dense(32))
abalone_model.add(layers.Dense(1))
abalone_model.compile(optimizer=tf.optimizers.Adam(), loss=tf.losses.MeanSquaredError())
abalone_model.fit(abalone_features, abalone_labels, epochs=100)
abalone_model.summary()
abalone_test = np.array([[0.545,0.130,0.120,0.3515,0.1145,0.0665,0.1600]])
abalone_test
def predict_age_test(in_data):
if in_data is not None:
abalone_age = abalone_model.predict(in_data)
else:
print("The input is empty...")
return abalone_age
print("The age is " + str(predict_age_test(abalone_test)[[0]])+ "years")
#install gradio
# if you are running on a local machine then only once.
# if running on colab then every single time you reset or open the notebook.
! pip install gradio
import gradio as gr
# a wrapper function builds input and output for the model
def predict_age(f1,f2,f3,f4,f5,f6,f7):
input_feature = np.array([[f1,f2,f3,f4,f5,f6,f7]])
if input_feature is not None:
abalone_age = abalone_model.predict(input_feature)
else:
pass
return "The approx age of the abalone is" + str(abalone_age[[0]])
#Build inputs slider optins
inputs_app = [gr.inputs.Slider(0,1, step=0.001, label='Length', default=0.545),
gr.inputs.Slider(0,1, step=0.001, label='Diameter', default=0.13),
gr.inputs.Slider(0,1, step=0.001, label='Height', default=0.12),
gr.inputs.Slider(0,1, step=0.001, label='Whole weight', default=0.351),
gr.inputs.Slider(0,1, step=0.001, label='Stucked weight', default=0.115),
gr.inputs.Slider(0,1, step=0.001, label='Viscera weight', default=0.067),
gr.inputs.Slider(0,1, step=0.001, label='Shell weight', default=0.16)]
#Build output
outputs_app = ["text"]
#Build the interface
age_predictor_app = gr.Interface(fn=predict_age,
inputs=inputs_app,
outputs=outputs_app,
live=True,
theme="dark-peach",
description="Enter parameters using sliders provided to predict the age of abalone."
)
#Launch the app
age_predictor_app.launch(share=True)