S01Nour commited on
Commit
8495bea
·
1 Parent(s): 030f20c

refractor: modified code

Browse files
Files changed (1) hide show
  1. app.py +84 -20
app.py CHANGED
@@ -1,33 +1,97 @@
1
- import os, gradio as gr, requests
 
 
 
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 fill_quitus(source_pdf, quitus_pdf, doc_type):
11
- files = {
12
- "source_pdf": ("source.pdf", source_pdf, "application/pdf"),
13
- "quitus_pdf": ("quitus.pdf", quitus_pdf, "application/pdf"),
14
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  headers = {"X-API-Key": API_KEY} if API_KEY else {}
16
- r = requests.post(API_URL, files=files, data={"doc_type": doc_type}, headers=headers)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  if r.status_code != 200:
18
  return None, f"Erreur API: {r.status_code} - {r.text}"
19
- return (r.content, "quitus_filled.pdf"), "OK"
 
 
 
 
 
 
 
 
 
 
20
 
21
- with gr.Blocks() as demo:
22
  gr.Markdown("## Remplir Quitus (Licence/Master)")
23
- with gr.Row():
24
- src = gr.File(label="PDF source (licence/master)", file_types=[".pdf"])
25
- # q = gr.File(label="Modèle Quitus (PDF)", file_types=[".pdf"])
26
- doc_type = gr.Radio(["licence","master"], value="licence", label="Type de document")
27
  out_pdf = gr.File(label="Quitus rempli")
28
- status = gr.Textbox(label="Statut")
29
- btn = gr.Button("Remplir et télécharger")
30
- # btn.click(fill_quitus, inputs=[src,q,doc_type], outputs=[out_pdf, status])
31
- btn.click(fill_quitus, inputs=[src,doc_type], outputs=[out_pdf, status])
 
 
32
 
33
- demo.launch()
 
 
 
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)