Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -16,6 +16,9 @@ faixas = {
|
|
16 |
}
|
17 |
|
18 |
def classificar(nome, valor):
|
|
|
|
|
|
|
19 |
try:
|
20 |
raw = valor.replace("K", "").replace(">", "").replace("<", "").strip()
|
21 |
val = float(raw)
|
@@ -36,7 +39,7 @@ def melhorar_imagem(img):
|
|
36 |
img = img.filter(ImageFilter.SHARPEN)
|
37 |
return img
|
38 |
|
39 |
-
# Extrai texto nativo + OCR
|
40 |
def extrair_texto_pdf(pdf_file):
|
41 |
texto_fitz = []
|
42 |
ocr_imgs = []
|
@@ -53,7 +56,7 @@ def extrair_texto_pdf(pdf_file):
|
|
53 |
texto_ocr = re.sub(r'\s+', ' ', texto_ocr)
|
54 |
return texto_fitz, texto_ocr
|
55 |
|
56 |
-
# Padrões regex
|
57 |
exames = {
|
58 |
"LEUCO": r"leuc[óo]citos.*?([\d.,]+)\s?(?:10\^3)?/u?l",
|
59 |
"B": r"bas[óo]filos.*?([\d.,]+)\s?%",
|
@@ -94,7 +97,7 @@ exames = {
|
|
94 |
"TTP": r"ttpa.*?([\d.,]+)\s?seg",
|
95 |
"LAC": r"lactato.*?([\d.,]+)\s?mmol/l",
|
96 |
"CKMB": r"ck[- ]?mb.*?([\d.,]+)\s?u/l",
|
97 |
-
"CPK":
|
98 |
"TROPO": r"troponina.*?([<>]?[\d.,]+)\s?ng/ml"
|
99 |
}
|
100 |
|
@@ -108,7 +111,7 @@ ordem = [
|
|
108 |
"TGO","TGP","TAP","INR","TTP","LAC","CKMB","CPK","TROPO"
|
109 |
]
|
110 |
|
111 |
-
#
|
112 |
def extrair_exames_formatado(pdf_file):
|
113 |
if not pdf_file:
|
114 |
return "Nenhum arquivo enviado.", None
|
@@ -132,7 +135,7 @@ def extrair_exames_formatado(pdf_file):
|
|
132 |
|
133 |
# Interface Gradio
|
134 |
with gr.Blocks() as demo:
|
135 |
-
gr.Markdown("## 🧪 Extrator Avançado com OCR -
|
136 |
pdf_file = gr.File(label="📄 PDF de exames", file_types=[".pdf"])
|
137 |
btn = gr.Button("🔍 Extrair Exames")
|
138 |
out_txt = gr.Textbox(label="📋 Exames Classificados", lines=10)
|
@@ -140,4 +143,4 @@ with gr.Blocks() as demo:
|
|
140 |
btn.click(extrair_exames_formatado, inputs=pdf_file, outputs=[out_txt, dl])
|
141 |
|
142 |
if __name__ == '__main__':
|
143 |
-
demo.launch()
|
|
|
16 |
}
|
17 |
|
18 |
def classificar(nome, valor):
|
19 |
+
"""
|
20 |
+
Recebe o nome do exame e valor em string; retorna valor com ↓ ou ↑ se fora da faixa.
|
21 |
+
"""
|
22 |
try:
|
23 |
raw = valor.replace("K", "").replace(">", "").replace("<", "").strip()
|
24 |
val = float(raw)
|
|
|
39 |
img = img.filter(ImageFilter.SHARPEN)
|
40 |
return img
|
41 |
|
42 |
+
# Extrai texto nativo + OCR das páginas do PDF
|
43 |
def extrair_texto_pdf(pdf_file):
|
44 |
texto_fitz = []
|
45 |
ocr_imgs = []
|
|
|
56 |
texto_ocr = re.sub(r'\s+', ' ', texto_ocr)
|
57 |
return texto_fitz, texto_ocr
|
58 |
|
59 |
+
# Padrões regex para extração de cada exame
|
60 |
exames = {
|
61 |
"LEUCO": r"leuc[óo]citos.*?([\d.,]+)\s?(?:10\^3)?/u?l",
|
62 |
"B": r"bas[óo]filos.*?([\d.,]+)\s?%",
|
|
|
97 |
"TTP": r"ttpa.*?([\d.,]+)\s?seg",
|
98 |
"LAC": r"lactato.*?([\d.,]+)\s?mmol/l",
|
99 |
"CKMB": r"ck[- ]?mb.*?([\d.,]+)\s?u/l",
|
100 |
+
"CPK": r"cpk.*?\bresultado\b\s*([\d.,]+)",
|
101 |
"TROPO": r"troponina.*?([<>]?[\d.,]+)\s?ng/ml"
|
102 |
}
|
103 |
|
|
|
111 |
"TGO","TGP","TAP","INR","TTP","LAC","CKMB","CPK","TROPO"
|
112 |
]
|
113 |
|
114 |
+
# Função principal de extração e formatação
|
115 |
def extrair_exames_formatado(pdf_file):
|
116 |
if not pdf_file:
|
117 |
return "Nenhum arquivo enviado.", None
|
|
|
135 |
|
136 |
# Interface Gradio
|
137 |
with gr.Blocks() as demo:
|
138 |
+
gr.Markdown("## 🧪 Extrator Avançado com OCR - Versão Corrigida")
|
139 |
pdf_file = gr.File(label="📄 PDF de exames", file_types=[".pdf"])
|
140 |
btn = gr.Button("🔍 Extrair Exames")
|
141 |
out_txt = gr.Textbox(label="📋 Exames Classificados", lines=10)
|
|
|
143 |
btn.click(extrair_exames_formatado, inputs=pdf_file, outputs=[out_txt, dl])
|
144 |
|
145 |
if __name__ == '__main__':
|
146 |
+
demo.launch()
|