Atualizando app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,53 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
from tensorflow.keras.models import load_model
|
4 |
-
from huggingface_hub import hf_hub_download
|
5 |
-
import pickle
|
6 |
-
from PIL import Image
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
with
|
19 |
-
|
20 |
-
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
)
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
import pickle
|
6 |
+
from PIL import Image
|
7 |
+
import zipfile
|
8 |
+
import os
|
9 |
+
|
10 |
+
# Repositório no Hugging Face
|
11 |
+
repo_id = "davebraga/wrdbTI6"
|
12 |
+
|
13 |
+
# Baixar o modelo compactado
|
14 |
+
model_zip_path = hf_hub_download(repo_id, "saved_model.zip")
|
15 |
+
|
16 |
+
# Descompactar o modelo se ainda não existir
|
17 |
+
if not os.path.exists("saved_model"):
|
18 |
+
with zipfile.ZipFile(model_zip_path, 'r') as zip_ref:
|
19 |
+
zip_ref.extractall("saved_model")
|
20 |
+
|
21 |
+
# Baixar os encoders
|
22 |
+
category_encoder_path = hf_hub_download(repo_id, "category_encoder.pkl")
|
23 |
+
color_encoder_path = hf_hub_download(repo_id, "color_encoder.pkl")
|
24 |
+
|
25 |
+
# Carregar modelo e encoders
|
26 |
+
model = load_model("saved_model")
|
27 |
+
with open(category_encoder_path, "rb") as f:
|
28 |
+
category_encoder = pickle.load(f)
|
29 |
+
with open(color_encoder_path, "rb") as f:
|
30 |
+
color_encoder = pickle.load(f)
|
31 |
+
|
32 |
+
# Função de previsão
|
33 |
+
def predict(image):
|
34 |
+
image = image.resize((160, 160))
|
35 |
+
image_array = np.array(image) / 255.0
|
36 |
+
image_array = np.expand_dims(image_array, axis=0)
|
37 |
+
|
38 |
+
category_pred, color_pred = model.predict(image_array)
|
39 |
+
category = category_encoder.inverse_transform([np.argmax(category_pred)])[0]
|
40 |
+
color = color_encoder.inverse_transform([np.argmax(color_pred)])[0]
|
41 |
+
|
42 |
+
return f"Categoria: {category}", f"Cor: {color}"
|
43 |
+
|
44 |
+
# Interface Gradio
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=predict,
|
47 |
+
inputs=gr.Image(type="pil"),
|
48 |
+
outputs=["text", "text"],
|
49 |
+
title="Classificador de Categoria e Cor",
|
50 |
+
description="Faça upload de uma imagem de uma peça de roupa para prever a categoria e a cor."
|
51 |
+
)
|
52 |
+
|
53 |
+
iface.launch()
|