Spaces:
Sleeping
Sleeping
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 | |
def index(): | |
return "β Weather API and Response Logger is running!" | |
# π Weather info endpoint (used by GPT) | |
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 | |
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() |