hoololi commited on
Commit
e2fe0e1
·
verified ·
1 Parent(s): 98187fe

Delete game_engine.py

Browse files
Files changed (1) hide show
  1. game_engine.py +0 -721
game_engine.py DELETED
@@ -1,721 +0,0 @@
1
- # ==========================================
2
- # game_engine.py - Calcul OCR v3.0
3
- # ==========================================
4
-
5
- """
6
- Moteur de jeu mathématique complet
7
- """
8
-
9
- import random
10
- import time
11
- import datetime
12
- import gradio as gr
13
- import os
14
- import uuid
15
- import gc
16
- import base64
17
- from io import BytesIO
18
- import numpy as np
19
- from PIL import Image
20
- import threading
21
- import queue
22
- from typing import Dict, Tuple, Optional
23
-
24
- # Auto-détection propre : GPU OU CPU uniquement
25
- ocr_module = None
26
- ocr_info = {"model_name": "Unknown", "device": "Unknown"}
27
-
28
- try:
29
- # Test GPU : torch + CUDA disponible
30
- import torch
31
- if torch.cuda.is_available():
32
- from image_processing_gpu import (
33
- recognize_number_fast_with_image,
34
- create_thumbnail_fast,
35
- create_white_canvas,
36
- cleanup_memory,
37
- log_memory_usage,
38
- get_ocr_model_info
39
- )
40
- ocr_module = "gpu"
41
- print("✅ Game Engine: Mode GPU - TrOCR activé")
42
- else:
43
- # Torch installé mais pas de GPU → CPU
44
- from image_processing_cpu import (
45
- recognize_number_fast_with_image,
46
- create_thumbnail_fast,
47
- create_white_canvas,
48
- cleanup_memory,
49
- log_memory_usage,
50
- get_ocr_model_info
51
- )
52
- ocr_module = "cpu"
53
- print("✅ Game Engine: Mode CPU - EasyOCR activé")
54
-
55
- except ImportError:
56
- # Torch pas installé → CPU obligatoire
57
- from image_processing_cpu import (
58
- recognize_number_fast_with_image,
59
- create_thumbnail_fast,
60
- create_white_canvas,
61
- cleanup_memory,
62
- log_memory_usage,
63
- get_ocr_model_info
64
- )
65
- ocr_module = "cpu"
66
- print("✅ Game Engine: Mode CPU - EasyOCR activé")
67
-
68
- # Récupérer les infos du modèle sélectionné
69
- try:
70
- ocr_info = get_ocr_model_info()
71
- print(f"🎯 OCR sélectionné: {ocr_info['model_name']} sur {ocr_info['device']}")
72
- except Exception as e:
73
- print(f"⚠️ Impossible de récupérer les infos OCR: {e}")
74
- ocr_info = {"model_name": "Error", "device": "Unknown"}
75
-
76
- # Imports dataset avec gestion d'erreur
77
- try:
78
- from datasets import Dataset, load_dataset
79
- DATASET_AVAILABLE = True
80
- print("✅ Modules dataset disponibles")
81
- except ImportError as e:
82
- DATASET_AVAILABLE = False
83
- print(f"⚠️ Modules dataset non disponibles: {e}")
84
-
85
- # Nom du nouveau dataset
86
- DATASET_NAME = "hoololi/calcul_ocr_dataset"
87
-
88
- # Configuration des difficultés par opération
89
- DIFFICULTY_RANGES = {
90
- "×": {
91
- "Facile": (2, 9),
92
- "Difficile": (4, 12)
93
- },
94
- "+": {
95
- "Facile": (1, 50),
96
- "Difficile": (10, 100)
97
- },
98
- "-": {
99
- "Facile": (1, 50),
100
- "Difficile": (10, 100)
101
- },
102
- "÷": {
103
- "Facile": (1, 10), # Pour les résultats
104
- "Difficile": (2, 12) # Pour les résultats
105
- }
106
- }
107
-
108
- def create_result_row_with_images(i: int, image: dict | np.ndarray | Image.Image, expected: int, operation_data: tuple[int, int, str, int]) -> dict:
109
-
110
- # OCR optimisé
111
- recognized, optimized_image, dataset_image_data = recognize_number_fast_with_image(image)
112
-
113
- try:
114
- recognized_num = int(recognized) if recognized.isdigit() else 0
115
- except:
116
- recognized_num = 0
117
-
118
- is_correct = recognized_num == expected
119
- a, b, operation, correct_result = operation_data
120
-
121
- status_icon = "✅" if is_correct else "❌"
122
- status_text = "Correct" if is_correct else "Incorrect"
123
- row_color = "#e8f5e8" if is_correct else "#ffe8e8"
124
-
125
- # Miniature
126
- image_thumbnail = create_thumbnail_fast(optimized_image, size=(50, 50))
127
-
128
- # Libérer mémoire
129
- if optimized_image and hasattr(optimized_image, 'close'):
130
- try:
131
- optimized_image.close()
132
- except:
133
- pass
134
-
135
- return {
136
- 'html_row': f"""
137
- <tr style="background-color: {row_color};">
138
- <td style="text-align: center; padding: 8px; border: 1px solid #ddd; color: #333;">{i+1}</td>
139
- <td style="text-align: center; padding: 8px; border: 1px solid #ddd; font-weight: bold; color: #333;">{a}</td>
140
- <td style="text-align: center; padding: 8px; border: 1px solid #ddd; font-weight: bold; color: #333;">{operation}</td>
141
- <td style="text-align: center; padding: 8px; border: 1px solid #ddd; font-weight: bold; color: #333;">{b}</td>
142
- <td style="text-align: center; padding: 8px; border: 1px solid #ddd; font-weight: bold; color: #333;">{expected}</td>
143
- <td style="text-align: center; padding: 8px; border: 1px solid #ddd;">{image_thumbnail}</td>
144
- <td style="text-align: center; padding: 8px; border: 1px solid #ddd; font-weight: bold; color: #333;">{recognized_num}</td>
145
- <td style="text-align: center; padding: 8px; border: 1px solid #ddd; color: #333;">{status_icon} {status_text}</td>
146
- </tr>
147
- """,
148
- 'is_correct': is_correct,
149
- 'recognized': recognized,
150
- 'recognized_num': recognized_num,
151
- 'dataset_image_data': dataset_image_data
152
- }
153
-
154
-
155
- class MathGame:
156
- """Moteur de jeu mathématique avec traitement parallèle"""
157
-
158
- def __init__(self):
159
- self.is_running = False
160
- self.start_time = 0
161
- self.current_operation = ""
162
- self.correct_answer = 0
163
- self.user_images = []
164
- self.expected_answers = []
165
- self.operations_history = []
166
- self.question_count = 0
167
- self.time_remaining = 30
168
- self.session_data = []
169
-
170
- # Configuration session
171
- self.duration = 30
172
- self.operation_type = "×"
173
- self.difficulty = "Facile"
174
-
175
- # Gestion export
176
- self.export_status = "not_exported"
177
- self.export_timestamp = None
178
- self.export_result = None
179
-
180
- # NOUVEAU: Traitement parallèle
181
- self.processing_queue = queue.Queue()
182
- self.results_cache: Dict[int, dict] = {} # {question_number: result_data}
183
- self.worker_thread: Optional[threading.Thread] = None
184
- self.processing_active = False
185
-
186
- def _start_background_processing(self) -> None:
187
- """Démarre le thread de traitement en arrière-plan"""
188
- if self.worker_thread is None or not self.worker_thread.is_alive():
189
- self.processing_active = True
190
- self.worker_thread = threading.Thread(target=self._process_images_worker, daemon=True)
191
- self.worker_thread.start()
192
- print("🔄 Thread de traitement parallèle démarré")
193
-
194
- def _stop_background_processing(self) -> None:
195
- """Arrête le thread de traitement"""
196
- self.processing_active = False
197
- if self.worker_thread and self.worker_thread.is_alive():
198
- print("⏹️ Arrêt du thread de traitement parallèle")
199
-
200
- def _process_images_worker(self) -> None:
201
- """Worker thread qui traite les images en arrière-plan"""
202
- print("🚀 Worker thread démarré")
203
- while self.processing_active:
204
- try:
205
- if not self.processing_queue.empty():
206
- question_num, image, expected, operation_data = self.processing_queue.get(timeout=1)
207
- print(f"🔄 Traitement parallèle image {question_num}...")
208
-
209
- start_time = time.time()
210
- result_data = create_result_row_with_images(question_num, image, expected, operation_data)
211
- processing_time = time.time() - start_time
212
-
213
- # Stocker le résultat
214
- self.results_cache[question_num] = result_data
215
- print(f"✅ Image {question_num} traitée en {processing_time:.1f}s (parallèle)")
216
-
217
- else:
218
- time.sleep(0.1) # Éviter la consommation CPU excessive
219
-
220
- except queue.Empty:
221
- continue
222
- except Exception as e:
223
- print(f"❌ Erreur traitement parallèle: {e}")
224
-
225
- print("🛑 Worker thread terminé")
226
-
227
- def _add_image_to_processing_queue(self, question_num: int, image: dict | np.ndarray | Image.Image,
228
- expected: int, operation_data: tuple) -> None:
229
- """Ajoute une image à la queue de traitement"""
230
- if image is not None:
231
- self.processing_queue.put((question_num, image, expected, operation_data))
232
- print(f"📝 Image {question_num} ajoutée à la queue de traitement")
233
- return {
234
- "status": self.export_status,
235
- "timestamp": self.export_timestamp,
236
- "result": self.export_result,
237
- "can_export": self.export_status == "not_exported" and len(self.session_data) > 0
238
- }
239
-
240
- def mark_export_in_progress(self) -> None:
241
- self.export_status = "exporting"
242
- self.export_timestamp = datetime.datetime.now().isoformat()
243
-
244
- def mark_export_completed(self, result: str) -> None:
245
- self.export_status = "exported"
246
- self.export_result = result
247
-
248
- def generate_multiplication(self, difficulty: str) -> tuple[str, int]:
249
- """Génère une multiplication"""
250
- min_val, max_val = DIFFICULTY_RANGES["×"][difficulty]
251
- a = random.randint(min_val, max_val)
252
- b = random.randint(min_val, max_val)
253
- return f"{a} × {b}", a * b
254
-
255
- def generate_addition(self, difficulty: str) -> tuple[str, int]:
256
- """Génère une addition"""
257
- min_val, max_val = DIFFICULTY_RANGES["+"][difficulty]
258
- a = random.randint(min_val, max_val)
259
- b = random.randint(min_val, max_val)
260
- return f"{a} + {b}", a + b
261
-
262
- def generate_subtraction(self, difficulty: str) -> tuple[str, int]:
263
- """Génère une soustraction (résultat toujours positif)"""
264
- min_val, max_val = DIFFICULTY_RANGES["-"][difficulty]
265
- a = random.randint(min_val, max_val)
266
- b = random.randint(min_val, a) # b <= a pour éviter les négatifs
267
- return f"{a} - {b}", a - b
268
-
269
- def generate_division(self, difficulty: str) -> tuple[str, int]:
270
- """Génère une division exacte"""
271
- min_result, max_result = DIFFICULTY_RANGES["÷"][difficulty]
272
- result = random.randint(min_result, max_result)
273
- if difficulty == "Facile":
274
- divisor = random.randint(2, 9)
275
- else:
276
- divisor = random.randint(2, 12)
277
- dividend = result * divisor
278
- return f"{dividend} ÷ {divisor}", result
279
-
280
- def generate_operation(self, operation_type: str, difficulty: str) -> tuple[str, int]:
281
- """Génère une opération selon le type et la difficulté"""
282
- if operation_type == "×":
283
- return self.generate_multiplication(difficulty)
284
- elif operation_type == "+":
285
- return self.generate_addition(difficulty)
286
- elif operation_type == "-":
287
- return self.generate_subtraction(difficulty)
288
- elif operation_type == "÷":
289
- return self.generate_division(difficulty)
290
- elif operation_type == "Aléatoire":
291
- # Choisir aléatoirement une opération
292
- random_op = random.choice(["×", "+", "-", "÷"])
293
- return self.generate_operation(random_op, difficulty)
294
- else:
295
- # Par défaut, multiplication
296
- return self.generate_multiplication(difficulty)
297
-
298
- def start_game(self, duration: str, operation: str, difficulty: str) -> tuple[str, Image.Image, str, str, gr.update, gr.update, str]:
299
- """Démarre le jeu avec la configuration choisie"""
300
-
301
- # log_memory_usage("avant nettoyage start_game") # DEBUG: Désactivé
302
-
303
- # Configuration
304
- self.duration = 60 if duration == "60 secondes" else 30
305
- self.operation_type = operation
306
- self.difficulty = difficulty
307
-
308
- # Nettoyage
309
- if hasattr(self, 'user_images') and self.user_images:
310
- for img in self.user_images:
311
- if hasattr(img, 'close'):
312
- try:
313
- img.close()
314
- except:
315
- pass
316
-
317
- if hasattr(self, 'session_data') and self.session_data:
318
- for entry in self.session_data:
319
- if 'user_drawing' in entry and entry['user_drawing']:
320
- entry['user_drawing'] = None
321
- self.session_data.clear()
322
-
323
- # Réinit avec nettoyage parallèle
324
- self._stop_background_processing()
325
- self.results_cache.clear()
326
- while not self.processing_queue.empty():
327
- try:
328
- self.processing_queue.get_nowait()
329
- except queue.Empty:
330
- break
331
-
332
- self.is_running = True
333
- self.start_time = time.time()
334
- self.user_images = []
335
- self.expected_answers = []
336
- self.operations_history = []
337
- self.question_count = 0
338
- self.time_remaining = self.duration
339
- self.session_data = []
340
-
341
- # Reset export
342
- self.export_status = "not_exported"
343
- self.export_timestamp = None
344
- self.export_result = None
345
-
346
- # Démarrer le traitement parallèle
347
- self._start_background_processing()
348
-
349
- gc.collect()
350
- # log_memory_usage("après nettoyage start_game") # DEBUG: Désactivé
351
-
352
- # Première opération
353
- operation_str, answer = self.generate_operation(self.operation_type, self.difficulty)
354
- self.current_operation = operation_str
355
- self.correct_answer = answer
356
-
357
- # Parser l'opération pour l'historique
358
- parts = operation_str.split()
359
- a, op, b = int(parts[0]), parts[1], int(parts[2])
360
- self.operations_history.append((a, b, op, answer))
361
-
362
- # Affichage adapté selon l'opération
363
- operation_emoji = {
364
- "×": "✖️", "+": "➕", "-": "➖", "÷": "➗", "Aléatoire": "🎲"
365
- }
366
- emoji = operation_emoji.get(self.operation_type, "🔢")
367
-
368
- return (
369
- f'<div style="font-size: 3em; font-weight: bold; text-align: center; padding: 20px; background: linear-gradient(45deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px;">{operation_str}</div>',
370
- create_white_canvas(),
371
- f"🎯 {emoji} {self.operation_type} • {self.difficulty} • Écrivez votre réponse !",
372
- f"⏱️ Temps restant: {self.time_remaining}s",
373
- gr.update(interactive=False),
374
- gr.update(interactive=True),
375
- ""
376
- )
377
-
378
- def next_question(self, image_data: dict | np.ndarray | Image.Image | None) -> tuple[str, Image.Image, str, str, gr.update, gr.update, str]:
379
- if not self.is_running:
380
- return (
381
- f'<div style="font-size: 3em; font-weight: bold; text-align: center; padding: 20px; background: linear-gradient(45deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px;">{self.current_operation}</div>',
382
- image_data,
383
- "❌ Le jeu n'est pas en cours !",
384
- "⏱️ Temps: 0s",
385
- gr.update(interactive=True),
386
- gr.update(interactive=False),
387
- ""
388
- )
389
-
390
- elapsed_time = time.time() - self.start_time
391
- if elapsed_time >= self.duration:
392
- return self.end_game(image_data)
393
-
394
- if image_data is not None:
395
- # Ajouter l'image à la liste ET au traitement parallèle
396
- self.user_images.append(image_data)
397
- self.expected_answers.append(self.correct_answer)
398
-
399
- # Parser l'opération actuelle pour le traitement
400
- parts = self.current_operation.split()
401
- a, op, b = int(parts[0]), parts[1], int(parts[2])
402
- current_operation_data = (a, b, op, self.correct_answer)
403
-
404
- # Lancer le traitement en parallèle de l'image qu'on vient de recevoir
405
- self._add_image_to_processing_queue(self.question_count, image_data, self.correct_answer, current_operation_data)
406
-
407
- self.question_count += 1
408
-
409
- # Nouvelle opération
410
- operation_str, answer = self.generate_operation(self.operation_type, self.difficulty)
411
- self.current_operation = operation_str
412
- self.correct_answer = answer
413
-
414
- # Parser pour l'historique
415
- parts = operation_str.split()
416
- a, op, b = int(parts[0]), parts[1], int(parts[2])
417
- self.operations_history.append((a, b, op, answer))
418
-
419
- time_remaining = max(0, self.duration - int(elapsed_time))
420
- self.time_remaining = time_remaining
421
-
422
- if time_remaining <= 0:
423
- return self.end_game(image_data)
424
-
425
- # Emoji pour l'opération
426
- operation_emoji = {
427
- "×": "✖️", "+": "➕", "-": "➖", "÷": "➗", "Aléatoire": "🎲"
428
- }
429
- emoji = operation_emoji.get(self.operation_type, "🔢")
430
-
431
- return (
432
- f'<div style="font-size: 3em; font-weight: bold; text-align: center; padding: 20px; background: linear-gradient(45deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px;">{operation_str}</div>',
433
- create_white_canvas(),
434
- f"🎯 {emoji} Question {self.question_count + 1} • {self.difficulty}",
435
- f"⏱️ Temps restant: {time_remaining}s",
436
- gr.update(interactive=False),
437
- gr.update(interactive=True),
438
- ""
439
- )
440
-
441
- def end_game(self, final_image: dict | np.ndarray | Image.Image | None) -> tuple[str, Image.Image, str, str, gr.update, gr.update, str]:
442
-
443
- self.is_running = False
444
-
445
- # log_memory_usage("début end_game") # DEBUG: Désactivé
446
-
447
- if final_image is not None:
448
- self.user_images.append(final_image)
449
- self.expected_answers.append(self.correct_answer)
450
- self.question_count += 1
451
- if len(self.operations_history) < len(self.user_images):
452
- parts = self.current_operation.split()
453
- a, op, b = int(parts[0]), parts[1], int(parts[2])
454
- self.operations_history.append((a, b, op, self.correct_answer))
455
-
456
- correct_answers = 0
457
- total_questions = len(self.user_images)
458
- table_rows_html = ""
459
-
460
- session_timestamp = datetime.datetime.now().isoformat()
461
- session_id = f"session_{int(datetime.datetime.now().timestamp())}_{str(uuid.uuid4())[:8]}"
462
-
463
- self.session_data = []
464
- images_saved = 0
465
- total_image_size_kb = 0
466
-
467
- # Traitement optimisé avec DEBUG
468
- print(f"🔄 Traitement de {total_questions} images...")
469
- start_processing = time.time()
470
-
471
- for i, (image, expected, operation_data) in enumerate(zip(self.user_images, self.expected_answers, self.operations_history)):
472
- print(f" → Image {i+1}/{total_questions}...")
473
- img_start = time.time()
474
-
475
- row_data = create_result_row_with_images(i, image, expected, operation_data)
476
- table_rows_html += row_data['html_row']
477
-
478
- img_time = time.time() - img_start
479
- print(f" ✅ Traitée en {img_time:.1f}s")
480
-
481
- if row_data['is_correct']:
482
- correct_answers += 1
483
-
484
- # Structure pour NOUVEAU DATASET CALCUL OCR
485
- a, b, operation, correct_result = operation_data
486
-
487
- # OCR & Résultats avec détection automatique du modèle
488
- ocr_info_data = get_ocr_model_info()
489
- entry = {
490
- "session_id": session_id,
491
- "timestamp": session_timestamp,
492
- "question_number": i + 1,
493
-
494
- # Configuration session
495
- "session_duration": self.duration,
496
- "operation_type": self.operation_type,
497
- "difficulty_level": self.difficulty,
498
-
499
- # Mathématiques
500
- "operand_a": a,
501
- "operand_b": b,
502
- "operation": operation,
503
- "correct_answer": expected,
504
-
505
- # OCR & Résultats
506
- "ocr_model": ocr_info_data.get("model_name", "Unknown"),
507
- "ocr_device": ocr_info_data.get("device", "Unknown"),
508
- "user_answer_ocr": row_data['recognized'],
509
- "user_answer_parsed": row_data['recognized_num'],
510
- "is_correct": row_data['is_correct'],
511
-
512
- # Métadonnées
513
- "total_questions": total_questions,
514
- "app_version": "3.0_calcul_ocr_parallel"
515
- }
516
-
517
- # Métadonnées
518
- "total_questions": total_questions,
519
- "app_version": "3.0_calcul_ocr_parallel" # Mis à jour pour le parallélisme
520
- }
521
-
522
- # Ajouter image si disponible
523
- if row_data['dataset_image_data']:
524
- entry["handwriting_image"] = row_data['dataset_image_data']["image_base64"]
525
- entry["image_width"] = int(row_data['dataset_image_data']["compressed_size"][0])
526
- entry["image_height"] = int(row_data['dataset_image_data']["compressed_size"][1])
527
- entry["image_size_kb"] = float(row_data['dataset_image_data']["file_size_kb"])
528
- entry["has_image"] = True
529
- images_saved += 1
530
- total_image_size_kb += row_data['dataset_image_data']["file_size_kb"]
531
- else:
532
- entry["has_image"] = False
533
-
534
- self.session_data.append(entry)
535
-
536
- processing_time = time.time() - start_processing
537
- print(f"⏱️ Traitement total: {processing_time:.1f}s")
538
-
539
- accuracy = (correct_answers / total_questions * 100) if total_questions > 0 else 0
540
-
541
- for entry in self.session_data:
542
- entry["session_accuracy"] = accuracy
543
-
544
- # Nettoyage mémoire
545
- for img in self.user_images:
546
- if hasattr(img, 'close'):
547
- try:
548
- img.close()
549
- except:
550
- pass
551
-
552
- gc.collect()
553
- # log_memory_usage("après nettoyage end_game") # DEBUG: Désactivé
554
-
555
- # HTML résultats
556
- table_html = f"""
557
- <div style="overflow-x: auto; margin: 20px 0;">
558
- <table style="width: 100%; border-collapse: collapse; border: 2px solid #4a90e2;">
559
- <thead>
560
- <tr style="background: #4a90e2; color: white;">
561
- <th style="padding: 8px;">Question</th>
562
- <th style="padding: 8px;">A</th>
563
- <th style="padding: 8px;">Op</th>
564
- <th style="padding: 8px;">B</th>
565
- <th style="padding: 8px;">Réponse</th>
566
- <th style="padding: 8px;">Votre dessin</th>
567
- <th style="padding: 8px;">OCR</th>
568
- <th style="padding: 8px;">Statut</th>
569
- </tr>
570
- </thead>
571
- <tbody>
572
- {table_rows_html}
573
- </tbody>
574
- </table>
575
- </div>
576
- """
577
-
578
- # Configuration session pour affichage
579
- config_display = f"{self.operation_type} • {self.difficulty} • {self.duration}s"
580
- operation_emoji = {
581
- "×": "✖️", "+": "➕", "-": "➖", "÷": "➗", "Aléatoire": "🎲"
582
- }
583
- emoji = operation_emoji.get(self.operation_type, "🔢")
584
-
585
- export_info = self.get_export_status()
586
- if export_info["can_export"]:
587
- export_section = f"""
588
- <div style="margin-top: 20px; padding: 15px; background-color: #e8f5e8; border-radius: 8px;">
589
- <h3 style="color: #2e7d32;">📤 Ajouter cette série au dataset ?</h3>
590
- <p style="color: #2e7d32;">
591
- ✅ {total_questions} réponses • 📊 {accuracy:.1f}% de précision<br>
592
- 📸 {images_saved} opérations et images sauvegardées ({total_image_size_kb:.1f}KB)<br>
593
- ⚙️ Configuration: {config_display}
594
- </p>
595
- </div>
596
- """
597
- else:
598
- export_section = ""
599
-
600
- final_results = f"""
601
- <div style="margin: 20px 0;">
602
- <h1 style="text-align: center; color: #4a90e2;">🎉 Session terminée !</h1>
603
- <div style="background: linear-gradient(45deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; border-radius: 10px; margin: 20px 0;">
604
- <h2>📈 Résultats</h2>
605
- <div style="text-align: center; margin-bottom: 15px;">
606
- <strong>{emoji} {config_display}</strong>
607
- </div>
608
- <div style="display: flex; justify-content: space-around; flex-wrap: wrap;">
609
- <div style="text-align: center; margin: 10px;">
610
- <div style="font-size: 2em; font-weight: bold;">{total_questions}</div>
611
- <div>Questions</div>
612
- </div>
613
- <div style="text-align: center; margin: 10px;">
614
- <div style="font-size: 2em; font-weight: bold; color: #90EE90;">{correct_answers}</div>
615
- <div>Correctes</div>
616
- </div>
617
- <div style="text-align: center; margin: 10px;">
618
- <div style="font-size: 2em; font-weight: bold; color: #FFB6C1;">{total_questions - correct_answers}</div>
619
- <div>Incorrectes</div>
620
- </div>
621
- <div style="text-align: center; margin: 10px;">
622
- <div style="font-size: 2em; font-weight: bold;">{accuracy:.1f}%</div>
623
- <div>Précision</div>
624
- </div>
625
- </div>
626
- </div>
627
- <h2 style="color: #4a90e2;">📊 Détail des Réponses</h2>
628
- {table_html}
629
- {export_section}
630
- </div>
631
- """
632
-
633
- return (
634
- """<div style="font-size: 3em; font-weight: bold; text-align: center; padding: 20px; background: linear-gradient(45deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px;">🏁 C'est fini !</div>""",
635
- create_white_canvas(),
636
- f"✨ Session {config_display} terminée !",
637
- "⏱️ Temps écoulé !",
638
- gr.update(interactive=True),
639
- gr.update(interactive=False),
640
- final_results
641
- )
642
-
643
-
644
- def export_to_clean_dataset(session_data: list[dict], dataset_name: str = DATASET_NAME) -> str:
645
- """Export vers le nouveau dataset calcul_ocr_dataset"""
646
- if not DATASET_AVAILABLE:
647
- return "❌ Modules dataset non disponibles"
648
-
649
- hf_token = os.getenv("HF_TOKEN") or os.getenv("tk_calcul_ocr") # Support des deux noms
650
- if not hf_token:
651
- return "❌ Token HuggingFace manquant (HF_TOKEN ou tk_calcul_ocr)"
652
-
653
- try:
654
- print(f"\n🚀 === EXPORT VERS DATASET CALCUL OCR ===")
655
- print(f"📊 Dataset: {dataset_name}")
656
-
657
- # Filtrer les entrées avec images
658
- clean_entries = []
659
-
660
- for entry in session_data:
661
- if entry.get('has_image', False):
662
- clean_entries.append(entry)
663
-
664
- print(f"✅ {len(clean_entries)} entrées avec images converties")
665
-
666
- if len(clean_entries) == 0:
667
- return "❌ Aucune entrée avec image à exporter"
668
-
669
- # Charger dataset existant OU créer nouveau
670
- try:
671
- existing_dataset = load_dataset(dataset_name, split="train")
672
- existing_data = existing_dataset.to_list()
673
- print(f"📊 {len(existing_data)} entrées existantes")
674
- except:
675
- existing_data = []
676
- print("📊 Création nouveau dataset calcul_ocr")
677
-
678
- # Combiner
679
- combined_data = existing_data + clean_entries
680
- clean_dataset = Dataset.from_list(combined_data)
681
-
682
- print(f"✅ Dataset créé - Features:")
683
- for feature_name in clean_dataset.features:
684
- print(f" - {feature_name}: {clean_dataset.features[feature_name]}")
685
-
686
- # Statistiques par opération
687
- operations_count = {}
688
- for entry in clean_entries:
689
- op = entry.get('operation_type', 'unknown')
690
- operations_count[op] = operations_count.get(op, 0) + 1
691
-
692
- operations_summary = ", ".join([f"{op}: {count}" for op, count in operations_count.items()])
693
-
694
- # Push vers HuggingFace
695
- print(f"📤 Push vers {dataset_name}...")
696
- clean_dataset.push_to_hub(
697
- dataset_name,
698
- private=False,
699
- token=hf_token,
700
- commit_message=f"Add {len(clean_entries)} handwriting samples for math OCR ({operations_summary})"
701
- )
702
-
703
- cleanup_memory()
704
-
705
- success_message = f"""✅ Session ajoutée au dataset avec succès !
706
-
707
- 📊 Dataset: {dataset_name}
708
- 📸 Images: {len(clean_entries)}
709
- 🔢 Opérations: {operations_summary}
710
- 📈 Total: {len(clean_dataset)}
711
-
712
- 🔗 Le dataset est consultable ici : https://huggingface.co/datasets/{dataset_name}"""
713
-
714
- return success_message
715
-
716
- except Exception as e:
717
- print(f"❌ ERREUR: {e}")
718
- import traceback
719
- traceback.print_exc()
720
- error_message = f"❌ Erreur: {str(e)}"
721
- return error_message