File size: 25,290 Bytes
04e136e
 
c39534f
04e136e
3fd73ef
3d378ae
 
 
 
 
8b4175c
3d378ae
 
 
 
 
9869c17
 
3d378ae
 
 
 
 
04e136e
 
 
 
 
 
 
3fd73ef
 
 
 
c39534f
3d378ae
 
 
 
 
 
 
 
 
 
 
 
d74ef19
92d6669
3d378ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92d6669
3d378ae
 
 
 
 
92d6669
3d378ae
 
 
92d6669
3d378ae
 
 
92d6669
3d378ae
 
 
 
 
 
 
 
 
 
 
 
 
92d6669
3d378ae
 
 
 
 
 
 
 
 
d74ef19
 
 
 
 
 
 
 
 
 
 
 
3d378ae
 
 
 
 
 
 
d74ef19
 
ea88565
d74ef19
3d378ae
 
488a33e
 
 
 
3d378ae
488a33e
 
 
 
 
 
3d378ae
d74ef19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e655aa6
 
 
 
d68e753
 
e655aa6
 
 
 
d68e753
e655aa6
 
 
 
 
 
d68e753
 
e655aa6
 
d74ef19
3d378ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d74ef19
3d378ae
ea88565
d74ef19
 
 
ea88565
d74ef19
 
 
 
 
 
 
3d378ae
 
 
 
 
 
 
 
c39534f
ea88565
3d378ae
d74ef19
3d378ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea88565
3d378ae
d74ef19
f4ceabe
 
 
 
 
 
 
 
 
 
 
 
 
92d6669
 
 
 
 
 
 
ea88565
92d6669
 
 
3d378ae
 
 
 
92d6669
3d378ae
 
 
 
d74ef19
 
 
 
 
92d6669
3d378ae
 
 
d74ef19
 
 
 
 
92d6669
3d378ae
 
 
d74ef19
 
 
 
92d6669
d74ef19
a518290
3d378ae
 
 
 
d74ef19
 
 
3d378ae
 
 
 
 
 
 
 
 
 
 
a4287ea
 
3d378ae
 
 
 
 
 
8b4175c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea88565
8b4175c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea88565
9869c17
8b4175c
 
 
 
 
 
 
 
 
 
9869c17
 
 
ea88565
9869c17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d378ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d74ef19
 
 
 
 
 
 
 
c39534f
 
04e136e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4287ea
04e136e
 
a4287ea
 
04e136e
 
 
 
 
 
 
a4287ea
04e136e
d74ef19
 
 
 
 
ea88565
d74ef19
 
 
 
 
ea88565
d74ef19
 
 
3d378ae
 
 
 
 
34be3ee
3d378ae
 
 
 
 
 
 
 
04e136e
 
 
92d6669
04e136e
d74ef19
92d6669
 
d74ef19
309c1c6
04e136e
 
 
309c1c6
a4287ea
04e136e
a4287ea
04e136e
3d378ae
04e136e
3d378ae
04e136e
a4287ea
3d378ae
 
 
 
 
 
34be3ee
3d378ae
 
 
 
 
 
 
d74ef19
 
 
 
 
 
 
 
 
a4287ea
3d378ae
a4287ea
 
d74ef19
3fd73ef
04e136e
a4287ea
8b4175c
 
 
 
 
 
 
 
 
a4287ea
d74ef19
 
 
 
 
 
 
 
 
 
3d378ae
 
 
 
 
 
 
 
 
 
 
d74ef19
 
 
 
 
 
3d378ae
04e136e
 
 
d74ef19
3fd73ef
04e136e
a4287ea
c39534f
9869c17
 
 
 
 
 
 
 
 
 
 
 
 
 
8b4175c
9869c17
 
8b4175c
9869c17
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
import os
import gradio as gr
from PIL import Image
import warnings
import torch
from typing import List, Tuple, Dict, Any
import time
import json
import uuid
from datetime import datetime
from threading import Lock, Thread
import tempfile
import shutil
from pathlib import Path
import yaml
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel

