davebraga commited on
Commit
834a10b
·
verified ·
1 Parent(s): 83a88cb

Atualizando app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -42
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
- # Baixar os arquivos
9
- repo_id = "davebraga/wrdbTI6"
10
- model_path = hf_hub_download(repo_id, "trained_model.keras")
11
- category_encoder_path = hf_hub_download(repo_id, "category_encoder.pkl")
12
- color_encoder_path = hf_hub_download(repo_id, "color_encoder.pkl")
13
-
14
- # Carregar modelo e encoders
15
- model = load_model(model_path)
16
- with open(category_encoder_path, "rb") as f:
17
- category_encoder = pickle.load(f)
18
- with open(color_encoder_path, "rb") as f:
19
- color_encoder = pickle.load(f)
20
-
21
- # Previsão
22
- def predict(image):
23
- image = image.resize((160, 160))
24
- image_array = np.array(image) / 255.0
25
- image_array = np.expand_dims(image_array, axis=0)
26
-
27
- category_pred, color_pred = model.predict(image_array)
28
- category = category_encoder.inverse_transform([np.argmax(category_pred)])[0]
29
- color = color_encoder.inverse_transform([np.argmax(color_pred)])[0]
30
-
31
- return f"Categoria: {category}", f"Cor: {color}"
32
-
33
- # Interface
34
- iface = gr.Interface(
35
- fn=predict,
36
- inputs=gr.Image(type="pil"),
37
- outputs=["text", "text"],
38
- title="Classificador de Categoria e Cor",
39
- description="Faça upload de uma imagem de uma peça de roupa para prever a categoria e a cor."
40
- )
41
-
42
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
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()