abalone / app.py
robitalhazmi's picture
Add app file
848b00d
import streamlit as st
import pandas as pd
import numpy as np
import pickle
# Load the pre-trained model
with open('abalone_model.pkl', 'rb') as file:
model = pickle.load(file)
# Define the prediction function
def predict_age(sex, length, diameter, height, whole_weight, shucked_weight, viscera_weight, shell_weight):
shell_volume = length * diameter * height
meat_ratio = whole_weight / shell_weight
bmi = whole_weight / (length ** 2)
shell_surface_area = 2 * (length * diameter + length * height + diameter * height)
volume_to_weight_ratio = shell_volume / whole_weight
shell_weight_to_length_ratio = shell_weight / length
meat_weight_to_lenth_ratio = whole_weight / length
features = [[
sex,
length,
diameter,
height,
whole_weight,
shucked_weight,
viscera_weight,
shell_weight,
shell_volume,
meat_ratio,
bmi,
shell_surface_area,
volume_to_weight_ratio,
shell_weight_to_length_ratio,
meat_weight_to_lenth_ratio
]]
columns = [
'Sex',
'Length',
'Diameter',
'Height',
'Whole_weight',
'Shucked_weight',
'Viscera_weight',
'Shell_weight',
'Shell_Volume',
'Meat_Ratio',
'BMI',
'Shell_Surface_Area',
'Volume_to Weigh_Ratio',
'Shell_Weight_to Length_Ratio',
'Mea_Weight_to_Length Ratio'
]
features_df = pd.DataFrame(data=features, columns=columns)
prediction = model.predict(features_df)
age = prediction[0] + 1.5
return age
# Streamlit app
st.title('Abalone Age Predictor')
st.write("Enter the physical measurements of the abalone to predict its age.")
# Input fields
sex = st.selectbox('Sex', ['M', 'F', 'I'])
length = st.number_input('Length (mm)', min_value=0.0, value=0.0)
diameter = st.number_input('Diameter (mm)', min_value=0.0, value=0.0)
height = st.number_input('Height (mm)', min_value=0.0, value=0.0)
whole_weight = st.number_input('Whole Weight (grams)', min_value=0.0, value=0.0)
shucked_weight = st.number_input('Shucked Weight (grams)', min_value=0.0, value=0.0)
viscera_weight = st.number_input('Viscera Weight (grams)', min_value=0.0, value=0.0)
shell_weight = st.number_input('Shell Weight (grams)', min_value=0.0, value=0.0)
# Predict button
if st.button('Predict Age'):
try:
# Check for valid input values
if length <= 0 or diameter <= 0 or height <= 0 or whole_weight <= 0 or shucked_weight <= 0 or viscera_weight <= 0 or shell_weight <= 0:
st.error("All input values must be greater than zero.")
else:
age = predict_age(sex, length, diameter, height, whole_weight, shucked_weight, viscera_weight, shell_weight)
st.write(f'The predicted age of the abalone is: {age:.2f} years')
except Exception as e:
st.error(f"An error occurred: {e}")