Spaces:
Runtime error
Runtime error
Delete demo
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566import gradio as grimport numpy as npimport pandas as pdimport refrom keras.models import load_model
# Load your model and data files from the Hubmodel = load_model("https://huggingface.co/username/modelname/raw/main/text_prediction_model.h5")train = pd.read_csv("https://huggingface.co/username/modelname/raw/main/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)}
# 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
# Define the prediction functiondef generate_text(input_text): # Convert the input text to a sequence of indices input_seq = [word_to_index[word] for word in input_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 = input_text + " " end_tokens = [".", "?", "!"] # tokens that indicate the end of a sentence
for i in range(50): # 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 the interface with a text input and a text outputinterface = gr.Interface( fn=generate_text, inputs=gr.inputs.Textbox(lines=2, label="Input Text"), outputs=gr.outputs.Textbox(label="Output Text"))
# Launch the interfaceinterface.launch()demoDeleted-66