Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import logging
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
from PIL import Image, ImageDraw
|
| 6 |
+
import torch
|
| 7 |
+
from surya.ocr import run_ocr
|
| 8 |
+
from surya.detection import batch_text_detection
|
| 9 |
+
from surya.layout import batch_layout_detection
|
| 10 |
+
from surya.ordering import batch_ordering
|
| 11 |
+
from surya.model.detection.model import load_model as load_det_model, load_processor as load_det_processor
|
| 12 |
+
from surya.model.recognition.model import load_model as load_rec_model
|
| 13 |
+
from surya.model.recognition.processor import load_processor as load_rec_processor
|
| 14 |
+
from surya.settings import settings
|
| 15 |
+
|
| 16 |
+
# Configuração de logging
|
| 17 |
+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 18 |
+
logger = logging.getLogger(__name__)
|
| 19 |
+
|
| 20 |
+
# Carregamento de modelos
|
| 21 |
+
det_processor, det_model = load_det_processor(), load_det_model()
|
| 22 |
+
rec_model, rec_processor = load_rec_model(), load_rec_processor()
|
| 23 |
+
|
| 24 |
+
class CustomJSONEncoder(json.JSONEncoder):
|
| 25 |
+
def default(self, obj):
|
| 26 |
+
if isinstance(obj, Image.Image):
|
| 27 |
+
return "Image object (not serializable)"
|
| 28 |
+
return str(obj)
|
| 29 |
+
|
| 30 |
+
def serialize_result(result):
|
| 31 |
+
return json.dumps(result, cls=CustomJSONEncoder, indent=2)
|
| 32 |
+
|
| 33 |
+
def save_metadata(results):
|
| 34 |
+
output_file = "/mnt/data/ocr_metadata.json" # Caminho de armazenamento persistente
|
| 35 |
+
with open(output_file, "w") as f:
|
| 36 |
+
json.dump(results, f, cls=CustomJSONEncoder, indent=2)
|
| 37 |
+
return output_file
|
| 38 |
+
|
| 39 |
+
def ocr_workflow(images, langs):
|
| 40 |
+
logger.info(f"Iniciando workflow OCR para {len(images)} imagens com idiomas: {langs}")
|
| 41 |
+
results = []
|
| 42 |
+
for image_file in images:
|
| 43 |
+
try:
|
| 44 |
+
image = Image.open(image_file.name)
|
| 45 |
+
predictions = run_ocr([image], [langs.split(',')], det_model, det_processor, rec_model, rec_processor)
|
| 46 |
+
formatted_text = "\n".join([line.text for line in predictions[0].text_lines])
|
| 47 |
+
results.append({
|
| 48 |
+
"image": image_file.name,
|
| 49 |
+
"text": formatted_text,
|
| 50 |
+
"predictions": predictions[0].text_lines # Assuming text_lines is serializable
|
| 51 |
+
})
|
| 52 |
+
except Exception as e:
|
| 53 |
+
logger.error(f"Erro com {image_file.name}: {e}")
|
| 54 |
+
results.append({"image": image_file.name, "error": str(e)})
|
| 55 |
+
|
| 56 |
+
metadata_file = save_metadata(results)
|
| 57 |
+
return serialize_result(results), metadata_file
|
| 58 |
+
|
| 59 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 60 |
+
gr.Markdown("# Análise de Documentos com Surya")
|
| 61 |
+
|
| 62 |
+
with gr.Tab("OCR"):
|
| 63 |
+
gr.Markdown("## Reconhecimento Óptico de Caracteres para Múltiplas Imagens")
|
| 64 |
+
with gr.Row():
|
| 65 |
+
ocr_input = gr.Files(label="Carregar Imagens ou PDFs (até 100)")
|
| 66 |
+
ocr_langs = gr.Textbox(label="Idiomas (separados por vírgula)", value="en")
|
| 67 |
+
ocr_button = gr.Button("Executar OCR")
|
| 68 |
+
ocr_output = gr.JSON(label="Resultados OCR")
|
| 69 |
+
ocr_file = gr.File(label="Baixar Metadata OCR")
|
| 70 |
+
|
| 71 |
+
# Executa a função OCR e salva o arquivo de metadata para download
|
| 72 |
+
ocr_button.click(ocr_workflow, inputs=[ocr_input, ocr_langs], outputs=[ocr_output, ocr_file])
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
logger.info("Iniciando aplicativo Gradio...")
|
| 76 |
+
demo.launch()
|