Spaces:
Runtime error
Runtime error
Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788import numpy as npimport pandas as pdimport refrom keras.models import load_modelimport gradio as gr
# Load the model and datamodel = load_model("text_prediction_model.h5")train = pd.read_csv("train.csv", usecols=[2])
# Preprocess the text datatext = train["text"].str.lower() # convert to lowercasetext = text.str.replace("[^a-z ]", "") # remove punctuation and numberstext = text.str.split() # split into words
# Create a vocabulary of unique wordsvocab = set()for words in text: vocab.update(words)vocab = sorted(vocab)vocab_size = len(vocab)print(f"Vocabulary size: {vocab_size}")
# Create a word to index mappingword_to_index = {word: i for i, word in enumerate(vocab)}index_to_word = {i: word for i, word in enumerate(vocab)}
# Convert text to sequences of indicessequences = []for words in text: indices = [word_to_index[word] for word in words] sequences.append(indices)
# Define the sequence length and the number of words to predictseq_length = 5 # the number of words to use as inputpred_length = 1 # the number of words to predict as output
# Create input and output sequencesX = [] # input sequencesy = [] # output sequencesfor seq in sequences: for i in range(0, len(seq) - seq_length - pred_length + 1, pred_length): X.append(seq[i:i+seq_length]) y.append(seq[i+seq_length:i+seq_length+pred_length])
X = np.array(X) # convert to numpy arrayy = np.array(y) # convert to numpy arrayX = X[:100000]y = y[:100000]
# Define a function to generate text using the modeldef generate_text(model, start_text, num_words): # Convert the start text to a sequence of indices input_seq = [word_to_index[word] for word in start_text.split()] input_seq = np.array(input_seq).reshape(1, -1)
# Generate words using the model until reaching the desired length or an end token output_text = start_text + " " end_tokens = [".", "?", "!"] # tokens that indicate the end of a sentence
for i in range(num_words): # Predict the next word using the model output_seq = model.predict(input_seq) output_word = index_to_word[np.argmax(output_seq)]
# Append the output word to the output text and update the input sequence output_text += output_word + " " input_seq = np.append(input_seq, [[word_to_index[output_word]]], axis=1) input_seq = input_seq[:, -seq_length:] # keep only the last seq_length words
# Stop if the output word is an end token if output_word in end_tokens: break return output_text
# Create a Gradio interface for the modelinterface = gr.Interface( fn=generate_text, # the function that connects the input and output inputs=gr.inputs.Textbox(lines=2, label="Start Text"), # the input type and label outputs=gr.outputs.Label(label="Generated Text"), # the output type and label title="Text Prediction Model", # the title of the interface description="A Keras LSTM model that predicts the next word given a start text.", # the description of the interface examples=[["the patient has a history"], ["the diagnosed patient had a history of vomiting and the cure was to take"], ["he complained of severe pain in his right"]], # some example inputs to show on the interface)
# Launch the interfaceinterface.launch()app.pyAdded+88