kor-name-gender / app.py
woo-kdis's picture
Update app.py
db935bf verified
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("<h3>Welcome to David's KDI School Machine Learning Project!</h3><h1>Predicting Gender Using <u>Hanja-based</u> Korean Names</h1>")
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("<h3>Acceptable Name Criteria:</h3>" +
"- Typed Strictly in Korean <strong>and</strong> Hanja-based<br />"+
"- Must avoid non-valid name characters (ex. ์€)<br />"+
"- Must not contain the surname<br />"+
"- Must be 3 characters or fewer<br /><br />")
input.submit(process, inputs=input, outputs = xgbmodel)
button.click(process, inputs=input, outputs = xgbmodel)
gr.Markdown("<strong>Valid</strong> Examples: ์ •์‹ฌ, ์žฌํ˜, ์†Œ์˜, ๊ฒฝ์ง„, ํ™”์ค€, ์„ค, ๋œฐ์—๋ด„<br/><strong>Invalid</strong> Examples: ๋คซใ…‡, ์ด์ž๋ฒจ๋ผ, ๊ทธ๋ผ๋ฏธ, ํ™๊ธธ๋™, David")
gr.Markdown("<h3>Other notes</h3>"+
"<i>Non-Hanja and other invalid names will either result in an error or an invalid result.<br />"+
"The closer 'Confidence' is to 50%, the more gender-neutral the name becomes.<br />"+
"This model assumes only the two traditional genders of 'male' and 'female'.</i>")
namegender.launch(share=True)