# HTRflow (mesma lógica do diretório app)
from htrflow.pipeline.pipeline import Pipeline
from htrflow.pipeline.steps import init_step
from htrflow.volume.volume import Collection

# Suprimir avisos desnecessários
warnings.filterwarnings("ignore")

# Configurar cache para modelos
os.environ.setdefault("TRANSFORMERS_CACHE", "/tmp/transformers_cache")
os.environ.setdefault("HF_HOME", "/tmp/hf_home")
# Otimizações CPU
os.environ.setdefault("OMP_NUM_THREADS", "1")
os.environ.setdefault("MKL_NUM_THREADS", "1")
torch.set_num_threads(1)

# Sistema de Status Global
class StatusManager:
    def __init__(self):
        self.jobs = {}
        self.lock = Lock()
        self.system_status = {
            "model_loaded": False,
            "system_ready": False,
            "total_processed": 0,
            "startup_time": datetime.now().isoformat(),
            "version": "2.0.0"
        }
        self.cancelled_jobs = set()
    
    def create_job(self, image_name: str = "unknown") -> str:
        job_id = str(uuid.uuid4())
        with self.lock:
            self.jobs[job_id] = {
                "id": job_id,
                "status": "pending",
                "progress": 0,
                "stage": "iniciando",
                "image_name": image_name,
                "created_at": datetime.now().isoformat(),
                "updated_at": datetime.now().isoformat(),
                "result": None,
                "error": None,
                "processing_time": None,
                "stages_completed": []
            }
        return job_id
    
    def update_job(self, job_id: str, **kwargs):
        with self.lock:
            if job_id in self.jobs:
                self.jobs[job_id].update(kwargs)
                self.jobs[job_id]["updated_at"] = datetime.now().isoformat()
    
    def get_job(self, job_id: str) -> Dict[str, Any]:
        with self.lock:
            return self.jobs.get(job_id, {})
    
    def get_all_jobs(self) -> Dict[str, Any]:
        with self.lock:
            return dict(self.jobs)
    
    def complete_job(self, job_id: str, result: str):
        with self.lock:
            if job_id in self.jobs:
                self.jobs[job_id].update({
                    "status": "completed",
                    "progress": 100,
                    "stage": "concluído",
                    "result": result,
                    "processing_time": time.time() - time.mktime(
                        datetime.fromisoformat(self.jobs[job_id]["created_at"]).timetuple()
                    )
                })
                self.system_status["total_processed"] += 1
    
    def fail_job(self, job_id: str, error: str):
        with self.lock:
            if job_id in self.jobs:
                self.jobs[job_id].update({
                    "status": "failed",
                    "stage": "erro",
                    "error": error
                })

    def cancel_job(self, job_id: str):
        with self.lock:
            if job_id in self.jobs and self.jobs[job_id]["status"] == "processing":
                self.jobs[job_id].update({
                    "status": "cancelled",
                    "stage": "cancelado",
                    "error": "Job cancelado pelo usuário"
                })
                self.cancelled_jobs.add(job_id)
                return True
            return False

# Instanciar o gerenciador de status
status_manager = StatusManager()

# Lazy-load: marcamos não carregado até inicializar a pipeline HTRflow
status_manager.system_status["model_loaded"] = False
status_manager.system_status["system_ready"] = False

