File size: 2,680 Bytes
afe86b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import nltk
nltk.download('punkt')
from nltk.tokenize import sent_tokenize
import streamlit as st

def load_model(model_id):
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    model = AutoModelForCausalLM.from_pretrained(model_id)
    return tokenizer, model

model_id = "asi/gpt-fr-cased-small"
tokenizer_fr, model_fr = load_model(model_id)

model_id = "gpt2"
tokenizer_en, model_en = load_model(model_id)

model_id = "dbmdz/german-gpt2"
tokenizer_de, model_de = load_model(model_id)

with st.form(key='Form'):
    text = st.text_area("Enter text here.")
    option = st.selectbox('Select Language',('English', 'German', 'French'))
    submitted = st.form_submit_button("Submit")

if submitted:
    text = text.replace('\n', '')
    
    with torch.no_grad():
        if option == 'German':
            encodings = tokenizer_de(text, return_tensors="pt")
            input_ids = encodings.input_ids
            target_ids = input_ids.clone()
            loss = model_de(input_ids, labels=target_ids).loss
        elif option == 'English':
            encodings = tokenizer_en(text, return_tensors="pt")
            input_ids = encodings.input_ids
            target_ids = input_ids.clone()
            loss = model_en(input_ids, labels=target_ids).loss
        else:
            encodings = tokenizer_fr(text, return_tensors="pt")
            input_ids = encodings.input_ids
            target_ids = input_ids.clone()
            loss = model_fr(input_ids, labels=target_ids).loss
        
        st.write("Entire Text")
        st.write("Perplexity: ", round(float(torch.exp(loss)), 2))
    
    for sentence in sent_tokenize(text):
        st.write("________________________")
        st.write(sentence)
        with torch.no_grad():
            if option == 'German':
                encodings = tokenizer_de(sentence, return_tensors="pt")
                input_ids = encodings.input_ids
                target_ids = input_ids.clone()
                loss = model_de(input_ids, labels=target_ids).loss
            elif option == 'English':
                encodings = tokenizer_en(sentence, return_tensors="pt")
                input_ids = encodings.input_ids
                target_ids = input_ids.clone()
                loss = model_en(input_ids, labels=target_ids).loss
            else:
                encodings = tokenizer_fr(sentence, return_tensors="pt")
                input_ids = encodings.input_ids
                target_ids = input_ids.clone()
                loss = model_fr(input_ids, labels=target_ids).loss
        st.write("Perplexity: ", round(float(torch.exp(loss)), 2))