Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ from tensorflow.keras.preprocessing import image
|
|
| 5 |
from huggingface_hub import snapshot_download
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
|
| 9 |
# Load the model from Hugging Face Hub
|
| 10 |
def load_model(repo_id):
|
| 11 |
download_dir = snapshot_download(repo_id)
|
|
@@ -13,7 +12,6 @@ def load_model(repo_id):
|
|
| 13 |
model = tf.keras.models.load_model(model_path)
|
| 14 |
return model
|
| 15 |
|
| 16 |
-
|
| 17 |
# Function to preprocess the uploaded image
|
| 18 |
def preprocess_image(img, target_size=(224, 224)):
|
| 19 |
img = img.resize(target_size) # Resize to match model input
|
|
@@ -22,7 +20,6 @@ def preprocess_image(img, target_size=(224, 224)):
|
|
| 22 |
img_array = tf.keras.applications.efficientnet.preprocess_input(img_array)
|
| 23 |
return img_array
|
| 24 |
|
| 25 |
-
|
| 26 |
# Perform inference
|
| 27 |
def predict(image_input):
|
| 28 |
class_names = ["Defective Tyre", "Good Tyre"]
|
|
@@ -37,9 +34,8 @@ def predict(image_input):
|
|
| 37 |
|
| 38 |
return f"Predicted Class: {predicted_class} (Confidence: {prediction:.5f})"
|
| 39 |
|
| 40 |
-
|
| 41 |
# Hugging Face Model Repository ID
|
| 42 |
-
REPO_ID = "
|
| 43 |
|
| 44 |
# Load the model
|
| 45 |
model = load_model(REPO_ID)
|
|
@@ -47,6 +43,13 @@ model = load_model(REPO_ID)
|
|
| 47 |
# Student ID
|
| 48 |
student_id = "Student ID: 9053220B"
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
# Create the Gradio interface
|
| 51 |
interface = gr.Interface(
|
| 52 |
fn=predict,
|
|
@@ -54,7 +57,15 @@ interface = gr.Interface(
|
|
| 54 |
outputs=gr.Textbox(label="Prediction"),
|
| 55 |
title="Binary Classification: Good vs. Defective Tire",
|
| 56 |
description=student_id,
|
|
|
|
|
|
|
| 57 |
)
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
# Launch the Gradio app
|
| 60 |
-
|
|
|
|
|
|
| 5 |
from huggingface_hub import snapshot_download
|
| 6 |
import os
|
| 7 |
|
|
|
|
| 8 |
# Load the model from Hugging Face Hub
|
| 9 |
def load_model(repo_id):
|
| 10 |
download_dir = snapshot_download(repo_id)
|
|
|
|
| 12 |
model = tf.keras.models.load_model(model_path)
|
| 13 |
return model
|
| 14 |
|
|
|
|
| 15 |
# Function to preprocess the uploaded image
|
| 16 |
def preprocess_image(img, target_size=(224, 224)):
|
| 17 |
img = img.resize(target_size) # Resize to match model input
|
|
|
|
| 20 |
img_array = tf.keras.applications.efficientnet.preprocess_input(img_array)
|
| 21 |
return img_array
|
| 22 |
|
|
|
|
| 23 |
# Perform inference
|
| 24 |
def predict(image_input):
|
| 25 |
class_names = ["Defective Tyre", "Good Tyre"]
|
|
|
|
| 34 |
|
| 35 |
return f"Predicted Class: {predicted_class} (Confidence: {prediction:.5f})"
|
| 36 |
|
|
|
|
| 37 |
# Hugging Face Model Repository ID
|
| 38 |
+
REPO_ID = "your-huggingface-repo-id" # Change this to your actual repo ID
|
| 39 |
|
| 40 |
# Load the model
|
| 41 |
model = load_model(REPO_ID)
|
|
|
|
| 43 |
# Student ID
|
| 44 |
student_id = "Student ID: 9053220B"
|
| 45 |
|
| 46 |
+
# Markdown description to show classification threshold
|
| 47 |
+
threshold_info = """
|
| 48 |
+
### Classification Threshold:
|
| 49 |
+
- A tyre is classified as **Good** if the confidence score is **≥ 0.5**.
|
| 50 |
+
- A tyre is classified as **Defective** if the confidence score is **< 0.5**.
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
# Create the Gradio interface
|
| 54 |
interface = gr.Interface(
|
| 55 |
fn=predict,
|
|
|
|
| 57 |
outputs=gr.Textbox(label="Prediction"),
|
| 58 |
title="Binary Classification: Good vs. Defective Tire",
|
| 59 |
description=student_id,
|
| 60 |
+
allow_flagging="never",
|
| 61 |
+
examples=[], # You can add example images here if needed
|
| 62 |
)
|
| 63 |
|
| 64 |
+
# Add the threshold information markdown
|
| 65 |
+
with gr.Blocks() as app:
|
| 66 |
+
gr.Markdown(threshold_info) # Display threshold info
|
| 67 |
+
interface.render()
|
| 68 |
+
|
| 69 |
# Launch the Gradio app
|
| 70 |
+
app.launch(share=True)
|
| 71 |
+
|