# YAML das pipelines por idioma (baseadas em app/assets/templates/)
LANGUAGE_PIPELINES = {
    "English": {
        "yaml": """
steps:
- step: Segmentation
  settings:
    model: yolo
    model_settings:
      model: Riksarkivet/yolov9-lines-within-regions-1
- step: TextRecognition
  settings:
    model: TrOCR
    model_settings:
      model: microsoft/trocr-base-handwritten
    generation_settings:
      batch_size: 16
- step: OrderLines
""",
        "description": "Modelo geral para português e inglês manuscrito moderno"
    },
    "Swedish": {
        "yaml": """
steps:
- step: Segmentation
  settings:
    model: yolo
    model_settings:
      model: Riksarkivet/yolov9-lines-within-regions-1
- step: TextRecognition
  settings:
    model: TrOCR
    model_settings:
      model: Riksarkivet/trocr-base-handwritten-hist-swe-2
    generation_settings:
      batch_size: 16
- step: OrderLines
""",
        "description": "Modelo especializado para sueco histórico manuscrito"
    },
    "Norwegian": {
        "yaml": """
steps:
- step: Segmentation
  settings:
    model: yolo
    model_settings:
      model: Riksarkivet/yolov9-lines-within-regions-1
- step: TextRecognition
  settings:
    model: TrOCR
    model_settings:
      model: Sprakbanken/TrOCR-norhand-v3
    generation_settings:
      batch_size: 16
- step: OrderLines
""",
        "description": "Modelo especializado para norueguês histórico manuscrito"
    },
    "Medieval": {
        "yaml": """
steps:
- step: Segmentation
  settings:
    model: yolo
    model_settings:
      model: Riksarkivet/yolov9-lines-within-regions-1
- step: TextRecognition
  settings:
    model: TrOCR
    model_settings:
      model: medieval-data/trocr-medieval-base
    generation_settings:
      batch_size: 16
- step: OrderLines
""",
        "description": "Modelo para textos medievais e manuscritos históricos"
    }

    ,"Math": {
        "yaml": """
steps:
- step: Segmentation
  settings:
    model: yolo
    model_settings:
      model: Riksarkivet/yolov9-lines-within-regions-1
- step: TextRecognition
  settings:
    model: TrOCR
    model_settings:
      model: fhswf/TrOCR_Math_handwritten
    generation_settings:
      batch_size: 16
- step: OrderLines
""",
        "description": "Modelo especializado para escrita matemática manuscrita"
    }
}
class PipelineWithProgress(Pipeline):
        @classmethod
        def from_config(cls, config: Dict[str, Any]):
                return cls([init_step(step["step"], step.get("settings", {})) for step in config["steps"]])

        def run(self, collection, start=0, progress=None):
                total_steps = len(self.steps[start:])
                for i, step in enumerate(self.steps[start:]):
                        if progress is not None:
                                try:
                                        progress((i + 1) / max(1, total_steps))
                                except Exception:
                                        pass
                        collection = step.run(collection)
                return collection


_PIPELINE_CACHE: Dict[str, Optional[PipelineWithProgress]] = {}

def _get_htrflow_pipeline(language: str = "English") -> PipelineWithProgress:
    global _PIPELINE_CACHE
    if language not in _PIPELINE_CACHE or _PIPELINE_CACHE[language] is None:
        if language not in LANGUAGE_PIPELINES:
            language = "English"  # fallback
        
        config = yaml.safe_load(LANGUAGE_PIPELINES[language]["yaml"])
        _PIPELINE_CACHE[language] = PipelineWithProgress.from_config(config)
        status_manager.system_status["model_loaded"] = True
        status_manager.system_status["system_ready"] = True
        print(f"✅ HTRflow pipeline inicializada para {language}")
    return _PIPELINE_CACHE[language]

def preprocess_image(image: Image.Image) -> Image.Image:
    """Mantida para compatibilidade, não utilizada com HTRflow (pipeline cuida disso)."""
    return image

def segment_text_lines(image: Image.Image) -> List[Image.Image]:
    """Mantida para compatibilidade, não utilizada com HTRflow (pipeline faz segmentação)."""
    return [image]

def recognize_text_batch(line_images: List[Image.Image], language: str = "English") -> str:
    """Substituída pela execução da pipeline HTRflow. Mantida por compatibilidade."""
    return htrflow_transcribe(line_images[0], language) if line_images else ""


