Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,61 @@
|
|
1 |
-
#
|
2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from PIL import Image
|
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 |
-
gerar_log("Modelo carregado: OK")
|
44 |
-
|
45 |
-
# 3. Gerar descrição
|
46 |
-
with st.spinner("Analisando conteúdo visual..."):
|
47 |
-
result = analyzer(image)
|
48 |
-
description = result[0]['generated_text']
|
49 |
-
gerar_log(f"Descrição gerada: {description[:50]}...")
|
50 |
-
|
51 |
-
# 4. Exibir resultados
|
52 |
-
st.subheader("Análise do Gráfico:")
|
53 |
-
st.write(description)
|
54 |
-
st.image(image, caption="Imagem analisada", width=400)
|
55 |
-
|
56 |
-
except Exception as e:
|
57 |
-
gerar_log(f"ERRO: {str(e)}", "ERROR")
|
58 |
-
st.error(f"Falha crítica: {str(e)}")
|
59 |
-
|
60 |
-
if __name__ == "__main__":
|
61 |
-
main()
|
62 |
-
gerar_log("PROCESSO FINALIZADO.")
|
|
|
1 |
+
# import streamlit as st
|
2 |
+
# from LogCustomizado import LogCustomizado as logc
|
3 |
+
#
|
4 |
+
# logc.gerar_log(mensagem_log="INICIANDO PROCESSO")
|
5 |
+
#
|
6 |
+
# st.set_page_config(page_title="CADIN - Cadastro Inteligente")
|
7 |
+
#
|
8 |
+
# st.title("CADIN - Cadastro Inteligente")
|
9 |
+
#
|
10 |
+
# arquivos_upload = st.file_uploader(
|
11 |
+
# label="Adicione todos os documentos que estão em formato de imagens.",
|
12 |
+
# type=["jpg", "jpeg", "png", "pdf"],
|
13 |
+
# accept_multiple_files=True,
|
14 |
+
# help="O arquitvo PDF precisa ser uma imagem"
|
15 |
+
# )
|
16 |
+
#
|
17 |
+
# if arquivos_upload is not None:
|
18 |
+
# for item in arquivos_upload:
|
19 |
+
# st.write(item)
|
20 |
+
|
21 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
22 |
from PIL import Image
|
23 |
+
import torch
|
24 |
+
|
25 |
+
# Carregar imagem e definir prompt
|
26 |
+
imagem = Image.open("cnh-michele-digital.jpg")
|
27 |
+
prompt = "Extraia os seguintes dados do documento: [nome, cpf e data de nascimento]"
|
28 |
+
|
29 |
+
# Carregar modelo e processador (forçando uso de CPU)
|
30 |
+
device = torch.device("cpu")
|
31 |
+
torch_dtype = torch.float32 # Usar precisão simples para economizar memória
|
32 |
+
|
33 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct")
|
34 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
35 |
+
"Qwen/Qwen2.5-VL-7B-Instruct",
|
36 |
+
torch_dtype=torch_dtype
|
37 |
+
).to(device)
|
38 |
+
|
39 |
+
# Preparar inputs
|
40 |
+
inputs = processor(
|
41 |
+
images=imagem,
|
42 |
+
text=prompt,
|
43 |
+
return_tensors="pt",
|
44 |
+
padding=True
|
45 |
+
).to(device, dtype=torch_dtype)
|
46 |
+
|
47 |
+
# Gerar resposta
|
48 |
+
with torch.no_grad():
|
49 |
+
outputs = model.generate(
|
50 |
+
**inputs,
|
51 |
+
max_new_tokens=512,
|
52 |
+
do_sample=False, # Para respostas determinísticas
|
53 |
+
use_cache=True # Melhora performance em CPU
|
54 |
+
)
|
55 |
+
|
56 |
+
# Decodificar resultado
|
57 |
+
resultado = processor.decode(outputs[0], skip_special_tokens=True)
|
58 |
+
|
59 |
+
print("Dados extraídos:")
|
60 |
+
print("="*40)
|
61 |
+
print(resultado)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|