Spaces:
Sleeping
Sleeping
Upload game_engine.py
Browse files- game_engine.py +39 -18
game_engine.py
CHANGED
|
@@ -25,10 +25,14 @@ from typing import Dict, Tuple, Optional
|
|
| 25 |
ocr_module = None
|
| 26 |
ocr_info = {"model_name": "Unknown", "device": "Unknown"}
|
| 27 |
|
|
|
|
| 28 |
try:
|
| 29 |
-
#
|
| 30 |
-
import
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
from image_processing_gpu import (
|
| 33 |
recognize_number_fast_with_image,
|
| 34 |
create_thumbnail_fast,
|
|
@@ -38,9 +42,39 @@ try:
|
|
| 38 |
get_ocr_model_info
|
| 39 |
)
|
| 40 |
ocr_module = "gpu"
|
| 41 |
-
print("✅ Game Engine: Mode GPU - TrOCR
|
| 42 |
else:
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
from image_processing_cpu import (
|
| 45 |
recognize_number_fast_with_image,
|
| 46 |
create_thumbnail_fast,
|
|
@@ -51,19 +85,6 @@ try:
|
|
| 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:
|
|
|
|
| 25 |
ocr_module = None
|
| 26 |
ocr_info = {"model_name": "Unknown", "device": "Unknown"}
|
| 27 |
|
| 28 |
+
# Forcer GPU en mode HF Spaces avec hardware GPU
|
| 29 |
try:
|
| 30 |
+
# Détecter si on est sur HF Spaces avec GPU allocé
|
| 31 |
+
import os
|
| 32 |
+
is_hf_gpu_space = os.getenv("SPACE_ID") and os.getenv("SPACE_HARDWARE") == "gpu"
|
| 33 |
+
|
| 34 |
+
if is_hf_gpu_space:
|
| 35 |
+
print("🎯 HF Spaces GPU détecté - Force mode TrOCR")
|
| 36 |
from image_processing_gpu import (
|
| 37 |
recognize_number_fast_with_image,
|
| 38 |
create_thumbnail_fast,
|
|
|
|
| 42 |
get_ocr_model_info
|
| 43 |
)
|
| 44 |
ocr_module = "gpu"
|
| 45 |
+
print("✅ Game Engine: Mode GPU - TrOCR forcé sur HF Spaces")
|
| 46 |
else:
|
| 47 |
+
raise ImportError("Pas en mode HF GPU")
|
| 48 |
+
|
| 49 |
+
except (ImportError, Exception) as e:
|
| 50 |
+
print(f"⚠️ Mode GPU non disponible: {e}")
|
| 51 |
+
# Fallback CPU normal
|
| 52 |
+
try:
|
| 53 |
+
import torch
|
| 54 |
+
if torch.cuda.is_available():
|
| 55 |
+
from image_processing_gpu import (
|
| 56 |
+
recognize_number_fast_with_image,
|
| 57 |
+
create_thumbnail_fast,
|
| 58 |
+
create_white_canvas,
|
| 59 |
+
cleanup_memory,
|
| 60 |
+
log_memory_usage,
|
| 61 |
+
get_ocr_model_info
|
| 62 |
+
)
|
| 63 |
+
ocr_module = "gpu"
|
| 64 |
+
print("✅ Game Engine: Mode GPU - TrOCR activé")
|
| 65 |
+
else:
|
| 66 |
+
from image_processing_cpu import (
|
| 67 |
+
recognize_number_fast_with_image,
|
| 68 |
+
create_thumbnail_fast,
|
| 69 |
+
create_white_canvas,
|
| 70 |
+
cleanup_memory,
|
| 71 |
+
log_memory_usage,
|
| 72 |
+
get_ocr_model_info
|
| 73 |
+
)
|
| 74 |
+
ocr_module = "cpu"
|
| 75 |
+
print("✅ Game Engine: Mode CPU - EasyOCR activé")
|
| 76 |
+
|
| 77 |
+
except ImportError:
|
| 78 |
from image_processing_cpu import (
|
| 79 |
recognize_number_fast_with_image,
|
| 80 |
create_thumbnail_fast,
|
|
|
|
| 85 |
)
|
| 86 |
ocr_module = "cpu"
|
| 87 |
print("✅ Game Engine: Mode CPU - EasyOCR activé")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
# Récupérer les infos du modèle sélectionné
|
| 90 |
try:
|