import gradio as gr import pandas as pd from jamo import h2j, j2hcj import dill as pickle enc = pickle.load(open('encoder.pkl','rb')) model = pickle.load(open('kornamegender.model','rb')) def process(name): name = list(name) name = name + [None] * (3 - len(name)) p1, p2, p3 = list(), list(), list() p1 = list(j2hcj(h2j(name[0]))) p1 = p1 + [None] * (3 - len(p1)) if name[1] == None: p2 = [None, None, None] else: p2 = list(j2hcj(h2j(name[1]))) p2 = p2 + [None] * (3 - len(p2)) if name[2] == None: p3 = [None, None, None] else: p3 = list(j2hcj(h2j(name[2]))) p3 = p3 + [None] * (3 - len(p3)) p = p1 + p2 + p3 p = pd.DataFrame(p).T en = enc.transform(p) holder = [] if model.predict(en) == 0: holder.append('Male') else: holder.append('Female') holder.append("{0:.3f}%".format(max(model.predict_proba(en)[0])*100)) return (holder[0], holder[1]) with gr.Blocks() as namegender: gr.Markdown("

Welcome to David's KDI School Machine Learning Project!

Predicting Gender Using Hanja-based Korean Names

") gr.Markdown("Type in a name (following the Acceptable Name Criteria) and see what the gender is predicted to be!") with gr.Row(): with gr.Column(): with gr.Row(): input=gr.Textbox(label="Korean Name", placeholder = "Only Korean Names (Ex: 형관)") button = gr.Button("Go!") with gr.Row(): xgbmodel=[gr.Textbox(label = "Gender Prediction"), gr.Textbox(label = "Confidence")] with gr.Column(): gr.Markdown("

Acceptable Name Criteria:

" + "- Typed Strictly in Korean and Hanja-based
"+ "- Must avoid non-valid name characters (ex. 쀏)
"+ "- Must not contain the surname
"+ "- Must be 3 characters or fewer

") input.submit(process, inputs=input, outputs = xgbmodel) button.click(process, inputs=input, outputs = xgbmodel) gr.Markdown("Valid Examples: 정심, 재혁, 소영, 경진, 화준, 설, 뜰에봄
Invalid Examples: 뤫ㅇ, 이자벨라, 그라미, 홍길동, David") gr.Markdown("

Other notes

"+ "Non-Hanja and other invalid names will either result in an error or an invalid result.
"+ "The closer 'Confidence' is to 50%, the more gender-neutral the name becomes.
"+ "This model assumes only the two traditional genders of 'male' and 'female'.
") namegender.launch(share=True)