Safman commited on
Commit
682f218
·
1 Parent(s): 8cab497

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -1
app.py CHANGED
@@ -1,3 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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():
@@ -28,7 +80,7 @@ def generate_text(model, start_text):
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
 
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 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):
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():
 
80
  # Return an error message if the start text is None or empty
81
  return "Please provide a valid start text."
82
 
83
+ # Create a Gradio interface for the model
84
  interface = gr.Interface(
85
  fn=generate_text, # the function that connects the input and output
86
  inputs=gr.inputs.Textbox(lines=2, label="Start Text"), # the input type and label