-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (50 loc) · 1.73 KB
/
Copy pathmain.py
File metadata and controls
67 lines (50 loc) · 1.73 KB
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
63
64
65
66
67
# app/routes.py
from flask import Flask, render_template, jsonify, request, session
from nlp.Amadeus import *
import dotenv
import os
app = Flask(__name__)
app.secret_key = 'secret'
config = dotenv.dotenv_values()
@app.route('/')
def home():
session.clear()
return render_template('index.html', message='This is a message')
@app.route('/send', methods=['POST'])
async def send():
raw_input = json.loads(request.data)['inputText']
chat_history = session.get('chat_history', [])
input = get_new_input(raw_input, chat_history)
print(raw_input, "NEW INPUT", input, flush=True)
try:
processed = await get_best_flights(input, chat_history)
except FieldException as e:
# print("Field exception", e, flush=True)
session['chat_history'] = e.data.get('chat_history')
return jsonify({
'missing': True,
'message': e.data.get('answer')
})
except AmadeusException as e:
session['chat_history'] = []
return jsonify({
'missing': True,
'message': e.message
})
session.clear()
best_flights = processed['best_flights']
if not len(best_flights):
return {
'missing': True,
'message': "Seems like I can not find any flights from my database for your request. Can you try a different request?"
}
best_flights = sorted(best_flights, key=lambda x: float(x['price']['grandTotal']))
# Sort the best_flights by price
return jsonify({
'ok': True,
'missing': False,
'best_flights': best_flights[:10],
'requests': processed['requests']
})
if __name__ == '__main__':
app.run(debug=True, port=os.environ.get('PORT', 5000), host='0.0.0.0')