Spaces:
Runtime error
Runtime error
Commit ·
91303f0
1
Parent(s): 0651ffe
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,28 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from keras.models import load_model
|
| 3 |
|
| 4 |
+
# Load the model
|
| 5 |
+
model = load_model('LithicAI-Education-Model.h5')
|
| 6 |
+
|
| 7 |
+
# Function to generate text
|
| 8 |
+
def generate_text(seed_text, next_words, model, max_sequence_len):
|
| 9 |
+
for _ in range(next_words):
|
| 10 |
+
# Convert seed text to sequence
|
| 11 |
+
token_list = tokenizer.texts_to_sequences([seed_text])[0]
|
| 12 |
+
# Pad the sequence
|
| 13 |
+
token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
|
| 14 |
+
# Predict the next word
|
| 15 |
+
predicted = model.predict_classes(token_list, verbose=0)
|
| 16 |
+
# Convert predicted word index to word
|
| 17 |
+
output_word = ""
|
| 18 |
+
for word, index in tokenizer.word_index.items():
|
| 19 |
+
if index == predicted:
|
| 20 |
+
output_word = word
|
| 21 |
+
break
|
| 22 |
+
# Add the predicted word to the seed text
|
| 23 |
+
seed_text += " "+output_word
|
| 24 |
+
return seed_text.title()
|
| 25 |
+
|
| 26 |
+
# Generate text
|
| 27 |
+
generated_text = generate_text("the quick brown", 5, model, 10)
|
| 28 |
+
print(generated_text)
|