Views
No views yet


conda install -c conda-forge tensorflowconda install -c conda-forge kerasconda install -c conda-forge gradioconda install -c conda-forge numpyconda env export > requirements.txtconda env create -f requirements.txt1# clone project
2git clone https://huggingface.co/spaces/KameliaZaman/French-to-English-Translation/tree/main
3
4# go inside the project directory
5cd French-to-English-Translation
6
7# install the required packages
8pip install -r requirements.txt
9
10# run the gradio app
11python app.py 
1def create_model(src_vocab, tar_vocab, src_timesteps, tar_timesteps, n_units):
2 # Create the model
3 model = Sequential()
4 model.add(Embedding(src_vocab_size, n_units, input_length=src_length, mask_zero=True))
5 model.add(LSTM(n_units))
6 model.add(RepeatVector(tar_timesteps))
7 model.add(LSTM(n_units, return_sequences=True))
8 model.add(TimeDistributed(Dense(tar_vocab, activation='softmax')))
9 return model
10
11model = create_model(src_vocab_size, tar_vocab_size, src_length, tar_length, 256)
12model.compile(optimizer='adam', loss='categorical_crossentropy')
13
14history = model.fit(trainX,
15 trainY,
16 epochs=20,
17 batch_size=64,
18 validation_split=0.1,
19 verbose=1,
20 callbacks=[
21 EarlyStopping(
22 monitor='val_loss',
23 patience=10,
24 restore_best_weights=True
25 )
26 ])


1import string
2import re
3from unicodedata import normalize
4import numpy as np
5from keras.preprocessing.text import Tokenizer
6from keras.preprocessing.sequence import pad_sequences
7from keras.utils import to_categorical
8from keras.models import Sequential,load_model
9from keras.layers import LSTM,Dense,Embedding,RepeatVector,TimeDistributed
10from keras.callbacks import EarlyStopping
11from nltk.translate.bleu_score import corpus_bleu
12import pandas as pd
13from string import punctuation
14import matplotlib.pyplot as plt
15from IPython.display import Markdown, display
16import gradio as gr
17import tensorflow as tf
18from tensorflow.keras.models import load_model
19
20total_sentences = 10000
21
22dataset = pd.read_csv("./eng_-french.csv", nrows = total_sentences)
23
24def clean(string):
25 # Clean the string
26 string = string.replace("\u202f"," ") # Replace no-break space with space
27 string = string.lower()
28
29 # Delete the punctuation and the numbers
30 for p in punctuation + "«»" + "0123456789":
31 string = string.replace(p," ")
32
33 string = re.sub('\s+',' ', string)
34 string = string.strip()
35
36 return string
37
38dataset = dataset.sample(frac=1, random_state=0)
39dataset["English words/sentences"] = dataset["English words/sentences"].apply(lambda x: clean(x))
40dataset["French words/sentences"] = dataset["French words/sentences"].apply(lambda x: clean(x))
41
42dataset = dataset.values
43dataset = dataset[:total_sentences]
44
45source_str, target_str = "French", "English"
46idx_src, idx_tar = 1, 0
47
48def create_tokenizer(lines):
49 # fit a tokenizer
50 tokenizer = Tokenizer()
51 tokenizer.fit_on_texts(lines)
52 return tokenizer
53
54def max_len(lines):
55 # max sentence length
56 return max(len(line.split()) for line in lines)
57
58def encode_sequences(tokenizer, length, lines):
59 # encode and pad sequences
60 X = tokenizer.texts_to_sequences(lines) # integer encode sequences
61 X = pad_sequences(X, maxlen=length, padding='post') # pad sequences with 0 values
62 return X
63
64def word_for_id(integer, tokenizer):
65 # map an integer to a word
66 for word, index in tokenizer.word_index.items():
67 if index == integer:
68 return word
69 return None
70
71def predict_seq(model, tokenizer, source):
72 # generate target from a source sequence
73 prediction = model.predict(source, verbose=0)[0]
74 integers = [np.argmax(vector) for vector in prediction]
75 target = list()
76 for i in integers:
77 word = word_for_id(i, tokenizer)
78 if word is None:
79 break
80 target.append(word)
81 return ' '.join(target)
82
83src_tokenizer = create_tokenizer(dataset[:, idx_src])
84src_vocab_size = len(src_tokenizer.word_index) + 1
85src_length = max_len(dataset[:, idx_src])
86tar_tokenizer = create_tokenizer(dataset[:, idx_tar])
87
88model = load_model('./french_to_english_translator.h5')
89
90def translate_french_english(french_sentence):
91 # Clean the input sentence
92 french_sentence = clean(french_sentence)
93 # Tokenize and pad the input sentence
94 input_sequence = encode_sequences(src_tokenizer, src_length, [french_sentence])
95 # Generate the translation
96 english_translation = predict_seq(model, tar_tokenizer, input_sequence)
97 return english_translation
98
99gr.Interface(
100 fn=translate_french_english,
101 inputs="text",
102 outputs="text",
103 title="French to English Translator",
104 description="Translate French sentences to English."
105).launch()
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)