OUAREDAEK commited on
Commit
0e449bc
·
verified ·
1 Parent(s): 945406e

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +39 -57
app.py CHANGED
@@ -26,15 +26,19 @@ from flask import Flask, render_template, request, jsonify, send_from_directory
26
  # --------------------------------------------------
27
  PORT = int(os.environ.get("PORT", 7860))
28
 
29
- CROSS_READING_DIR = "cross_reading"
 
 
 
 
 
30
 
31
  SCENARIOS_FILE = os.path.join(CROSS_READING_DIR, "scenarios.json")
32
  STATS_FILE = os.path.join(CROSS_READING_DIR, "trust_stats.json")
33
  REVIEW_FILE = os.path.join(CROSS_READING_DIR, "review.json")
34
 
35
- TEMPLATES_DIR = "templates"
36
- STATIC_DIR = "static"
37
- DATA_DIR = "data"
38
 
39
  app = Flask(
40
  __name__,
@@ -52,11 +56,14 @@ def index():
52
  @app.route("/cross_readers.html")
53
  @app.route("/cross_reading/cross_readers.html")
54
  def cross_readers():
55
- """Page d'analyse croisée"""
56
- try:
57
- return render_template("cross_readers.html")
58
- except:
59
- return send_from_directory(CROSS_READING_DIR, "cross_readers.html")
 
 
 
60
 
61
  # --------------------------------------------------
62
  # 💾 API – TRUST (quantitatif)
@@ -73,12 +80,6 @@ def save_trust():
73
  "timestamp": datetime.now().isoformat()
74
  }
75
 
76
- os.makedirs(CROSS_READING_DIR, exist_ok=True)
77
-
78
- if not os.path.exists(STATS_FILE):
79
- with open(STATS_FILE, "w", encoding="utf-8") as f:
80
- json.dump({"evaluations": []}, f, indent=2)
81
-
82
  with open(STATS_FILE, "r+", encoding="utf-8") as f:
83
  content = json.load(f)
84
  content["evaluations"].append(record)
@@ -89,24 +90,22 @@ def save_trust():
89
 
90
  @app.route("/api/evaluations")
91
  def get_evaluations():
92
- """Récupérer toutes les évaluations de confiance"""
93
- if not os.path.exists(STATS_FILE):
94
- return jsonify({"evaluations": []})
95
-
96
  with open(STATS_FILE, "r", encoding="utf-8") as f:
97
  data = json.load(f)
98
 
99
- # Normalisation des métriques (compatibilité)
100
  for e in data.get("evaluations", []):
101
  if "trust_metrics" in e:
102
  m = {}
103
  for k, v in e["trust_metrics"].items():
104
- if k == "RA": m["AR"] = v
105
- elif k == "EA": m["AE"] = v
106
- elif k == "RE": m["ESR"] = v
107
- elif k == "MCS":m["SDM"] = v
108
- elif k == "MS": m["SM"] = v
109
- else: m[k] = v
 
 
110
  e["trust_metrics"] = m
111
 
112
  return jsonify(data)
@@ -116,9 +115,6 @@ def get_evaluations():
116
  # --------------------------------------------------
117
  @app.route("/api/scenarios")
118
  def get_scenarios():
119
- if not os.path.exists(SCENARIOS_FILE):
120
- return jsonify({"scenarios": []})
121
-
122
  with open(SCENARIOS_FILE, "r", encoding="utf-8") as f:
123
  return jsonify(json.load(f))
124
 
@@ -127,9 +123,6 @@ def get_scenarios():
127
  # --------------------------------------------------
128
  @app.route("/api/reviews")
129
  def get_reviews():
130
- if not os.path.exists(REVIEW_FILE):
131
- return jsonify({"reviews": []})
132
-
133
  with open(REVIEW_FILE, "r", encoding="utf-8") as f:
134
  return jsonify(json.load(f))
135
 
@@ -144,12 +137,6 @@ def save_review():
144
  "timestamp": datetime.now().isoformat()
145
  }
146
 
147
- os.makedirs(CROSS_READING_DIR, exist_ok=True)
148
-
149
- if not os.path.exists(REVIEW_FILE):
150
- with open(REVIEW_FILE, "w", encoding="utf-8") as f:
151
- json.dump({"reviews": []}, f, indent=2)
152
-
153
  with open(REVIEW_FILE, "r+", encoding="utf-8") as f:
154
  content = json.load(f)
155
  content["reviews"].append(review)
@@ -165,35 +152,30 @@ def save_review():
165
  def serve_cross_reading(filename):
166
  return send_from_directory(CROSS_READING_DIR, filename)
167
 
168
- @app.route("/static/<path:filename>")
169
- def serve_static(filename):
170
- return send_from_directory(STATIC_DIR, filename)
171
-
172
  # --------------------------------------------------
173
  # 🚀 MAIN
174
  # --------------------------------------------------
175
  if __name__ == "__main__":
176
 
177
- print(f"🚀 Application lancée sur http://0.0.0.0:{PORT}")
178
- print(f"📊 Dashboard : http://0.0.0.0:{PORT}/")
179
- print(f"📈 Cross-reading : http://0.0.0.0:{PORT}/cross_readers.html")
180
 
181
- # Création des dossiers
182
  for folder in [CROSS_READING_DIR, TEMPLATES_DIR, STATIC_DIR, DATA_DIR]:
