File size: 1,987 Bytes
5e108cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import torch
import torch.nn.functional as F
import string
import gradio as gr

all_letters = string.ascii_letters + " .,;'"
n_letters = len(all_letters)

all_categories = ['Arabic','Chinese','Czech','Dutch','English','French','German','Greek',
                'Irish','Italian','Japanese','Korean','Polish','Portuguese','Russian','Scottish',
                'Spanish','Vietnamese']
                
# Find letter index from all_letters: Ex: "a" = 0
def letterToIndex(letter):
    return all_letters.find(letter)
    
# Giving each charachter in name a one hot vector
def lineToTensor(line):
    tensor = torch.zeros(len(line),1,n_letters)
    for li,letter in enumerate(line):
        tensor[li][0][letterToIndex(letter)] = 1

    return tensor 
    
# Loading in torchscript model
my_model = torch.jit.load('name_classifier_ts.ptl')

# Return output given a line_tensor
def evaluate(line_tensor):
    hidden = torch.zeros(1,128)

    for i in range(line_tensor.size()[0]):
        output, hidden = my_model(line_tensor[i], hidden)

    return output
    
# Feeding in a name and number of top predictions you want to output
def predict(last_name,n_predictions=3):
    
    last_name = last_name.title()
    with torch.no_grad():
        output = evaluate(lineToTensor(last_name))
        output = F.softmax(output,dim=1)

        topv,topi = output.topk(n_predictions,1,True)
        
        top_3_countries = ''
        for i in range(n_predictions):
            value = topv[0]
            category_index = topi[0][i].item()
            top_3_countries += f'{all_categories[category_index]}: {round(value[i].item()*100,2)}%'
            top_3_countries += '\n'
    return top_3_countries

demo = gr.Interface(predict,
                        inputs = "text", 
                        outputs = "text", 
                        description="Classify name into language of origin. Returns top 3 languages of origin"
                    )
                    
demo.launch(inline=False)