Spaces:
Sleeping
Sleeping
refractor: modified code
Browse files
app.py
CHANGED
|
@@ -1,33 +1,97 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
|
| 4 |
load_dotenv()
|
| 5 |
|
| 6 |
-
API_URL = os.getenv("API_URL")
|
| 7 |
API_KEY = os.getenv("API_KEY", "")
|
| 8 |
-
assert API_URL, "API_URL manquante"
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
headers = {"X-API-Key": API_KEY} if API_KEY else {}
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
if r.status_code != 200:
|
| 18 |
return None, f"Erreur API: {r.status_code} - {r.text}"
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
with gr.Blocks() as demo:
|
| 22 |
gr.Markdown("## Remplir Quitus (Licence/Master)")
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
out_pdf = gr.File(label="Quitus rempli")
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# ui/app.py (remplace par ce bloc complet si tu veux)
|
| 2 |
+
import os, re, tempfile
|
| 3 |
+
import requests
|
| 4 |
+
import gradio as gr
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
+
API_URL = os.getenv("API_URL","http://127.0.0.1:8000/process")
|
| 10 |
API_KEY = os.getenv("API_KEY", "")
|
|
|
|
| 11 |
|
| 12 |
+
def _to_bytes(f):
|
| 13 |
+
if f is None:
|
| 14 |
+
return None
|
| 15 |
+
if isinstance(f, (bytes, bytearray)):
|
| 16 |
+
return bytes(f)
|
| 17 |
+
path = getattr(f, "name", f)
|
| 18 |
+
with open(path, "rb") as fh:
|
| 19 |
+
return fh.read()
|
| 20 |
+
|
| 21 |
+
def _base_api(url: str) -> str:
|
| 22 |
+
# retire /process final s'il existe
|
| 23 |
+
return re.sub(r"/process/?$", "", url or "")
|
| 24 |
+
|
| 25 |
+
def fill_quitus(source_pdf, doc_type):
|
| 26 |
+
if not API_URL:
|
| 27 |
+
return None, "API_URL manquante."
|
| 28 |
+
payload = _to_bytes(source_pdf)
|
| 29 |
+
if not payload:
|
| 30 |
+
return None, "Fichier vide ou introuvable."
|
| 31 |
+
|
| 32 |
+
files = {"source_pdf": ("source.pdf", payload, "application/pdf")}
|
| 33 |
+
data = {"doc_type": (doc_type or "licence").lower()}
|
| 34 |
headers = {"X-API-Key": API_KEY} if API_KEY else {}
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
r = requests.post(API_URL, files=files, data=data, headers=headers, timeout=90)
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return None, f"Erreur réseau: {e}"
|
| 40 |
+
|
| 41 |
+
ctype = (r.headers.get("content-type") or "").split(";")[0].strip().lower()
|
| 42 |
+
if r.status_code != 200 or ctype != "application/pdf":
|
| 43 |
+
return None, f"Erreur API: {r.status_code} - {r.text}"
|
| 44 |
+
|
| 45 |
+
# récupère le nom de fichier renvoyé par l'API
|
| 46 |
+
filename = f"quitus_{data['doc_type']}.pdf"
|
| 47 |
+
cd = r.headers.get("content-disposition", "")
|
| 48 |
+
m = re.search(r'filename\*?=(?:UTF-8\'\')?"?([^";]+)"?', cd, re.IGNORECASE)
|
| 49 |
+
if m:
|
| 50 |
+
filename = m.group(1)
|
| 51 |
+
|
| 52 |
+
tmp_dir = tempfile.mkdtemp()
|
| 53 |
+
out_path = os.path.join(tmp_dir, filename)
|
| 54 |
+
with open(out_path, "wb") as f:
|
| 55 |
+
f.write(r.content)
|
| 56 |
+
|
| 57 |
+
return out_path, "OK"
|
| 58 |
+
|
| 59 |
+
def download_excel():
|
| 60 |
+
base = _base_api(API_URL)
|
| 61 |
+
url = f"{base}/download/excel"
|
| 62 |
+
headers = {"X-API-Key": API_KEY} if API_KEY else {}
|
| 63 |
+
try:
|
| 64 |
+
r = requests.get(url, headers=headers, timeout=60)
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return None, f"Erreur réseau: {e}"
|
| 67 |
if r.status_code != 200:
|
| 68 |
return None, f"Erreur API: {r.status_code} - {r.text}"
|
| 69 |
+
# filename depuis Content-Disposition
|
| 70 |
+
filename = "students_data.xlsx"
|
| 71 |
+
cd = r.headers.get("content-disposition", "")
|
| 72 |
+
m = re.search(r'filename\*?=(?:UTF-8\'\')?"?([^";]+)"?', cd, re.IGNORECASE)
|
| 73 |
+
if m:
|
| 74 |
+
filename = m.group(1)
|
| 75 |
+
tmp_dir = tempfile.mkdtemp()
|
| 76 |
+
out_path = os.path.join(tmp_dir, filename)
|
| 77 |
+
with open(out_path, "wb") as f:
|
| 78 |
+
f.write(r.content)
|
| 79 |
+
return out_path, "OK"
|
| 80 |
|
| 81 |
+
with gr.Blocks(title="Quitus Filler") as demo:
|
| 82 |
gr.Markdown("## Remplir Quitus (Licence/Master)")
|
| 83 |
+
|
| 84 |
+
src = gr.File(label="PDF source (licence/master)", file_types=[".pdf"])
|
| 85 |
+
dtype = gr.Radio(["licence", "master"], value="licence", label="Type de document")
|
| 86 |
+
|
| 87 |
out_pdf = gr.File(label="Quitus rempli")
|
| 88 |
+
excel_file = gr.File(label="students_data.xlsx")
|
| 89 |
+
status = gr.Textbox(label="Statut", interactive=False)
|
| 90 |
+
|
| 91 |
+
with gr.Row():
|
| 92 |
+
gr.Button("Remplir et télécharger").click(fill_quitus, [src, dtype], [out_pdf, status])
|
| 93 |
+
gr.Button("Télécharger l’Excel").click(download_excel, [], [excel_file, status])
|
| 94 |
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
port = int(os.getenv("PORT", "7860"))
|
| 97 |
+
demo.launch(server_name="0.0.0.0", server_port=port)
|