def _read_txt_from_collection(collection: Collection) -> str:
    """Exporta a collection como TXT e retorna o conteúdo concatenado."""
    temp_dir = tempfile.mkdtemp(prefix="htrflow_txt_")
    try:
        collection.save(directory=temp_dir, serializer="txt")
        texts: list[str] = []
        for root, _, files in os.walk(temp_dir):
            for fname in files:
                if fname.lower().endswith(".txt"):
                    with open(Path(root) / fname, "r", encoding="utf-8") as f:
                        texts.append(f.read().strip())
        return "\n".join([t for t in texts if t])
    finally:
        shutil.rmtree(temp_dir, ignore_errors=True)


def htrflow_transcribe(image: Image.Image, language: str = "English") -> str:
    """Executa a mesma pipeline do diretório app (HTRflow) e retorna a transcrição em texto puro."""
    pipe = _get_htrflow_pipeline(language)
    
    # Salvar imagem temporariamente como arquivo (HTRflow espera caminhos)
    temp_dir = tempfile.mkdtemp(prefix="htrflow_img_")
    temp_image_path = Path(temp_dir) / "input.png"
    image.save(temp_image_path)
    
    try:
        collection = Collection([str(temp_image_path)])
        collection.label = "demo_output"
        collection = pipe.run(collection)
        return _read_txt_from_collection(collection).strip()
    finally:
        shutil.rmtree(temp_dir, ignore_errors=True)

def _sanitize_text(text: str) -> str:
    # Normaliza espaços e remove quebras de linha indesejadas
    text = " ".join(text.replace("\n", " ").split())
    return text.strip()


def ocr_handwritten(image, language="English"):
    if image is None:
        return "Por favor, carregue uma imagem."
    
    # Criar job de monitoramento
    image_name = getattr(image, 'name', 'unknown') if hasattr(image, 'name') else 'unknown'
    job_id = status_manager.create_job(image_name)
    
    try:
        # 1. (HTRflow cuida do pré-processamento internamente)
        status_manager.update_job(job_id, 
                                status="processing", 
                                progress=10, 
                                stage=f"inicializando pipeline ({language})")
        # Checar cancelamento
        if job_id in status_manager.cancelled_jobs:
            status_manager.fail_job(job_id, "Job cancelado pelo usuário")
            return "❌ Job cancelado."
        
        # 2. Carregar/obter pipeline e segmentação
        status_manager.update_job(job_id, 
                                progress=30, 
                                stage=f"segmentação (HTRflow-{language})")
        _ = _get_htrflow_pipeline(language)  # garante cache carregado
        if job_id in status_manager.cancelled_jobs:
            status_manager.fail_job(job_id, "Job cancelado pelo usuário")
            return "❌ Job cancelado."
        
        # 3. Reconhecimento de texto (pipeline completa)
        status_manager.update_job(job_id, 
                                progress=70, 
                                stage=f"reconhecimento (HTRflow-{language})")
        if job_id in status_manager.cancelled_jobs:
            status_manager.fail_job(job_id, "Job cancelado pelo usuário")
            return "❌ Job cancelado."
        
        recognized_text = htrflow_transcribe(image, language)
        
        # 4. Pós-processamento
        status_manager.update_job(job_id, 
                                progress=90, 
                                stage="finalizando")
        if job_id in status_manager.cancelled_jobs:
            status_manager.fail_job(job_id, "Job cancelado pelo usuário")
            return "❌ Job cancelado."
        
        final_text = _sanitize_text(recognized_text)
        
        if not final_text.strip():
            status_manager.fail_job(job_id, "Texto não foi reconhecido")
            return "❌ Não foi possível reconhecer texto na imagem. Tente com uma imagem mais nítida."
        
        # Completar job
        status_manager.complete_job(job_id, final_text)
        
        return final_text
        
    except Exception as e:
        error_msg = f"Erro no processamento: {str(e)}"
        status_manager.fail_job(job_id, error_msg)
        print(f"❌ {error_msg}")
        return f"❌ Erro ao processar imagem: {str(e)}"


