Zafar249/Gender_Classification
0
1import gradio as gr2import numpy as np3import joblib4import json5import pandas as pd6 7disclaimer = """8**Disclaimer:** This is a demo/prototype for educational/experimental purposes only. 9Do not rely on this for real-world decisions. The creator is not responsible for any misuse or consequences.10"""11 12def predict_gender(color, genre, beverage, drink):13 # Predicts the gender of a person using the input features 14 15 # Open file in read binary format16 file = open("model.joblib", "rb")17 18 # Load the model19 model = joblib.load(file)20 21 file2 = open("columns.json")22 23 # Load a list of columns used in the training of the model24 training_columns = json.load(file2)25 26 # Create a dataframe using the input features passed as arguments27 test_df = pd.DataFrame([{28 "Favorite Color": color,29 "Favorite Music Genre": genre,30 "Favorite Beverage": beverage,31 "Favorite Soft Drink": drink32 }])33 34 for col in test_df.columns:35 # One-hot encode the categorical variables and convert them to labels36 dummy = pd.get_dummies(test_df[col])37 38 # Concatenate the dummy column with the dataframe39 test_df = pd.concat([test_df, dummy], axis=1)40 41 # Drop the irrelevant column42 test_df.drop(col, axis=1, inplace=True)43 44 # Reshape the dataframe so that it can be inputted into the model45 test_df = test_df.reindex(columns=training_columns, fill_value=0)46 47 # Get a prediction of the gender using the input features and the model48 y_pred = model.predict(test_df)[0]49 50 # If the model predicts gender to be male51 if y_pred:52 y_pred = "Male"53 # Else the gender is female54 else:55 y_pred = "Female"56 57 return y_pred58 59 60# Define a list of inputs61inputs = [62 gr.Radio(63 choices=["Cool", "Neutral", "Warm"],64 label = "Favourite Color?",65 interactive=True66 ),67 gr.Dropdown(68 choices=sorted(["Electronic", "Folk/Traditional", "Pop", "R&B and soul", "Rock", "Hip hop", "Jazz/Blues"]),69 label="Favourite Music?",70 interactive=True71 ),72 gr.Dropdown(73 choices=sorted(["Vodka", "Beer", "Wine", "Whiskey"]) + ["Other", "Doesn't drink"],74 label="Favourite Beverage?",75 interactive=True76 ),77 gr.Radio(78 choices=["Coca Cola/Pepsi", "7UP/Sprite", "Fanta", "Other"],79 label = "Favourite Soft Drink?",80 interactive=True81 )82]83 84# Create a gradio interface85demo = gr.Interface(86 fn = predict_gender, # functio to use87 inputs = inputs,88 outputs = ["text"],89 description=disclaimer90)91 92# Launch the interface93demo.launch()