-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather_app.py
52 lines (41 loc) · 1.57 KB
/
weather_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
from flask import Flask, request, jsonify
app = Flask(__name__)
def weather_data(where: str = None, when: str = None) -> str:
'''
given a location and a time period, this custom function
returns weather forecast description in natural language.
This is a mockup function, returning a fixed text tempalte.
The function could wrap an external API returning realtime weather forecast.
parameters:
where: location as text, e.g. 'Genova, Italy'
when: time period, e.g. 'today, now'
returns:
weather forecast description as flat text.
'''
if where and when:
# return a fake/hardcoded weather forecast sentence
return f'in {where}, {when} is sunny! Temperature is 20 degrees Celsius.'
elif not where:
return 'where?'
elif not when:
return 'when?'
else:
return 'I don\'t know'
def weather(action_input: dict) -> str:
where = action_input["location"]
when = action_input["date"]
return weather_data(where=where, when=when)
@app.route('/process', methods=['POST'])
def process_request():
try:
data = request.get_json() # Get JSON payload from the request
print(data)
if data is None:
return jsonify({'error': 'No JSON data provided'}), 400
result = {}
result["output"] = weather(data["payload"])
return jsonify(result) # Sending back the JSON payload as response
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=50001, debug=True)