SamilG2 commited on
Commit
37d841f
·
0 Parent(s):

deploy: backend CheckScan inicial

Browse files
Files changed (49) hide show
  1. .dockerignore +31 -0
  2. .gitattributes +35 -0
  3. Dockerfile +37 -0
  4. README.md +16 -0
  5. __pycache__/config.cpython-310.pyc +0 -0
  6. __pycache__/config.cpython-312.pyc +0 -0
  7. app.py +39 -0
  8. config.py +27 -0
  9. engines/.gitkeep +0 -0
  10. modules/analysis/__init__.py +1 -0
  11. modules/analysis/__pycache__/__init__.cpython-310.pyc +0 -0
  12. modules/analysis/__pycache__/__init__.cpython-312.pyc +0 -0
  13. modules/analysis/__pycache__/stockfish_wrapper.cpython-310.pyc +0 -0
  14. modules/analysis/__pycache__/stockfish_wrapper.cpython-312.pyc +0 -0
  15. modules/analysis/classifier.py +16 -0
  16. modules/analysis/stockfish_wrapper.py +173 -0
  17. modules/llm/__init__.py +1 -0
  18. modules/llm/__pycache__/__init__.cpython-310.pyc +0 -0
  19. modules/llm/__pycache__/prompt_builder.cpython-310.pyc +0 -0
  20. modules/llm/__pycache__/qwen_model.cpython-310.pyc +0 -0
  21. modules/llm/__pycache__/template_coach.cpython-310.pyc +0 -0
  22. modules/llm/guardrails.py +50 -0
  23. modules/llm/prompt_builder.py +20 -0
  24. modules/llm/qwen.py +59 -0
  25. modules/transcription/__init__.py +1 -0
  26. modules/transcription/__pycache__/__init__.cpython-310.pyc +0 -0
  27. modules/transcription/__pycache__/trocr_model.cpython-310.pyc +0 -0
  28. modules/transcription/__pycache__/validator.cpython-310.pyc +0 -0
  29. modules/transcription/trocr_model.py +96 -0
  30. modules/transcription/validator.py +95 -0
  31. modules/vision/__init__.py +1 -0
  32. modules/vision/__pycache__/__init__.cpython-310.pyc +0 -0
  33. modules/vision/__pycache__/aligner.cpython-310.pyc +0 -0
  34. modules/vision/__pycache__/segmenter.cpython-310.pyc +0 -0
  35. modules/vision/aligner.py +90 -0
  36. modules/vision/segmenter.py +139 -0
  37. requirements-training.txt +11 -0
  38. requirements.txt +28 -0
  39. routes/__init__.py +1 -0
  40. routes/__pycache__/__init__.cpython-310.pyc +0 -0
  41. routes/__pycache__/__init__.cpython-312.pyc +0 -0
  42. routes/__pycache__/analyze.cpython-310.pyc +0 -0
  43. routes/__pycache__/analyze.cpython-312.pyc +0 -0
  44. routes/__pycache__/chat.cpython-310.pyc +0 -0
  45. routes/__pycache__/upload.cpython-310.pyc +0 -0
  46. routes/analyze.py +32 -0
  47. routes/chat.py +1 -0
  48. routes/comment.py +57 -0
  49. routes/upload.py +80 -0