# -----------------------------
# API-First (Jobs assíncronos)
# -----------------------------

def _process_job_worker(job_id: str, image_path: str, language: str):
    """Worker que processa o job em background."""
    try:
        # Abrir imagem
        try:
            img = Image.open(image_path)
        except Exception as e:
            status_manager.fail_job(job_id, f"Falha ao abrir imagem: {e}")
            return

        # 1. Inicialização
        status_manager.update_job(job_id,
                                   status="processing",
                                   progress=10,
                                   stage=f"inicializando pipeline ({language})")
        if job_id in status_manager.cancelled_jobs:
            status_manager.fail_job(job_id, "Job cancelado pelo usuário")
            return

        # 2. Pipeline / segmentação
        status_manager.update_job(job_id,
                                   progress=30,
                                   stage=f"segmentação (HTRflow-{language})")
        _ = _get_htrflow_pipeline(language)
        if job_id in status_manager.cancelled_jobs:
            status_manager.fail_job(job_id, "Job cancelado pelo usuário")
            return

        # 3. Reconhecimento
        status_manager.update_job(job_id,
                                   progress=70,
                                   stage=f"reconhecimento (HTRflow-{language})")
        if job_id in status_manager.cancelled_jobs:
            status_manager.fail_job(job_id, "Job cancelado pelo usuário")
            return

        recognized_text = htrflow_transcribe(img, language)

        # 4. Pós-processamento
        status_manager.update_job(job_id,
                                   progress=90,
                                   stage="finalizando")
        if job_id in status_manager.cancelled_jobs:
            status_manager.fail_job(job_id, "Job cancelado pelo usuário")
            return

        final_text = _sanitize_text(recognized_text)
        if not final_text.strip():
            status_manager.fail_job(job_id, "Texto não foi reconhecido")
            return

        status_manager.complete_job(job_id, final_text)
    except Exception as e:
        status_manager.fail_job(job_id, f"Erro interno: {e}")


def start_job_api(image, language="English"):
    """Inicia um job assíncrono e retorna somente o job_id.

    Espera-se que 'image' seja um objeto do Gradio (PIL) ou caminho temporário.
    """
    if image is None:
        return {"error": "Imagem não enviada"}

    # Determinar nome e salvar temporariamente se for PIL
    if isinstance(image, Image.Image):
        temp_dir = tempfile.mkdtemp(prefix="jobimg_")
        image_path = str(Path(temp_dir) / "input.png")
        image.save(image_path)
        cleanup_dir = temp_dir
    elif isinstance(image, (str, Path)) and os.path.exists(str(image)):
        image_path = str(image)
        cleanup_dir = None
    else:
        return {"error": "Formato de imagem não suportado"}

    job_id = status_manager.create_job(Path(image_path).name)

    # Thread para processamento
    t = Thread(target=_process_job_worker, args=(job_id, image_path, language), daemon=True)
    t.start()

    return {"job_id": job_id, "status": "started"}


def get_job_status_api(job_id: str):
    job = status_manager.get_job(job_id)
    if not job:
        return {"error": "Job não encontrado"}
    return job


def list_jobs_api():
    return status_manager.get_all_jobs()


def cancel_job_rest(job_id: str):
    ok = status_manager.cancel_job(job_id)
    if ok:
        return {"success": True, "message": "Job cancelado"}
    return {"success": False, "message": "Não foi possível cancelar (verifique status)"}


def start_job_from_path(path: str = None, language: str = "English"):
    """Função interna usada pelas rotas REST."""
    if not path:
        return {"error": "'path' não fornecido"}
    if not os.path.exists(path):
        return {"error": f"Arquivo não encontrado no servidor: {path}"}
    job_id = status_manager.create_job(Path(path).name)
    t = Thread(target=_process_job_worker, args=(job_id, path, language), daemon=True)
    t.start()
    return {"job_id": job_id, "status": "started", "language": language}


