Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
# ==========================================
|
| 2 |
-
#
|
| 3 |
# ==========================================
|
| 4 |
|
| 5 |
"""
|
| 6 |
-
Application principale
|
| 7 |
"""
|
| 8 |
|
| 9 |
import gradio as gr
|
|
@@ -18,44 +18,54 @@ warnings.filterwarnings("ignore")
|
|
| 18 |
|
| 19 |
# Import avec structure claire : GPU ou CPU uniquement
|
| 20 |
try:
|
| 21 |
-
# Test GPU : torch + CUDA disponible
|
| 22 |
import torch
|
| 23 |
if torch.cuda.is_available():
|
| 24 |
from image_processing_gpu import init_ocr_model, create_white_canvas, cleanup_memory
|
| 25 |
print("📱 Interface: Mode GPU détecté - TrOCR")
|
| 26 |
else:
|
| 27 |
-
# Torch installé mais pas de GPU → CPU
|
| 28 |
from image_processing_cpu import init_ocr_model, create_white_canvas, cleanup_memory
|
| 29 |
print("📱 Interface: Mode CPU détecté - EasyOCR")
|
| 30 |
except ImportError:
|
| 31 |
-
# Torch pas installé → CPU obligatoire
|
| 32 |
from image_processing_cpu import init_ocr_model, create_white_canvas, cleanup_memory
|
| 33 |
print("📱 Interface: Mode CPU détecté - EasyOCR")
|
| 34 |
|
| 35 |
from game_engine import MathGame, export_to_clean_dataset
|
| 36 |
|
| 37 |
print("🚀 Initialisation Calcul OCR v3.0...")
|
| 38 |
-
|
| 39 |
print("🔄 Chargement modèle OCR...")
|
| 40 |
init_ocr_model()
|
| 41 |
print("✅ Modèle OCR prêt")
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
def start_game_wrapper(duration: str, operation: str, difficulty: str) -> tuple:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
cleanup_memory()
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
def next_question_wrapper(image_data
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
def export_current_session() -> str:
|
| 53 |
-
"""Export
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
if not hasattr(
|
| 56 |
return "❌ Aucune donnée de session à exporter"
|
| 57 |
|
| 58 |
-
export_info =
|
| 59 |
|
| 60 |
if export_info["status"] == "exported":
|
| 61 |
return f"""✅ Session déjà exportée !
|
|
@@ -71,19 +81,19 @@ def export_current_session() -> str:
|
|
| 71 |
if not export_info["can_export"]:
|
| 72 |
return "❌ Aucune donnée à exporter"
|
| 73 |
|
| 74 |
-
|
| 75 |
|
| 76 |
try:
|
| 77 |
-
result = export_to_clean_dataset(
|
| 78 |
-
|
| 79 |
cleanup_memory()
|
| 80 |
return result
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
-
|
| 84 |
return f"❌ Erreur export: {str(e)}"
|
| 85 |
|
| 86 |
-
# Interface Gradio
|
| 87 |
with gr.Blocks(
|
| 88 |
title="🧮 Calcul OCR - Entraînement mathématiques",
|
| 89 |
theme=gr.themes.Soft(),
|
|
@@ -113,49 +123,46 @@ with gr.Blocks(
|
|
| 113 |
head="<meta name='viewport' content='width=device-width, initial-scale=1.0'>"
|
| 114 |
) as demo:
|
| 115 |
|
|
|
|
|
|
|
|
|
|
| 116 |
gr.Markdown(
|
| 117 |
"""
|
| 118 |
-
# 🧮 Entraînement
|
| 119 |
|
| 120 |
-
**Nouveau !** Choisissez votre configuration et entraînez-vous sur différents types de calculs !
|
| 121 |
-
|
| 122 |
**Comment jouer :**
|
| 123 |
1. **Configurez** votre session ci-dessous
|
| 124 |
2. Cliquez sur **🚀 GO !** pour démarrer
|
| 125 |
3. **Écrivez** ✏️ votre réponse sur le tableau
|
| 126 |
4. Cliquez sur **➡️ NEXT !** pour la question suivante
|
| 127 |
|
| 128 |
-
À la fin, vous pourrez contribuer au dataset ouvert
|
| 129 |
-
|
| 130 |
---
|
| 131 |
"""
|
| 132 |
)
|
| 133 |
|
| 134 |
# Configuration de la session
|
| 135 |
-
with gr.
|
| 136 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
)
|
| 152 |
-
|
| 153 |
-
difficulty_choice = gr.Radio(
|
| 154 |
-
choices=["Facile", "Difficile"],
|
| 155 |
-
value="Facile",
|
| 156 |
-
label="🎯 Difficulté",
|
| 157 |
-
elem_classes=["radio-group"]
|
| 158 |
-
)
|
| 159 |
|
| 160 |
with gr.Row():
|
| 161 |
with gr.Column(scale=1):
|
|
@@ -191,30 +198,31 @@ with gr.Blocks(
|
|
| 191 |
results_display = gr.HTML("")
|
| 192 |
|
| 193 |
# Export vers dataset dédié
|
| 194 |
-
gr.
|
| 195 |
-
export_button = gr.Button("📤 Ajouter la série au dataset calcul_ocr", variant="primary", size="lg")
|
| 196 |
export_status = gr.Markdown("")
|
| 197 |
|
| 198 |
-
#
|
| 199 |
go_button.click(
|
| 200 |
fn=start_game_wrapper,
|
| 201 |
-
inputs=[duration_choice, operation_choice, difficulty_choice],
|
| 202 |
-
outputs=[question_display, canvas, status_display, timer_display, go_button, next_button, results_display]
|
| 203 |
)
|
| 204 |
|
| 205 |
next_button.click(
|
| 206 |
fn=next_question_wrapper,
|
| 207 |
-
inputs=[canvas],
|
| 208 |
-
outputs=[question_display, canvas, status_display, timer_display, go_button, next_button, results_display]
|
| 209 |
)
|
| 210 |
|
| 211 |
export_button.click(
|
| 212 |
fn=export_current_session,
|
|
|
|
| 213 |
outputs=[export_status]
|
| 214 |
)
|
| 215 |
|
| 216 |
if __name__ == "__main__":
|
| 217 |
-
print("🚀 Lancement Calcul OCR v3.0...")
|
|
|
|
| 218 |
print("🎯 Dataset: calcul_ocr_dataset")
|
| 219 |
print("📊 Opérations: ×, +, -, ÷, Aléatoire")
|
| 220 |
print("⚙️ Durées: 30s, 60s")
|
|
|
|
| 1 |
# ==========================================
|
| 2 |
+
# app_fixed.py - Calcul OCR v3.0 - Sessions isolées
|
| 3 |
# ==========================================
|
| 4 |
|
| 5 |
"""
|
| 6 |
+
Application principale avec isolation des sessions utilisateurs
|
| 7 |
"""
|
| 8 |
|
| 9 |
import gradio as gr
|
|
|
|
| 18 |
|
| 19 |
# Import avec structure claire : GPU ou CPU uniquement
|
| 20 |
try:
|
|
|
|
| 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 |
from image_processing_cpu import init_ocr_model, create_white_canvas, cleanup_memory
|
| 27 |
print("📱 Interface: Mode CPU détecté - EasyOCR")
|
| 28 |
except ImportError:
|
|
|
|
| 29 |
from image_processing_cpu import init_ocr_model, create_white_canvas, cleanup_memory
|
| 30 |
print("📱 Interface: Mode CPU détecté - EasyOCR")
|
| 31 |
|
| 32 |
from game_engine import MathGame, export_to_clean_dataset
|
| 33 |
|
| 34 |
print("🚀 Initialisation Calcul OCR v3.0...")
|
|
|
|
| 35 |
print("🔄 Chargement modèle OCR...")
|
| 36 |
init_ocr_model()
|
| 37 |
print("✅ Modèle OCR prêt")
|
| 38 |
|
| 39 |
+
def create_new_game_session():
|
| 40 |
+
"""Crée une nouvelle instance de jeu pour chaque session"""
|
| 41 |
+
return MathGame()
|
| 42 |
|
| 43 |
+
def start_game_wrapper(duration: str, operation: str, difficulty: str, game_state) -> tuple:
|
| 44 |
+
"""Wrapper avec isolation de session"""
|
| 45 |
+
if game_state is None:
|
| 46 |
+
game_state = create_new_game_session()
|
| 47 |
+
|
| 48 |
cleanup_memory()
|
| 49 |
+
result = game_state.start_game(duration, operation, difficulty)
|
| 50 |
+
return result + (game_state,) # Retourner l'état mis à jour
|
| 51 |
|
| 52 |
+
def next_question_wrapper(image_data, game_state) -> tuple:
|
| 53 |
+
"""Wrapper avec isolation de session"""
|
| 54 |
+
if game_state is None:
|
| 55 |
+
game_state = create_new_game_session()
|
| 56 |
+
|
| 57 |
+
result = game_state.next_question(image_data)
|
| 58 |
+
return result + (game_state,) # Retourner l'état mis à jour
|
| 59 |
|
| 60 |
+
def export_current_session(game_state) -> str:
|
| 61 |
+
"""Export avec isolation de session"""
|
| 62 |
+
if game_state is None:
|
| 63 |
+
return "❌ Aucune session active"
|
| 64 |
|
| 65 |
+
if not hasattr(game_state, 'session_data') or not game_state.session_data:
|
| 66 |
return "❌ Aucune donnée de session à exporter"
|
| 67 |
|
| 68 |
+
export_info = game_state.get_export_status()
|
| 69 |
|
| 70 |
if export_info["status"] == "exported":
|
| 71 |
return f"""✅ Session déjà exportée !
|
|
|
|
| 81 |
if not export_info["can_export"]:
|
| 82 |
return "❌ Aucune donnée à exporter"
|
| 83 |
|
| 84 |
+
game_state.mark_export_in_progress()
|
| 85 |
|
| 86 |
try:
|
| 87 |
+
result = export_to_clean_dataset(game_state.session_data)
|
| 88 |
+
game_state.mark_export_completed(result)
|
| 89 |
cleanup_memory()
|
| 90 |
return result
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
+
game_state.export_status = "not_exported"
|
| 94 |
return f"❌ Erreur export: {str(e)}"
|
| 95 |
|
| 96 |
+
# Interface Gradio avec State management
|
| 97 |
with gr.Blocks(
|
| 98 |
title="🧮 Calcul OCR - Entraînement mathématiques",
|
| 99 |
theme=gr.themes.Soft(),
|
|
|
|
| 123 |
head="<meta name='viewport' content='width=device-width, initial-scale=1.0'>"
|
| 124 |
) as demo:
|
| 125 |
|
| 126 |
+
# ✅ STATE MANAGEMENT - Chaque utilisateur a sa propre instance
|
| 127 |
+
game_state = gr.State(value=None)
|
| 128 |
+
|
| 129 |
gr.Markdown(
|
| 130 |
"""
|
| 131 |
+
# 🧮 Entraînement au calcul mental avec OCR
|
| 132 |
|
|
|
|
|
|
|
| 133 |
**Comment jouer :**
|
| 134 |
1. **Configurez** votre session ci-dessous
|
| 135 |
2. Cliquez sur **🚀 GO !** pour démarrer
|
| 136 |
3. **Écrivez** ✏️ votre réponse sur le tableau
|
| 137 |
4. Cliquez sur **➡️ NEXT !** pour la question suivante
|
| 138 |
|
| 139 |
+
À la fin, vous pourrez consulter vos résultats et contribuer au dataset ouvert !
|
|
|
|
| 140 |
---
|
| 141 |
"""
|
| 142 |
)
|
| 143 |
|
| 144 |
# Configuration de la session
|
| 145 |
+
with gr.Row():
|
| 146 |
+
duration_choice = gr.Radio(
|
| 147 |
+
choices=["30 secondes", "60 secondes"],
|
| 148 |
+
value="30 secondes",
|
| 149 |
+
label="⏱️ Durée",
|
| 150 |
+
elem_classes=["radio-group"]
|
| 151 |
+
)
|
| 152 |
|
| 153 |
+
operation_choice = gr.Radio(
|
| 154 |
+
choices=["×", "+", "-", "÷", "Aléatoire"],
|
| 155 |
+
value="×",
|
| 156 |
+
label="🔢 Opération",
|
| 157 |
+
elem_classes=["radio-group"]
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
difficulty_choice = gr.Radio(
|
| 161 |
+
choices=["Facile", "Difficile"],
|
| 162 |
+
value="Facile",
|
| 163 |
+
label="🎯 Difficulté",
|
| 164 |
+
elem_classes=["radio-group"]
|
| 165 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
with gr.Row():
|
| 168 |
with gr.Column(scale=1):
|
|
|
|
| 198 |
results_display = gr.HTML("")
|
| 199 |
|
| 200 |
# Export vers dataset dédié
|
| 201 |
+
export_button = gr.Button("📤 Exporter la série vers le dataset", variant="primary", size="lg")
|
|
|
|
| 202 |
export_status = gr.Markdown("")
|
| 203 |
|
| 204 |
+
# ✅ ÉVÉNEMENTS avec State management
|
| 205 |
go_button.click(
|
| 206 |
fn=start_game_wrapper,
|
| 207 |
+
inputs=[duration_choice, operation_choice, difficulty_choice, game_state],
|
| 208 |
+
outputs=[question_display, canvas, status_display, timer_display, go_button, next_button, results_display, game_state]
|
| 209 |
)
|
| 210 |
|
| 211 |
next_button.click(
|
| 212 |
fn=next_question_wrapper,
|
| 213 |
+
inputs=[canvas, game_state],
|
| 214 |
+
outputs=[question_display, canvas, status_display, timer_display, go_button, next_button, results_display, game_state]
|
| 215 |
)
|
| 216 |
|
| 217 |
export_button.click(
|
| 218 |
fn=export_current_session,
|
| 219 |
+
inputs=[game_state],
|
| 220 |
outputs=[export_status]
|
| 221 |
)
|
| 222 |
|
| 223 |
if __name__ == "__main__":
|
| 224 |
+
print("🚀 Lancement Calcul OCR v3.0 - Sessions isolées...")
|
| 225 |
+
print("🔒 Isolation: Chaque utilisateur a sa propre instance")
|
| 226 |
print("🎯 Dataset: calcul_ocr_dataset")
|
| 227 |
print("📊 Opérations: ×, +, -, ÷, Aléatoire")
|
| 228 |
print("⚙️ Durées: 30s, 60s")
|