Spaces:
Build error
Build error
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104from keras.models import load_modelfrom keras.preprocessing.sequence import pad_sequencesimport sys# load the model from diskmodel = load_model("model_pretrain.h5")
char_to_int = {' ': 0, 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}
int_to_char = dict((i, c) for c, i in char_to_int.items())
def get_sequence_from_encoding(sequence_encoded, ind_to_word): in_text = '' for index in sequence_encoded: if index in ind_to_word: word = ind_to_word[index] else: word = '' in_text += '' + word return in_text
def get_encoding_from_sequence(sequence, word_to_ind): out_encode = [] for word in list(sequence.lower()): if word in word_to_ind: index = word_to_ind[word] else: index = 0 out_encode.append(index) return out_encode
def generate_text(start_text,text_length=100): encoding = get_encoding_from_sequence(start_text.lower(),char_to_int) decoding = get_sequence_from_encoding(encoding,int_to_char) print("Input sequence: ", start_text) print("Start generating the paragraph: \n")
line_print = '' new_sequence = start_text sys.stdout.write(start_text) for repeat in range(text_length): test_data = np.reshape(encoding, (1, len(encoding))) maxlen = 20 # specify how long the sequences should be. This cuts sequences that exceed that number. test_data_pad = pad_sequences(test_data, padding='pre', maxlen=maxlen)
prediction = model.predict(test_data_pad, verbose=0) index = np.argmax(prediction) result = int_to_char[index] seq_in = [int_to_char[value] for value in encoding] if len(line_print) > 70 and result == ' ': sys.stdout.write("\n") line_print = ''
sys.stdout.write(result) line_print = line_print + result new_sequence = new_sequence + result encoding.append(index) encoding = encoding[1:len(encoding)]
### configure inputs/outputs
set_input = gr.Textbox.(label = 'Starting words')set_output = gr.Textbox.(label = 'Generated sentences')
### configure gradio, detailed can be found at https://www.gradio.app/docs/#i_sliderinterface = gr.Interface(fn=predict_price, inputs=set_input, outputs=set_output, title="CSCI4750/5750 Demo 8: Web Application for Text Generation using RNN", description= "Click examples below for a quick demo", theme = 'huggingface', layout = 'vertical' )interface.launch(debug=True)app.pyAdded+104