Safman commited on
Commit
31dc405
·
1 Parent(s): f0852f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -23
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
- # 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(
 
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(