Added Flask app and model
Browse files- Procfile +1 -0
- app.py +84 -0
- requirements.txt +6 -0
- tokenizer.pkl +3 -0
Procfile
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
web: gunicorn app:app
|
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
import pickle
|
| 5 |
+
import numpy as np
|
| 6 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 7 |
+
import os
|
| 8 |
+
# import requests
|
| 9 |
+
import gdown
|
| 10 |
+
|
| 11 |
+
app = Flask(__name__)
|
| 12 |
+
CORS(app)
|
| 13 |
+
|
| 14 |
+
# Load tokenizer and model
|
| 15 |
+
with open("tokenizer.pkl", "rb") as f:
|
| 16 |
+
tokenizer = pickle.load(f)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
MODEL_PATH = "model.keras"
|
| 20 |
+
MODEL_URL = (
|
| 21 |
+
"https://drive.google.com/uc?export=download&id=1uqqZiZsmI2wnxG6r9Z4IQq94rT9fGwgH"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
def download_model():
|
| 25 |
+
if not os.path.exists(MODEL_PATH):
|
| 26 |
+
print("Downloading model using gdown...")
|
| 27 |
+
|
| 28 |
+
# Use gdown to download the file
|
| 29 |
+
gdown.download(MODEL_URL, MODEL_PATH, quiet=False)
|
| 30 |
+
|
| 31 |
+
if os.path.exists(MODEL_PATH):
|
| 32 |
+
print("Model downloaded successfully.")
|
| 33 |
+
else:
|
| 34 |
+
print("Error: Model download failed.")
|
| 35 |
+
|
| 36 |
+
download_model()
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
print("Loading model...")
|
| 40 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 41 |
+
|
| 42 |
+
MAX_LEN = 30
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def predict_next_words(text, top_k=3):
|
| 46 |
+
# Tokenize input text
|
| 47 |
+
sequence = tokenizer.texts_to_sequences([text])[0]
|
| 48 |
+
|
| 49 |
+
if len(sequence) == 0: # If tokenization results in empty list
|
| 50 |
+
return []
|
| 51 |
+
|
| 52 |
+
# Keep only the last 30 tokens if input is too long
|
| 53 |
+
sequence = sequence[-MAX_LEN:]
|
| 54 |
+
|
| 55 |
+
# Pad sequence to required length (post-padding)
|
| 56 |
+
sequence = pad_sequences([sequence], maxlen=MAX_LEN, padding="post")
|
| 57 |
+
|
| 58 |
+
# Predict next word probabilities
|
| 59 |
+
predictions = model.predict(sequence)[0]
|
| 60 |
+
|
| 61 |
+
# Get top-k word indices
|
| 62 |
+
top_indices = np.argsort(predictions)[-top_k:][::-1]
|
| 63 |
+
|
| 64 |
+
# Convert indices back to words
|
| 65 |
+
word_index = tokenizer.index_word
|
| 66 |
+
top_words = [word_index.get(i, "") for i in top_indices]
|
| 67 |
+
|
| 68 |
+
return [word for word in top_words if word]
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
@app.route("/predict", methods=["POST"])
|
| 72 |
+
def predict():
|
| 73 |
+
data = request.get_json()
|
| 74 |
+
text = data.get("text", "").strip()
|
| 75 |
+
|
| 76 |
+
if not text:
|
| 77 |
+
return jsonify({"error": "No text provided"}), 400
|
| 78 |
+
|
| 79 |
+
predictions = predict_next_words(text)
|
| 80 |
+
return jsonify({"predictions": predictions})
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
app.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
flask
|
| 3 |
+
numpy
|
| 4 |
+
flask_cors
|
| 5 |
+
gunicorn
|
| 6 |
+
gdown
|
tokenizer.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6c3f8818b6bb3faf829b875f7b6de44c9fba10b574f2918f3a61e8e2b11634a3
|
| 3 |
+
size 8931605
|