go00od commited on
Commit
2e8c165
ยท
verified ยท
1 Parent(s): 19c6e2e

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +53 -0
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
+