# --------- FastAPI Wrappers (evita dependência de add_server_route do Gradio) ---------
class StartFromPathBody(BaseModel):
    path: str
    language: str = "English"

class CancelBody(BaseModel):
    reason: Optional[str] = None  # reservado para futuro

# FastAPI app principal
api_app = FastAPI(title="Professor IA OCR API", version="2.0")

@api_app.get("/jobs")
def api_list_jobs():
    return list_jobs_api()

@api_app.get("/jobs/{job_id}")
def api_job_status(job_id: str):
    return get_job_status_api(job_id)

@api_app.post("/jobs/{job_id}/cancel")
def api_cancel_job(job_id: str, body: CancelBody | None = None):  # body não usado ainda
    return cancel_job_rest(job_id)

@api_app.post("/jobs/start_from_path")
def api_start_from_path(body: StartFromPathBody):
    return start_job_from_path(body.path, body.language)

@api_app.get("/system/status")
def api_system_status():
    return get_system_status()


# Endpoints para Monitoramento
def get_system_status():
    """Retorna status do sistema"""
    return status_manager.system_status

def get_job_status(job_id: str):
    """Retorna status de um job específico"""
    job = status_manager.get_job(job_id)
    if not job:
        return {"error": "Job não encontrado"}
    return job

def get_all_jobs_status():
    """Retorna todos os jobs"""
    return status_manager.get_all_jobs()

# Endpoint para cancelar job
def cancel_job_api(job_id: str):
    ok = status_manager.cancel_job(job_id)
    if ok:
        return {"success": True, "message": "Job cancelado"}
    else:
        return {"success": False, "message": "Não foi possível cancelar (job não está em processamento)"}


