File size: 1,710 Bytes
221e2fc
 
1825498
 
221e2fc
 
1825498
221e2fc
1825498
 
221e2fc
a8fdef3
 
1825498
a8fdef3
1825498
 
221e2fc
 
1825498
 
 
 
 
 
 
 
 
 
 
221e2fc
1825498
 
 
 
 
 
 
 
 
 
 
 
2e69532
 
1825498
 
 
 
 
 
 
 
221e2fc
1825498
 
 
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
from flask import Flask, request, jsonify
import requests
from flask_cors import CORS
import datetime

app = Flask(__name__)
CORS(app)

# πŸ” Your OpenWeather API key (secured inside backend)
API_KEY = "6d0b5a223205f8e88b2b9d45a0ad532a"  # <-- Replace with your real key

@app.route("/")
def index():
    return "βœ… Weather API and Response Logger is running!"

# πŸ” Weather info endpoint (used by GPT)
@app.route("/get_weather", methods=["GET"])
def get_weather():
    city = request.args.get("city")
    if not city:
        return jsonify({"error": "Missing city parameter"}), 400
    try:
        res = requests.get(
            f"http://api.openweathermap.org/data/2.5/weather",
            params={"q": city, "appid": API_KEY, "units": "metric"}
        )
        data = res.json()
        return jsonify(data)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

# 🧠 GPT response logger endpoint
@app.route("/store_response", methods=["POST"])
def store_response():
    try:
        data = request.get_json()
        city = data.get("city")
        response = data.get("response")
        timestamp = datetime.datetime.now().isoformat()

        log_entry = f"[{timestamp}] City: {city} | GPT Response: {response}\n"
        print(log_entry)

        # βœ… Use temporary folder that HF Spaces allow
        with open("/tmp/gpt_responses.log", "a") as f:
            f.write(log_entry)

        return jsonify({"status": "received", "message": "GPT response logged"}), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500

# Hugging Face Spaces requirement
def launch():
    app.run(host="0.0.0.0", port=7860)

if __name__ == "__main__":
    launch()