forked from KaushikKoirala/mlip-api-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
37 lines (28 loc) · 1.12 KB
/
app.py
File metadata and controls
37 lines (28 loc) · 1.12 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
from flask import Flask, request, jsonify, render_template
from analyze import get_itinerary
app = Flask(__name__)
app.json.ensure_ascii = False # to support UTF-8 characters
app.json.sort_keys = False
app.config["JSONIFY_PRETTYPRINT_REGULAR"] = True # always-on pretty print JSON responses
@app.route("/")
def index():
return render_template("index.html")
@app.get("/api/v1/itinerary")
def itinerary():
destination = request.args.get("destination", "").strip()
# Basic request validation
if not destination:
return jsonify({"error": "Missing required query parameter: destination"}), 400
if len(destination) > 120:
return jsonify({"error": "destination is too long (max 120 chars)"}), 400
try:
result = get_itinerary(destination)
return jsonify(result), 200
except ValueError as e:
# Client-side input errors
return jsonify({"error": str(e)}), 400
except Exception as e:
# Upstream/model errors
return jsonify({"error": f"Failed to generate itinerary: {e}"}), 502
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True)