# Interface Gradio
with gr.Blocks(
    title="Professor IA - OCR para Escrita Manual",
    theme=gr.themes.Soft(),
    css="""
    .gradio-container {
        max-width: 1200px;
        margin: auto;
    }
    """
) as iface:
    gr.HTML("""
    <div style="text-align: center; margin-bottom: 20px;">
        <h1>🎓 Professor IA - OCR para Escrita Manual</h1>
        <p>Faça upload de uma imagem com texto manuscrito e obtenha a transcrição usando IA</p>
    </div>
    """)
    
    with gr.Row(equal_height=True):
        with gr.Column(scale=1):
            image_input = gr.Image(
                type="pil",
                label="📤 Carregue sua imagem aqui",
                height=400
            )
            submit_btn = gr.Button(
                "🔍 Transcrever Texto", 
                variant="primary",
                size="lg"
            )
            
            # Seleção de Idioma/Pipeline
            with gr.Group():
                gr.HTML("<h3>🌍 Idioma/Modelo</h3>")
                language_dropdown = gr.Dropdown(
                    choices=list(LANGUAGE_PIPELINES.keys()),
                    value="English",
                    label="Selecione o idioma/modelo",
                    info="Escolha o modelo mais adequado para seu texto"
                )
                
                language_info = gr.HTML(
                    value=LANGUAGE_PIPELINES["English"]["description"],
                    label="Descrição do modelo"
                )
            
            # Status do Sistema
            with gr.Group():
                gr.HTML("<h3>📊 Status do Sistema</h3>")
                system_status_display = gr.JSON(
                    label="Status",
                    value=status_manager.system_status
                )
                
                refresh_status_btn = gr.Button(
                    "🔄 Atualizar Status",
                    variant="secondary",
                    size="sm"
                )
            
            gr.HTML("""
            <div style="margin-top: 15px; padding: 10px; background: #f0f0f0; border-radius: 5px;">
                <small>
                <b>💡 Dicas para melhor resultado:</b><br>
                • Use imagens claras e bem iluminadas<br>
                • Selecione o idioma/modelo correto acima<br>
                • Evite letras muito pequenas ou borradas<br>
                • Funciona melhor com texto organizado em linhas<br>
                • Para documentos longos, pode demorar mais para processar<br>
                • <b>Modelos disponíveis:</b> Português/Inglês, Sueco, Norueguês, Medieval, Math
                </small>
            </div>
            """)

        
        with gr.Column(scale=1):
            text_output = gr.Textbox(
                label="📝 Texto Transcrito",
                lines=10,
                placeholder="O texto transcrito aparecerá aqui...",
                max_lines=15,
                show_copy_button=True
            )
            
            # Monitor de Jobs
            with gr.Group():
                gr.HTML("<h3>📈 Monitor de Processamento</h3>")
                jobs_display = gr.JSON(
                    label="Jobs Ativos",
                    value={}
                )
                
                refresh_jobs_btn = gr.Button(
                    "🔄 Atualizar Jobs",
                    variant="secondary",
                    size="sm"
                )
                cancel_job_id = gr.Textbox(
                    label="ID do Job para Cancelar",
                    placeholder="Cole o ID do job aqui"
                )
                cancel_job_btn = gr.Button(
                    "❌ Cancelar Job",
                    variant="stop",
                    size="sm"
                )
    
    # Event Handlers
    submit_btn.click(
        fn=ocr_handwritten,
        inputs=[image_input, language_dropdown],
        outputs=text_output,
        show_progress=True
    )

    # Novo: endpoint Gradio para iniciar job assíncrono (retorna somente job_id)
    start_job_btn = gr.Button("🧵 Iniciar Job Assíncrono (retorna job_id)", variant="secondary")
    job_id_box = gr.Textbox(label="Job ID Gerado", interactive=False)
    start_job_btn.click(
        fn=start_job_api,
        inputs=[image_input, language_dropdown],
        outputs=job_id_box
    )
    
    # Atualizar descrição do modelo quando idioma muda
    def update_language_info(language):
        return LANGUAGE_PIPELINES[language]["description"]
    
    language_dropdown.change(
        fn=update_language_info,
        inputs=language_dropdown,
        outputs=language_info
    )
    
    # Atualizar status do sistema
    refresh_status_btn.click(
        fn=get_system_status,
        outputs=system_status_display
    )
    
    # Atualizar jobs
    refresh_jobs_btn.click(
        fn=get_all_jobs_status,
        outputs=jobs_display
    )
    # Cancelar job
    cancel_job_btn.click(
        fn=cancel_job_api,
        inputs=cancel_job_id,
        outputs=jobs_display
    )
    
    # Também permitir transcrição automática ao carregar imagem
    image_input.change(
        fn=ocr_handwritten,
        inputs=[image_input, language_dropdown],
        outputs=text_output,
        show_progress=True
    )

# Configuração para Hugging Face Spaces / execução local
# Montamos a interface Gradio dentro do FastAPI para garantir rotas REST sempre disponíveis.
iface.queue(default_concurrency_limit=20)

# Gradio fornece helper para montar dentro de FastAPI
try:
    # A partir do Gradio 4.x: gr.mount_gradio_app
    from gradio import mount_gradio_app
    app = mount_gradio_app(api_app, iface, path="/")
    print("✅ Gradio montado dentro do FastAPI (mount_gradio_app)")
except Exception:
    # Fallback manual: anexar atributo .app (algumas versões expõem .app em launch)
    app = api_app
    print("⚠️ mount_gradio_app indisponível - será necessário iniciar via iface.launch() em modo standalone.")

if __name__ == "__main__":
    # Se mount funcionou, apenas rodar via uvicorn implícito do launch apontando para app.
    try:
        import uvicorn  # garantido em HF spaces base; se não, fallback
        # Executa uvicorn servindo FastAPI com Gradio montado.
        uvicorn.run(app, host="0.0.0.0", port=7860)
    except Exception:
        # Fallback: lançar somente interface (perderá rotas REST se não montadas).
        iface.launch(
            server_name="0.0.0.0",
            server_port=7860,
            share=False,
            show_error=True,
            quiet=False
        )