Theingh02 commited on
Commit
8b96091
·
verified ·
1 Parent(s): 657f01e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+ from tensorflow.keras.models import load_model
5
+ from PIL import Image
6
+ from sklearn.preprocessing import LabelEncoder
7
+
8
+ # Load the trained model
9
+ model = load_model("best_dog_breed_model.keras") # ✅ match the actual file name
10
+
11
+ # Load and process labels
12
+ df = pd.read_csv("labels.csv")
13
+ le = LabelEncoder()
14
+ le.fit(df['breed'])
15
+ breed_list = list(le.classes_)
16
+
17
+ # Define prediction function
18
+ def predict(image):
19
+ image = image.resize((224, 224))
20
+ img_array = np.expand_dims(np.array(image) / 255.0, axis=0)
21
+ preds = model.predict(img_array)[0]
22
+ top5_idx = preds.argsort()[-5:][::-1]
23
+ return {breed_list[i]: float(preds[i]) for i in top5_idx}
24
+
25
+ # Create Gradio interface
26
+ iface = gr.Interface(
27
+ fn=predict,
28
+ inputs=gr.Image(type="pil"),
29
+ outputs=gr.Label(num_top_classes=5),
30
+ title="Dog Breed Classifier",
31
+ description="Upload a dog image to see the top 5 predicted breeds."
32
+ )
33
+
34
+ iface.launch()