Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import re
|
| 4 |
+
from keras.models import load_model
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Load the model and data
|
| 8 |
+
model = load_model("text_prediction_model.h5")
|
| 9 |
+
train = pd.read_csv("train.csv", usecols=[2])
|
| 10 |
+
|
| 11 |
+
# Preprocess the text data
|
| 12 |
+
text = train["text"].str.lower() # convert to lowercase
|
| 13 |
+
text = text.str.replace("[^a-z ]", "") # remove punctuation and numbers
|
| 14 |
+
text = text.str.split() # split into words
|
| 15 |
+
|
| 16 |
+
# Create a vocabulary of unique words
|
| 17 |
+
vocab = set()
|
| 18 |
+
for words in text:
|
| 19 |
+
vocab.update(words)
|
| 20 |
+
vocab = sorted(vocab)
|
| 21 |
+
vocab_size = len(vocab)
|
| 22 |
+
print(f"Vocabulary size: {vocab_size}")
|
| 23 |
+
|
| 24 |
+
# Create a word to index mapping
|
| 25 |
+
word_to_index = {word: i for i, word in enumerate(vocab)}
|
| 26 |
+
index_to_word = {i: word for i, word in enumerate(vocab)}
|
| 27 |
+
|
| 28 |
+
# Convert text to sequences of indices
|
| 29 |
+
sequences = []
|
| 30 |
+
for words in text:
|
| 31 |
+
indices = [word_to_index[word] for word in words]
|
| 32 |
+
sequences.append(indices)
|
| 33 |
+
|
| 34 |
+
# Define the sequence length and the number of words to predict
|
| 35 |
+
seq_length = 5 # the number of words to use as input
|
| 36 |
+
pred_length = 1 # the number of words to predict as output
|
| 37 |
+
|
| 38 |
+
# Create input and output sequences
|
| 39 |
+
X = [] # input sequences
|
| 40 |
+
y = [] # output sequences
|
| 41 |
+
for seq in sequences:
|
| 42 |
+
for i in range(0, len(seq) - seq_length - pred_length + 1, pred_length):
|
| 43 |
+
X.append(seq[i:i+seq_length])
|
| 44 |
+
y.append(seq[i+seq_length:i+seq_length+pred_length])
|
| 45 |
+
|
| 46 |
+
X = np.array(X) # convert to numpy array
|
| 47 |
+
y = np.array(y) # convert to numpy array
|
| 48 |
+
X = X[:100000]
|
| 49 |
+
y = y[:100000]
|
| 50 |
+
|
| 51 |
+
# Define a function to generate text using the model
|
| 52 |
+
def generate_text(model, start_text, num_words):
|
| 53 |
+
# Convert the start text to a sequence of indices
|
| 54 |
+
input_seq = [word_to_index[word] for word in start_text.split()]
|
| 55 |
+
input_seq = np.array(input_seq).reshape(1, -1)
|
| 56 |
+
|
| 57 |
+
# Generate words using the model until reaching the desired length or an end token
|
| 58 |
+
output_text = start_text + " "
|
| 59 |
+
end_tokens = [".", "?", "!"] # tokens that indicate the end of a sentence
|
| 60 |
+
|
| 61 |
+
for i in range(num_words):
|
| 62 |
+
# Predict the next word using the model
|
| 63 |
+
output_seq = model.predict(input_seq)
|
| 64 |
+
output_word = index_to_word[np.argmax(output_seq)]
|
| 65 |
+
|
| 66 |
+
# Append the output word to the output text and update the input sequence
|
| 67 |
+
output_text += output_word + " "
|
| 68 |
+
input_seq = np.append(input_seq, [[word_to_index[output_word]]], axis=1)
|
| 69 |
+
input_seq = input_seq[:, -seq_length:] # keep only the last seq_length words
|
| 70 |
+
|
| 71 |
+
# Stop if the output word is an end token
|
| 72 |
+
if output_word in end_tokens:
|
| 73 |
+
break
|
| 74 |
+
|
| 75 |
+
return output_text
|
| 76 |
+
|
| 77 |
+
# Create a Gradio interface for the model
|
| 78 |
+
interface = gr.Interface(
|
| 79 |
+
fn=generate_text, # the function that connects the input and output
|
| 80 |
+
inputs=gr.inputs.Textbox(lines=2, label="Start Text"), # the input type and label
|
| 81 |
+
outputs=gr.outputs.Label(label="Generated Text"), # the output type and label
|
| 82 |
+
title="Text Prediction Model", # the title of the interface
|
| 83 |
+
description="A Keras LSTM model that predicts the next word given a start text.", # the description of the interface
|
| 84 |
+
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
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Launch the interface
|
| 88 |
+
interface.launch()
|