Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,60 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from huggingface_hub import snapshot_download
|
| 5 |
import os
|
| 6 |
|
| 7 |
|
|
|
|
| 8 |
def load_model(repo_id):
|
| 9 |
download_dir = snapshot_download(repo_id)
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
detection_model = YOLO(path, task='detect')
|
| 14 |
-
return detection_model
|
| 15 |
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Student ID
|
| 29 |
student_id = "Student ID: 9053220B"
|
| 30 |
|
| 31 |
# Create the Gradio interface
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
inputs=[
|
| 40 |
-
gr.Image(type="pil", label="Input Image"),
|
| 41 |
-
gr.Slider(0, 1, value=confidence_default.value, label="Confidence Threshold"), # Default to 0.5
|
| 42 |
-
gr.Slider(0, 1, value=iou_default.value, label="IOU Threshold") # Default to 0.6
|
| 43 |
-
],
|
| 44 |
-
outputs=gr.Image(type="pil", label="Output Image"),
|
| 45 |
-
title="Object Detection with YOLOv8",
|
| 46 |
-
description=student_id,
|
| 47 |
-
live=False,
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
return interface
|
| 51 |
-
|
| 52 |
|
| 53 |
# Launch the Gradio app
|
| 54 |
-
|
| 55 |
-
app_interface.launch(share=True)
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import tensorflow as tf
|
| 3 |
import gradio as gr
|
| 4 |
+
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)
|
| 12 |
+
model_path = os.path.join(download_dir, "full_model.weights.h5")
|
| 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
|
| 20 |
+
img_array = image.img_to_array(img)
|
| 21 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
| 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"]
|
| 29 |
+
|
| 30 |
+
# Preprocess image
|
| 31 |
+
img_array = preprocess_image(image_input)
|
| 32 |
+
|
| 33 |
+
# Get prediction
|
| 34 |
+
prediction = model.predict(img_array)[0][0] # Scalar sigmoid output
|
| 35 |
+
predicted_class_idx = int(prediction >= 0.5) # 0 if <0.5, 1 if >=0.5
|
| 36 |
+
predicted_class = class_names[predicted_class_idx] # Get class name
|
| 37 |
+
|
| 38 |
+
return f"Predicted Class: {predicted_class} (Confidence: {prediction:.5f})"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Hugging Face Model Repository ID
|
| 42 |
+
REPO_ID = "skngew/9053220B" # Change this to your actual repo ID
|
| 43 |
+
|
| 44 |
+
# Load the model
|
| 45 |
+
model = load_model(REPO_ID)
|
| 46 |
|
| 47 |
# Student ID
|
| 48 |
student_id = "Student ID: 9053220B"
|
| 49 |
|
| 50 |
# Create the Gradio interface
|
| 51 |
+
interface = gr.Interface(
|
| 52 |
+
fn=predict,
|
| 53 |
+
inputs=gr.Image(type="pil", label="Upload an Image"),
|
| 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 |
+
interface.launch(share=True)
|
|
|