KavinduHansaka commited on
Commit
1825498
Β·
verified Β·
1 Parent(s): a8fdef3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -6
app.py CHANGED
@@ -1,19 +1,57 @@
1
  from flask import Flask, request, jsonify
2
  import requests
 
 
3
 
4
  app = Flask(__name__)
 
5
 
6
- API_KEY = "6d0b5a223205f8e88b2b9d45a0ad532a"
 
7
 
8
- @app.route("/get_weather", methods=["GET"])
9
  @app.route("/")
10
  def index():
11
- return "βœ… Weather API is running!"
12
 
 
 
13
  def get_weather():
14
  city = request.args.get("city")
15
- res = requests.get(f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric")
16
- return jsonify(res.json())
 
 
 
 
 
 
 
 
 
17
 
18
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  app.run(host="0.0.0.0", port=7860)
 
 
 
 
1
  from flask import Flask, request, jsonify
2
  import requests
3
+ from flask_cors import CORS
4
+ import datetime
5
 
6
  app = Flask(__name__)
7
+ CORS(app)
8
 
9
+ # πŸ” Your OpenWeather API key (secured inside backend)
10
+ API_KEY = "6d0b5a223205f8e88b2b9d45a0ad532a" # <-- Replace with your real key
11
 
 
12
  @app.route("/")
13
  def index():
14
+ return "βœ… Weather API and Response Logger is running!"
15
 
16
+ # πŸ” Weather info endpoint (used by GPT)
17
+ @app.route("/get_weather", methods=["GET"])
18
  def get_weather():
19
  city = request.args.get("city")
20
+ if not city:
21
+ return jsonify({"error": "Missing city parameter"}), 400
22
+ try:
23
+ res = requests.get(
24
+ f"http://api.openweathermap.org/data/2.5/weather",
25
+ params={"q": city, "appid": API_KEY, "units": "metric"}
26
+ )
27
+ data = res.json()
28
+ return jsonify(data)
29
+ except Exception as e:
30
+ return jsonify({"error": str(e)}), 500
31
 
32
+ # 🧠 GPT response logger endpoint
33
+ @app.route("/store_response", methods=["POST"])
34
+ def store_response():
35
+ try:
36
+ data = request.get_json()
37
+ city = data.get("city")
38
+ response = data.get("response")
39
+ timestamp = datetime.datetime.now().isoformat()
40
+
41
+ log_entry = f"[{timestamp}] City: {city} | GPT Response: {response}\n"
42
+ print(log_entry)
43
+
44
+ # Optionally, log to file (comment out if not needed on HF Spaces)
45
+ with open("gpt_responses.log", "a") as f:
46
+ f.write(log_entry)
47
+
48
+ return jsonify({"status": "received", "message": "GPT response logged"}), 200
49
+ except Exception as e:
50
+ return jsonify({"error": str(e)}), 500
51
+
52
+ # Hugging Face Spaces requirement
53
+ def launch():
54
  app.run(host="0.0.0.0", port=7860)
55
+
56
+ if __name__ == "__main__":
57
+ launch()