DiegoTheExplorar commited on
Commit
3a3c829
·
verified ·
1 Parent(s): 9674766

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -83
app.py CHANGED
@@ -1,83 +1,83 @@
1
- import torch
2
- import tensorflow as tf
3
- import gradio as gr
4
- import re
5
-
6
- from Seq2SeqModel import Seq2SeqModel
7
- from DataPPwithspecial import preprocess
8
- from Decoder import Decoder
9
- from Encoder import Encoder
10
- # Model parameters
11
- n_layers = 2
12
- emb_dim = 256
13
- hid_dim = 512
14
- dropout = 0.5
15
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # Use GPU if available, otherwise use CPU
16
-
17
- # Load preprocessed data and model parameters
18
- (english_tokenizer, klingon_tokenizer, max_english_length,
19
- _, _, _, _, _, _) = preprocess() # We don't need training data for inference
20
- input_dim = len(english_tokenizer.word_index) + 1 # Add 1 for the padding token
21
- output_dim = len(klingon_tokenizer.word_index) + 1 # Add 1 for the padding token
22
-
23
- # Initialize encoder and decoder
24
- encoder = Encoder(input_dim, emb_dim, hid_dim, n_layers, dropout).to(device)
25
- decoder = Decoder(output_dim, emb_dim, hid_dim, n_layers, dropout).to(device)
26
-
27
- # Initialize the Seq2SeqModel
28
- model = Seq2SeqModel(encoder, decoder, device).to(device)
29
-
30
- # Load the saved model
31
- model.load_state_dict(torch.load('./backend/English_to_Klingon.pth'))
32
- model.eval() # Set the model to evaluation mode
33
-
34
- #tokenize the English input
35
- def preprocess_sentence(sentence, tokenizer, max_length):
36
- # Tokenize the sentence
37
- tokenized_sentence = tokenizer.texts_to_sequences([sentence])
38
- # Pad the sequence
39
- padded_sentence = tf.keras.preprocessing.sequence.pad_sequences(tokenized_sentence, maxlen=max_length, padding='post')
40
- return torch.tensor(padded_sentence, dtype=torch.long).to(device)
41
-
42
- # Translation function for Gradio
43
- def translate_english_to_klingon(english_sentence):
44
- # Preprocess the input English sentence
45
- input_sentence = preprocess_sentence(english_sentence, english_tokenizer, max_english_length)
46
-
47
- # Remove the extra dimension added by unsqueeze(1)
48
- input_sentence = input_sentence.squeeze(0)
49
-
50
- # Perform inference
51
- with torch.no_grad():
52
- # Pass input as both input and target with teacher forcing ratio 0
53
- output = model(input_sentence.unsqueeze(1), input_sentence.unsqueeze(1), 0)
54
-
55
- # Convert output indices to Klingon words
56
- output_indices = torch.argmax(output, dim=-1).squeeze().tolist()
57
- klingon_sentence = ' '.join([klingon_tokenizer.index_word[idx] for idx in output_indices if idx != 0]) # Remove padding token
58
- #regex to remove eos
59
- klingon_sentence = re.sub(r'\beos\b', '', klingon_sentence).strip()
60
- return klingon_sentence
61
-
62
-
63
- # Create Gradio interface
64
- examples = [
65
- ["Hello, how are you?"],
66
- ["What is your name?"],
67
- ["I love learning new languages."],
68
- ["Where is the nearest starbase?"],
69
- ["Can you tell me more about your planet?"]
70
- ]
71
-
72
- iface = gr.Interface(
73
- fn=translate_english_to_klingon,
74
- inputs=gr.Textbox(label = "English Phrase",lines=2, placeholder="Enter English text here..."),
75
- outputs=gr.Textbox(label="Klingon Translation",lines=2),
76
- title="English to Klingon Translation",
77
- description="Enter text in English and get its translation in Klingon. This translator helps you convert everyday English phrases into the fictional language spoken by the Klingon species in the Star Trek universe. Try one of the example sentences to see how it works!",
78
- examples=examples,
79
- theme="default"
80
- )
81
-
82
- iface.launch(share = True)
83
-
 
