# ========================================== # app.py - Calcul OCR v3.0 # ========================================== """ Application principale - Entraînement aux calculs avec OCR """ import gradio as gr import warnings import os import gc import numpy as np from PIL import Image import simple_gpu warnings.filterwarnings("ignore") # Import avec structure claire : GPU ou CPU uniquement try: # Test GPU : torch + CUDA disponible import torch if torch.cuda.is_available(): from image_processing_gpu import init_ocr_model, create_white_canvas, cleanup_memory print("📱 Interface: Mode GPU détecté - TrOCR") else: # Torch installé mais pas de GPU → CPU from image_processing_cpu import init_ocr_model, create_white_canvas, cleanup_memory print("📱 Interface: Mode CPU détecté - EasyOCR") except ImportError: # Torch pas installé → CPU obligatoire from image_processing_cpu import init_ocr_model, create_white_canvas, cleanup_memory print("📱 Interface: Mode CPU détecté - EasyOCR") from game_engine import MathGame, export_to_clean_dataset print("🚀 Initialisation Calcul OCR v3.0...") print("🔄 Chargement modèle OCR...") init_ocr_model() print("✅ Modèle OCR prêt") game = MathGame() def start_game_wrapper(duration: str, operation: str, difficulty: str) -> tuple: cleanup_memory() return game.start_game(duration, operation, difficulty) def next_question_wrapper(image_data: dict | np.ndarray | Image.Image | None) -> tuple: return game.next_question(image_data) def export_current_session() -> str: """Export vers le nouveau dataset calcul_ocr_dataset""" if not hasattr(game, 'session_data') or not game.session_data: return "❌ Aucune donnée de session à exporter" export_info = game.get_export_status() if export_info["status"] == "exported": return f"""✅ Session déjà exportée ! 📅 Exporté le: {export_info['timestamp'][:19].replace('T', ' ')} 📊 Résultat: {export_info['result'][:100]}... 💡 Jouez une nouvelle session pour contribuer davantage !""" if export_info["status"] == "exporting": return "⏳ Export en cours..." if not export_info["can_export"]: return "❌ Aucune donnée à exporter" game.mark_export_in_progress() try: result = export_to_clean_dataset(game.session_data) game.mark_export_completed(result) cleanup_memory() return result except Exception as e: game.export_status = "not_exported" return f"❌ Erreur export: {str(e)}" # Interface Gradio with gr.Blocks( title="🧮 Calcul OCR - Entraînement mathématiques", theme=gr.themes.Soft(), css=""" .gradio-container { max-width: 1200px !important; } .config-section { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 15px; border-radius: 10px; margin: 10px 0; } .dataset-info { background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%); color: white; padding: 15px; border-radius: 10px; margin: 10px 0; } .radio-group { background: #f8f9fa; padding: 10px; border-radius: 8px; margin: 5px 0; } """, head="" ) as demo: gr.Markdown( """ # 🧮 Entraînement aux calculs avec OCR **Nouveau !** Choisissez votre configuration et entraînez-vous sur différents types de calculs ! **Comment jouer :** 1. **Configurez** votre session ci-dessous 2. Cliquez sur **🚀 GO !** pour démarrer 3. **Écrivez** ✏️ votre réponse sur le tableau 4. Cliquez sur **➡️ NEXT !** pour la question suivante À la fin, vous pourrez contribuer au dataset ouvert pour améliorer l'OCR mathématique ! --- """ ) # Configuration de la session with gr.Group(): gr.Markdown("### ⚙️ Configuration de la session", elem_classes=["config-section"]) with gr.Row(): duration_choice = gr.Radio( choices=["30 secondes", "60 secondes"], value="30 secondes", label="⏱️ Durée", elem_classes=["radio-group"] ) operation_choice = gr.Radio( choices=["×", "+", "-", "÷", "Aléatoire"], value="×", label="🔢 Opération", elem_classes=["radio-group"] ) difficulty_choice = gr.Radio( choices=["Facile", "Difficile"], value="Facile", label="🎯 Difficulté", elem_classes=["radio-group"] ) with gr.Row(): with gr.Column(scale=1): # Question question_display = gr.HTML( value='