Robsonfll commited on
Commit
cd3447c
·
verified ·
1 Parent(s): ee58101

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +8 -87
src/streamlit_app.py CHANGED
@@ -1,94 +1,15 @@
1
  import streamlit as st
2
- import numpy as np
3
- from PIL import Image
4
- import json
5
- from pathlib import Path
6
- import tensorflow as tf
7
 
8
- # Caminho base: volta da pasta src/ para a raiz (/app)
9
- BASE_DIR = Path(__file__).resolve().parent
10
 
 
11
 
12
- # ================================
13
- # CARREGAR MODELO TFLITE
14
- # ================================
15
- @st.cache_resource
16
- def load_tflite():
17
- model_path = BASE_DIR / "model.tflite"
18
- interpreter = tf.lite.Interpreter(model_path=str(model_path))
19
- interpreter.allocate_tensors()
20
- input_details = interpreter.get_input_details()
21
- output_details = interpreter.get_output_details()
22
- return interpreter, input_details, output_details
23
-
24
- # ================================
25
- # PRÉ-PROCESSAMENTO
26
- # ================================
27
- def preprocess(img: Image.Image, size: int):
28
- img = img.convert("RGB").resize((size, size))
29
- arr = np.array(img, dtype=np.float32) / 255.0
30
- return np.expand_dims(arr, axis=0)
31
-
32
- # ================================
33
- # RODAR INFERÊNCIA
34
- # ================================
35
- def predict(img, interpreter, input_details, output_details, labels, img_size):
36
- x = preprocess(img, img_size).astype(input_details[0]["dtype"])
37
- interpreter.set_tensor(input_details[0]["index"], x)
38
- interpreter.invoke()
39
- y = interpreter.get_tensor(output_details[0]["index"])[0]
40
- idx = int(np.argmax(y))
41
- return labels[idx], y[idx]
42
-
43
- # ================================
44
- # CARREGAR META E LABELS
45
- # ================================
46
- with open(BASE_DIR / "meta.json", "r", encoding="utf-8") as f:
47
- meta = json.load(f)
48
-
49
- labels = []
50
- with open(BASE_DIR / "labels.txt", "r", encoding="utf-8") as f:
51
- for line in f:
52
- line = line.strip()
53
- if line:
54
- labels.append(line)
55
-
56
- img_size = int(meta["img_size"])
57
-
58
- # Carrega o modelo
59
- interpreter, input_details, output_details = load_tflite()
60
-
61
- # ================================
62
- # INTERFACE STREAMLIT
63
- # ================================
64
- st.set_page_config(
65
- page_title="ArqueoIA 🗿",
66
- page_icon="🗿",
67
- layout="centered"
68
- )
69
-
70
- st.title("ArqueoIA 🗿")
71
- st.subheader("Sistema de IA para identificar e classificar artefatos arqueológicos.")
72
-
73
- st.write(
74
- """
75
- Envie uma foto de um possível artefato (osso, cerâmica, moeda etc.)
76
- para que o ArqueoIA faça a análise da imagem.
77
- """
78
- )
79
-
80
- uploaded = st.file_uploader("Envie uma imagem (JPG ou PNG)", type=["jpg", "jpeg", "png"])
81
-
82
- if uploaded:
83
- img = Image.open(uploaded)
84
- st.image(img, caption="Imagem enviada", use_column_width=True)
85
-
86
- # Processar previsão
87
- label, confidence = predict(img, interpreter, input_details, output_details, labels, img_size)
88
-
89
- st.success(f"**Classe identificada:** {label}")
90
- st.info(f"**Confiança:** {confidence:.4f}")
91
 
 
 
 
92
  else:
93
- st.info("📤 Envie uma imagem para iniciar a análise.")
 
94
 
 
1
  import streamlit as st
 
 
 
 
 
2
 
3
+ st.set_page_config(page_title="Teste upload", page_icon="🧪", layout="centered")
 
4
 
5
+ st.title("Teste Streamlit no Hugging Face")
6
 
7
+ file = st.file_uploader("Envie qualquer imagem")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ if file:
10
+ st.image(file)
11
+ st.success("Upload OK!")
12
  else:
13
+ st.info("Nenhum arquivo enviado ainda.")
14
+
15