183
- if not os.path.exists(folder):
184
- os.makedirs(folder)
185
- print(f"📁 Dossier créé : {folder}")
186
-
187
- # Initialisation des fichiers JSON
188
- for path, content in [
189
- (SCENARIOS_FILE, {"scenarios": []}),
190
- (STATS_FILE, {"evaluations": []}),
191
- (REVIEW_FILE, {"reviews": []})
192
- ]:
193
  if not os.path.exists(path):
194
  with open(path, "w", encoding="utf-8") as f:
195
  json.dump(content, f, indent=2, ensure_ascii=False)
196
- print(f"📄 Fichier créé : {path}")
197
 
198
  app.run(
199
  host="0.0.0.0",
 
26
  # --------------------------------------------------
27
  PORT = int(os.environ.get("PORT", 7860))
28
 
29
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
30
+
31
+ CROSS_READING_DIR = os.path.join(BASE_DIR, "cross_reading")
32
+ TEMPLATES_DIR = os.path.join(BASE_DIR, "templates")
33
+ STATIC_DIR = os.path.join(BASE_DIR, "static")
34
+ DATA_DIR = os.path.join(BASE_DIR, "data")
35
 
36
  SCENARIOS_FILE = os.path.join(CROSS_READING_DIR, "scenarios.json")
37
  STATS_FILE = os.path.join(CROSS_READING_DIR, "trust_stats.json")
38
  REVIEW_FILE = os.path.join(CROSS_READING_DIR, "review.json")
39
 
40
+ CROSS_HTML = "cross_readers.html"
41
+ CROSS_JS = "cross_readers.js"
 
42
 
43
  app = Flask(
44
  __name__,
 
56
  @app.route("/cross_readers.html")
57
  @app.route("/cross_reading/cross_readers.html")
58
  def cross_readers():
59
+ """Page Cross Reading"""
60
+ if os.path.exists(os.path.join(CROSS_READING_DIR, CROSS_HTML)):
61
+ return send_from_directory(CROSS_READING_DIR, CROSS_HTML)
62
+ return "cross_readers.html introuvable", 404
63
+
64
+ @app.route("/cross_reading/cross_readers.js")
65
+ def cross_readers_js():
66
+ return send_from_directory(CROSS_READING_DIR, CROSS_JS)
67
 
68
  # --------------------------------------------------
69
  # 💾 API – TRUST (quantitatif)
 
80
  "timestamp": datetime.now().isoformat()
81
  }
82
 
 
 
 
 
 
 
83
  with open(STATS_FILE, "r+", encoding="utf-8") as f:
84
  content = json.load(f)
85
  content["evaluations"].append(record)
 
90
 
91
  @app.route("/api/evaluations")
92
  def get_evaluations():
 
 
 
 
93
  with open(STATS_FILE, "r", encoding="utf-8") as f:
94
  data = json.load(f)
95
 
96
+ # Normalisation métriques
97
  for e in data.get("evaluations", []):
98
  if "trust_metrics" in e:
99
  m = {}
100
  for k, v in e["trust_metrics"].items():
101
+ mapping = {
102
+ "RA": "AR",
103
+ "EA": "AE",
104
+ "RE": "ESR",
105
+ "MCS": "SDM",
106
+ "MS": "SM"
107
+ }
108
+ m[mapping.get(k, k)] = v
109
  e["trust_metrics"] = m
110
 
111
  return jsonify(data)
 
115
  # --------------------------------------------------
116
  @app.route("/api/scenarios")
117
  def get_scenarios():
 
 
 
118
  with open(SCENARIOS_FILE, "r", encoding="utf-8") as f:
119
  return jsonify(json.load(f))
120
 
 
123
  # --------------------------------------------------
124
  @app.route("/api/reviews")
125
  def get_reviews():
 
 
 
126
  with open(REVIEW_FILE, "r", encoding="utf-8") as f:
127
  return jsonify(json.load(f))
128
 
 
137
  "timestamp": datetime.now().isoformat()
138
  }
139
 
 
 
 
 
 
 
140
  with open(REVIEW_FILE, "r+", encoding="utf-8") as f:
141
  content = json.load(f)
142
  content["reviews"].append(review)
 
152
  def serve_cross_reading(filename):
153
  return send_from_directory(CROSS_READING_DIR, filename)
154
 
 
 
 
 
155
  # --------------------------------------------------
156
  # 🚀 MAIN
157
  # --------------------------------------------------
158
  if __name__ == "__main__":
159
 
160
+ print(f"🚀 Application : http://0.0.0.0:{PORT}")
161
+ print(f"📈 Cross Reading : http://0.0.0.0:{PORT}/cross_readers.html")
 
162
 
163
+ # Création dossiers
164
  for folder in [CROSS_READING_DIR, TEMPLATES_DIR, STATIC_DIR, DATA_DIR]:
165
+ os.makedirs(folder, exist_ok=True)
166
+
167
+ # Initialisation fichiers
168
+ defaults = {
169
+ SCENARIOS_FILE: {"scenarios": []},
170
+ STATS_FILE: {"evaluations": []},
171
+ REVIEW_FILE: {"reviews": []}
172
+ }
173
+
174
+ for path, content in defaults.items():
175
  if not os.path.exists(path):
176
  with open(path, "w", encoding="utf-8") as f:
177
  json.dump(content, f, indent=2, ensure_ascii=False)
178
+ print(f"📄 Créé : {path}")
179
 
180
  app.run(
181
  host="0.0.0.0",