Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,231 +0,0 @@
|
|
| 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 la nouvelle structure auto-détectée
|
| 19 |
-
try:
|
| 20 |
-
# Essayer la structure GPU d'abord
|
| 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é")
|
| 25 |
-
else:
|
| 26 |
-
raise ImportError("Pas de GPU")
|
| 27 |
-
except (ImportError, Exception):
|
| 28 |
-
try:
|
| 29 |
-
# Fallback CPU
|
| 30 |
-
from image_processing_cpu import init_ocr_model, create_white_canvas, cleanup_memory
|
| 31 |
-
print("📱 Interface: Mode CPU détecté")
|
| 32 |
-
except ImportError:
|
| 33 |
-
# Fallback legacy
|
| 34 |
-
from image_processing import init_ocr_model, create_white_canvas, cleanup_memory
|
| 35 |
-
print("📱 Interface: Mode legacy détecté")
|
| 36 |
-
|
| 37 |
-
from game_engine import MathGame, export_to_clean_dataset
|
| 38 |
-
|
| 39 |
-
print("🚀 Initialisation Calcul OCR v3.0...")
|
| 40 |
-
|
| 41 |
-
print("🔄 Chargement modèle OCR...")
|
| 42 |
-
init_ocr_model()
|
| 43 |
-
print("✅ Modèle OCR prêt")
|
| 44 |
-
|
| 45 |
-
game = MathGame()
|
| 46 |
-
|
| 47 |
-
def start_game_wrapper(duration: str, operation: str, difficulty: str) -> tuple:
|
| 48 |
-
cleanup_memory()
|
| 49 |
-
return game.start_game(duration, operation, difficulty)
|
| 50 |
-
|
| 51 |
-
def next_question_wrapper(image_data: dict | np.ndarray | Image.Image | None) -> tuple:
|
| 52 |
-
return game.next_question(image_data)
|
| 53 |
-
|
| 54 |
-
def export_current_session() -> str:
|
| 55 |
-
"""Export vers le nouveau dataset calcul_ocr_dataset"""
|
| 56 |
-
|
| 57 |
-
if not hasattr(game, 'session_data') or not game.session_data:
|
| 58 |
-
return "❌ Aucune donnée de session à exporter"
|
| 59 |
-
|
| 60 |
-
export_info = game.get_export_status()
|
| 61 |
-
|
| 62 |
-
if export_info["status"] == "exported":
|
| 63 |
-
return f"""✅ Session déjà exportée !
|
| 64 |
-
|
| 65 |
-
📅 Exporté le: {export_info['timestamp'][:19].replace('T', ' ')}
|
| 66 |
-
📊 Résultat: {export_info['result'][:100]}...
|
| 67 |
-
|
| 68 |
-
💡 Jouez une nouvelle session pour contribuer davantage !"""
|
| 69 |
-
|
| 70 |
-
if export_info["status"] == "exporting":
|
| 71 |
-
return "⏳ Export en cours..."
|
| 72 |
-
|
| 73 |
-
if not export_info["can_export"]:
|
| 74 |
-
return "❌ Aucune donnée à exporter"
|
| 75 |
-
|
| 76 |
-
game.mark_export_in_progress()
|
| 77 |
-
|
| 78 |
-
try:
|
| 79 |
-
result = export_to_clean_dataset(game.session_data)
|
| 80 |
-
game.mark_export_completed(result)
|
| 81 |
-
cleanup_memory()
|
| 82 |
-
return result
|
| 83 |
-
|
| 84 |
-
except Exception as e:
|
| 85 |
-
game.export_status = "not_exported"
|
| 86 |
-
return f"❌ Erreur export: {str(e)}"
|
| 87 |
-
|
| 88 |
-
# Interface Gradio
|
| 89 |
-
with gr.Blocks(
|
| 90 |
-
title="🧮 Calcul OCR - Entraînement mathématiques",
|
| 91 |
-
theme=gr.themes.Soft(),
|
| 92 |
-
css="""
|
| 93 |
-
.gradio-container { max-width: 1200px !important; }
|
| 94 |
-
.config-section {
|
| 95 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 96 |
-
color: white;
|
| 97 |
-
padding: 15px;
|
| 98 |
-
border-radius: 10px;
|
| 99 |
-
margin: 10px 0;
|
| 100 |
-
}
|
| 101 |
-
.dataset-info {
|
| 102 |
-
background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
|
| 103 |
-
color: white;
|
| 104 |
-
padding: 15px;
|
| 105 |
-
border-radius: 10px;
|
| 106 |
-
margin: 10px 0;
|
| 107 |
-
}
|
| 108 |
-
.radio-group {
|
| 109 |
-
background: #f8f9fa;
|
| 110 |
-
padding: 10px;
|
| 111 |
-
border-radius: 8px;
|
| 112 |
-
margin: 5px 0;
|
| 113 |
-
}
|
| 114 |
-
""",
|
| 115 |
-
head="<meta name='viewport' content='width=device-width, initial-scale=1.0'>"
|
| 116 |
-
) as demo:
|
| 117 |
-
|
| 118 |
-
gr.Markdown(
|
| 119 |
-
"""
|
| 120 |
-
# 🧮 Entraînement aux calculs avec OCR
|
| 121 |
-
|
| 122 |
-
**Nouveau !** Choisissez votre configuration et entraînez-vous sur différents types de calculs !
|
| 123 |
-
|
| 124 |
-
**Comment jouer :**
|
| 125 |
-
1. **Configurez** votre session ci-dessous
|
| 126 |
-
2. Cliquez sur **🚀 GO !** pour démarrer
|
| 127 |
-
3. **Écrivez** ✏️ votre réponse sur le tableau
|
| 128 |
-
4. Cliquez sur **➡️ NEXT !** pour la question suivante
|
| 129 |
-
|
| 130 |
-
À la fin, vous pourrez contribuer au dataset ouvert pour améliorer l'OCR mathématique !
|
| 131 |
-
|
| 132 |
-
---
|
| 133 |
-
"""
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
-
# Configuration de la session
|
| 137 |
-
with gr.Group():
|
| 138 |
-
gr.Markdown("### ⚙️ Configuration de la session", elem_classes=["config-section"])
|
| 139 |
-
|
| 140 |
-
with gr.Row():
|
| 141 |
-
duration_choice = gr.Radio(
|
| 142 |
-
choices=["30 secondes", "60 secondes"],
|
| 143 |
-
value="30 secondes",
|
| 144 |
-
label="⏱️ Durée",
|
| 145 |
-
elem_classes=["radio-group"]
|
| 146 |
-
)
|
| 147 |
-
|
| 148 |
-
operation_choice = gr.Radio(
|
| 149 |
-
choices=["×", "+", "-", "÷", "Aléatoire"],
|
| 150 |
-
value="×",
|
| 151 |
-
label="🔢 Opération",
|
| 152 |
-
elem_classes=["radio-group"]
|
| 153 |
-
)
|
| 154 |
-
|
| 155 |
-
difficulty_choice = gr.Radio(
|
| 156 |
-
choices=["Facile", "Difficile"],
|
| 157 |
-
value="Facile",
|
| 158 |
-
label="🎯 Difficulté",
|
| 159 |
-
elem_classes=["radio-group"]
|
| 160 |
-
)
|
| 161 |
-
|
| 162 |
-
with gr.Row():
|
| 163 |
-
with gr.Column(scale=1):
|
| 164 |
-
# Question
|
| 165 |
-
question_display = gr.HTML(
|
| 166 |
-
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>'
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
-
# Contrôles
|
| 170 |
-
with gr.Row():
|
| 171 |
-
go_button = gr.Button("🚀 GO !", variant="primary", size="lg")
|
| 172 |
-
next_button = gr.Button("➡️ NEXT !", variant="secondary", size="lg", interactive=False)
|
| 173 |
-
|
| 174 |
-
# Status
|
| 175 |
-
status_display = gr.Markdown("### 🎯 Configurez votre session et cliquez sur GO !")
|
| 176 |
-
timer_display = gr.Markdown("### ⏱️ --")
|
| 177 |
-
|
| 178 |
-
with gr.Column(scale=1):
|
| 179 |
-
# Zone de dessin
|
| 180 |
-
canvas = gr.ImageEditor(
|
| 181 |
-
label="✏️ Votre réponse",
|
| 182 |
-
height=350,
|
| 183 |
-
width=350,
|
| 184 |
-
value=create_white_canvas(350, 350),
|
| 185 |
-
brush=gr.Brush(default_size=8, default_color="#000000"),
|
| 186 |
-
sources=[],
|
| 187 |
-
layers=False,
|
| 188 |
-
transforms=[],
|
| 189 |
-
eraser=gr.Eraser(default_size=20)
|
| 190 |
-
)
|
| 191 |
-
|
| 192 |
-
# Résultats
|
| 193 |
-
results_display = gr.HTML("")
|
| 194 |
-
|
| 195 |
-
# Export vers dataset dédié
|
| 196 |
-
gr.Markdown("### 📤 Contribuer au dataset", elem_classes=["dataset-info"])
|
| 197 |
-
export_button = gr.Button("📤 Ajouter la série au dataset calcul_ocr", variant="primary", size="lg")
|
| 198 |
-
export_status = gr.Markdown("")
|
| 199 |
-
|
| 200 |
-
# Événements
|
| 201 |
-
go_button.click(
|
| 202 |
-
fn=start_game_wrapper,
|
| 203 |
-
inputs=[duration_choice, operation_choice, difficulty_choice],
|
| 204 |
-
outputs=[question_display, canvas, status_display, timer_display, go_button, next_button, results_display]
|
| 205 |
-
)
|
| 206 |
-
|
| 207 |
-
next_button.click(
|
| 208 |
-
fn=next_question_wrapper,
|
| 209 |
-
inputs=[canvas],
|
| 210 |
-
outputs=[question_display, canvas, status_display, timer_display, go_button, next_button, results_display]
|
| 211 |
-
)
|
| 212 |
-
|
| 213 |
-
export_button.click(
|
| 214 |
-
fn=export_current_session,
|
| 215 |
-
outputs=[export_status]
|
| 216 |
-
)
|
| 217 |
-
|
| 218 |
-
if __name__ == "__main__":
|
| 219 |
-
print("🚀 Lancement Calcul OCR v3.0...")
|
| 220 |
-
print("🎯 Dataset: calcul_ocr_dataset")
|
| 221 |
-
print("📊 Opérations: ×, +, -, ÷, Aléatoire")
|
| 222 |
-
print("⚙️ Durées: 30s, 60s")
|
| 223 |
-
print("🎯 Difficultés: Facile, Difficile")
|
| 224 |
-
demo.launch(
|
| 225 |
-
share=False,
|
| 226 |
-
show_error=True,
|
| 227 |
-
server_name="0.0.0.0",
|
| 228 |
-
server_port=7860,
|
| 229 |
-
show_api=False,
|
| 230 |
-
favicon_path=None
|
| 231 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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