go00od's picture
Upload app.py with huggingface_hub
2e8c165 verified
raw
history blame
1.57 kB
import gradio as gr
import torch
from model_def import RNN
from name_change import hangul_to_roman, lineToTensor
import torch.nn.functional as F
# ๋ชจ๋ธ ๋กœ๋“œ
RNN(input_size=57, hidden_size=128, output_size=2)
rnn_load = torch.load('rnn_08050114.pt')
rnn_load.eval()
# ์ฃผ์–ด์ง„ ๋ผ์ธ์˜ ์ถœ๋ ฅ ๋ฐ˜ํ™˜
all_categories=['์—ฌ์ž','๋‚จ์ž']
def evaluate(line_tensor):
hidden = rnn_load.initHidden()
for i in range(line_tensor.size()[0]):
output, hidden = rnn_load(line_tensor[i], hidden)
return output
def predict(input_line, n_predictions=2):
print('\n> %s' % input_line)
input_line=hangul_to_roman(input_line)
with torch.no_grad():
output = evaluate(lineToTensor(input_line))
probabilities = F.softmax(output, dim=1)
topv, topi = torch.topk(probabilities, n_predictions)
predictions = [(round(topv[0][i].item(), 2), all_categories[topi[0][i].item()]) for i in range(n_predictions)]
return predictions
def name_classifier(name):
result=predict(name)
print(result)
return {result[0][1]: result[0][0], result[1][1]: result[1][0]}
demo = gr.Interface(
fn=name_classifier,
inputs="text",
outputs="label",
title="ํ•œ๊ตญ์ด๋ฆ„ ์„ฑ๋ณ„ ์˜ˆ์ธก ๋ชจ๋ธ",
description="์ด ๋ชจ๋ธ์€ ์ž…๋ ฅ๋œ ์ด๋ฆ„์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์„ฑ๋ณ„์„ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ์„ฑ์„ ์ œ์™ธํ•œ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜๊ณ  ์˜ˆ์ธก๋œ ์„ฑ๋ณ„๊ณผ ๊ทธ ํ™•๋ฅ ์„ ํ™•์ธํ•˜์„ธ์š”.",
examples=[["ํ•˜์€"], ["ํ˜„๋ฐฐ"], ["์‹œ๊ฒฝ"]]
)
demo.launch()