.dockerignore ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Modelos locales y datos de entrenamiento (pesados, no van en la imagen)
2
+ data/
3
+ models/
4
+ engines/stockfish.exe
5
+
6
+ # Python
7
+ __pycache__/
8
+ *.py[cod]
9
+ *.pyo
10
+ *.pyd
11
+ .Python
12
+ *.egg-info/
13
+ dist/
14
+ build/
15
+
16
+ # Entornos virtuales
17
+ venv/
18
+ .venv/
19
+ env/
20
+
21
+ # Git
22
+ .git/
23
+ .gitignore
24
+
25
+ # IDE
26
+ .vscode/
27
+ .idea/
28
+
29
+ # Logs y temporales
30
+ *.log
31
+ *.tmp
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ # Stockfish (Linux) + dependencias mínimas para opencv, compilación y llama-cpp-python
4
+ RUN apt-get update && apt-get install -y --no-install-recommends \
5
+ stockfish \
6
+ libgl1 \
7
+ libglib2.0-0 \
8
+ build-essential \
9
+ cmake \
10
+ && rm -rf /var/lib/apt/lists/*
11
+
12
+ WORKDIR /app
13
+
14
+ # Requirements primero para aprovechar layer cache
15
+ COPY requirements.txt .
16
+ RUN pip install --no-cache-dir --upgrade pip \
17
+ && pip install --no-cache-dir numpy==1.26.0 \
18
+ && pip install --no-cache-dir torch torchvision --index-url https://download.pytorch.org/whl/cpu \
19
+ && pip install --no-cache-dir llama-cpp-python==0.3.2 --prefer-binary --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu \
20
+ && pip install --no-cache-dir -r requirements.txt
21
+
22
+ # Pre-descarga del modelo Qwen GGUF en build (arranque rápido en runtime)
23
+ RUN mkdir -p /app/models && \
24
+ python -c "from huggingface_hub import hf_hub_download; hf_hub_download(repo_id='Qwen/Qwen2.5-3B-Instruct-GGUF', filename='qwen2.5-3b-instruct-q4_k_m.gguf', local_dir='/app/models')"
25
+
26
+ # Código de la app
27
+ COPY . /app
28
+
29
+ ENV STOCKFISH_PATH=/usr/games/stockfish \
30
+ QWEN_MODEL_PATH=/app/models/qwen2.5-3b-instruct-q4_k_m.gguf \
31
+ STOCKFISH_THREADS=2 \
32
+ STOCKFISH_HASH_MB=128 \
33
+ PYTHONUNBUFFERED=1
34
+
35
+ EXPOSE 7860
36
+
37
+ CMD ["gunicorn", "-w", "1", "-b", "0.0.0.0:7860", "--timeout", "600", "app:app"]
README.md ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: CheckScan API
3
+ colorFrom: green
4
+ colorTo: blue
5
+ sdk: docker
6
+ app_port: 7860
7
+ pinned: false
8
+ ---
9
+
10
+ # CheckScan API
11
+
12
+ Backend Flask para CheckScan-Coach.
13
+
14
+ - Vision pipeline (OpenCV) y OCR (TrOCR) para transcribir planillas.
15
+ - Análisis de partida con Stockfish.
16
+ - Comentarios del entrenador con Qwen2.5-3B-Instruct (GGUF Q4_K_M).
__pycache__/config.cpython-310.pyc ADDED
Binary file (898 Bytes). View file
 
__pycache__/config.cpython-312.pyc ADDED
Binary file (1.74 kB). View file
 
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import traceback
3
+ from flask import Flask, jsonify, request
4
+ from flask_cors import CORS
5
+
6
+ from routes.upload import upload_bp
7
+ from routes.analyze import analyze_bp
8
+ from routes.comment import comment_bp
9
+
10
+ app = Flask(__name__)
11
+
12
+ # CORS — en prod restringe al dominio de Vercel vía env var ALLOWED_ORIGINS
13
+ # (lista separada por comas). En local, sin la var, permite todo.
14
+ _origins = os.getenv("ALLOWED_ORIGINS", "*")
15
+ CORS(app, origins=[o.strip() for o in _origins.split(",")] if _origins != "*" else "*")
16
+
17
+ app.register_blueprint(upload_bp, url_prefix='/api')
18
+ app.register_blueprint(analyze_bp, url_prefix='/api')
19
+ app.register_blueprint(comment_bp, url_prefix='/api')
20
+
21
+ @app.route('/api/health')
22
+ def health():
23
+ return {'status': 'ok', 'service': 'CheckScan API'}
24
+
25
+
26
+ @app.errorhandler(Exception)
27
+ def handle_any_error(err):
28
+ """Convierte cualquier excepción en JSON limpio para /api/*."""
29
+ if request.path.startswith('/api/'):
30
+ traceback.print_exc() # log completo en la consola
31
+ return jsonify({
32
+ 'error': str(err) or err.__class__.__name__,
33
+ 'type': err.__class__.__name__,
34
+ }), 500
35
+ raise err # rutas no-API mantienen el debugger HTML
36
+
37
+
38
+ if __name__ == '__main__':
39
+ app.run(debug=True, port=5000)
config.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ _ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
4
+
5
+ # ─── Modelos ─────────────────────────────────────────────────────────────────
6
+ TROCR_MODEL_PATH = os.getenv("TROCR_MODEL_PATH", os.path.join(_ROOT, "data", "models", "trocr_finetuned"))
7
+ QWEN_MODEL_PATH = os.getenv("QWEN_MODEL_PATH", os.path.join(_ROOT, "data", "models", "qwen2.5-3b-instruct-q4_k_m.gguf"))
8
+
9
+ # ─── Stockfish (motor UCI) ───────────────────────────────────────────────────
10
+ # En Windows colocar stockfish.exe en backend/engines/ (ver AGENTS.md).
11
+ _DEFAULT_STOCKFISH = os.path.join(os.path.dirname(__file__), "engines",
12
+ "stockfish.exe" if os.name == "nt" else "stockfish")
13
+ STOCKFISH_PATH = os.getenv("STOCKFISH_PATH", _DEFAULT_STOCKFISH)
14
+ STOCKFISH_DEPTH = int(os.getenv("STOCKFISH_DEPTH", "15"))
15
+ STOCKFISH_THREADS = int(os.getenv("STOCKFISH_THREADS", "2"))
16
+ STOCKFISH_HASH_MB = int(os.getenv("STOCKFISH_HASH_MB", "256"))
17
+
18
+ # ─── TrOCR (inferencia) ──────────────────────────────────────────────────────
19
+ # Batch size para transcribir celdas (mayor → menos forward passes, más RAM).
20
+ TROCR_BATCH_SIZE = int(os.getenv("TROCR_BATCH_SIZE", "8"))
21
+ # Cuantización INT8 dinámica en CPU (3-4x más rápido, <1% pérdida de accuracy).
22
+ # Se desactiva automáticamente si hay GPU CUDA disponible.
23
+ TROCR_QUANTIZE = os.getenv("TROCR_QUANTIZE", "auto").lower() # "auto" | "true" | "false"
24
+
25
+ # ─── App ─────────────────────────────────────────────────────────────────────
26
+ MAX_UPLOAD_MB = 10
27
+ ALLOWED_EXTENSIONS = {"jpg", "jpeg", "png"}
engines/.gitkeep ADDED
File without changes
modules/analysis/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # TODO: backend/modules/analysis/__init__.py
modules/analysis/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (143 Bytes). View file
 
modules/analysis/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (147 Bytes). View file
 
modules/analysis/__pycache__/stockfish_wrapper.cpython-310.pyc ADDED
Binary file (4.77 kB). View file
 
modules/analysis/__pycache__/stockfish_wrapper.cpython-312.pyc ADDED
Binary file (6.88 kB). View file
 
modules/analysis/classifier.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def classify(diferencia_cp: int) -> str:
2
+ """Clasifica una jugada según centipeones (diferencia_cp desde POV del jugador).
3
+
4
+ - diferencia_cp > 0 → el jugador ganó ventaja en la jugada (hallazgo táctico).
5
+ - diferencia_cp < 0 → el jugador perdió ventaja (cuánto = -diferencia_cp).
6
+ """
7
+ # Brillante: el jugador ganó más de 1.5 peones de eval en una sola jugada,
8
+ # señal de una jugada táctica encontrada por encima de la línea esperada.
9
+ if diferencia_cp > 150:
10
+ return "Brillante"
11
+
12
+ loss = max(0, -diferencia_cp)
13
+ if loss <= 10: return "Excelente"
14
+ if loss <= 30: return "Buena"
15
+ if loss <= 80: return "Imprecisión"
16
+ return "Error"
modules/analysis/stockfish_wrapper.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Wrapper UCI para Stockfish usando python-chess.
2
+
3
+ Singleton lazy: el motor se inicia en la primera llamada y se reutiliza
4
+ entre requests. Si el binario no existe, se lanza FileNotFoundError con
5
+ un mensaje claro para que el frontend lo reporte.
6
+ """
7
+ import os
8
+ import threading
9
+ import chess
10
+ import chess.engine
11
+ import chess.pgn
12
+ import io
13
+
14
+ from config import STOCKFISH_PATH, STOCKFISH_DEPTH, STOCKFISH_THREADS, STOCKFISH_HASH_MB
15
+
16
+ _engine = None
17
+ _lock = threading.Lock()
18
+
19
+
20
+ def _get_engine() -> chess.engine.SimpleEngine:
21
+ global _engine
22
+ if _engine is not None:
23
+ return _engine
24
+ with _lock:
25
+ if _engine is not None:
26
+ return _engine
27
+ if not os.path.isfile(STOCKFISH_PATH):
28
+ raise FileNotFoundError(
29
+ f"Stockfish no encontrado en '{STOCKFISH_PATH}'. "
30
+ f"Descargalo de https://stockfishchess.org/download/ y "
31
+ f"coloca el binario en backend/engines/ (ver AGENTS.md)."
32
+ )
33
+ print(f"[Stockfish] Iniciando motor: {STOCKFISH_PATH}")
34
+ eng = chess.engine.SimpleEngine.popen_uci(STOCKFISH_PATH)
35
+ eng.configure({"Threads": STOCKFISH_THREADS, "Hash": STOCKFISH_HASH_MB})
36
+ _engine = eng
37
+ print(f"[Stockfish] Listo (threads={STOCKFISH_THREADS}, hash={STOCKFISH_HASH_MB}MB, depth={STOCKFISH_DEPTH})")
38
+ return _engine
39
+
40
+
41
+ def shutdown():
42
+ """Cierra el motor (llamar al apagar el servidor; opcional)."""
43
+ global _engine
44
+ if _engine is not None:
45
+ try:
46
+ _engine.quit()
47
+ except Exception:
48
+ pass
49
+ _engine = None
50
+
51
+
52
+ def _score_to_cp(score: chess.engine.PovScore) -> int:
53
+ """Convierte un PovScore a centipawns desde el punto de vista de BLANCAS.
54
+
55
+ Mate se mapea a ±100000 cp para que las diferencias sigan siendo comparables.
56
+ """
57
+ white = score.white()
58
+ if white.is_mate():
59
+ mate_in = white.mate()
60
+ return 100000 if mate_in > 0 else -100000
61
+ return white.score(mate_score=100000)
62
+
63
+
64
+ def _cp_for_terminal(board: chess.Board) -> int:
65
+ """Eval en centipawns para una posición terminal (mate/ahogado/tablas)."""
66
+ outcome = board.outcome()
67
+ if outcome is None or outcome.winner is None:
68
+ return 0 # tablas
69
+ return 100000 if outcome.winner == chess.WHITE else -100000
70
+
71
+
72
+ def analyze_pgn(pgn_text: str, depth: int | None = None) -> dict:
73
+ """Analiza un PGN jugada a jugada con Stockfish.
74
+
75
+ Args:
76
+ pgn_text: Movimientos en notación inglesa (SAN). Acepta con o sin headers.
77
+ depth: Profundidad. Si es None usa STOCKFISH_DEPTH de config.
78
+
79
+ Returns:
80
+ {
81
+ 'depth': int,
82
+ 'total_moves': int,
83
+ 'moves': [
84
+ {
85
+ 'number': int,
86
+ 'color': 'white' | 'black',
87
+ 'move': str, # SAN jugado
88
+ 'eval_antes_cp': int, # eval desde POV blancas, antes de jugar
89
+ 'eval_despues_cp': int, # eval desde POV blancas, después de jugar
90
+ 'diferencia_cp': int, # eval_despues - eval_antes (signo según color)
91
+ 'mejor_jugada': str | None, # SAN de la mejor jugada según Stockfish
92
+ 'mejor_eval_cp': int, # eval si se hubiera jugado la mejor
93
+ },
94
+ ...
95
+ ]
96
+ }
97
+ """
98
+ engine = _get_engine()
99
+ used_depth = depth if depth is not None else STOCKFISH_DEPTH
100
+ limit = chess.engine.Limit(depth=used_depth, time=1.0)
101
+
102
+ # Parsear el PGN. Acepta con headers o solo línea de movimientos.
103
+ game = chess.pgn.read_game(io.StringIO(pgn_text))
104
+ if game is None or not list(game.mainline_moves()):
105
+ # Intentar como línea suelta de movimientos
106
+ board = chess.Board()
107
+ moves = []
108
+ for token in pgn_text.split():
109
+ if token.endswith('.') or token.replace('.', '').isdigit():
110
+ continue
111
+ try:
112
+ moves.append(board.parse_san(token))
113
+ board.push(moves[-1])
114
+ except ValueError:
115
+ continue
116
+ if not moves:
117
+ return {'depth': used_depth, 'total_moves': 0, 'moves': []}
118
+ game = chess.pgn.Game()
119
+ node = game
120
+ for mv in moves:
121
+ node = node.add_variation(mv)
122
+
123
+ board = game.board()
124
+ results: list[dict] = []
125
+
126
+ for ply_idx, move in enumerate(game.mainline_moves()):
127
+ is_white_to_move = board.turn == chess.WHITE
128
+ move_san = board.san(move)
129
+
130
+ # Eval ANTES de la jugada (posición actual) + mejor jugada sugerida
131
+ info_before = engine.analyse(board, limit)
132
+ score_before = info_before.get("score")
133
+ eval_antes_cp = _score_to_cp(score_before) if score_before is not None else 0
134
+ pv = info_before.get("pv") or []
135
+ if pv:
136
+ best_move_san = board.san(pv[0])
137
+ else:
138
+ best_move_san = None
139
+ # eval_antes_cp ya representa el valor óptimo de esta posición
140
+ best_eval_cp = eval_antes_cp
141
+
142
+ # Aplicar la jugada y evaluar DESPUÉS
143
+ board.push(move)
144
+ if board.is_game_over():
145
+ eval_despues_cp = _cp_for_terminal(board)
146
+ else:
147
+ info_after = engine.analyse(board, limit)
148
+ score_after = info_after.get("score")
149
+ eval_despues_cp = _score_to_cp(score_after) if score_after is not None else eval_antes_cp
150
+
151
+ # Diferencia desde la perspectiva del jugador que movió.
152
+ # Si jugó blancas, perder centipawns es negativo. Si jugó negras, ganar
153
+ # centipawns para negras es que white_eval bajó → invertimos signo.
154
+ delta = eval_despues_cp - eval_antes_cp
155
+ if not is_white_to_move:
156
+ delta = -delta
157
+
158
+ results.append({
159
+ 'number': ply_idx // 2 + 1,
160
+ 'color': 'white' if is_white_to_move else 'black',
161
+ 'move': move_san,
162
+ 'eval_antes_cp': eval_antes_cp,
163
+ 'eval_despues_cp': eval_despues_cp,
164
+ 'diferencia_cp': delta,
165
+ 'mejor_jugada': best_move_san,
166
+ 'mejor_eval_cp': best_eval_cp,
167
+ })
168
+
169
+ return {
170
+ 'depth': used_depth,
171
+ 'total_moves': len(results),
172
+ 'moves': results,
173
+ }
modules/llm/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # TODO: backend/modules/llm/__init__.py
modules/llm/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (138 Bytes). View file
 
modules/llm/__pycache__/prompt_builder.cpython-310.pyc ADDED
Binary file (1 kB). View file
 
modules/llm/__pycache__/qwen_model.cpython-310.pyc ADDED
Binary file (2.52 kB). View file
 
modules/llm/__pycache__/template_coach.cpython-310.pyc ADDED
Binary file (3.01 kB). View file
 
modules/llm/guardrails.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Validación de salida del LLM + plantillas deterministas (estilo chess.com).
2
+
3
+ Plantillas BREVE para jugadas rutinarias (Excelente/Buena): siempre deterministas,
4
+ no se llama al LLM. Plantillas FALLBACK para Brillante/Imprecisión/Error: se usan
5
+ si la salida del LLM falla la validación.
6
+ """
7
+ import re
8
+
9
+ # Comentarios breves deterministas: jugadas que no requieren LLM.
10
+ _BRIEF = {
11
+ "Excelente": "{move} es la mejor jugada en esta posición.",
12
+ "Buena": "{move} es sólida y mantiene el equilibrio.",
13
+ }
14
+
15
+ # Fallback para categorías que sí piden LLM (si la generación falla validación).
16
+ _FALLBACK = {
17
+ "Brillante": "{move} es un hallazgo táctico que gana ventaja. ¡Muy bien encontrado!",
18
+ "Imprecisión": "{move} cede algo de ventaja. Yo te recomendaría {mejor}, que mantiene mejor la posición.",
19
+ "Error": "{move} cede ventaja significativa. Yo te recomendaría {mejor} para evitar el problema.",
20
+ }
21
+
22
+
23
+ def brief(move: dict, clasificacion: str) -> str:
24
+ """Comentario corto determinista para jugadas no-interesantes (Excelente/Buena)."""
25
+ tmpl = _BRIEF.get(clasificacion, "Jugada procesada.")
26
+ return tmpl.format(move=move.get("move", ""))
27
+
28
+
29
+ _PREFIX_RE = re.compile(
30
+ r'^Jugada\s+\d+\s*\([^)]+\)\s*:\s*\S+[.,]?\s*'
31
+ r'Clasificaci[oó]n\s*:\s*\w+[.,]?\s*'
32
+ r'(?:Evaluaci[oó]n[^.]+\.\s*)?'
33
+ r'(?:Mejor jugada\s*:\s*\S+[.,]?\s*)?',
34
+ re.IGNORECASE,
35
+ )
36
+
37
+
38
+ def validate(text: str, move: dict, clasificacion: str) -> str:
39
+ """Valida la salida del LLM. Elimina prefijo de metadatos si el modelo lo repite."""
40
+ text = (text or "").strip()
41
+ text = _PREFIX_RE.sub('', text).strip()
42
+ if len(text) < 15:
43
+ return _make_fallback(move, clasificacion)
44
+ return text
45
+
46
+
47
+ def _make_fallback(move: dict, clasificacion: str) -> str:
48
+ mejor = move.get("mejor_jugada") or "otra jugada"
49
+ tmpl = _FALLBACK.get(clasificacion, "Jugada procesada.")
50
+ return tmpl.format(move=move.get("move", ""), mejor=mejor)
modules/llm/prompt_builder.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ SYSTEM = (
2
+ "Eres un entrenador de ajedrez personal que habla en primera persona. "
3
+ "Escribe DOS oraciones cortas en español: "
4
+ "primero explica qué pasó con la jugada, "
5
+ "luego di 'Yo te recomendaría {mejor jugada} porque...' con una razón breve y concreta. "
6
+ "Máximo 30 palabras en total. Sin mencionar Stockfish. Solo ajedrez."
7
+ )
8
+
9
+
10
+ def build(move: dict, clasificacion: str) -> str:
11
+ color_str = "Blancas" if move["color"] == "white" else "Negras"
12
+ antes = move["eval_antes_cp"] / 100
13
+ despues = move["eval_despues_cp"] / 100
14
+ mejor = move.get("mejor_jugada") or "no disponible"
15
+ return (
16
+ f"Jugada {move['number']} ({color_str}): {move['move']}. "
17
+ f"Clasificación: {clasificacion}. "
18
+ f"Evaluación antes: {antes:+.1f}, después: {despues:+.1f}. "
19
+ f"Mejor jugada: {mejor}."
20
+ )
modules/llm/qwen.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Singleton lazy para Qwen2.5-3B-Instruct vía llama-cpp-python (GGUF Q4_K_M).
2
+
3
+ El modelo se carga en la primera llamada y se reutiliza entre requests.
4
+ Requiere el archivo GGUF en la ruta configurada en config.QWEN_MODEL_PATH.
5
+
6
+ Descarga:
7
+ hf download Qwen/Qwen2.5-3B-Instruct-GGUF qwen2.5-3b-instruct-q4_k_m.gguf \\
8
+ --local-dir data/models/
9
+ """
10
+ import os
11
+ from llama_cpp import Llama
12
+ from modules.llm.prompt_builder import SYSTEM
13
+
14
+ _model: Llama | None = None
15
+
16
+
17
+ def _load() -> None:
18
+ global _model
19
+ if _model is not None:
20
+ return
21
+ from config import QWEN_MODEL_PATH
22
+ if not os.path.isfile(QWEN_MODEL_PATH):
23
+ raise FileNotFoundError(
24
+ f"Modelo Qwen no encontrado en '{QWEN_MODEL_PATH}'. "
25
+ "Descargalo de huggingface.co/Qwen/Qwen2.5-3B-Instruct-GGUF "
26
+ "(archivo qwen2.5-3b-instruct-q4_k_m.gguf) y colócalo en data/models/."
27
+ )
28
+ print(f"[Qwen] Cargando modelo: {QWEN_MODEL_PATH}")
29
+ try:
30
+ _model = Llama(
31
+ model_path=QWEN_MODEL_PATH,
32
+ n_ctx=2048,
33
+ n_threads=4,
34
+ verbose=False,
35
+ )
36
+ except Exception as e:
37
+ print(f"[Qwen] ERROR al construir Llama: {type(e).__name__}: {e}")
38
+ raise
39
+ print("[Qwen] Modelo listo.")
40
+
41
+
42
+ def generate(prompt: str) -> str:
43
+ import time
44
+ _load()
45
+ print(f"[Qwen] >>> generando (prompt {len(prompt)} chars)...", flush=True)
46
+ t0 = time.time()
47
+ out = _model.create_chat_completion(
48
+ messages=[
49
+ {"role": "system", "content": SYSTEM},
50
+ {"role": "user", "content": prompt},
51
+ ],
52
+ max_tokens=150,
53
+ temperature=0.7,
54
+ stop=["<|im_end|>", "\n\n\n"],
55
+ )
56
+ dt = time.time() - t0
57
+ text = out["choices"][0]["message"]["content"].strip()
58
+ print(f"[Qwen] <<< {dt:.1f}s | {len(text)} chars: {text[:80]!r}", flush=True)
59
+ return text
modules/transcription/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # TODO: backend/modules/transcription/__init__.py
modules/transcription/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (148 Bytes). View file
 
modules/transcription/__pycache__/trocr_model.cpython-310.pyc ADDED
Binary file (3.66 kB). View file
 
modules/transcription/__pycache__/validator.cpython-310.pyc ADDED
Binary file (2.6 kB). View file
 
modules/transcription/trocr_model.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Inferencia TrOCR optimizada para CPU (Intel Iris Xe y similares).
2
+
3
+ Mejoras vs versión original:
4
+ - Inferencia en BATCH: una sola pasada por N celdas en lugar de N pasadas.
5
+ - Cuantización INT8 dinámica en CPU (3-4x más rápido, <1% pérdida de accuracy).
6
+ - Greedy decoding (num_beams=1, do_sample=False) → mucho menos cómputo que beam search.
7
+
8
+ En GPU (CUDA) se desactiva la cuantización automáticamente, ya que solo aporta en CPU.
9
+ """
10
+ import os
11
+ import numpy as np
12
+ import torch
13
+ from PIL import Image
14
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
15
+
16
+ _processor = None
17
+ _model = None
18
+ _device = None
19
+
20
+ _FALLBACK_MODEL = "gcruzl3/trocr-checkscan"
21
+
22
+
23
+ def _should_quantize() -> bool:
24
+ from config import TROCR_QUANTIZE
25
+ if TROCR_QUANTIZE == "true":
26
+ return True
27
+ if TROCR_QUANTIZE == "false":
28
+ return False
29
+ # "auto" → cuantizar solo en CPU
30
+ return not torch.cuda.is_available()
31
+
32
+
33
+ def _load():
34
+ global _processor, _model, _device
35
+ if _model is not None:
36
+ return
37
+ from config import TROCR_MODEL_PATH
38
+
39
+ _device = "cuda" if torch.cuda.is_available() else "cpu"
40
+ model_id = TROCR_MODEL_PATH if os.path.isdir(TROCR_MODEL_PATH) else _FALLBACK_MODEL
41
+ print(f"[TrOCR] Cargando modelo '{model_id}' en {_device}...")
42
+
43
+ _processor = TrOCRProcessor.from_pretrained(model_id)
44
+ model = VisionEncoderDecoderModel.from_pretrained(model_id)
45
+ model.eval()
46
+
47
+ if _device == "cpu" and _should_quantize():
48
+ print("[TrOCR] Aplicando cuantización INT8 dinámica (Linear layers)...")
49
+ model = torch.quantization.quantize_dynamic(
50
+ model, {torch.nn.Linear}, dtype=torch.qint8
51
+ )
52
+ # Threads de CPU: usa todos los cores físicos.
53
+ try:
54
+ torch.set_num_threads(max(1, (os.cpu_count() or 4) - 1))
55
+ except Exception:
56
+ pass
57
+ print(f"[TrOCR] Cuantizado. Threads CPU = {torch.get_num_threads()}")
58
+
59
+ _model = model.to(_device)
60
+ print("[TrOCR] Modelo listo.")
61
+
62
+
63
+ def _to_pil_rgb(cell_gray: np.ndarray) -> Image.Image:
64
+ return Image.fromarray(cell_gray).convert("RGB")
65
+
66
+
67
+ def transcribe_batch(cells: list[np.ndarray], batch_size: int | None = None) -> list[str]:
68
+ """Transcribe una lista de celdas preprocesadas en batches.
69
+
70
+ Args:
71
+ cells: lista de arrays gris 64x384 (salida de preprocess_for_trocr).
72
+ batch_size: tamaño de batch. None → usa config.TROCR_BATCH_SIZE.
73
+
74
+ Returns:
75
+ Lista de strings transcritos, mismo orden que `cells`.
76
+ """
77
+ if not cells:
78
+ return []
79
+ _load()
80
+ from config import TROCR_BATCH_SIZE
81
+ bs = batch_size or TROCR_BATCH_SIZE
82
+
83
+ images = [_to_pil_rgb(c) for c in cells]
84
+ results: list[str] = []
85
+ for i in range(0, len(images), bs):
86
+ chunk = images[i:i + bs]
87
+ pixel_values = _processor(chunk, return_tensors="pt").pixel_values.to(_device)
88
+ with torch.no_grad():
89
+ ids = _model.generate(pixel_values, num_beams=1, do_sample=False)
90
+ results.extend(t.strip() for t in _processor.batch_decode(ids, skip_special_tokens=True))
91
+ return results
92
+
93
+
94
+ def transcribe_cell(cell_gray: np.ndarray) -> str:
95
+ """Compat: transcribe una sola celda. Internamente usa el path de batch."""
96
+ return transcribe_batch([cell_gray], batch_size=1)[0]
modules/transcription/validator.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import chess
3
+ import chess.pgn
4
+
5
+ # Mapeo de piezas: español → inglés
6
+ _PIECE_MAP = {'T': 'R', 'C': 'N', 'A': 'B', 'D': 'Q', 'R': 'K'}
7
+ _PROMO_MAP = {'T': 'R', 'C': 'N', 'A': 'B', 'D': 'Q'}
8
+
9
+
10
+ def translate_move(move_es: str) -> str:
11
+ """Traduce una jugada de notacion espanola a notacion inglesa (SAN)."""
12
+ if not move_es:
13
+ return move_es
14
+ # Enroque igual en ambos idiomas
15
+ if move_es in ('O-O-O', 'O-O', '0-0-0', '0-0'):
16
+ return move_es
17
+
18
+ result = move_es
19
+ # Promocion: =T → =R, =C → =N, etc. (antes de traducir piezas para evitar conflictos)
20
+ result = re.sub(r'=([TCAD])', lambda m: '=' + _PROMO_MAP.get(m.group(1), m.group(1)), result)
21
+ # Pieza al inicio: Cf3 → Nf3, Axc4 → Bxc4, Td1 → Rd1
22
+ result = re.sub(r'^([TCADR])', lambda m: _PIECE_MAP.get(m.group(1), m.group(1)), result)
23
+ return result
24
+
25
+
26
+ def _strip_annotations(san: str) -> str:
27
+ return re.sub(r'[+#!?]', '', san).strip()
28
+
29
+
30
+ def validate_moves(moves_es: list[dict]) -> dict:
31
+ """
32
+ Valida la lista de jugadas con python-chess y construye el PGN en ingles.
33
+
34
+ Entrada:
35
+ moves_es: [{'number': 1, 'white': 'e4', 'black': 'c5'}, ...]
36
+
37
+ Salida:
38
+ {
39
+ 'pgn_en': str — movimientos validos hasta el primer error,
40
+ 'errors': list — todas las jugadas ilegales detectadas,
41
+ 'is_valid': bool — True si no hay errores,
42
+ 'valid_moves_count': int — cantidad de medias jugadas validas,
43
+ }
44
+ """
45
+ board = chess.Board()
46
+ errors = []
47
+ valid_sans = []
48
+ stop = False # tras el primer error de legalidad, el tablero se desfasa y todo lo demas seria falso positivo
49
+
50
+ for entry in moves_es:
51
+ if stop:
52
+ break
53
+ n = entry.get('number', '?')
54
+ for color, key in [('white', 'white'), ('black', 'black')]:
55
+ move_es = entry.get(key, '').strip()
56
+ if not move_es:
57
+ continue
58
+
59
+ move_en = translate_move(move_es)
60
+ move_clean = _strip_annotations(move_en)
61
+
62
+ try:
63
+ move = board.parse_san(move_clean)
64
+ board.push(move)
65
+ valid_sans.append(move_en)
66
+ except (chess.IllegalMoveError, chess.AmbiguousMoveError, ValueError) as e:
67
+ errors.append({
68
+ 'number': n,
69
+ 'color': color,
70
+ 'move_es': move_es,
71
+ 'move_en': move_en,
72
+ 'reason': str(e),
73
+ })
74
+ stop = True
75
+ break
76
+
77
+ # Construir solo la linea de movimientos (sin headers de PGN)
78
+ tmp = chess.Board()
79
+ moves_pgn = []
80
+ for i, san in enumerate(valid_sans):
81
+ move = tmp.parse_san(_strip_annotations(san))
82
+ tmp.push(move)
83
+ if i % 2 == 0:
84
+ moves_pgn.append(f"{i // 2 + 1}. {san}")
85
+ else:
86
+ moves_pgn[-1] += f" {san}"
87
+
88
+ pgn_en = " ".join(moves_pgn)
89
+
90
+ return {
91
+ 'pgn_en': pgn_en,
92
+ 'errors': errors,
93
+ 'is_valid': len(errors) == 0,
94
+ 'valid_moves_count': len(valid_sans),
95
+ }
modules/vision/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # TODO: backend/modules/vision/__init__.py
modules/vision/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (141 Bytes). View file
 
modules/vision/__pycache__/aligner.cpython-310.pyc ADDED
Binary file (3.38 kB). View file
 
modules/vision/__pycache__/segmenter.cpython-310.pyc ADDED
Binary file (5.34 kB). View file
 
modules/vision/aligner.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+
4
+
5
+ def _order_points(pts):
6
+ rect = np.zeros((4, 2), dtype='float32')
7
+ s = pts.sum(axis=1)
8
+ rect[0] = pts[np.argmin(s)]
9
+ rect[2] = pts[np.argmax(s)]
10
+ d = np.diff(pts, axis=1)
11
+ rect[1] = pts[np.argmin(d)]
12
+ rect[3] = pts[np.argmax(d)]
13
+ return rect
14
+
15
+
16
+ def _apply_perspective(img, pts):
17
+ rect = _order_points(pts.astype('float32'))
18
+ width_top = np.linalg.norm(rect[1] - rect[0])
19
+ width_bot = np.linalg.norm(rect[2] - rect[3])
20
+ height_left = np.linalg.norm(rect[3] - rect[0])
21
+ height_right = np.linalg.norm(rect[2] - rect[1])
22
+ max_width = int(max(width_top, width_bot))
23
+ max_height = int(max(height_left, height_right))
24
+ scale = 800 / max_width
25
+ target_w = 800
26
+ target_h = max(600, min(int(max_height * scale), 1400))
27
+ dst = np.array([[0, 0], [target_w, 0], [target_w, target_h], [0, target_h]], dtype='float32')
28
+ M = cv2.getPerspectiveTransform(rect, dst)
29
+ return cv2.warpPerspective(img, M, (target_w, target_h))
30
+
31
+
32
+ def _method1_paper_edges(img):
33
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
34
+ blurred = cv2.GaussianBlur(gray, (7, 7), 0)
35
+ edges = cv2.Canny(blurred, 30, 100)
36
+ edges = cv2.dilate(edges, None, iterations=2)
37
+ contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
38
+ if not contours:
39
+ return None
40
+ img_area = img.shape[0] * img.shape[1]
41
+ for cnt in sorted(contours, key=cv2.contourArea, reverse=True)[:5]:
42
+ if cv2.contourArea(cnt) < img_area * 0.2:
43
+ continue
44
+ epsilon = 0.02 * cv2.arcLength(cnt, True)
45
+ approx = cv2.approxPolyDP(cnt, epsilon, True)
46
+ if len(approx) == 4:
47
+ return _apply_perspective(img, approx.reshape(4, 2))
48
+ return None
49
+
50
+
51
+ def _method2_grid_lines(img):
52
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
53
+ blurred = cv2.GaussianBlur(gray, (5, 5), 0)
54
+ binary = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
55
+ cv2.THRESH_BINARY_INV, 15, 10)
56
+ h, w = binary.shape
57
+ h_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (w // 8, 1))
58
+ h_lines = cv2.morphologyEx(binary, cv2.MORPH_OPEN, h_kernel, iterations=2)
59
+ v_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, h // 8))
60
+ v_lines = cv2.morphologyEx(binary, cv2.MORPH_OPEN, v_kernel, iterations=2)
61
+ grid = cv2.dilate(cv2.add(h_lines, v_lines), None, iterations=3)
62
+ contours, _ = cv2.findContours(grid, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
63
+ if not contours:
64
+ return None
65
+ largest = max(contours, key=cv2.contourArea)
66
+ if cv2.contourArea(largest) < (h * w) * 0.15:
67
+ return None
68
+ for source in [largest, cv2.convexHull(largest)]:
69
+ epsilon = 0.02 * cv2.arcLength(source, True)
70
+ approx = cv2.approxPolyDP(source, epsilon, True)
71
+ if len(approx) == 4:
72
+ return _apply_perspective(img, approx.reshape(4, 2))
73
+ return None
74
+
75
+
76
+ def _method3_fallback(img):
77
+ h, w = img.shape[:2]
78
+ scale = 800 / w
79
+ return cv2.resize(img, (800, int(h * scale)))
80
+
81
+
82
+ def rectify_scoresheet(img: np.ndarray) -> tuple[np.ndarray, str]:
83
+ """Rectifica la perspectiva de una planilla. Devuelve (imagen_rectificada, metodo_usado)."""
84
+ result = _method1_paper_edges(img)
85
+ if result is not None:
86
+ return result, "metodo1_bordes_papel"
87
+ result = _method2_grid_lines(img)
88
+ if result is not None:
89
+ return result, "metodo2_grilla"
90
+ return _method3_fallback(img), "metodo3_fallback"
modules/vision/segmenter.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+
4
+
5
+ def _detect_lines(gray_img):
6
+ h, w = gray_img.shape
7
+ clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
8
+ enhanced = clahe.apply(gray_img)
9
+ binary = cv2.adaptiveThreshold(enhanced, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
10
+ cv2.THRESH_BINARY_INV, 11, 8)
11
+ h_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (w // 10, 1))
12
+ h_lines = cv2.dilate(cv2.morphologyEx(binary, cv2.MORPH_OPEN, h_kernel, iterations=2),
13
+ None, iterations=1)
14
+ v_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, h // 15))
15
+ v_lines = cv2.dilate(cv2.morphologyEx(binary, cv2.MORPH_OPEN, v_kernel, iterations=2),
16
+ None, iterations=1)
17
+ return h_lines, v_lines
18
+
19
+
20
+ def _extract_peaks(projection, threshold):
21
+ above = projection > threshold
22
+ positions = []
23
+ in_peak = False
24
+ peak_vals = []
25
+ for i in range(len(above)):
26
+ if above[i] and not in_peak:
27
+ in_peak = True
28
+ peak_vals = [i]
29
+ elif above[i] and in_peak:
30
+ peak_vals.append(i)
31
+ elif not above[i] and in_peak:
32
+ positions.append(int(np.mean(peak_vals)))
33
+ in_peak = False
34
+ peak_vals = []
35
+ if in_peak and peak_vals:
36
+ positions.append(int(np.mean(peak_vals)))
37
+ return positions
38
+
39
+
40
+ def _get_line_positions(line_mask, axis, expected_range=None):
41
+ projection = np.sum(line_mask, axis=(1 if axis == 0 else 0)).astype(float)
42
+ if projection.max() == 0:
43
+ return []
44
+ projection /= projection.max()
45
+ best_positions = []
46
+ best_score = float('inf')
47
+ for threshold in [0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.40, 0.50]:
48
+ positions = _extract_peaks(projection, threshold)
49
+ if expected_range:
50
+ min_exp, max_exp = expected_range
51
+ if min_exp <= len(positions) <= max_exp:
52
+ return positions
53
+ score = abs(len(positions) - (min_exp + max_exp) / 2)
54
+ if score < best_score:
55
+ best_score = score
56
+ best_positions = positions
57
+ elif threshold == 0.20:
58
+ return positions
59
+ return best_positions
60
+
61
+
62
+ def _filter_table_region(h_positions, v_positions):
63
+ if len(h_positions) < 3:
64
+ return h_positions, v_positions
65
+ clean_v = [v_positions[0]]
66
+ for i in range(1, len(v_positions)):
67
+ if v_positions[i] - clean_v[-1] > 20:
68
+ clean_v.append(v_positions[i])
69
+ return h_positions[1:], clean_v
70
+
71
+
72
+ def _extract_cells(img_rect, h_positions, v_positions, content_cols):
73
+ cells = []
74
+ for i in range(len(h_positions) - 1):
75
+ y1, y2 = h_positions[i], h_positions[i + 1]
76
+ for j in content_cols:
77
+ x1, x2 = v_positions[j], v_positions[j + 1]
78
+ pad_x = max(2, int((x2 - x1) * 0.03))
79
+ pad_y = max(2, int((y2 - y1) * 0.08))
80
+ cell_img = img_rect[y1+pad_y:y2-pad_y, x1+pad_x:x2-pad_x]
81
+ if cell_img.size == 0:
82
+ continue
83
+ cells.append({'image': cell_img, 'row': i, 'col': j})
84
+ return cells
85
+
86
+
87
+ def _assign_move_numbers(cells, col_to_content):
88
+ for cell in cells:
89
+ cell['col_index'] = col_to_content.get(cell['col'], 0)
90
+
91
+ rows = sorted(set(c['row'] for c in cells))
92
+ num_rows = len(rows)
93
+ row_to_idx = {r: i for i, r in enumerate(rows)}
94
+
95
+ for cell in cells:
96
+ block = cell['col_index'] // 2
97
+ is_black = cell['col_index'] % 2
98
+ row_idx = row_to_idx[cell['row']]
99
+ cell['move_number'] = row_idx + 1 + (block * num_rows)
100
+ cell['color'] = 'black' if is_black else 'white'
101
+
102
+ cells.sort(key=lambda c: (c['move_number'], 0 if c['color'] == 'white' else 1))
103
+ return cells
104
+
105
+
106
+ def preprocess_for_trocr(cell_img: np.ndarray) -> np.ndarray | None:
107
+ """Convierte una celda a escala de grises 64x384px para TrOCR."""
108
+ gray = cv2.cvtColor(cell_img, cv2.COLOR_BGR2GRAY) if len(cell_img.shape) == 3 else cell_img.copy()
109
+ gray = cv2.normalize(gray, None, 0, 255, cv2.NORM_MINMAX)
110
+ h, w = gray.shape
111
+ if h == 0 or w == 0:
112
+ return None
113
+ scale = 64 / h
114
+ new_w = max(1, int(w * scale))
115
+ resized = cv2.resize(gray, (new_w, 64), interpolation=cv2.INTER_AREA)
116
+ canvas = np.full((64, 384), 255, dtype=np.uint8)
117
+ canvas[:, :min(new_w, 384)] = resized[:, :384]
118
+ return canvas
119
+
120
+
121
+ def segment_scoresheet(img_rect: np.ndarray) -> list[dict]:
122
+ """Detecta la cuadricula y extrae celdas de movimientos ordenadas por jugada."""
123
+ gray = cv2.GaussianBlur(cv2.cvtColor(img_rect, cv2.COLOR_BGR2GRAY), (3, 3), 0)
124
+ h_lines_mask, v_lines_mask = _detect_lines(gray)
125
+ h_positions = _get_line_positions(h_lines_mask, axis=0, expected_range=(21, 27))
126
+ v_positions = _get_line_positions(v_lines_mask, axis=1, expected_range=(7, 13))
127
+ h_positions, v_positions = _filter_table_region(h_positions, v_positions)
128
+
129
+ col_widths = [v_positions[j+1] - v_positions[j] for j in range(len(v_positions) - 1)]
130
+ if not col_widths:
131
+ return []
132
+
133
+ median_width = np.median(col_widths)
134
+ content_cols = [j for j, w in enumerate(col_widths) if w > median_width * 0.5]
135
+ col_to_content = {j: idx for idx, j in enumerate(content_cols)}
136
+
137
+ cells = _extract_cells(img_rect, h_positions, v_positions, content_cols)
138
+ cells = _assign_move_numbers(cells, col_to_content)
139
+ return cells
requirements-training.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ─── Dependencias EXTRA para entrenar / experimentar con TrOCR ───────────────
2
+ # Solo instalar si vas a correr scripts en experiments/ (no necesario para el backend).
3
+ # Asume que ya instalaste requirements.txt.
4
+
5
+ datasets>=2.18.0
6
+ evaluate>=0.4.0
7
+ accelerate>=0.27.0
8
+ jiwer>=3.0.0
9
+ scikit-learn>=1.4.0
10
+ pandas>=2.2.0
11
+ openpyxl>=3.1.2
requirements.txt ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ─── Backend runtime (lo que necesita Flask para servir /api) ────────────────
2
+ #
3
+ # DEPENDENCIAS GESTIONADAS FUERA DE ESTE ARCHIVO (ver Dockerfile):
4
+ # numpy==1.26.0 → se instala ANTES que torch para evitar downgrade implícito
5
+ # torch/torchvision → pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
6
+ # llama-cpp-python → pip install llama-cpp-python==0.3.2 --prefer-binary
7
+ # --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu
8
+ #
9
+ # En desarrollo local (Windows, sin Docker) instálalas en ese orden antes de este archivo.
10
+
11
+ # Servidor
12
+ flask==3.0.0
13
+ flask-cors==4.0.0
14
+ gunicorn==21.2.0
15
+
16
+ # Vision pipeline (headless: sin libs GUI, ideal para servidor)
17
+ opencv-python-headless==4.9.0.80
18
+ numpy==1.26.0
19
+ Pillow==10.2.0
20
+
21
+ # OCR — TrOCR via HuggingFace
22
+ transformers>=4.40.0
23
+ tokenizers>=0.19.0
24
+ safetensors>=0.4.0
25
+ huggingface-hub>=0.20.0
26
+
27
+ # Validacion de jugadas (SAN → PGN)
28
+ chess==1.10.0
routes/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # TODO: backend/routes/__init__.py
routes/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (133 Bytes). View file
 
routes/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (137 Bytes). View file
 
routes/__pycache__/analyze.cpython-310.pyc ADDED
Binary file (1.16 kB). View file
 
routes/__pycache__/analyze.cpython-312.pyc ADDED
Binary file (1.68 kB). View file
 
routes/__pycache__/chat.cpython-310.pyc ADDED
Binary file (761 Bytes). View file
 
routes/__pycache__/upload.cpython-310.pyc ADDED
Binary file (2.69 kB). View file
 
routes/analyze.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Endpoint de análisis: recibe un PGN y devuelve evaluación jugada a jugada."""
2
+ from flask import Blueprint, request, jsonify
3
+ from modules.analysis.stockfish_wrapper import analyze_pgn
4
+
5
+ analyze_bp = Blueprint('analyze', __name__)
6
+
7
+
8
+ @analyze_bp.route('/analyze', methods=['POST'])
9
+ def analyze():
10
+ """
11
+ Body JSON:
12
+ { "pgn_en": "1. e4 e5 2. Nf3 ...", "depth": 18 (opcional) }
13
+
14
+ Respuesta:
15
+ { "depth": int, "total_moves": int, "moves": [...] }
16
+ """
17
+ data = request.get_json(silent=True) or {}
18
+ pgn = (data.get('pgn_en') or '').strip()
19
+ if not pgn:
20
+ return jsonify({'error': "Se esperaba {'pgn_en': '...'}"}), 400
21
+
22
+ depth = data.get('depth')
23
+ if depth is not None:
24
+ try:
25
+ depth = int(depth)
26
+ if not (1 <= depth <= 30):
27
+ return jsonify({'error': 'depth debe estar entre 1 y 30'}), 400
28
+ except (TypeError, ValueError):
29
+ return jsonify({'error': 'depth debe ser entero'}), 400
30
+
31
+ result = analyze_pgn(pgn, depth=depth)
32
+ return jsonify(result)
routes/chat.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # TODO: backend/routes/chat.py
routes/comment.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Endpoint /api/comment — genera clasificación y comentario por jugada.
2
+
3
+ Estrategia tipo chess.com:
4
+ - Excelente / Buena → comentario BREVE determinista (no se llama al LLM).
5
+ - Brillante / Imprecisión / Error → comentario DETALLADO con LLM
6
+ (con fallback determinista si el LLM falla validación).
7
+ """
8
+ from flask import Blueprint, request, jsonify
9
+ from modules.analysis.classifier import classify
10
+ from modules.llm.prompt_builder import build
11
+ from modules.llm.qwen import generate
12
+ from modules.llm.guardrails import validate, brief
13
+
14
+ comment_bp = Blueprint('comment', __name__)
15
+
16
+ # Solo estas clasificaciones disparan el LLM.
17
+ NEEDS_LLM = {"Brillante", "Imprecisión", "Error"}
18
+
19
+
20
+ @comment_bp.route('/comment', methods=['POST'])
21
+ def comment():
22
+ """
23
+ Body JSON:
24
+ { "moves": [...] } — array de moves devuelto por /api/analyze
25
+
26
+ Respuesta:
27
+ { "comments": [{ "number", "color", "clasificacion", "comentario" }, ...] }
28
+ """
29
+ data = request.get_json(silent=True) or {}
30
+ moves = data.get('moves')
31
+ if not moves or not isinstance(moves, list):
32
+ return jsonify({'error': "Se esperaba {'moves': [...]}"}), 400
33
+
34
+ n_llm = sum(1 for m in moves if classify(m.get('diferencia_cp', 0)) in NEEDS_LLM)
35
+ print(f"[comment] {len(moves)} jugadas, {n_llm} requieren LLM", flush=True)
36
+
37
+ comments = []
38
+ for move in moves:
39
+ clasificacion = classify(move.get('diferencia_cp', 0))
40
+
41
+ if clasificacion in NEEDS_LLM:
42
+ try:
43
+ text = generate(build(move, clasificacion))
44
+ text = validate(text, move, clasificacion)
45
+ except Exception:
46
+ text = validate("", move, clasificacion)
47
+ else:
48
+ text = brief(move, clasificacion)
49
+
50
+ comments.append({
51
+ 'number': move.get('number'),
52
+ 'color': move.get('color'),
53
+ 'clasificacion': clasificacion,
54
+ 'comentario': text,
55
+ })
56
+
57
+ return jsonify({'comments': comments})
routes/upload.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ from flask import Blueprint, request, jsonify
4
+ from modules.vision.aligner import rectify_scoresheet
5
+ from modules.vision.segmenter import segment_scoresheet, preprocess_for_trocr
6
+ from modules.transcription.trocr_model import transcribe_batch
7
+ from modules.transcription.validator import validate_moves
8
+ from config import MAX_UPLOAD_MB, ALLOWED_EXTENSIONS
9
+
10
+ upload_bp = Blueprint('upload', __name__)
11
+
12
+
13
+ def _allowed_file(filename: str) -> bool:
14
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
15
+
16
+
17
+ @upload_bp.route('/validate', methods=['POST'])
18
+ def validate():
19
+ """Revalida jugadas editadas por el usuario. Recibe moves en español, devuelve PGN en inglés."""
20
+ data = request.get_json()
21
+ if not data or 'moves' not in data:
22
+ return jsonify({'error': 'Se esperaba {moves: [...]}'}), 400
23
+ result = validate_moves(data['moves'])
24
+ return jsonify(result)
25
+
26
+
27
+ @upload_bp.route('/upload', methods=['POST'])
28
+ def upload():
29
+ if 'planilla' not in request.files:
30
+ return jsonify({'error': 'No se recibio archivo'}), 400
31
+
32
+ file = request.files['planilla']
33
+ if not file or not _allowed_file(file.filename):
34
+ return jsonify({'error': 'Formato no soportado. Use JPG o PNG'}), 400
35
+
36
+ file_bytes = file.read()
37
+ if len(file_bytes) > MAX_UPLOAD_MB * 1024 * 1024:
38
+ return jsonify({'error': f'Archivo demasiado grande. Maximo {MAX_UPLOAD_MB}MB'}), 413
39
+
40
+ img = cv2.imdecode(np.frombuffer(file_bytes, np.uint8), cv2.IMREAD_COLOR)
41
+ if img is None:
42
+ return jsonify({'error': 'No se pudo leer la imagen'}), 400
43
+
44
+ img_rect, method = rectify_scoresheet(img)
45
+ print(f"[upload] Rectificacion: {method} | shape: {img_rect.shape}")
46
+ cells = segment_scoresheet(img_rect)
47
+
48
+ if not cells:
49
+ return jsonify({'error': 'No se detecto la cuadricula de la planilla'}), 422
50
+
51
+ # Preprocesar todas las celdas y luego transcribirlas en un solo batch.
52
+ prepped: list = []
53
+ cell_meta: list[dict] = []
54
+ for cell in cells:
55
+ preprocessed = preprocess_for_trocr(cell['image'])
56
+ if preprocessed is None:
57
+ continue
58
+ prepped.append(preprocessed)
59
+ cell_meta.append({'move_number': cell['move_number'], 'color': cell['color']})
60
+
61
+ texts = transcribe_batch(prepped)
62
+
63
+ moves_raw: dict[int, dict] = {}
64
+ for meta, text in zip(cell_meta, texts):
65
+ n = meta['move_number']
66
+ if n not in moves_raw:
67
+ moves_raw[n] = {'number': n, 'white': '', 'black': ''}
68
+ moves_raw[n][meta['color']] = text
69
+
70
+ moves = [moves_raw[n] for n in sorted(moves_raw)]
71
+ validation = validate_moves(moves)
72
+
73
+ return jsonify({
74
+ 'moves': moves, # jugadas en español (para el editor)
75
+ 'pgn_en': validation['pgn_en'], # PGN en inglés listo para Stockfish
76
+ 'errors': validation['errors'], # jugadas ilegales detectadas
77
+ 'is_valid': validation['is_valid'],
78
+ 'method': method,
79
+ 'total': len(moves),
80
+ })