-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
39 lines (31 loc) · 1.23 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from flask import Flask, request, jsonify, render_template
import joblib
import pandas as pd
import os
from sklearn.feature_extraction.text import CountVectorizer
# Define the directory where your models are saved
directory = 'model'
# Load the model and other necessary components
model = joblib.load(os.path.join(directory, 'multinomial_nb_model.pkl'))
le = joblib.load(os.path.join(directory, 'label_encoder.pkl'))
count_vectorizer = joblib.load(os.path.join(directory, 'count_vectorizer.pkl'))
def predict_language(text):
x = count_vectorizer.transform([text]).toarray()
language = model.predict(x)
language = le.inverse_transform(language)
return language[0]
#Web App begins
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
text = request.form['text']
x = count_vectorizer.transform([text]).toarray()
language = model.predict(x)
language = le.inverse_transform(language)
return render_template('index.html', prediction=language[0])
if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000)) #default 5000 for testing if its not found
app.run(host='0.0.0.0', port=port) #0.0.0.0 any ip adress