Markuspierre commited on
Commit
fb07b1e
·
verified ·
1 Parent(s): 04e7c52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +193 -198
app.py CHANGED
@@ -1,30 +1,23 @@
1
-
2
  import os
3
  import tempfile
4
-
5
  import gradio as gr
6
  import pandas as pd
7
  import pytesseract
8
-
9
  import ocr_core as oc
10
 
11
  try:
12
  import spaces
13
-
14
  @spaces.GPU
15
  def _gpu_placeholder():
16
  return None
17
  except Exception:
18
  pass
19
 
20
-
21
  COLS = oc.COLS
22
 
23
-
24
  # =========================================================
25
  # STYLE CSS
26
  # =========================================================
27
-
28
  CSS = """
29
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,400,0,0');
30
 
@@ -43,121 +36,201 @@ CSS = """
43
  }
44
 
45
  .gradio-container {
46
- max-width: 1250px !important;
47
- margin: auto !important;
48
- font-family: 'Inter', sans-serif !important;
 
49
  }
50
 
51
- body {
52
- background: #f4f7fb !important;
53
  }
54
 
 
55
  .hero {
56
- background: linear-gradient(120deg, #10243f, #1d426c);
57
- padding: 32px;
58
- border-radius: 20px;
59
  color: white;
60
- margin-bottom: 22px;
61
- box-shadow: 0 8px 25px rgba(20, 45, 75, 0.12);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  }
63
 
64
  .hero h1 {
65
  color: white !important;
66
- font-size: 30px !important;
67
  font-weight: 800 !important;
68
- margin-bottom: 8px !important;
 
69
  }
70
 
71
  .hero p {
72
- color: #d3e3f5 !important;
73
- font-size: 14px;
74
- }
75
-
76
- .hero-tag {
77
- display: inline-flex;
78
- align-items: center;
79
- gap: 7px;
80
- background: rgba(104, 171, 255, 0.18);
81
- border: 1px solid rgba(104, 171, 255, 0.4);
82
- padding: 7px 13px;
83
- border-radius: 30px;
84
- color: white;
85
- font-size: 12px;
86
- margin-bottom: 15px;
87
  }
88
 
89
  .hero-features {
90
- margin-top: 22px;
91
  display: flex;
92
  flex-wrap: wrap;
93
- gap: 12px;
94
  }
95
 
96
  .hero-feature {
97
  display: inline-flex;
98
  align-items: center;
99
  gap: 8px;
100
- background: rgba(255,255,255,0.09);
101
- padding: 9px 13px;
102
- border-radius: 9px;
103
- color: #e3efff;
104
- font-size: 12px;
 
 
105
  }
106
 
107
  .hero-feature .material-symbols-outlined {
108
  color: #68abff;
 
109
  }
110
 
 
111
  .panel {
112
- background: white;
113
- border: 1px solid #e5eaf1;
114
  border-radius: 16px;
115
- padding: 20px;
116
- box-shadow: 0 3px 12px rgba(20, 45, 75, 0.04);
 
117
  }
118
 
119
  .section-title {
120
- color: #183453;
 
 
 
121
  font-weight: 700;
122
- font-size: 17px;
123
- margin-bottom: 5px;
 
 
 
 
 
124
  }
125
 
126
  .section-description {
127
- color: #78889b;
128
  font-size: 13px;
129
  margin-bottom: 18px;
 
130
  }
131
 
 
132
  .primary-btn {
133
- background: #68abff !important;
134
- color: #10243f !important;
135
  border: none !important;
136
- border-radius: 10px !important;
137
  font-weight: 700 !important;
138
- min-height: 48px !important;
 
 
 
 
139
  }
140
 
141
  .primary-btn:hover {
142
- background: #4c98f5 !important;
 
 
143
  }
144
 
145
  .secondary-btn {
146
  border-radius: 10px !important;
 
 
 
 
 
 
147
  }
148
 
149
  .result-title {
150
- color: #183453;
151
- font-size: 21px;
 
 
 
152
  font-weight: 750;
153
  }
154
 
155
- .footer {
156
- text-align: center;
157
- color: #94a3b8;
158
- font-size: 12px;
159
- padding: 20px;
160
- margin-top: 15px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  }
162
 
163
  .category-list {
@@ -171,43 +244,66 @@ body {
171
  align-items: center;
172
  gap: 6px;
173
  background: white;
174
- padding: 8px 12px;
175
- border-radius: 8px;
176
- color: #2563eb;
177
  font-weight: 600;
178
  font-size: 12px;
179
- border: 1px solid #e1edfc;
 
180
  }
181
 
182
  .category-item .material-symbols-outlined {
183
- font-size: 17px;
 
184
  }
185
 
186
- """
 
 
 
 
 
187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
 
189
  # =========================================================
190
  # TRAITEMENT ET EXPORT
191
  # =========================================================
192
-
193
  def build_outputs(df):
194
-
195
  df = df[df["Image"].astype(str).str.upper() != "TOTAL"].copy()
196
-
197
  if "Remarque" not in df:
198
  df["Remarque"] = ""
199
-
200
  for c in COLS:
201
  df[c] = pd.to_numeric(
202
  df[c], errors="coerce"
203
  ).fillna(0).astype(int)
204
-
205
  total = {
206
  "Image": "TOTAL",
207
  **{c: int(df[c].sum()) for c in COLS},
208
  "Remarque": ""
209
  }
210
-
211
  report = pd.concat(
212
  [
213
  df[["Image"] + COLS + ["Remarque"]],
@@ -215,96 +311,72 @@ def build_outputs(df):
215
  ],
216
  ignore_index=True
217
  )
218
-
219
  folder = tempfile.mkdtemp()
220
-
221
  csv_path = os.path.join(folder, "decompte.csv")
222
  xlsx_path = os.path.join(folder, "decompte.xlsx")
223
-
224
  report.to_csv(
225
  csv_path,
226
  index=False,
227
  sep=";",
228
  encoding="utf-8-sig"
229
  )
230
-
231
  report.to_excel(
232
  xlsx_path,
233
  index=False,
234
  sheet_name="Décompte"
235
  )
236
-
237
  return report, [csv_path, xlsx_path]
238
 
239
 
240
  # =========================================================
241
  # ANALYSE OCR
242
  # =========================================================
243
-
244
  def run(files, lang, psm, fuzzy):
245
-
246
  if not files:
247
  raise gr.Error("Veuillez importer au moins une image.")
248
-
249
  rows = []
250
-
251
  for f in files:
252
-
253
  path = f if isinstance(f, str) else f.name
254
  name = os.path.basename(path)
255
-
256
  try:
257
-
258
  counts, notes = oc.analyse(
259
  path,
260
  lang=lang,
261
  psm=int(psm),
262
  fuzzy=fuzzy
263
  )
264
-
265
  except pytesseract.TesseractNotFoundError:
266
-
267
  raise gr.Error(
268
  "Tesseract est introuvable sur le serveur."
269
  )
270
-
271
  except Exception as e:
272
-
273
  rows.append({
274
  "Image": name,
275
  **{c: 0 for c in COLS},
276
  "Remarque": f"Erreur : {e}"
277
  })
278
-
279
  continue
280
-
281
  rows.append({
282
  "Image": name,
283
  **counts,
284
  "Remarque": " ; ".join(notes)
285
  })
286
-
287
  report, out = build_outputs(pd.DataFrame(rows))
288
-
289
  return report, out
290
 
291
 
292
  # =========================================================
293
  # RECALCUL
294
  # =========================================================
295
-
296
  def recompute(df):
297
-
298
  if df is None:
299
  raise gr.Error("Aucun tableau à recalculer.")
300
-
301
  return build_outputs(pd.DataFrame(df))
302
 
303
 
304
  # =========================================================
305
  # INTERFACE
306
  # =========================================================
307
-
308
  with gr.Blocks(
309
  title="OCR COUNT | Décompte intelligent",
310
  theme=gr.themes.Soft(
@@ -316,108 +388,74 @@ with gr.Blocks(
316
  ) as demo:
317
 
318
  # EN-TÊTE
319
-
320
  gr.HTML("""
321
  <div class="hero">
322
-
323
  <div class="hero-tag">
324
- <span class="material-symbols-outlined"
325
- style="font-size:16px;">verified</span>
326
- SYSTÈME DE DÉCOMPTE INTELLIGENT
327
  </div>
328
-
329
  <h1>OCR COUNT</h1>
330
-
331
  <p>
332
  Plateforme de reconnaissance et de décompte automatique
333
  des données à partir de tableaux photographiés.
334
  </p>
335
-
336
  <div class="hero-features">
337
-
338
  <div class="hero-feature">
339
- <span class="material-symbols-outlined">
340
- document_scanner
341
- </span>
342
  Reconnaissance OCR
343
  </div>
344
-
345
  <div class="hero-feature">
346
- <span class="material-symbols-outlined">
347
- analytics
348
- </span>
349
  Analyse des données
350
  </div>
351
-
352
  <div class="hero-feature">
353
- <span class="material-symbols-outlined">
354
- download
355
- </span>
356
  Exportation des rapports
357
  </div>
358
-
359
  </div>
360
-
361
  </div>
362
  """)
363
 
364
  # IMPORTATION
365
-
366
  with gr.Group(elem_classes="panel"):
367
-
368
  gr.HTML("""
369
  <div class="section-title">
370
- <span class="material-symbols-outlined"
371
- style="color:#68abff;">cloud_upload</span>
372
  Importation des documents
373
  </div>
374
-
375
  <div class="section-description">
376
- Sélectionnez une ou plusieurs photographies contenant
377
- les tableaux à analyser.
378
  </div>
379
  """)
380
-
381
  files = gr.File(
382
  label="Déposer vos images ici",
383
  file_count="multiple",
384
  file_types=["image"],
385
  type="filepath"
386
  )
387
-
388
  gr.Markdown(
389
- "Formats acceptés : **JPG · JPEG · PNG**"
390
- " &nbsp; | &nbsp; Plusieurs images simultanément."
391
  )
392
 
393
  # CONFIGURATION OCR
394
-
395
- with gr.Accordion(
396
- "Configuration avancée de l'OCR",
397
- open=False
398
- ):
399
-
400
  gr.HTML("""
401
- <div class="section-description">
402
  Personnalisez les paramètres de reconnaissance des caractères.
403
  </div>
404
  """)
405
-
406
  with gr.Row():
407
-
408
  lang = gr.Textbox(
409
  value="eng",
410
  label="Langue OCR",
411
  info="Langue utilisée par Tesseract"
412
  )
413
-
414
  psm = gr.Dropdown(
415
- choices=[11, 6, 4],
416
  value=11,
417
  label="Mode de segmentation",
418
  info="Mode de lecture du tableau"
419
  )
420
-
421
  fuzzy = gr.Checkbox(
422
  value=True,
423
  label="Activer la tolérance aux erreurs de lecture",
@@ -425,7 +463,6 @@ with gr.Blocks(
425
  )
426
 
427
  # BOUTON D'ANALYSE
428
-
429
  go = gr.Button(
430
  "LANCER LE DÉCOMPTE",
431
  variant="primary",
@@ -434,30 +471,20 @@ with gr.Blocks(
434
  )
435
 
436
  # RÉSULTATS
437
-
438
  gr.HTML("""
439
- <div style="margin:28px 0 12px 0;">
440
-
441
  <div class="result-title">
442
- <span class="material-symbols-outlined"
443
- style="color:#68abff;font-size:26px;">
444
- analytics
445
- </span>
446
  Résultats de l'analyse
447
  </div>
448
-
449
- <div style="color:#8291a4;font-size:13px;margin-top:5px;">
450
- Consultez les données extraites, corrigez les valeurs
451
- si nécessaire et recalculez les totaux.
452
  </div>
453
-
454
  </div>
455
  """)
456
 
457
  # TABLEAU
458
-
459
  with gr.Group(elem_classes="panel"):
460
-
461
  table = gr.Dataframe(
462
  headers=["Image"] + COLS + ["Remarque"],
463
  datatype=[
@@ -475,7 +502,6 @@ with gr.Blocks(
475
  label="Rapport détaillé",
476
  wrap=True
477
  )
478
-
479
  recalc = gr.Button(
480
  "Recalculer le total",
481
  variant="secondary",
@@ -483,21 +509,16 @@ with gr.Blocks(
483
  )
484
 
485
  # EXPORT
486
-
487
  with gr.Group(elem_classes="panel"):
488
-
489
  gr.HTML("""
490
  <div class="section-title">
491
- <span class="material-symbols-outlined"
492
- style="color:#68abff;">file_download</span>
493
  Exportation des résultats
494
  </div>
495
-
496
  <div class="section-description">
497
  Téléchargez votre rapport au format Excel ou CSV.
498
  </div>
499
  """)
500
-
501
  exports = gr.File(
502
  label="Fichiers générés",
503
  file_count="multiple",
@@ -505,91 +526,65 @@ with gr.Blocks(
505
  )
506
 
507
  # CATÉGORIES
508
-
509
  gr.HTML("""
510
- <div style="
511
- background:#edf5ff;
512
- border:1px solid #d8e9ff;
513
- padding:20px;
514
- border-radius:13px;
515
- margin-top:18px;
516
- ">
517
-
518
- <div style="font-weight:700;color:#183453;margin-bottom:12px;">
519
- <span class="material-symbols-outlined"
520
- style="color:#68abff;">category</span>
521
  Catégories prises en compte
522
  </div>
523
-
524
  <div class="category-list">
525
-
526
  <div class="category-item">
527
  <span class="material-symbols-outlined">payments</span>
528
  PASF
529
  </div>
530
-
531
  <div class="category-item">
532
  <span class="material-symbols-outlined">health_and_safety</span>
533
  IPM / ASS
534
  </div>
535
-
536
  <div class="category-item">
537
  <span class="material-symbols-outlined">receipt_long</span>
538
  PR
539
  </div>
540
-
541
  <div class="category-item">
542
  <span class="material-symbols-outlined">account_balance</span>
543
  MUT
544
  </div>
545
-
546
  <div class="category-item">
547
  <span class="material-symbols-outlined">fact_check</span>
548
  RESULTAT
549
  </div>
550
-
551
  <div class="category-item">
552
  <span class="material-symbols-outlined">verified_user</span>
553
  CONTROL
554
  </div>
555
-
556
  </div>
557
-
558
- <div style="font-size:12px;color:#64748b;margin-top:12px;">
559
  IPM et ASS sont regroupés dans une seule catégorie : IPM.
560
  Seule la deuxième colonne du tableau est comptabilisée.
561
  </div>
562
-
563
  </div>
564
  """)
565
 
566
  # PIED DE PAGE
567
-
568
  gr.HTML("""
569
  <div class="footer">
570
- <span class="material-symbols-outlined"
571
- style="font-size:16px;">document_scanner</span>
572
- <br>
573
- OCR COUNT · Système intelligent de décompte
574
- <br>
575
  Reconnaissance automatique · Analyse · Exportation
576
  </div>
577
  """)
578
 
579
  # ÉVÉNEMENTS
580
-
581
  go.click(
582
  fn=run,
583
  inputs=[files, lang, psm, fuzzy],
584
  outputs=[table, exports]
585
  )
586
-
587
  recalc.click(
588
  fn=recompute,
589
  inputs=table,
590
  outputs=[table, exports]
591
  )
592
 
593
-
594
  if __name__ == "__main__":
595
- demo.launch(ssr_mode=False)
 
 
1
  import os
2
  import tempfile
 
3
  import gradio as gr
4
  import pandas as pd
5
  import pytesseract
 
6
  import ocr_core as oc
7
 
8
  try:
9
  import spaces
 
10
  @spaces.GPU
11
  def _gpu_placeholder():
12
  return None
13
  except Exception:
14
  pass
15
 
 
16
  COLS = oc.COLS
17
 
 
18
  # =========================================================
19
  # STYLE CSS
20
  # =========================================================
 
21
  CSS = """
22
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,400,0,0');
23
 
 
36
  }
37
 
38
  .gradio-container {
39
+ max-width: 1180px !important;
40
+ margin: 0 auto !important;
41
+ font-family: 'Inter', system-ui, sans-serif !important;
42
+ padding-bottom: 40px !important;
43
  }
44
 
45
+ body, .gradio-container {
46
+ background: #f0f4f9 !important;
47
  }
48
 
49
+ /* ---------- HERO ---------- */
50
  .hero {
51
+ background: linear-gradient(135deg, #0b1e36 0%, #163a5f 55%, #1e4d7b 100%);
52
+ padding: 36px 40px;
53
+ border-radius: 22px;
54
  color: white;
55
+ margin-bottom: 28px;
56
+ box-shadow: 0 12px 40px rgba(11, 30, 54, 0.18);
57
+ position: relative;
58
+ overflow: hidden;
59
+ }
60
+
61
+ .hero::after {
62
+ content: '';
63
+ position: absolute;
64
+ top: -40%;
65
+ right: -10%;
66
+ width: 320px;
67
+ height: 320px;
68
+ background: radial-gradient(circle, rgba(104,171,255,0.18) 0%, transparent 70%);
69
+ pointer-events: none;
70
+ }
71
+
72
+ .hero-tag {
73
+ display: inline-flex;
74
+ align-items: center;
75
+ gap: 8px;
76
+ background: rgba(104, 171, 255, 0.15);
77
+ border: 1px solid rgba(104, 171, 255, 0.35);
78
+ padding: 6px 14px;
79
+ border-radius: 999px;
80
+ color: #b8d7ff;
81
+ font-size: 11px;
82
+ font-weight: 600;
83
+ letter-spacing: 0.04em;
84
+ text-transform: uppercase;
85
+ margin-bottom: 16px;
86
  }
87
 
88
  .hero h1 {
89
  color: white !important;
90
+ font-size: 32px !important;
91
  font-weight: 800 !important;
92
+ margin: 0 0 10px 0 !important;
93
+ letter-spacing: -0.03em;
94
  }
95
 
96
  .hero p {
97
+ color: #a8c4e0 !important;
98
+ font-size: 15px;
99
+ max-width: 520px;
100
+ line-height: 1.55;
101
+ margin: 0;
 
 
 
 
 
 
 
 
 
 
102
  }
103
 
104
  .hero-features {
105
+ margin-top: 28px;
106
  display: flex;
107
  flex-wrap: wrap;
108
+ gap: 10px;
109
  }
110
 
111
  .hero-feature {
112
  display: inline-flex;
113
  align-items: center;
114
  gap: 8px;
115
+ background: rgba(255,255,255,0.07);
116
+ border: 1px solid rgba(255,255,255,0.08);
117
+ padding: 9px 14px;
118
+ border-radius: 10px;
119
+ color: #d6e8fb;
120
+ font-size: 13px;
121
+ font-weight: 500;
122
  }
123
 
124
  .hero-feature .material-symbols-outlined {
125
  color: #68abff;
126
+ font-size: 18px;
127
  }
128
 
129
+ /* ---------- PANELS ---------- */
130
  .panel {
131
+ background: #ffffff;
132
+ border: 1px solid #e2e8f0;
133
  border-radius: 16px;
134
+ padding: 24px;
135
+ box-shadow: 0 1px 3px rgba(15, 23, 42, 0.04), 0 4px 16px rgba(15, 23, 42, 0.03);
136
+ margin-bottom: 18px;
137
  }
138
 
139
  .section-title {
140
+ display: flex;
141
+ align-items: center;
142
+ gap: 8px;
143
+ color: #0f2744;
144
  font-weight: 700;
145
+ font-size: 16px;
146
+ margin-bottom: 4px;
147
+ }
148
+
149
+ .section-title .material-symbols-outlined {
150
+ color: #3b82f6;
151
+ font-size: 20px;
152
  }
153
 
154
  .section-description {
155
+ color: #64748b;
156
  font-size: 13px;
157
  margin-bottom: 18px;
158
+ line-height: 1.5;
159
  }
160
 
161
+ /* ---------- BUTTONS ---------- */
162
  .primary-btn {
163
+ background: linear-gradient(135deg, #3b82f6, #2563eb) !important;
164
+ color: white !important;
165
  border: none !important;
166
+ border-radius: 12px !important;
167
  font-weight: 700 !important;
168
+ font-size: 15px !important;
169
+ min-height: 52px !important;
170
+ letter-spacing: 0.02em;
171
+ box-shadow: 0 4px 14px rgba(37, 99, 235, 0.35) !important;
172
+ transition: transform 0.15s ease, box-shadow 0.15s ease !important;
173
  }
174
 
175
  .primary-btn:hover {
176
+ background: linear-gradient(135deg, #2563eb, #1d4ed8) !important;
177
+ box-shadow: 0 6px 20px rgba(37, 99, 235, 0.45) !important;
178
+ transform: translateY(-1px);
179
  }
180
 
181
  .secondary-btn {
182
  border-radius: 10px !important;
183
+ font-weight: 600 !important;
184
+ }
185
+
186
+ /* ---------- RESULTS HEADER ---------- */
187
+ .result-header {
188
+ margin: 32px 0 14px 0;
189
  }
190
 
191
  .result-title {
192
+ display: flex;
193
+ align-items: center;
194
+ gap: 8px;
195
+ color: #0f2744;
196
+ font-size: 20px;
197
  font-weight: 750;
198
  }
199
 
200
+ .result-title .material-symbols-outlined {
201
+ color: #3b82f6;
202
+ font-size: 24px;
203
+ }
204
+
205
+ .result-subtitle {
206
+ color: #64748b;
207
+ font-size: 13px;
208
+ margin-top: 4px;
209
+ margin-left: 32px;
210
+ }
211
+
212
+ /* ---------- CATEGORIES ---------- */
213
+ .categories-box {
214
+ background: linear-gradient(135deg, #eff6ff 0%, #f0f9ff 100%);
215
+ border: 1px solid #bfdbfe;
216
+ padding: 22px 24px;
217
+ border-radius: 16px;
218
+ margin-top: 8px;
219
+ }
220
+
221
+ .categories-title {
222
+ display: flex;
223
+ align-items: center;
224
+ gap: 8px;
225
+ font-weight: 700;
226
+ color: #0f2744;
227
+ margin-bottom: 14px;
228
+ font-size: 14px;
229
+ }
230
+
231
+ .categories-title .material-symbols-outlined {
232
+ color: #3b82f6;
233
+ font-size: 18px;
234
  }
235
 
236
  .category-list {
 
244
  align-items: center;
245
  gap: 6px;
246
  background: white;
247
+ padding: 8px 13px;
248
+ border-radius: 9px;
249
+ color: #1e40af;
250
  font-weight: 600;
251
  font-size: 12px;
252
+ border: 1px solid #dbeafe;
253
+ box-shadow: 0 1px 2px rgba(37, 99, 235, 0.06);
254
  }
255
 
256
  .category-item .material-symbols-outlined {
257
+ font-size: 16px;
258
+ color: #3b82f6;
259
  }
260
 
261
+ .categories-note {
262
+ font-size: 12px;
263
+ color: #64748b;
264
+ margin-top: 14px;
265
+ line-height: 1.5;
266
+ }
267
 
268
+ /* ---------- FOOTER ---------- */
269
+ .footer {
270
+ text-align: center;
271
+ color: #94a3b8;
272
+ font-size: 12px;
273
+ padding: 28px 20px 8px;
274
+ margin-top: 12px;
275
+ line-height: 1.7;
276
+ }
277
+
278
+ .footer .material-symbols-outlined {
279
+ font-size: 18px;
280
+ color: #94a3b8;
281
+ margin-bottom: 4px;
282
+ }
283
+
284
+ /* ---------- ACCORDION / MISC ---------- */
285
+ .gradio-accordion {
286
+ border-radius: 14px !important;
287
+ margin-bottom: 8px !important;
288
+ }
289
+ """
290
 
291
  # =========================================================
292
  # TRAITEMENT ET EXPORT
293
  # =========================================================
 
294
  def build_outputs(df):
 
295
  df = df[df["Image"].astype(str).str.upper() != "TOTAL"].copy()
 
296
  if "Remarque" not in df:
297
  df["Remarque"] = ""
 
298
  for c in COLS:
299
  df[c] = pd.to_numeric(
300
  df[c], errors="coerce"
301
  ).fillna(0).astype(int)
 
302
  total = {
303
  "Image": "TOTAL",
304
  **{c: int(df[c].sum()) for c in COLS},
305
  "Remarque": ""
306
  }
 
307
  report = pd.concat(
308
  [
309
  df[["Image"] + COLS + ["Remarque"]],
 
311
  ],
312
  ignore_index=True
313
  )
 
314
  folder = tempfile.mkdtemp()
 
315
  csv_path = os.path.join(folder, "decompte.csv")
316
  xlsx_path = os.path.join(folder, "decompte.xlsx")
 
317
  report.to_csv(
318
  csv_path,
319
  index=False,
320
  sep=";",
321
  encoding="utf-8-sig"
322
  )
 
323
  report.to_excel(
324
  xlsx_path,
325
  index=False,
326
  sheet_name="Décompte"
327
  )
 
328
  return report, [csv_path, xlsx_path]
329
 
330
 
331
  # =========================================================
332
  # ANALYSE OCR
333
  # =========================================================
 
334
  def run(files, lang, psm, fuzzy):
 
335
  if not files:
336
  raise gr.Error("Veuillez importer au moins une image.")
 
337
  rows = []
 
338
  for f in files:
 
339
  path = f if isinstance(f, str) else f.name
340
  name = os.path.basename(path)
 
341
  try:
 
342
  counts, notes = oc.analyse(
343
  path,
344
  lang=lang,
345
  psm=int(psm),
346
  fuzzy=fuzzy
347
  )
 
348
  except pytesseract.TesseractNotFoundError:
 
349
  raise gr.Error(
350
  "Tesseract est introuvable sur le serveur."
351
  )
 
352
  except Exception as e:
 
353
  rows.append({
354
  "Image": name,
355
  **{c: 0 for c in COLS},
356
  "Remarque": f"Erreur : {e}"
357
  })
 
358
  continue
 
359
  rows.append({
360
  "Image": name,
361
  **counts,
362
  "Remarque": " ; ".join(notes)
363
  })
 
364
  report, out = build_outputs(pd.DataFrame(rows))
 
365
  return report, out
366
 
367
 
368
  # =========================================================
369
  # RECALCUL
370
  # =========================================================
 
371
  def recompute(df):
 
372
  if df is None:
373
  raise gr.Error("Aucun tableau à recalculer.")
 
374
  return build_outputs(pd.DataFrame(df))
375
 
376
 
377
  # =========================================================
378
  # INTERFACE
379
  # =========================================================
 
380
  with gr.Blocks(
381
  title="OCR COUNT | Décompte intelligent",
382
  theme=gr.themes.Soft(
 
388
  ) as demo:
389
 
390
  # EN-TÊTE
 
391
  gr.HTML("""
392
  <div class="hero">
 
393
  <div class="hero-tag">
394
+ <span class="material-symbols-outlined" style="font-size:15px;">verified</span>
395
+ Système de décompte intelligent
 
396
  </div>
 
397
  <h1>OCR COUNT</h1>
 
398
  <p>
399
  Plateforme de reconnaissance et de décompte automatique
400
  des données à partir de tableaux photographiés.
401
  </p>
 
402
  <div class="hero-features">
 
403
  <div class="hero-feature">
404
+ <span class="material-symbols-outlined">document_scanner</span>
 
 
405
  Reconnaissance OCR
406
  </div>
 
407
  <div class="hero-feature">
408
+ <span class="material-symbols-outlined">analytics</span>
 
 
409
  Analyse des données
410
  </div>
 
411
  <div class="hero-feature">
412
+ <span class="material-symbols-outlined">download</span>
 
 
413
  Exportation des rapports
414
  </div>
 
415
  </div>
 
416
  </div>
417
  """)
418
 
419
  # IMPORTATION
 
420
  with gr.Group(elem_classes="panel"):
 
421
  gr.HTML("""
422
  <div class="section-title">
423
+ <span class="material-symbols-outlined">cloud_upload</span>
 
424
  Importation des documents
425
  </div>
 
426
  <div class="section-description">
427
+ Sélectionnez une ou plusieurs photographies contenant les tableaux à analyser.
 
428
  </div>
429
  """)
 
430
  files = gr.File(
431
  label="Déposer vos images ici",
432
  file_count="multiple",
433
  file_types=["image"],
434
  type="filepath"
435
  )
 
436
  gr.Markdown(
437
+ "Formats acceptés : **JPG · JPEG · PNG** &nbsp;|&nbsp; Plusieurs images simultanément."
 
438
  )
439
 
440
  # CONFIGURATION OCR
441
+ with gr.Accordion("Configuration avancée de l'OCR", open=False):
 
 
 
 
 
442
  gr.HTML("""
443
+ <div class="section-description" style="margin-top:8px;">
444
  Personnalisez les paramètres de reconnaissance des caractères.
445
  </div>
446
  """)
 
447
  with gr.Row():
 
448
  lang = gr.Textbox(
449
  value="eng",
450
  label="Langue OCR",
451
  info="Langue utilisée par Tesseract"
452
  )
 
453
  psm = gr.Dropdown(
454
+ choices=[11, 6,, 4],
455
  value=11,
456
  label="Mode de segmentation",
457
  info="Mode de lecture du tableau"
458
  )
 
459
  fuzzy = gr.Checkbox(
460
  value=True,
461
  label="Activer la tolérance aux erreurs de lecture",
 
463
  )
464
 
465
  # BOUTON D'ANALYSE
 
466
  go = gr.Button(
467
  "LANCER LE DÉCOMPTE",
468
  variant="primary",
 
471
  )
472
 
473
  # RÉSULTATS
 
474
  gr.HTML("""
475
+ <div class="result-header">
 
476
  <div class="result-title">
477
+ <span class="material-symbols-outlined">analytics</span>
 
 
 
478
  Résultats de l'analyse
479
  </div>
480
+ <div class="result-subtitle">
481
+ Consultez les données extraites, corrigez les valeurs si nécessaire et recalculez les totaux.
 
 
482
  </div>
 
483
  </div>
484
  """)
485
 
486
  # TABLEAU
 
487
  with gr.Group(elem_classes="panel"):
 
488
  table = gr.Dataframe(
489
  headers=["Image"] + COLS + ["Remarque"],
490
  datatype=[
 
502
  label="Rapport détaillé",
503
  wrap=True
504
  )
 
505
  recalc = gr.Button(
506
  "Recalculer le total",
507
  variant="secondary",
 
509
  )
510
 
511
  # EXPORT
 
512
  with gr.Group(elem_classes="panel"):
 
513
  gr.HTML("""
514
  <div class="section-title">
515
+ <span class="material-symbols-outlined">file_download</span>
 
516
  Exportation des résultats
517
  </div>
 
518
  <div class="section-description">
519
  Téléchargez votre rapport au format Excel ou CSV.
520
  </div>
521
  """)
 
522
  exports = gr.File(
523
  label="Fichiers générés",
524
  file_count="multiple",
 
526
  )
527
 
528
  # CATÉGORIES
 
529
  gr.HTML("""
530
+ <div class="categories-box">
531
+ <div class="categories-title">
532
+ <span class="material-symbols-outlined">category</span>
 
 
 
 
 
 
 
 
533
  Catégories prises en compte
534
  </div>
 
535
  <div class="category-list">
 
536
  <div class="category-item">
537
  <span class="material-symbols-outlined">payments</span>
538
  PASF
539
  </div>
 
540
  <div class="category-item">
541
  <span class="material-symbols-outlined">health_and_safety</span>
542
  IPM / ASS
543
  </div>
 
544
  <div class="category-item">
545
  <span class="material-symbols-outlined">receipt_long</span>
546
  PR
547
  </div>
 
548
  <div class="category-item">
549
  <span class="material-symbols-outlined">account_balance</span>
550
  MUT
551
  </div>
 
552
  <div class="category-item">
553
  <span class="material-symbols-outlined">fact_check</span>
554
  RESULTAT
555
  </div>
 
556
  <div class="category-item">
557
  <span class="material-symbols-outlined">verified_user</span>
558
  CONTROL
559
  </div>
 
560
  </div>
561
+ <div class="categories-note">
 
562
  IPM et ASS sont regroupés dans une seule catégorie : IPM.
563
  Seule la deuxième colonne du tableau est comptabilisée.
564
  </div>
 
565
  </div>
566
  """)
567
 
568
  # PIED DE PAGE
 
569
  gr.HTML("""
570
  <div class="footer">
571
+ <span class="material-symbols-outlined">document_scanner</span><br>
572
+ OCR COUNT · Système intelligent de décompte<br>
 
 
 
573
  Reconnaissance automatique · Analyse · Exportation
574
  </div>
575
  """)
576
 
577
  # ÉVÉNEMENTS
 
578
  go.click(
579
  fn=run,
580
  inputs=[files, lang, psm, fuzzy],
581
  outputs=[table, exports]
582
  )
 
583
  recalc.click(
584
  fn=recompute,
585
  inputs=table,
586
  outputs=[table, exports]
587
  )
588
 
 
589
  if __name__ == "__main__":
590
+ demo.launch(ssr_mode=False)