hoololi commited on
Commit
271b2e1
·
verified ·
1 Parent(s): 3db8788

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +228 -0
  2. game_engine.py +715 -0
app.py ADDED
@@ -0,0 +1,228 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ==========================================
2
+ # app.py - Calcul OCR v3.0
3
+ # ==========================================
4
+
5
+ """
6
+ Application principale - Entraînement aux calculs avec OCR
7
+ """
8
+
9
+ import gradio as gr
10
+ import warnings
11
+ import os
12
+ import gc
13
+ import numpy as np
14
+ from PIL import Image
15
+
16
+ warnings.filterwarnings("ignore")
17
+
18
+ # Import avec structure claire : GPU ou CPU uniquement
19
+ try:
20
+ # Test GPU : torch + CUDA disponible
21
+ import torch
22
+ if torch.cuda.is_available():
23
+ from image_processing_gpu import init_ocr_model, create_white_canvas, cleanup_memory
24
+ print("📱 Interface: Mode GPU détecté - TrOCR")
25
+ else:
26
+ # Torch installé mais pas de GPU → CPU
27
+ from image_processing_cpu import init_ocr_model, create_white_canvas, cleanup_memory
28
+ print("📱 Interface: Mode CPU détecté - EasyOCR")
29
+ except ImportError:
30
+ # Torch pas installé → CPU obligatoire
31
+ from image_processing_cpu import init_ocr_model, create_white_canvas, cleanup_memory
32
+ print("📱 Interface: Mode CPU détecté - EasyOCR")
33
+
34
+ from game_engine import MathGame, export_to_clean_dataset
35
+
36
+ print("🚀 Initialisation Calcul OCR v3.0...")
37
+
38
+ print("🔄 Chargement modèle OCR...")
39
+ init_ocr_model()
40
+ print("✅ Modèle OCR prêt")
41
+
42
+ game = MathGame()
43
+
44
+ def start_game_wrapper(duration: str, operation: str, difficulty: str) -> tuple:
45
+ cleanup_memory()
46
+ return game.start_game(duration, operation, difficulty)
47
+
48
+ def next_question_wrapper(image_data: dict | np.ndarray | Image.Image | None) -> tuple:
49
+ return game.next_question(image_data)
50
+
51
+ def export_current_session() -> str:
52
+ """Export vers le nouveau dataset calcul_ocr_dataset"""
53
+
54
+ if not hasattr(game, 'session_data') or not game.session_data:
55
+ return "❌ Aucune donnée de session à exporter"
56
+
57
+ export_info = game.get_export_status()
58
+
59
+ if export_info["status"] == "exported":
60
+ return f"""✅ Session déjà exportée !
61
+
62
+ 📅 Exporté le: {export_info['timestamp'][:19].replace('T', ' ')}
63
+ 📊 Résultat: {export_info['result'][:100]}...
64
+
65
+ 💡 Jouez une nouvelle session pour contribuer davantage !"""
66
+
67
+ if export_info["status"] == "exporting":
68
+ return "⏳ Export en cours..."
69
+
70
+ if not export_info["can_export"]:
71
+ return "❌ Aucune donnée à exporter"
72
+
73
+ game.mark_export_in_progress()
74
+
75
+ try:
76
+ result = export_to_clean_dataset(game.session_data)
77
+ game.mark_export_completed(result)
78
+ cleanup_memory()
79
+ return result
80
+
81
+ except Exception as e:
82
+ game.export_status = "not_exported"
83
+ return f"❌ Erreur export: {str(e)}"
84
+
85
+ # Interface Gradio
86
+ with gr.Blocks(
87
+ title="🧮 Calcul OCR - Entraînement mathématiques",
88
+ theme=gr.themes.Soft(),
89
+ css="""
90
+ .gradio-container { max-width: 1200px !important; }
91
+ .config-section {
92
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
93
+ color: white;
94
+ padding: 15px;
95
+ border-radius: 10px;
96
+ margin: 10px 0;
97
+ }
98
+ .dataset-info {
99
+ background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
100
+ color: white;
101
+ padding: 15px;
102
+ border-radius: 10px;
103
+ margin: 10px 0;
104
+ }
105
+ .radio-group {
106
+ background: #f8f9fa;
107
+ padding: 10px;
108
+ border-radius: 8px;
109
+ margin: 5px 0;
110
+ }
111
+ """,
112
+ head="<meta name='viewport' content='width=device-width, initial-scale=1.0'>"
113
+ ) as demo:
114
+
115
+ gr.Markdown(
116
+ """
117
+ # 🧮 Entraînement aux calculs avec OCR
118
+
119
+ **Nouveau !** Choisissez votre configuration et entraînez-vous sur différents types de calculs !
120
+
121
+ **Comment jouer :**
122
+ 1. **Configurez** votre session ci-dessous
123
+ 2. Cliquez sur **🚀 GO !** pour démarrer
124
+ 3. **Écrivez** ✏️ votre réponse sur le tableau
125
+ 4. Cliquez sur **➡️ NEXT !** pour la question suivante
126
+
127
+ À la fin, vous pourrez contribuer au dataset ouvert pour améliorer l'OCR mathématique !
128
+
129
+ ---
130
+ """
131
+ )
132
+
133
+ # Configuration de la session
134
+ with gr.Group():
135
+ gr.Markdown("### ⚙️ Configuration de la session", elem_classes=["config-section"])
136
+
137
+ with gr.Row():
138
+ duration_choice = gr.Radio(
139
+ choices=["30 secondes", "60 secondes"],
140
+ value="30 secondes",
141
+ label="⏱️ Durée",
142
+ elem_classes=["radio-group"]
143
+ )
144
+
145
+ operation_choice = gr.Radio(
146
+ choices=["×", "+", "-", "÷", "Aléatoire"],
147
+ value="×",
148
+ label="🔢 Opération",
149
+ elem_classes=["radio-group"]
150
+ )
151
+
152
+ difficulty_choice = gr.Radio(
153
+ choices=["Facile", "Difficile"],
154
+ value="Facile",
155
+ label="🎯 Difficulté",
156
+ elem_classes=["radio-group"]
157
+ )
158
+
159
+ with gr.Row():
160
+ with gr.Column(scale=1):
161
+ # Question
162
+ question_display = gr.HTML(
163
+ value='<div style="font-size: 2.5em; font-weight: bold; text-align: center; padding: 20px; background: linear-gradient(45deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px;">Prêt à jouer ?</div>'
164
+ )
165
+
166
+ # Contrôles
167
+ with gr.Row():
168
+ go_button = gr.Button("🚀 GO !", variant="primary", size="lg")
169
+ next_button = gr.Button("➡️ NEXT !", variant="secondary", size="lg", interactive=False)
170
+
171
+ # Status
172
+ status_display = gr.Markdown("### 🎯 Configurez votre session et cliquez sur GO !")
173
+ timer_display = gr.Markdown("### ⏱️ --")
174
+
175
+ with gr.Column(scale=1):
176
+ # Zone de dessin
177
+ canvas = gr.ImageEditor(
178
+ label="✏️ Votre réponse",
179
+ height=350,
180
+ width=350,
181
+ value=create_white_canvas(350, 350),
182
+ brush=gr.Brush(default_size=8, default_color="#000000"),
183
+ sources=[],
184
+ layers=False,
185
+ transforms=[],
186
+ eraser=gr.Eraser(default_size=20)
187
+ )
188
+
189
+ # Résultats
190
+ results_display = gr.HTML("")
191
+
192
+ # Export vers dataset dédié
193
+ gr.Markdown("### 📤 Contribuer au dataset", elem_classes=["dataset-info"])
194
+ export_button = gr.Button("📤 Ajouter la série au dataset calcul_ocr", variant="primary", size="lg")
195
+ export_status = gr.Markdown("")
196
+
197
+ # Événements
198
+ go_button.click(
199
+ fn=start_game_wrapper,
200
+ inputs=[duration_choice, operation_choice, difficulty_choice],
201
+ outputs=[question_display, canvas, status_display, timer_display, go_button, next_button, results_display]
202
+ )
203
+
204
+ next_button.click(
205
+ fn=next_question_wrapper,
206
+ inputs=[canvas],
207
+ outputs=[question_display, canvas, status_display, timer_display, go_button, next_button, results_display]
208
+ )
209
+
210
+ export_button.click(
211
+ fn=export_current_session,
212
+ outputs=[export_status]
213
+ )
214
+
215
+ if __name__ == "__main__":
216
+ print("🚀 Lancement Calcul OCR v3.0...")
217
+ print("🎯 Dataset: calcul_ocr_dataset")
218
+ print("📊 Opérations: ×, +, -, ÷, Aléatoire")
219
+ print("⚙️ Durées: 30s, 60s")
220
+ print("🎯 Difficultés: Facile, Difficile")
221
+ demo.launch(
222
+ share=False,
223
+ show_error=True,
224
+ server_name="0.0.0.0",
225
+ server_port=7860,
226
+ show_api=False,
227
+ favicon_path=None
228
+ )
game_engine.py ADDED
@@ -0,0 +1,715 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ entry = {
488
+ "session_id": session_id,
489
+ "timestamp": session_timestamp,
490
+ "question_number": i + 1,
491
+
492
+ # Configuration session
493
+ "session_duration": self.duration,
494
+ "operation_type": self.operation_type,
495
+ "difficulty_level": self.difficulty,
496
+
497
+ # Mathématiques
498
+ "operand_a": a,
499
+ "operand_b": b,
500
+ "operation": operation,
501
+ "correct_answer": expected,
502
+
503
+ # OCR & Résultats avec détection automatique du modèle
504
+ ocr_info = get_ocr_model_info()
505
+ "ocr_model": ocr_info.get("model_name", "Unknown"),
506
+ "ocr_device": ocr_info.get("device", "Unknown"),
507
+ "user_answer_ocr": row_data['recognized'],
508
+ "user_answer_parsed": row_data['recognized_num'],
509
+ "is_correct": row_data['is_correct'],
510
+
511
+ # Métadonnées
512
+ "total_questions": total_questions,
513
+ "app_version": "3.0_calcul_ocr_parallel" # Mis à jour pour le parallélisme
514
+ }
515
+
516
+ # Ajouter image si disponible
517
+ if row_data['dataset_image_data']:
518
+ entry["handwriting_image"] = row_data['dataset_image_data']["image_base64"]
519
+ entry["image_width"] = int(row_data['dataset_image_data']["compressed_size"][0])
520
+ entry["image_height"] = int(row_data['dataset_image_data']["compressed_size"][1])
521
+ entry["image_size_kb"] = float(row_data['dataset_image_data']["file_size_kb"])
522
+ entry["has_image"] = True
523
+ images_saved += 1
524
+ total_image_size_kb += row_data['dataset_image_data']["file_size_kb"]
525
+ else:
526
+ entry["has_image"] = False
527
+
528
+ self.session_data.append(entry)
529
+
530
+ processing_time = time.time() - start_processing
531
+ print(f"⏱️ Traitement total: {processing_time:.1f}s")
532
+
533
+ accuracy = (correct_answers / total_questions * 100) if total_questions > 0 else 0
534
+
535
+ for entry in self.session_data:
536
+ entry["session_accuracy"] = accuracy
537
+
538
+ # Nettoyage mémoire
539
+ for img in self.user_images:
540
+ if hasattr(img, 'close'):
541
+ try:
542
+ img.close()
543
+ except:
544
+ pass
545
+
546
+ gc.collect()
547
+ # log_memory_usage("après nettoyage end_game") # DEBUG: Désactivé
548
+
549
+ # HTML résultats
550
+ table_html = f"""
551
+ <div style="overflow-x: auto; margin: 20px 0;">
552
+ <table style="width: 100%; border-collapse: collapse; border: 2px solid #4a90e2;">
553
+ <thead>
554
+ <tr style="background: #4a90e2; color: white;">
555
+ <th style="padding: 8px;">Question</th>
556
+ <th style="padding: 8px;">A</th>
557
+ <th style="padding: 8px;">Op</th>
558
+ <th style="padding: 8px;">B</th>
559
+ <th style="padding: 8px;">Réponse</th>
560
+ <th style="padding: 8px;">Votre dessin</th>
561
+ <th style="padding: 8px;">OCR</th>
562
+ <th style="padding: 8px;">Statut</th>
563
+ </tr>
564
+ </thead>
565
+ <tbody>
566
+ {table_rows_html}
567
+ </tbody>
568
+ </table>
569
+ </div>
570
+ """
571
+
572
+ # Configuration session pour affichage
573
+ config_display = f"{self.operation_type} • {self.difficulty} • {self.duration}s"
574
+ operation_emoji = {
575
+ "×": "✖️", "+": "➕", "-": "➖", "÷": "➗", "Aléatoire": "🎲"
576
+ }
577
+ emoji = operation_emoji.get(self.operation_type, "🔢")
578
+
579
+ export_info = self.get_export_status()
580
+ if export_info["can_export"]:
581
+ export_section = f"""
582
+ <div style="margin-top: 20px; padding: 15px; background-color: #e8f5e8; border-radius: 8px;">
583
+ <h3 style="color: #2e7d32;">📤 Ajouter cette série au dataset ?</h3>
584
+ <p style="color: #2e7d32;">
585
+ ✅ {total_questions} réponses • 📊 {accuracy:.1f}% de précision<br>
586
+ 📸 {images_saved} opérations et images sauvegardées ({total_image_size_kb:.1f}KB)<br>
587
+ ⚙️ Configuration: {config_display}
588
+ </p>
589
+ </div>
590
+ """
591
+ else:
592
+ export_section = ""
593
+
594
+ final_results = f"""
595
+ <div style="margin: 20px 0;">
596
+ <h1 style="text-align: center; color: #4a90e2;">🎉 Session terminée !</h1>
597
+ <div style="background: linear-gradient(45deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; border-radius: 10px; margin: 20px 0;">
598
+ <h2>📈 Résultats</h2>
599
+ <div style="text-align: center; margin-bottom: 15px;">
600
+ <strong>{emoji} {config_display}</strong>
601
+ </div>
602
+ <div style="display: flex; justify-content: space-around; flex-wrap: wrap;">
603
+ <div style="text-align: center; margin: 10px;">
604
+ <div style="font-size: 2em; font-weight: bold;">{total_questions}</div>
605
+ <div>Questions</div>
606
+ </div>
607
+ <div style="text-align: center; margin: 10px;">
608
+ <div style="font-size: 2em; font-weight: bold; color: #90EE90;">{correct_answers}</div>
609
+ <div>Correctes</div>
610
+ </div>
611
+ <div style="text-align: center; margin: 10px;">
612
+ <div style="font-size: 2em; font-weight: bold; color: #FFB6C1;">{total_questions - correct_answers}</div>
613
+ <div>Incorrectes</div>
614
+ </div>
615
+ <div style="text-align: center; margin: 10px;">
616
+ <div style="font-size: 2em; font-weight: bold;">{accuracy:.1f}%</div>
617
+ <div>Précision</div>
618
+ </div>
619
+ </div>
620
+ </div>
621
+ <h2 style="color: #4a90e2;">📊 Détail des Réponses</h2>
622
+ {table_html}
623
+ {export_section}
624
+ </div>
625
+ """
626
+
627
+ return (
628
+ """<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>""",
629
+ create_white_canvas(),
630
+ f"✨ Session {config_display} terminée !",
631
+ "⏱️ Temps écoulé !",
632
+ gr.update(interactive=True),
633
+ gr.update(interactive=False),
634
+ final_results
635
+ )
636
+
637
+
638
+ def export_to_clean_dataset(session_data: list[dict], dataset_name: str = DATASET_NAME) -> str:
639
+ """Export vers le nouveau dataset calcul_ocr_dataset"""
640
+ if not DATASET_AVAILABLE:
641
+ return "❌ Modules dataset non disponibles"
642
+
643
+ hf_token = os.getenv("HF_TOKEN") or os.getenv("tk_calcul_ocr") # Support des deux noms
644
+ if not hf_token:
645
+ return "❌ Token HuggingFace manquant (HF_TOKEN ou tk_calcul_ocr)"
646
+
647
+ try:
648
+ print(f"\n🚀 === EXPORT VERS DATASET CALCUL OCR ===")
649
+ print(f"📊 Dataset: {dataset_name}")
650
+
651
+ # Filtrer les entrées avec images
652
+ clean_entries = []
653
+
654
+ for entry in session_data:
655
+ if entry.get('has_image', False):
656
+ clean_entries.append(entry)
657
+
658
+ print(f"✅ {len(clean_entries)} entrées avec images converties")
659
+
660
+ if len(clean_entries) == 0:
661
+ return "❌ Aucune entrée avec image à exporter"
662
+
663
+ # Charger dataset existant OU créer nouveau
664
+ try:
665
+ existing_dataset = load_dataset(dataset_name, split="train")
666
+ existing_data = existing_dataset.to_list()
667
+ print(f"📊 {len(existing_data)} entrées existantes")
668
+ except:
669
+ existing_data = []
670
+ print("📊 Création nouveau dataset calcul_ocr")
671
+
672
+ # Combiner
673
+ combined_data = existing_data + clean_entries
674
+ clean_dataset = Dataset.from_list(combined_data)
675
+
676
+ print(f"✅ Dataset créé - Features:")
677
+ for feature_name in clean_dataset.features:
678
+ print(f" - {feature_name}: {clean_dataset.features[feature_name]}")
679
+
680
+ # Statistiques par opération
681
+ operations_count = {}
682
+ for entry in clean_entries:
683
+ op = entry.get('operation_type', 'unknown')
684
+ operations_count[op] = operations_count.get(op, 0) + 1
685
+
686
+ operations_summary = ", ".join([f"{op}: {count}" for op, count in operations_count.items()])
687
+
688
+ # Push vers HuggingFace
689
+ print(f"📤 Push vers {dataset_name}...")
690
+ clean_dataset.push_to_hub(
691
+ dataset_name,
692
+ private=False,
693
+ token=hf_token,
694
+ commit_message=f"Add {len(clean_entries)} handwriting samples for math OCR ({operations_summary})"
695
+ )
696
+
697
+ cleanup_memory()
698
+
699
+ success_message = f"""✅ Session ajoutée au dataset avec succès !
700
+
701
+ 📊 Dataset: {dataset_name}
702
+ 📸 Images: {len(clean_entries)}
703
+ 🔢 Opérations: {operations_summary}
704
+ 📈 Total: {len(clean_dataset)}
705
+
706
+ 🔗 Le dataset est consultable ici : https://huggingface.co/datasets/{dataset_name}"""
707
+
708
+ return success_message
709
+
710
+ except Exception as e:
711
+ print(f"❌ ERREUR: {e}")
712
+ import traceback
713
+ traceback.print_exc()
714
+ error_message = f"❌ Erreur: {str(e)}"
715
+ return error_message