Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
from flask import Flask, render_template, request, jsonify
|
| 5 |
+
|
| 6 |
+
# --- CONFIG ---
|
| 7 |
+
PORT = int(os.environ.get("PORT", 7860)) # 🔑 HF uses 7860
|
| 8 |
+
|
| 9 |
+
app = Flask(__name__, template_folder="templates")
|
| 10 |
+
|
| 11 |
+
STATS_FILE = "data/trust_stats.json"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@app.route("/")
|
| 15 |
+
def index():
|
| 16 |
+
return render_template("index.html")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@app.route("/save_trust", methods=["POST"])
|
| 20 |
+
def save_trust():
|
| 21 |
+
data = request.json
|
| 22 |
+
|
| 23 |
+
record = {
|
| 24 |
+
"scenario_id": data["scenario_id"],
|
| 25 |
+
"trust_metrics": data["trust_metrics"],
|
| 26 |
+
"trust_score": data["trust_score"],
|
| 27 |
+
"comment": data["comment"],
|
| 28 |
+
"timestamp": datetime.now().isoformat()
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
os.makedirs(os.path.dirname(STATS_FILE), exist_ok=True)
|
| 32 |
+
|
| 33 |
+
if not os.path.exists(STATS_FILE):
|
| 34 |
+
with open(STATS_FILE, "w", encoding="utf-8") as f:
|
| 35 |
+
json.dump({"evaluations": []}, f, indent=2)
|
| 36 |
+
|
| 37 |
+
with open(STATS_FILE, "r+", encoding="utf-8") as f:
|
| 38 |
+
content = json.load(f)
|
| 39 |
+
content["evaluations"].append(record)
|
| 40 |
+
f.seek(0)
|
| 41 |
+
json.dump(content, f, indent=2, ensure_ascii=False)
|
| 42 |
+
|
| 43 |
+
return jsonify({"status": "ok"})
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
print(f"🚀 Application lancée sur http://0.0.0.0:{PORT}")
|
| 48 |
+
app.run(
|
| 49 |
+
host="0.0.0.0", # 🔑 required for Docker
|
| 50 |
+
port=PORT, # 🔑 7860 for HF
|
| 51 |
+
debug=False,
|
| 52 |
+
use_reloader=False
|
| 53 |
+
)
|