Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,30 +49,37 @@ 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 |
-
#
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# Create a Gradio interface for the model
|
| 78 |
interface = gr.Interface(
|
|
|
|
| 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.split()]
|
| 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
|
| 61 |
+
output_text = start_text + " "
|
| 62 |
+
end_tokens = [".", "?", "!"] # tokens that indicate the end of a sentence
|
| 63 |
+
|
| 64 |
+
for i in range(num_words):
|
| 65 |
+
# Predict the next word using the model
|
| 66 |
+
output_seq = model.predict(input_seq)
|
| 67 |
+
output_word = index_to_word[np.argmax(output_seq)]
|
| 68 |
+
|
| 69 |
+
# Append the output word to the output text and update the input sequence
|
| 70 |
+
output_text += output_word + " "
|
| 71 |
+
input_seq = np.append(input_seq, [[word_to_index[output_word]]], axis=1)
|
| 72 |
+
input_seq = input_seq[:, -seq_length:] # keep only the last seq_length words
|
| 73 |
+
|
| 74 |
+
# Stop if the output word is an end token
|
| 75 |
+
if output_word in end_tokens:
|
| 76 |
+
break
|
| 77 |
+
|
| 78 |
+
return output_text
|
| 79 |
+
else:
|
| 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(
|