Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,8 @@
|
|
| 1 |
-
|
| 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 datas
|
| 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 |
-
|
| 53 |
-
def generate_text(model, start_text, num_words):
|
| 54 |
# Check if the start text is not None and has a non-empty string value
|
| 55 |
if start_text is not None and start_text.strip():
|
| 56 |
# Convert the start text to a sequence of indices
|
| 57 |
-
input_seq = [word_to_index[word] for word in start_text
|
| 58 |
input_seq = np.array(input_seq).reshape(1, -1)
|
| 59 |
|
| 60 |
# Generate words using the model until reaching the desired length or an end token
|
|
@@ -80,16 +28,15 @@ def generate_text(model, start_text, num_words):
|
|
| 80 |
# Return an error message if the start text is None or empty
|
| 81 |
return "Please provide a valid start text."
|
| 82 |
|
| 83 |
-
|
| 84 |
-
# Create a Gradio interface for the model
|
| 85 |
interface = gr.Interface(
|
| 86 |
fn=generate_text, # the function that connects the input and output
|
| 87 |
inputs=gr.inputs.Textbox(lines=2, label="Start Text"), # the input type and label
|
| 88 |
-
outputs=gr.outputs.Label(label="Generated Text"), # the output type and label
|
| 89 |
title="Text Prediction Model", # the title of the interface
|
| 90 |
description="A Keras LSTM model that predicts the next word given a start text.", # the description of the interface
|
| 91 |
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
|
| 92 |
)
|
| 93 |
|
| 94 |
# Launch the interface
|
| 95 |
-
interface.launch()
|
|
|
|
| 1 |
+
def generate_text(model, start_text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
# Check if the start text is not None and has a non-empty string value
|
| 3 |
if start_text is not None and start_text.strip():
|
| 4 |
# Convert the start text to a sequence of indices
|
| 5 |
+
input_seq = [word_to_index[word] for word in start_text]
|
| 6 |
input_seq = np.array(input_seq).reshape(1, -1)
|
| 7 |
|
| 8 |
# Generate words using the model until reaching the desired length or an end token
|
|
|
|
| 28 |
# Return an error message if the start text is None or empty
|
| 29 |
return "Please provide a valid start text."
|
| 30 |
|
| 31 |
+
|
|
|
|
| 32 |
interface = gr.Interface(
|
| 33 |
fn=generate_text, # the function that connects the input and output
|
| 34 |
inputs=gr.inputs.Textbox(lines=2, label="Start Text"), # the input type and label
|
| 35 |
+
outputs=gr.outputs.List(gr.outputs.Label(label="Generated Text"), max_items=10), # the output type and label
|
| 36 |
title="Text Prediction Model", # the title of the interface
|
| 37 |
description="A Keras LSTM model that predicts the next word given a start text.", # the description of the interface
|
| 38 |
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
|
| 39 |
)
|
| 40 |
|
| 41 |
# Launch the interface
|
| 42 |
+
interface.launch()
|