Spaces:
Runtime error
Runtime error
Commit
·
afc2b20
1
Parent(s):
0c06521
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from tensorflow import keras
|
| 5 |
+
|
| 6 |
+
from huggingface_hub import from_pretrained_keras
|
| 7 |
+
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
latent_dim = 256
|
| 11 |
+
num_encoder_tokens = 71
|
| 12 |
+
max_encoder_seq_length = 15
|
| 13 |
+
num_decoder_tokens = 92
|
| 14 |
+
max_decoder_seq_length = 59
|
| 15 |
+
|
| 16 |
+
with open("input_vocab.json", "r", encoding="utf-8") as f:
|
| 17 |
+
input_token_index = json.load(f)
|
| 18 |
+
with open("target_vocab.json", "r", encoding="utf-8") as f:
|
| 19 |
+
target_token_index = json.load(f)
|
| 20 |
+
|
| 21 |
+
model = from_pretrained_keras("keras-io/char-lstm-seq2seq")
|
| 22 |
+
|
| 23 |
+
# Define sampling models
|
| 24 |
+
# Restore the model and construct the encoder and decoder.
|
| 25 |
+
|
| 26 |
+
encoder_inputs = model.input[0] # input_1
|
| 27 |
+
encoder_outputs, state_h_enc, state_c_enc = model.layers[2].output # lstm_1
|
| 28 |
+
encoder_states = [state_h_enc, state_c_enc]
|
| 29 |
+
encoder_model = keras.Model(encoder_inputs, encoder_states)
|
| 30 |
+
|
| 31 |
+
decoder_inputs = model.input[1] # input_2
|
| 32 |
+
decoder_state_input_h = keras.Input(shape=(latent_dim,), name="input_3")
|
| 33 |
+
decoder_state_input_c = keras.Input(shape=(latent_dim,), name="input_4")
|
| 34 |
+
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
|
| 35 |
+
decoder_lstm = model.layers[3]
|
| 36 |
+
decoder_outputs, state_h_dec, state_c_dec = decoder_lstm(
|
| 37 |
+
decoder_inputs, initial_state=decoder_states_inputs
|
| 38 |
+
)
|
| 39 |
+
decoder_states = [state_h_dec, state_c_dec]
|
| 40 |
+
decoder_dense = model.layers[4]
|
| 41 |
+
decoder_outputs = decoder_dense(decoder_outputs)
|
| 42 |
+
|
| 43 |
+
decoder_model = keras.Model(
|
| 44 |
+
[decoder_inputs] + decoder_states_inputs, [decoder_outputs] + decoder_states
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Reverse-lookup token index to decode sequences back to
|
| 48 |
+
# something readable.
|
| 49 |
+
reverse_input_char_index = dict((i, char) for char, i in input_token_index.items())
|
| 50 |
+
reverse_target_char_index = dict((i, char) for char, i in target_token_index.items())
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def decode_sequence(input_seq):
|
| 54 |
+
# Encode the input as state vectors.
|
| 55 |
+
states_value = encoder_model.predict(input_seq)
|
| 56 |
+
|
| 57 |
+
# Generate empty target sequence of length 1.
|
| 58 |
+
target_seq = np.zeros((1, 1, num_decoder_tokens))
|
| 59 |
+
# Populate the first character of target sequence with the start character.
|
| 60 |
+
target_seq[0, 0, target_token_index["\t"]] = 1.0
|
| 61 |
+
|
| 62 |
+
# Sampling loop for a batch of sequences
|
| 63 |
+
# (to simplify, here we assume a batch of size 1).
|
| 64 |
+
stop_condition = False
|
| 65 |
+
decoded_sentence = ""
|
| 66 |
+
while not stop_condition:
|
| 67 |
+
output_tokens, h, c = decoder_model.predict([target_seq] + states_value)
|
| 68 |
+
|
| 69 |
+
# Sample a token
|
| 70 |
+
sampled_token_index = np.argmax(output_tokens[0, -1, :])
|
| 71 |
+
sampled_char = reverse_target_char_index[sampled_token_index]
|
| 72 |
+
decoded_sentence += sampled_char
|
| 73 |
+
|
| 74 |
+
# Exit condition: either hit max length
|
| 75 |
+
# or find stop character.
|
| 76 |
+
if sampled_char == "\n" or len(decoded_sentence) > max_decoder_seq_length:
|
| 77 |
+
stop_condition = True
|
| 78 |
+
|
| 79 |
+
# Update the target sequence (of length 1).
|
| 80 |
+
target_seq = np.zeros((1, 1, num_decoder_tokens))
|
| 81 |
+
target_seq[0, 0, sampled_token_index] = 1.0
|
| 82 |
+
|
| 83 |
+
# Update states
|
| 84 |
+
states_value = [h, c]
|
| 85 |
+
return decoded_sentence
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def translate(input_text):
|
| 89 |
+
encoder_input_data = np.zeros(
|
| 90 |
+
(1, max_encoder_seq_length, num_encoder_tokens), dtype="float32"
|
| 91 |
+
)
|
| 92 |
+
for t, char in enumerate(input_text):
|
| 93 |
+
encoder_input_data[0, t, input_token_index[char]] = 1.0
|
| 94 |
+
encoder_input_data[0, t + 1 :, input_token_index[" "]] = 1.0
|
| 95 |
+
target_text = decode_sequence(encoder_input_data)
|
| 96 |
+
return target_text
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
input_box = gr.inputs.Textbox(type="str", label="Input Text")
|
| 100 |
+
target = gr.outputs.Textbox()
|
| 101 |
+
|
| 102 |
+
iface = gr.Interface(
|
| 103 |
+
translate,
|
| 104 |
+
input_box,
|
| 105 |
+
target,
|
| 106 |
+
title="Character-level recurrent sequence-to-sequence model",
|
| 107 |
+
description="Model for Translating English to French using a Character-level recurrent sequence-to-sequence trained with small data.",
|
| 108 |
+
article='Author: <a href="https://huggingface.co/anuragshas">Anurag Singh</a> . Based on the keras example from <a href="https://keras.io/examples/nlp/lstm_seq2seq//">fchollet</a>',
|
| 109 |
+
examples=["Hi.", "Wait!", "Go on."],
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
iface.launch()
|