import gradio as gr import pickle import numpy as np import seaborn as sns import matplotlib.pyplot as plt with open('iris_knn.pkl', 'rb') as file: model = pickle.load(file) df = sns.load_dataset('penguins') island_map = {'Torgersen':1, 'Biscoe':2, 'Dream':3} sex_map = {'Male':0, 'Female':1} species_map = {'Adelie':1, 'Chinstrap':2, 'Gentoo':3} reverse_dict = lambda dict_: {v:k for k,v in dict_.items()} def pred_fn(island, bill_length, bill_depth, flipper_length, body_mass, sex): try: # Convert inputs to numeric values island_num = island_map[island] sex_num = sex_map[sex] bill_length_num = float(bill_length) bill_depth_num = float(bill_depth) flipper_length_num = float(flipper_length) body_mass_num = float(body_mass) # Create the input array for the model inputs = np.array([island_num, bill_length_num, bill_depth_num, flipper_length_num, body_mass_num, sex_num]).reshape(1, -1) # Make the prediction pred = model.predict(inputs)[0] # Convert the prediction back to species name species = reverse_dict(species_map)[pred] sns.scatterplot(df, x='bill_length_mm', y='bill_depth_mm', hue='species') plt.scatter(x=bill_length_num, y=bill_depth_num, color='r', label='new instance') plt.legend() plot_path = 'plots/scatter.png' plt.savefig(plot_path) plt.close() return species, plot_path except: return "Invalid Inputs", "plots/pls.gif" inputs = [ gr.Dropdown(choices=list(island_map.keys()), label='Island'), gr.Textbox(label="Bill Length"), gr.Textbox(label="Bill Depth"), gr.Textbox(label="Flipper Length"), gr.Textbox(label="Body Mass"), gr.Dropdown(choices=list(sex_map.keys()), label="Sex") ] output = [gr.Textbox(label='Species'), gr.Image(label='Scatter Plot')] iface = gr.Interface(fn=pred_fn, inputs=inputs, outputs=output, title="Penguin Prediction") iface.launch()