1
+ import torch
2
+ import tensorflow as tf
3
+ import gradio as gr
4
+ import re
5
+
6
+ from Seq2SeqModel import Seq2SeqModel
7
+ from DataPPwithspecial import preprocess
8
+ from Decoder import Decoder
9
+ from Encoder import Encoder
10
+ # Model parameters
11
+ n_layers = 2
12
+ emb_dim = 256
13
+ hid_dim = 512
14
+ dropout = 0.5
15
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # Use GPU if available, otherwise use CPU
16
+
17
+ # Load preprocessed data and model parameters
18
+ (english_tokenizer, klingon_tokenizer, max_english_length,
19
+ _, _, _, _, _, _) = preprocess() # We don't need training data for inference
20
+ input_dim = len(english_tokenizer.word_index) + 1 # Add 1 for the padding token
21
+ output_dim = len(klingon_tokenizer.word_index) + 1 # Add 1 for the padding token
22
+
23
+ # Initialize encoder and decoder
24
+ encoder = Encoder(input_dim, emb_dim, hid_dim, n_layers, dropout).to(device)
25
+ decoder = Decoder(output_dim, emb_dim, hid_dim, n_layers, dropout).to(device)
26
+
27
+ # Initialize the Seq2SeqModel
28
+ model = Seq2SeqModel(encoder, decoder, device).to(device)
29
+
30
+ # Load the saved model
31
+ model.load_state_dict(torch.load('English_to_Klingon.pth'))
32
+ model.eval() # Set the model to evaluation mode
33
+
34
+ #tokenize the English input
35
+ def preprocess_sentence(sentence, tokenizer, max_length):
36
+ # Tokenize the sentence
37
+ tokenized_sentence = tokenizer.texts_to_sequences([sentence])
38
+ # Pad the sequence
39
+ padded_sentence = tf.keras.preprocessing.sequence.pad_sequences(tokenized_sentence, maxlen=max_length, padding='post')
40
+ return torch.tensor(padded_sentence, dtype=torch.long).to(device)
41
+
42
+ # Translation function for Gradio
43
+ def translate_english_to_klingon(english_sentence):
44
+ # Preprocess the input English sentence
45
+ input_sentence = preprocess_sentence(english_sentence, english_tokenizer, max_english_length)
46
+
47
+ # Remove the extra dimension added by unsqueeze(1)
48
+ input_sentence = input_sentence.squeeze(0)
49
+
50
+ # Perform inference
51
+ with torch.no_grad():
52
+ # Pass input as both input and target with teacher forcing ratio 0
53
+ output = model(input_sentence.unsqueeze(1), input_sentence.unsqueeze(1), 0)
54
+
55
+ # Convert output indices to Klingon words
56
+ output_indices = torch.argmax(output, dim=-1).squeeze().tolist()
57
+ klingon_sentence = ' '.join([klingon_tokenizer.index_word[idx] for idx in output_indices if idx != 0]) # Remove padding token
58
+ #regex to remove eos
59
+ klingon_sentence = re.sub(r'\beos\b', '', klingon_sentence).strip()
60
+ return klingon_sentence
61
+
62
+
63
+ # Create Gradio interface
64
+ examples = [
65
+ ["Hello, how are you?"],
66
+ ["What is your name?"],
67
+ ["I love learning new languages."],
68
+ ["Where is the nearest starbase?"],
69
+ ["Can you tell me more about your planet?"]
70
+ ]
71
+
72
+ iface = gr.Interface(
73
+ fn=translate_english_to_klingon,
74
+ inputs=gr.Textbox(label = "English Phrase",lines=2, placeholder="Enter English text here..."),
75
+ outputs=gr.Textbox(label="Klingon Translation",lines=2),
76
+ title="English to Klingon Translation",
77
+ description="Enter text in English and get its translation in Klingon. This translator helps you convert everyday English phrases into the fictional language spoken by the Klingon species in the Star Trek universe. Try one of the example sentences to see how it works!",
78
+ examples=examples,
79
+ theme="default"
80
+ )
81
+
82
+ iface.launch(share = True)
83
+