chouchouvs commited on
Commit
60865cc
·
verified ·
1 Parent(s): 73a463e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +172 -0
app.py ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py — Space API FastAPI (Docker)
2
+ from __future__ import annotations
3
+ from typing import Dict, Any
4
+ import os, json, uuid, threading, time, traceback
5
+ from pathlib import Path
6
+ import logging, requests
7
+
8
+ from fastapi import FastAPI, HTTPException, Query
9
+ from fastapi.responses import JSONResponse, FileResponse
10
+ from fastapi.middleware.cors import CORSMiddleware
11
+
12
+ # --- Remplace ces imports par ton vrai parseur/exports
13
+ # from rfp_parser.prompting import build_chat_payload
14
+ # from rfp_parser.exports_xls import build_xls_from_doc
15
+
16
+ def build_chat_payload(text: str, model: str) -> Dict[str, Any]:
17
+ # TODO: branche ton vrai payload
18
+ return {
19
+ "model": model,
20
+ "max_tokens": 2048,
21
+ "messages": [{"role":"user","content": text}],
22
+ "temperature": 0.2,
23
+ }
24
+
25
+ def build_xls_from_doc(doc: Dict[str, Any], out_path: str, baseline_kg: float = 100.0):
26
+ # TODO: branche ton vrai export XLSX
27
+ # Ici on crée juste un xlsx vide pour la démo
28
+ import pandas as pd
29
+ df = pd.DataFrame([{"baseline_kg": baseline_kg, "ok": True}])
30
+ df.to_excel(out_path, index=False)
31
+
32
+ # ---------------- Config ----------------
33
+ DEEPINFRA_API_KEY = os.environ.get("DEEPINFRA_API_KEY", "")
34
+ MODEL_NAME = os.environ.get("RFP_MODEL", "meta-llama/Meta-Llama-3.1-70B-Instruct")
35
+ DEEPINFRA_URL = os.environ.get("DEEPINFRA_URL", "https://api.deepinfra.com/v1/openai/chat/completions")
36
+ RFP_DEBUG = str(os.environ.get("RFP_DEBUG", "0")).lower() in {"1", "true", "yes"}
37
+ BASE_TMP = Path("/tmp/rfp_jobs"); BASE_TMP.mkdir(parents=True, exist_ok=True)
38
+
39
+ logger = logging.getLogger("RFP_API")
40
+ if not logger.handlers:
41
+ h = logging.StreamHandler()
42
+ h.setFormatter(logging.Formatter("[API] %(levelname)s: %(message)s"))
43
+ logger.addHandler(h)
44
+ logger.setLevel(logging.DEBUG if RFP_DEBUG else logging.INFO)
45
+
46
+ # -------------- Jobs en mémoire --------------
47
+ JOBS: Dict[str, Dict[str, Any]] = {}
48
+ JOBS_LOCK = threading.Lock()
49
+
50
+ def new_job(text: str) -> str:
51
+ job_id = uuid.uuid4().hex[:12]
52
+ with JOBS_LOCK:
53
+ JOBS[job_id] = {
54
+ "status": "queued",
55
+ "error": None,
56
+ "xlsx_path": None,
57
+ "xlsx_url": None,
58
+ "started_at": time.time(),
59
+ "done_at": None,
60
+ "meta": {"model": MODEL_NAME, "length": len(text or "")},
61
+ }
62
+ return job_id
63
+
64
+ def set_job_status(job_id: str, **updates):
65
+ with JOBS_LOCK:
66
+ if job_id in JOBS:
67
+ JOBS[job_id].update(**updates)
68
+
69
+ # -------------- Cœur pipeline --------------
70
+ def parse_with_deepinfra(text: str) -> Dict[str, Any]:
71
+ if not DEEPINFRA_API_KEY:
72
+ raise RuntimeError("DEEPINFRA_API_KEY non défini.")
73
+ payload = build_chat_payload(text, model=MODEL_NAME)
74
+ headers = {"Authorization": f"Bearer {DEEPINFRA_API_KEY}", "Content-Type": "application/json"}
75
+ logger.info("Appel DeepInfra model=%s max_tokens=%s", payload.get("model"), payload.get("max_tokens"))
76
+ r = requests.post(DEEPINFRA_URL, headers=headers, json=payload, timeout=120)
77
+ if r.status_code // 100 != 2:
78
+ raise RuntimeError(f"DeepInfra HTTP {r.status_code}: {r.text}")
79
+ data = r.json()
80
+ try:
81
+ content = data["choices"][0]["message"]["content"]
82
+ except Exception:
83
+ raise RuntimeError(f"Réponse inattendue DeepInfra: {json.dumps(data)[:400]}")
84
+ try:
85
+ doc = json.loads(content)
86
+ except Exception as e:
87
+ logger.warning("Échec json.loads(content); tentative strip. Err=%s", e)
88
+ doc = json.loads(content.strip().strip('`').strip())
89
+ if not isinstance(doc, dict):
90
+ raise RuntimeError("Le contenu renvoyé n'est pas un objet JSON.")
91
+ return doc
92
+
93
+ def build_xlsx(doc: Dict[str, Any], job_dir: Path) -> str:
94
+ job_dir.mkdir(parents=True, exist_ok=True)
95
+ out_path = str(job_dir / "feuille_de_charge.xlsx")
96
+ baseline = (doc.get("assumptions") or {}).get("baseline_uop_kg") or 100.0
97
+ try:
98
+ baseline = float(baseline)
99
+ except Exception:
100
+ baseline = 100.0
101
+ build_xls_from_doc(doc, out_path, baseline_kg=baseline)
102
+ return out_path
103
+
104
+ def run_job(job_id: str, text: str) -> None:
105
+ set_job_status(job_id, status="running")
106
+ job_dir = BASE_TMP / job_id
107
+ try:
108
+ doc = parse_with_deepinfra(text)
109
+ xlsx_path = build_xlsx(doc, job_dir)
110
+ xlsx_url = f"/results/{job_id}/feuille_de_charge.xlsx" # pas de /api en Docker (c’est la racine)
111
+ set_job_status(job_id, status="done", xlsx_path=xlsx_path, xlsx_url=xlsx_url,
112
+ done_at=time.time(), meta={**JOBS[job_id]["meta"], "assumptions": doc.get("assumptions")})
113
+ logger.info("Job %s terminé -> %s", job_id, xlsx_path)
114
+ except Exception as e:
115
+ logger.error("Job %s échoué: %s\n%s", job_id, e, traceback.format_exc())
116
+ set_job_status(job_id, status="error", error=str(e), done_at=time.time())
117
+
118
+ # -------------- FastAPI app --------------
119
+ app = FastAPI(title="RFP_MASTER API", version="1.0.0")
120
+ app.add_middleware(
121
+ CORSMiddleware,
122
+ allow_origins=["*"],
123
+ allow_credentials=True,
124
+ allow_methods=["*"],
125
+ allow_headers=["*"],
126
+ )
127
+
128
+ @app.get("/health")
129
+ def health():
130
+ return {"ok": True, "ts": time.time(), "model": MODEL_NAME}
131
+
132
+ @app.post("/submit")
133
+ def submit(payload: Dict[str, Any]):
134
+ text = (payload or {}).get("text", "")
135
+ if not isinstance(text, str) or not text.strip():
136
+ raise HTTPException(400, "Champ 'text' manquant ou vide.")
137
+ job_id = new_job(text)
138
+ logger.info("Submit reçu job_id=%s len(text)=%d", job_id, len(text))
139
+ t = threading.Thread(target=run_job, args=(job_id, text), daemon=True)
140
+ t.start()
141
+ return JSONResponse({"job_id": job_id, "status": "queued"})
142
+
143
+ @app.get("/status")
144
+ def status(job_id: str = Query(..., description="Identifiant renvoyé par /submit")):
145
+ with JOBS_LOCK:
146
+ info = JOBS.get(job_id)
147
+ if not info:
148
+ raise HTTPException(404, f"job_id inconnu: {job_id}")
149
+ return JSONResponse({
150
+ "job_id": job_id,
151
+ "status": info.get("status"),
152
+ "xlsx_url": info.get("xlsx_url"),
153
+ "error": info.get("error"),
154
+ "meta": info.get("meta"),
155
+ })
156
+
157
+ @app.get("/results/{job_id}/feuille_de_charge.xlsx")
158
+ def download(job_id: str):
159
+ with JOBS_LOCK:
160
+ info = JOBS.get(job_id)
161
+ if not info:
162
+ raise HTTPException(404, f"job_id inconnu: {job_id}")
163
+ if info.get("status") != "done":
164
+ raise HTTPException(409, f"job {job_id} non prêt (status={info.get('status')})")
165
+ xlsx_path = info.get("xlsx_path")
166
+ if not xlsx_path or not Path(xlsx_path).exists():
167
+ raise HTTPException(404, "Fichier indisponible.")
168
+ return FileResponse(
169
+ xlsx_path,
170
+ media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
171
+ filename="feuille_de_charge.xlsx",
172
+ )