-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
62 lines (51 loc) · 1.83 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import re
import pandas as pd
from wsgiref import simple_server
from flask import Flask, render_template, request
from src.feature_extraction import generate_features
from src.predict import predict
HOST = "0.0.0.0"
PORT = 10000
app = Flask(__name__)
def _predict(question1, question2):
question1 = question1.strip()
question2 = question2.strip()
if not re.search('[a-zA-Z]', question1):
return "Please enter a valid Question 1"
elif not re.search('[a-zA-Z]', question2):
return "Please enter a valid Question 2"
elif question1.lower() == question2.lower():
return "Similar"
df = pd.DataFrame(columns=['id', 'qid1', 'qid2', 'question1', 'question2'])
df.loc[0, :] = [0, 1, 2, question1, question2]
try:
df = generate_features(df, is_train=False)
prediction = predict(df.drop(columns=['id']), proba=True)[:, 1]
print(prediction)
except Exception as e:
print(e)
return "Sorry, something went wrong."
if prediction > 0.75:
result = "Similar"
elif prediction > 0.5:
result = f"Similar, only {round(prediction[0]*100, 1)}% confident"
elif prediction > 0.25:
result = f"Not similar, only {round(1-prediction[0], 1)*100}% confident"
else:
result = "Not similar"
return result
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
@app.route('/predict', methods=['GET', 'POST'])
def get_predictions():
if 'POST' == request.method:
question1 = str(request.form['question1'])
question2 = str(request.form['question2'])
return _predict(question1, question2)
return None
if "__main__" == __name__:
port = int(os.getenv("PORT", PORT))
server = simple_server.make_server(HOST, port=port, app=app)
server.serve_forever()