saurabh091 commited on
Commit
a561bc0
·
verified ·
1 Parent(s): a76a901

Upload 6 files

Browse files
Files changed (6) hide show
  1. Dockerfile +17 -0
  2. app.py +75 -0
  3. requirements.txt +9 -0
  4. templates/document.html +101 -0
  5. templates/home.html +189 -0
  6. tokenizer.pickle +3 -0
Dockerfile ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use a base image with Python
2
+ FROM python:3.9
3
+
4
+ # Set the working directory
5
+ WORKDIR /app
6
+
7
+ # Copy all files to the container
8
+ COPY . .
9
+
10
+ # Install dependencies
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # Expose port (Hugging Face requires port 7860)
14
+ EXPOSE 7860
15
+
16
+ # Run Flask
17
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request
2
+ from keras.models import load_model
3
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
4
+ import numpy as np
5
+ import pickle
6
+ import time
7
+ import os
8
+
9
+ import kagglehub
10
+
11
+ # Download latest version
12
+ model_folder = kagglehub.model_download("saurabhmaulekhi/next_word_prediction/keras/version-1")
13
+
14
+ files_in_folder = os.listdir(model_folder)
15
+ model_name = files_in_folder[0]
16
+
17
+ model_path = os.path.join(model_folder,model_name)
18
+
19
+
20
+ model = load_model(model_path)
21
+
22
+ with open("tokenizer.pickle", 'rb') as handle:
23
+ tokenizer = pickle.load(handle)
24
+
25
+ app = Flask(__name__)
26
+
27
+ @app.route('/')
28
+ def home():
29
+ return render_template('home.html')
30
+
31
+ @app.route('/home')
32
+ def home_click():
33
+ return render_template('home.html')
34
+
35
+ @app.route('/', methods=['GET', 'POST'])
36
+ def predict_show():
37
+ if request.method == 'POST':
38
+ try:
39
+ user_input = request.form['user_input']
40
+ num_of_words = int(request.form['num'])
41
+ result = prediction(user_input, num_of_words)
42
+ except ValueError:
43
+ result = "Invalid input. Please enter a number."
44
+ return render_template('home.html', result=result)
45
+
46
+ @app.route('/document')
47
+ def document():
48
+ return render_template('document.html')
49
+
50
+ def prediction(text, no_of_words):
51
+ sentence = []
52
+ sentence.append(text+" ")
53
+ for i in range(no_of_words):
54
+ # tokenize
55
+ token_text = tokenizer.texts_to_sequences([text])[0]
56
+ # padding
57
+ padded_token_text = pad_sequences([token_text], maxlen=56, padding='pre')
58
+ # predict
59
+ pos = np.argmax(model.predict(padded_token_text)) ## predicting new word
60
+
61
+ for word, index in tokenizer.word_index.items():
62
+ if index == pos:
63
+ text = word
64
+ sentence.append(text)
65
+ sentence.append(" ")
66
+
67
+ sentence.pop()
68
+ return sentence
69
+
70
+ # for i in sentence:
71
+ # print(i, end=" ")
72
+ # time.sleep(0.6) ## time in seconds to predict new word
73
+
74
+ if __name__ == "__main__":
75
+ app.run(host='0.0.0.0', port=7860)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ flask
2
+ keras.models
3
+ tensorflow.keras.preprocessing.sequence
4
+ numpy
5
+ pickle
6
+ time
7
+ os
8
+ gunicorn
9
+ kagglehub
templates/document.html ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>Title</title>
6
+ <style>
7
+ .body {
8
+ width: 1480px; /* Set width to 300 pixels */
9
+ height: 700px; /* Set height to 200 pixels */
10
+ background: rgb(48,18,115);
11
+ background: linear-gradient(180deg, rgba(48,18,115,1) 20%, rgba(53,51,205,1) 48%, rgba(35,18,122,1) 91%);
12
+ padding: 10px; /* Add some padding inside the div */
13
+ }
14
+
15
+ #nav_bar {
16
+ background-color: #f64848;
17
+ overflow: hidden;
18
+ border: 3px solid black;
19
+ }
20
+
21
+ .outer-box {
22
+ position: relative; /* Necessary for positioning relative to this element */
23
+ display: inline-block; /* Make the outer box inline-block to allow positioning relative to it */
24
+ padding: 5px 10px;
25
+ border: 3px solid black;
26
+ background-color: #f64848;
27
+ margin-left: 350px;
28
+ margin-bottom: 5px;
29
+ border-radius: 30px; /* Add rounded corners */
30
+ }
31
+
32
+ #nav_bar ul {
33
+ list-style: none;
34
+ margin: 0;
35
+ padding: 0;
36
+ display: flex; /* This is the key to display items in a line */
37
+ }
38
+
39
+ #nav_bar li {
40
+ float: left; /* Alternatively, use 'display: inline-block;' */
41
+ }
42
+
43
+ #nav_bar a {
44
+ display: block;
45
+ color: white;
46
+ text-decoration: none;
47
+ padding: 14px 16px;
48
+ }
49
+
50
+ #nav_bar a:hover {
51
+ border: 1px solid black;
52
+ background-color: #ff914d;
53
+ }
54
+
55
+ #docs ul {
56
+ margin: 30px;
57
+ padding: 10px;
58
+ }
59
+
60
+ #docs a {
61
+ width: 350px;
62
+ margin:20px;
63
+ padding: 40px;
64
+ height: 10px;
65
+ display: block;
66
+ background-color: #ff914d;
67
+ color: black;
68
+ text-decoration: none;
69
+ padding: 14px 16px;
70
+ }
71
+
72
+ #docs a:hover {
73
+ border: 1px solid black;
74
+ background-color: #f64848;
75
+ }
76
+
77
+ h1{
78
+ background-color: #ff914d;
79
+ position: 100; /* Necessary for absolute positioning of the inner box */
80
+ }
81
+ </style>
82
+ </head>
83
+
84
+ <body class="body">
85
+ <div class="outer-box"><h1> Next Word Prediction With 75% Accuracy </h1> </div>
86
+ <nav id="nav_bar">
87
+ <ul>
88
+ <li><a href="/home">Home</a></li>
89
+ <li><a href="/document">Documentation</a></li>
90
+ </ul>
91
+ </nav>
92
+
93
+ <nav id="docs">
94
+ <ul>
95
+ <li><a href="https://www.kaggle.com/"><b>RNN, LSTM, GRU, BILSTM, BiGRU Notes</b></a></li>
96
+ <li><a href="https://www.kaggle.com/"><b>ML Model Deployment (Kaggle NoteBook)</b></a></li>
97
+ <li><a href="#"><b>Github Repo</b></a></li>
98
+ </ul>
99
+ </nav>
100
+ </body>
101
+ </html>
templates/home.html ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+
5
+ <style>
6
+ .outer-box {
7
+ position: relative; /* Necessary for positioning relative to this element */
8
+ display: inline-block; /* Make the outer box inline-block to allow positioning relative to it */
9
+ padding: 5px 10px;
10
+ border: 3px solid black;
11
+ background-color: #f64848;
12
+ margin-left: 350px;
13
+ margin-bottom: 5px;
14
+ border-radius: 30px; /* Add rounded corners */
15
+ }
16
+
17
+ h1{
18
+ background-color: #ff914d;
19
+ position: 100; /* Necessary for absolute positioning of the inner box */
20
+ }
21
+
22
+ nav {
23
+ background-color: #f64848;
24
+ overflow: hidden;
25
+ border: 3px solid black;
26
+ }
27
+
28
+ nav ul {
29
+ list-style: none;
30
+ margin: 0;
31
+ padding: 0;
32
+ display: flex; /* This is the key to display items in a line */
33
+ }
34
+
35
+ nav li {
36
+ float: left; /* Alternatively, use 'display: inline-block;' */
37
+ }
38
+
39
+ nav a {
40
+ display: block;
41
+ color: white;
42
+ text-decoration: none;
43
+ padding: 14px 16px;
44
+ }
45
+
46
+ nav a:hover {
47
+ border: 1px solid black;
48
+ background-color: #ff914d;
49
+ }
50
+
51
+ .txt{
52
+ font-size: 24px;
53
+ }
54
+
55
+ .body {
56
+ width: 1480px; /* Set width to 300 pixels */
57
+ height: 700px; /* Set height to 200 pixels */
58
+ background: rgb(48,18,115);
59
+ background: linear-gradient(180deg, rgba(48,18,115,1) 20%, rgba(53,51,205,1) 48%, rgba(35,18,122,1) 91%);
60
+ padding: 10px; /* Add some padding inside the div */
61
+ }
62
+
63
+ form{
64
+ margin-top: 20px;
65
+ width: 500px; /* Set width to 300 pixels */
66
+ height: 150px; /* Set height to 200 pixels */
67
+ background-color: #f64848;
68
+ padding: 20px; /* Add padding around the form */
69
+ border: 3px solid black; /* Add a border around the form */
70
+ border-radius: 40px; /* Add rounded corners */
71
+ }
72
+
73
+
74
+ #submit{
75
+ background-color: #ff914d;
76
+ border: 2px solid black;
77
+ color: white;
78
+ padding: 5px 15px;
79
+ text-align: center;
80
+ text-decoration: none;
81
+ display: inline-block;
82
+ font-size: 16px;
83
+ border-radius: 4px;
84
+ margin-left: 100px;
85
+ margin-top: 15px;
86
+ }
87
+
88
+
89
+ #display_outer{
90
+ margin-top: 20px;
91
+ width: 650px;
92
+ height: 250px;
93
+ background-color: #f64848;
94
+ padding: 20px;
95
+ border: 3px solid black;
96
+ border-radius: 40px;
97
+ margin-left: 400px;
98
+ }
99
+
100
+ #display{
101
+ margin-top: 40px;
102
+ margin-left: 100px;
103
+ width: 500px;
104
+ height: 120px;
105
+ font-size: 24px;
106
+ font-weight: bold;
107
+ background-color: #c84646;
108
+ padding: 20px;
109
+ border: 3px solid black;
110
+ border-radius: 40px;
111
+ }
112
+
113
+ #predictions{
114
+ background-color: #ff914d;
115
+ margin-top : 2px;
116
+ padding: 3px;
117
+ width: 150px;
118
+ height: 30px;
119
+ font-size: 22px;
120
+ border-radius: 10px;
121
+ border: 2px solid black;
122
+ color: white;
123
+ }
124
+ </style>
125
+
126
+ <meta charset="UTF-8">
127
+ <title>Next Word Prediction</title>
128
+ </head>
129
+ <body class="body">
130
+ <div class="outer-box"><h1> Next Word Prediction With 75% Accuracy </h1> </div>
131
+ <nav>
132
+ <ul>
133
+ <li><a href="/home">Home</a></li>
134
+ <li><a href="/document">Documentation</a></li>
135
+
136
+ </ul>
137
+ </nav>
138
+
139
+ <form action="/" method="POST" class="form">
140
+ <label class="txt" for="user_input"><b>Give the initial word: </b></label>
141
+ <input type="text" id="user_input" name="user_input" class="form-control" placeholder="Enter the initial word">
142
+ <br><br>
143
+ <label class="txt" for="num"> <b>Number of Words to Predict: </b></label>
144
+ <select name="num" id="num">
145
+ <option value="1">1</option>
146
+ <option value="2">2</option>
147
+ <option value="3">3</option>
148
+ <option value="4">4</option>
149
+ <option value="5">5</option>
150
+ <option value="6">6</option>
151
+ <option value="7">7</option>
152
+ <option value="8">8</option>
153
+ <option value="9">9</option>
154
+ <option value="10">10</option>
155
+ </select>
156
+ <br><br>
157
+ <input id="submit" type="submit" value="Submit">
158
+ </form>
159
+
160
+ <div id="display_outer">
161
+ <p id="predictions"><b>Predictions:</b></p>
162
+ <div id="display">
163
+ </div>
164
+ </div>
165
+
166
+ {% if result is defined %}
167
+ <script>
168
+ const myList = {{ result | tojson }};
169
+ let currentIndex = 0;
170
+ const displayDiv = document.getElementById('display');
171
+
172
+ function displayElement() {
173
+ if (myList.length === 0) {
174
+ displayDiv.textContent = "No predictions found.";
175
+ } else {
176
+ if (currentIndex < myList.length) {
177
+ displayDiv.textContent += myList[currentIndex] + " ";
178
+ currentIndex++;
179
+ setTimeout(displayElement,100);
180
+ }
181
+ }
182
+ }
183
+ window.onload = displayElement;
184
+ </script>
185
+ {% else %}
186
+
187
+ {% endif %}
188
+ </body>
189
+ </html>
tokenizer.pickle ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:06eaaa88deaaa69d9241e0ae5f7684b125fdfd80b96463ded339878fa5d5f5e4
3
+ size 356388