Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from model_def import RNN
|
4 |
+
from name_change import hangul_to_roman, lineToTensor
|
5 |
+
import torch.nn.functional as F
|
6 |
+
|
7 |
+
|
8 |
+
# ๋ชจ๋ธ ๋ก๋
|
9 |
+
RNN(input_size=57, hidden_size=128, output_size=2)
|
10 |
+
rnn_load = torch.load('rnn_08050114.pt')
|
11 |
+
rnn_load.eval()
|
12 |
+
|
13 |
+
# ์ฃผ์ด์ง ๋ผ์ธ์ ์ถ๋ ฅ ๋ฐํ
|
14 |
+
all_categories=['์ฌ์','๋จ์']
|
15 |
+
|
16 |
+
def evaluate(line_tensor):
|
17 |
+
hidden = rnn_load.initHidden()
|
18 |
+
|
19 |
+
for i in range(line_tensor.size()[0]):
|
20 |
+
output, hidden = rnn_load(line_tensor[i], hidden)
|
21 |
+
|
22 |
+
return output
|
23 |
+
|
24 |
+
def predict(input_line, n_predictions=2):
|
25 |
+
print('\n> %s' % input_line)
|
26 |
+
input_line=hangul_to_roman(input_line)
|
27 |
+
with torch.no_grad():
|
28 |
+
output = evaluate(lineToTensor(input_line))
|
29 |
+
|
30 |
+
probabilities = F.softmax(output, dim=1)
|
31 |
+
topv, topi = torch.topk(probabilities, n_predictions)
|
32 |
+
predictions = [(round(topv[0][i].item(), 2), all_categories[topi[0][i].item()]) for i in range(n_predictions)]
|
33 |
+
return predictions
|
34 |
+
|
35 |
+
def name_classifier(name):
|
36 |
+
result=predict(name)
|
37 |
+
print(result)
|
38 |
+
return {result[0][1]: result[0][0], result[1][1]: result[1][0]}
|
39 |
+
|
40 |
+
|
41 |
+
demo = gr.Interface(
|
42 |
+
fn=name_classifier,
|
43 |
+
inputs="text",
|
44 |
+
outputs="label",
|
45 |
+
title="ํ๊ตญ์ด๋ฆ ์ฑ๋ณ ์์ธก ๋ชจ๋ธ",
|
46 |
+
description="์ด ๋ชจ๋ธ์ ์
๋ ฅ๋ ์ด๋ฆ์ ๊ธฐ๋ฐ์ผ๋ก ์ฑ๋ณ์ ์์ธกํฉ๋๋ค. ์ฑ์ ์ ์ธํ ์ด๋ฆ์ ์
๋ ฅํ๊ณ ์์ธก๋ ์ฑ๋ณ๊ณผ ๊ทธ ํ๋ฅ ์ ํ์ธํ์ธ์.",
|
47 |
+
examples=[["ํ์"], ["ํ๋ฐฐ"], ["์๊ฒฝ"]]
|
48 |
+
)
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
demo.launch()
|
53 |
+
|