chouchouvs commited on
Commit
f6e60ab
·
verified ·
1 Parent(s): fa26bd8

Delete rfp_api_app.py

Browse files
Files changed (1) hide show
  1. rfp_api_app.py +0 -280
rfp_api_app.py DELETED
@@ -1,280 +0,0 @@
1
- # rfp_api_app.py
2
- # -*- coding: utf-8 -*-
3
- """
4
- API FastAPI pour RFP Parser & Exports (DeepInfra en backend)
5
- ===========================================================
6
-
7
- Ce que fait ce fichier
8
- ----------------------
9
- - Expose 4 endpoints principaux :
10
- - GET /health : ping + modèle utilisé.
11
- - POST /submit : lance un job asynchrone {text} -> job_id.
12
- - GET /status : renvoie l'état du job, liens de résultats (raw.json, own.csv, xlsx),
13
- et un aperçu court du JSON pour “live JSON”.
14
- - GET /results/{job_id}/... : sert les artefacts (raw.json, own.csv, feuille_de_charge.xlsx).
15
-
16
- Ce que propose chaque fonction
17
- ------------------------------
18
- - new_job(text: str) -> str :
19
- Crée un job en mémoire, status "queued", retourne job_id.
20
- - set_job_status(job_id: str, **updates) -> None :
21
- Met à jour les métadonnées d'un job (thread-safe).
22
- - parse_with_deepinfra(text: str) -> dict :
23
- Construit le payload LLM (via rfp_parser.prompting.build_chat_payload),
24
- appelle DeepInfra et parse le JSON renvoyé (strippé si fenced).
25
- - persist_doc(job_dir: Path, doc: dict) -> tuple[str, str] :
26
- Sauvegarde doc dans raw.json (UTF-8), retourne (path, url).
27
- - build_csv_if_available(doc: dict, job_dir: Path) -> tuple[str|None, str|None] :
28
- (Optionnel) si ton repo expose un export CSV, le génère; sinon None.
29
- - build_xlsx(doc: dict, job_dir: Path) -> str :
30
- Construit la feuille Excel dynamique (via rfp_parser.exports_xls.build_xls_from_doc).
31
- - run_job(job_id: str, text: str) -> None :
32
- Orchestration : parse -> persist raw.json -> (own.csv si dispo) -> xlsx -> maj status.
33
-
34
- Logs
35
- ----
36
- - Logger [API] avec niveau INFO (ou DEBUG si RFP_DEBUG=1).
37
- - Traces détaillées sur DeepInfra / persistences / erreurs.
38
-
39
- Notes
40
- -----
41
- - Jobs en mémoire : si le process redémarre, l'état est perdu (simple mais suffisant en Space).
42
- - CORS permissif (*): autorise les requêtes depuis le Space Gradio.
43
- """
44
-
45
- from __future__ import annotations
46
- from typing import Dict, Any, Tuple, Optional
47
- import os, json, uuid, threading, time, traceback
48
- from pathlib import Path
49
- import logging
50
- import requests
51
-
52
- from fastapi import FastAPI, HTTPException, Query
53
- from fastapi.responses import JSONResponse, FileResponse
54
- from fastapi.middleware.cors import CORSMiddleware
55
-
56
- # === Imports depuis ton repo RFPmaster ===
57
- from rfp_parser.prompting import build_chat_payload
58
- from rfp_parser.exports_xls import build_xls_from_doc
59
- # Si tu as un export CSV (ex: rfp_parser.exports_csv), décommente et branche ici :
60
- # from rfp_parser.exports_csv import build_csv_from_doc
61
-
62
- # --------- Config ---------
63
- DEEPINFRA_API_KEY = os.environ.get("DEEPINFRA_API_KEY", "")
64
- MODEL_NAME = os.environ.get("RFP_MODEL", "meta-llama/Meta-Llama-3.1-70B-Instruct")
65
- DEEPINFRA_URL = os.environ.get("DEEPINFRA_URL", "https://api.deepinfra.com/v1/openai/chat/completions")
66
- RFP_DEBUG = str(os.environ.get("RFP_DEBUG", "0")).lower() in {"1", "true", "yes"}
67
- BASE_TMP = Path("/tmp/rfp_jobs"); BASE_TMP.mkdir(parents=True, exist_ok=True)
68
-
69
- logger = logging.getLogger("RFP_API")
70
- if not logger.handlers:
71
- h = logging.StreamHandler()
72
- h.setFormatter(logging.Formatter("[API] %(levelname)s: %(message)s"))
73
- logger.addHandler(h)
74
- logger.setLevel(logging.DEBUG if RFP_DEBUG else logging.INFO)
75
-
76
- # --------- Jobs en mémoire ---------
77
- JOBS: Dict[str, Dict[str, Any]] = {}
78
- JOBS_LOCK = threading.Lock()
79
-
80
- def new_job(text: str) -> str:
81
- job_id = uuid.uuid4().hex[:12]
82
- with JOBS_LOCK:
83
- JOBS[job_id] = {
84
- "status": "queued",
85
- "error": None,
86
- "raw_json_path": None,
87
- "raw_json_url": None,
88
- "own_csv_path": None,
89
- "own_csv_url": None,
90
- "xlsx_path": None,
91
- "xlsx_url": None,
92
- "started_at": time.time(),
93
- "done_at": None,
94
- "meta": {"model": MODEL_NAME, "length": len(text or "")},
95
- "json_preview": None, # court extrait pour le “live JSON”
96
- }
97
- return job_id
98
-
99
- def set_job_status(job_id: str, **updates):
100
- with JOBS_LOCK:
101
- if job_id in JOBS:
102
- JOBS[job_id].update(**updates)
103
-
104
- # --------- Cœur pipeline ---------
105
- def parse_with_deepinfra(text: str) -> Dict[str, Any]:
106
- if not DEEPINFRA_API_KEY:
107
- raise RuntimeError("DEEPINFRA_API_KEY manquant (Settings → Secrets du Space).")
108
- payload = build_chat_payload(text, model=MODEL_NAME)
109
- headers = {"Authorization": f"Bearer {DEEPINFRA_API_KEY}", "Content-Type": "application/json"}
110
- logger.info("DeepInfra call model=%s max_tokens=%s", payload.get("model"), payload.get("max_tokens"))
111
- r = requests.post(DEEPINFRA_URL, headers=headers, json=payload, timeout=120)
112
- if r.status_code // 100 != 2:
113
- raise RuntimeError(f"DeepInfra HTTP {r.status_code}: {r.text}")
114
- data = r.json()
115
- try:
116
- content = data["choices"][0]["message"]["content"]
117
- except Exception:
118
- raise RuntimeError(f"Réponse inattendue DeepInfra: {json.dumps(data)[:400]}")
119
- try:
120
- doc = json.loads(content)
121
- except Exception as e:
122
- logger.warning("json.loads(content) a échoué; strip fallback. Err=%s", e)
123
- doc = json.loads(content.strip().strip('`').strip())
124
- if not isinstance(doc, dict):
125
- raise RuntimeError("Le contenu renvoyé n'est pas un objet JSON.")
126
- return doc
127
-
128
- def persist_doc(job_dir: Path, doc: Dict[str, Any]) -> Tuple[str, str]:
129
- job_dir.mkdir(parents=True, exist_ok=True)
130
- raw_path = job_dir / "raw.json"
131
- with open(raw_path, "w", encoding="utf-8") as f:
132
- json.dump(doc, f, indent=2, ensure_ascii=False)
133
- raw_url = f"/results/{job_dir.name}/raw.json"
134
- return str(raw_path), raw_url
135
-
136
- def build_csv_if_available(doc: Dict[str, Any], job_dir: Path) -> Tuple[Optional[str], Optional[str]]:
137
- """
138
- Si tu as un export CSV dans ton repo, branche-le ici.
139
- Sinon, on renvoie (None, None) sans erreur pour rester permissif.
140
- """
141
- try:
142
- # Exemple si tu as build_csv_from_doc(doc, out_path)
143
- # out_path = job_dir / "own.csv"
144
- # build_csv_from_doc(doc, str(out_path))
145
- # return str(out_path), f"/results/{job_dir.name}/own.csv"
146
- return None, None
147
- except Exception as e:
148
- logger.warning("CSV non généré: %s", e)
149
- return None, None
150
-
151
- def build_xlsx(doc: Dict[str, Any], job_dir: Path) -> str:
152
- job_dir.mkdir(parents=True, exist_ok=True)
153
- out_path = str(job_dir / "feuille_de_charge.xlsx")
154
- baseline = (doc.get("assumptions") or {}).get("baseline_uop_kg") or 100.0
155
- try:
156
- baseline = float(baseline)
157
- except Exception:
158
- baseline = 100.0
159
- build_xls_from_doc(doc, out_path, baseline_kg=baseline)
160
- return out_path
161
-
162
- def run_job(job_id: str, text: str) -> None:
163
- set_job_status(job_id, status="running")
164
- job_dir = BASE_TMP / job_id
165
- try:
166
- # 1) Parse LLM
167
- doc = parse_with_deepinfra(text)
168
-
169
- # 2) Persist raw.json immédiatement (pour JSON-first côté client)
170
- raw_path, raw_url = persist_doc(job_dir, doc)
171
- preview = json.dumps(doc, ensure_ascii=False)[:1500] # court extrait
172
- set_job_status(job_id, raw_json_path=raw_path, raw_json_url=raw_url, json_preview=preview)
173
-
174
- # 3) (Optionnel) Génère own.csv si dispo
175
- csv_path, csv_url = build_csv_if_available(doc, job_dir)
176
- if csv_path and csv_url:
177
- set_job_status(job_id, own_csv_path=csv_path, own_csv_url=csv_url)
178
-
179
- # 4) XLSX (peut être le plus long)
180
- xlsx_path = build_xlsx(doc, job_dir)
181
- xlsx_url = f"/results/{job_id}/feuille_de_charge.xlsx"
182
-
183
- # 5) Terminé
184
- set_job_status(
185
- job_id,
186
- status="done",
187
- xlsx_path=xlsx_path,
188
- xlsx_url=xlsx_url,
189
- done_at=time.time(),
190
- meta={**JOBS[job_id]["meta"], "assumptions": doc.get("assumptions")},
191
- )
192
- logger.info("Job %s terminé -> %s", job_id, xlsx_path)
193
- except Exception as e:
194
- logger.error("Job %s échoué: %s\n%s", job_id, e, traceback.format_exc())
195
- set_job_status(job_id, status="error", error=str(e), done_at=time.time())
196
-
197
- # --------- FastAPI ---------
198
- app = FastAPI(title="RFP_MASTER API", version="1.1.0")
199
- app.add_middleware(
200
- CORSMiddleware,
201
- allow_origins=["*"], # restreins ici si tu veux limiter au Space Gradio
202
- allow_credentials=True,
203
- allow_methods=["*"],
204
- allow_headers=["*"],
205
- )
206
-
207
- @app.get("/health")
208
- def health():
209
- return {"ok": True, "ts": time.time(), "model": MODEL_NAME}
210
-
211
- @app.post("/submit")
212
- def submit(payload: Dict[str, Any]):
213
- text = (payload or {}).get("text", "")
214
- if not isinstance(text, str) or not text.strip():
215
- raise HTTPException(400, "Champ 'text' manquant ou vide.")
216
- job_id = new_job(text)
217
- logger.info("Submit reçu job_id=%s len(text)=%d", job_id, len(text))
218
- t = threading.Thread(target=run_job, args=(job_id, text), daemon=True)
219
- t.start()
220
- return JSONResponse({"job_id": job_id, "status": "queued"})
221
-
222
- @app.get("/status")
223
- def status(job_id: str = Query(..., description="Identifiant renvoyé par /submit")):
224
- with JOBS_LOCK:
225
- info = JOBS.get(job_id)
226
- if not info:
227
- raise HTTPException(404, f"job_id inconnu: {job_id}")
228
- # On expose les URLs disponibles + un petit aperçu JSON (pour “live JSON”)
229
- return JSONResponse({
230
- "job_id": job_id,
231
- "status": info.get("status"),
232
- "error": info.get("error"),
233
- "meta": info.get("meta"),
234
- "raw_json_url": info.get("raw_json_url"),
235
- "own_csv_url": info.get("own_csv_url"),
236
- "xlsx_url": info.get("xlsx_url"),
237
- "json_preview": info.get("json_preview"),
238
- })
239
-
240
- # ---- Résultats ----
241
- @app.get("/results/{job_id}/raw.json")
242
- def download_raw(job_id: str):
243
- with JOBS_LOCK:
244
- info = JOBS.get(job_id)
245
- if not info:
246
- raise HTTPException(404, f"job_id inconnu: {job_id}")
247
- raw_path = info.get("raw_json_path")
248
- if not raw_path or not Path(raw_path).exists():
249
- raise HTTPException(404, "raw.json indisponible.")
250
- return FileResponse(raw_path, media_type="application/json", filename="raw.json")
251
-
252
- @app.get("/results/{job_id}/own.csv")
253
- def download_csv(job_id: str):
254
- with JOBS_LOCK:
255
- info = JOBS.get(job_id)
256
- if not info:
257
- raise HTTPException(404, f"job_id inconnu: {job_id}")
258
- csv_path = info.get("own_csv_path")
259
- if not csv_path:
260
- raise HTTPException(404, "own.csv non généré sur ce job.")
261
- if not Path(csv_path).exists():
262
- raise HTTPException(404, "own.csv indisponible.")
263
- return FileResponse(csv_path, media_type="text/csv", filename="own.csv")
264
-
265
- @app.get("/results/{job_id}/feuille_de_charge.xlsx")
266
- def download_xlsx(job_id: str):
267
- with JOBS_LOCK:
268
- info = JOBS.get(job_id)
269
- if not info:
270
- raise HTTPException(404, f"job_id inconnu: {job_id}")
271
- if info.get("status") != "done":
272
- raise HTTPException(409, f"job {job_id} non prêt (status={info.get('status')})")
273
- xlsx_path = info.get("xlsx_path")
274
- if not xlsx_path or not Path(xlsx_path).exists():
275
- raise HTTPException(404, "Fichier indisponible.")
276
- return FileResponse(
277
- xlsx_path,
278
- media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
279
- filename="feuille_de_charge.xlsx",
280
- )